Skip to content

Commit f45197c

Browse files
author
Patrick Zheng
authored
fix: fix signerInfo.authenticSigningTime according to spec (#211)
Signed-off-by: Patrick Zheng <patrickzheng@microsoft.com>
1 parent e18808c commit f45197c

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

signature/types.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"crypto/x509"
1919
"errors"
20+
"fmt"
2021
"time"
2122

2223
"github.com/notaryproject/tspclient-go"
@@ -197,17 +198,21 @@ func (signerInfo *SignerInfo) ExtendedAttribute(key string) (Attribute, error) {
197198
return Attribute{}, errors.New("key not in ExtendedAttributes")
198199
}
199200

200-
// AuthenticSigningTime returns the authentic signing time
201+
// AuthenticSigningTime returns the authentic signing time under signing scheme
202+
// notary.x509.signingAuthority.
203+
// For signing scheme notary.x509, since it only supports authentic timestamp,
204+
// an error is returned.
205+
//
206+
// Reference: https://github.com/notaryproject/specifications/blob/3b0743cd9bb99faee60600dc31d706149775fd49/specs/signature-specification.md#signing-time--authentic-signing-time
201207
func (signerInfo *SignerInfo) AuthenticSigningTime() (time.Time, error) {
202-
switch signerInfo.SignedAttributes.SigningScheme {
208+
switch signingScheme := signerInfo.SignedAttributes.SigningScheme; signingScheme {
203209
case SigningSchemeX509SigningAuthority:
204-
return signerInfo.SignedAttributes.SigningTime, nil
205-
case SigningSchemeX509:
206-
if len(signerInfo.UnsignedAttributes.TimestampSignature) > 0 {
207-
// TODO: Add TSA support for AutheticSigningTime
208-
// https://github.com/notaryproject/notation-core-go/issues/38
209-
return time.Time{}, errors.New("TSA checking has not been implemented")
210+
signingTime := signerInfo.SignedAttributes.SigningTime
211+
if signingTime.IsZero() {
212+
return time.Time{}, fmt.Errorf("authentic signing time must be present under signing scheme %q", signingScheme)
210213
}
214+
return signingTime, nil
215+
default:
216+
return time.Time{}, fmt.Errorf("authentic signing time not supported under signing scheme %q", signingScheme)
211217
}
212-
return time.Time{}, errors.New("authenticSigningTime not found")
213218
}

signature/types_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"fmt"
1919
"testing"
20+
"time"
2021
)
2122

2223
func TestSignRequestContext(t *testing.T) {
@@ -51,3 +52,42 @@ func TestSignRequestWithContext(t *testing.T) {
5152
}()
5253
r.WithContext(nil) // should panic
5354
}
55+
56+
func TestAuthenticSigningTime(t *testing.T) {
57+
testTime := time.Now()
58+
signerInfo := SignerInfo{
59+
SignedAttributes: SignedAttributes{
60+
SigningScheme: "notary.x509.signingAuthority",
61+
SigningTime: testTime,
62+
},
63+
}
64+
authenticSigningTime, err := signerInfo.AuthenticSigningTime()
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
if !authenticSigningTime.Equal(testTime) {
69+
t.Fatalf("expected %s, but got %s", testTime, authenticSigningTime)
70+
}
71+
72+
signerInfo = SignerInfo{
73+
SignedAttributes: SignedAttributes{
74+
SigningScheme: "notary.x509.signingAuthority",
75+
},
76+
}
77+
expectedErrMsg := "authentic signing time must be present under signing scheme \"notary.x509.signingAuthority\""
78+
_, err = signerInfo.AuthenticSigningTime()
79+
if err == nil || err.Error() != expectedErrMsg {
80+
t.Fatalf("expected %s, but got %s", expectedErrMsg, err)
81+
}
82+
83+
signerInfo = SignerInfo{
84+
SignedAttributes: SignedAttributes{
85+
SigningScheme: "notary.x509",
86+
},
87+
}
88+
expectedErrMsg = "authentic signing time not supported under signing scheme \"notary.x509\""
89+
_, err = signerInfo.AuthenticSigningTime()
90+
if err == nil || err.Error() != expectedErrMsg {
91+
t.Fatalf("expected %s, but got %s", expectedErrMsg, err)
92+
}
93+
}

0 commit comments

Comments
 (0)