Skip to content

Commit d66840f

Browse files
Export SignedData in cms. Fixes #89
1 parent 3564e86 commit d66840f

7 files changed

Lines changed: 42 additions & 42 deletions

File tree

ietf-cms/sign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ func SignDetached(data []byte, chain []*x509.Certificate, signer crypto.Signer)
4545
// leaf certificate associated with the signer. Any additional intermediates
4646
// will also be added to the SignedData.
4747
func (sd *SignedData) Sign(chain []*x509.Certificate, signer crypto.Signer) error {
48-
return sd.psd.AddSignerInfo(chain, signer)
48+
return sd.AddSignerInfo(chain, signer)
4949
}

ietf-cms/sign_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestSign(t *testing.T) {
3333
}
3434

3535
// test that we're including whole chain in sd
36-
sdCerts, err := sd2.psd.X509Certificates()
36+
sdCerts, err := sd2.X509Certificates()
3737
if err != nil {
3838
t.Fatal(err)
3939
}
@@ -53,7 +53,7 @@ func TestSign(t *testing.T) {
5353
}
5454

5555
// check that we're including signing time attribute
56-
st, err := sd2.psd.SignerInfos[0].GetSigningTimeAttribute()
56+
st, err := sd2.SignerInfos[0].GetSigningTimeAttribute()
5757
if st.After(time.Now().Add(time.Second)) || st.Before(time.Now().Add(-time.Second)) {
5858
t.Fatal("expected SigningTime to be now. Difference was", st.Sub(time.Now()))
5959
}
@@ -77,7 +77,7 @@ func TestSignDetached(t *testing.T) {
7777
}
7878

7979
// test that we're including whole chain in sd
80-
sdCerts, err := sd2.psd.X509Certificates()
80+
sdCerts, err := sd2.X509Certificates()
8181
if err != nil {
8282
t.Fatal(err)
8383
}
@@ -97,7 +97,7 @@ func TestSignDetached(t *testing.T) {
9797
}
9898

9999
// check that we're including signing time attribute
100-
st, err := sd2.psd.SignerInfos[0].GetSigningTimeAttribute()
100+
st, err := sd2.SignerInfos[0].GetSigningTimeAttribute()
101101
if st.After(time.Now().Add(time.Second)) || st.Before(time.Now().Add(-time.Second)) {
102102
t.Fatal("expected SigningTime to be now. Difference was", st.Sub(time.Now()))
103103
}

ietf-cms/signed_data.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// SignedData represents a signed message or detached signature.
1111
type SignedData struct {
12-
psd *protocol.SignedData
12+
*protocol.SignedData
1313
}
1414

1515
// NewSignedData creates a new SignedData from the given data.
@@ -19,12 +19,12 @@ func NewSignedData(data []byte) (*SignedData, error) {
1919
return nil, err
2020
}
2121

22-
psd, err := protocol.NewSignedData(eci)
22+
sd, err := protocol.NewSignedData(eci)
2323
if err != nil {
2424
return nil, err
2525
}
2626

27-
return &SignedData{psd}, nil
27+
return &SignedData{sd}, nil
2828
}
2929

3030
// ParseSignedData parses a SignedData from BER encoded data.
@@ -34,32 +34,32 @@ func ParseSignedData(ber []byte) (*SignedData, error) {
3434
return nil, err
3535
}
3636

37-
psd, err := ci.SignedDataContent()
37+
sd, err := ci.SignedDataContent()
3838
if err != nil {
3939
return nil, err
4040
}
4141

42-
return &SignedData{psd}, nil
42+
return &SignedData{sd}, nil
4343
}
4444

4545
// GetData gets the encapsulated data from the SignedData. Nil will be returned
4646
// if this is a detached signature. A protocol.ErrWrongType will be returned if
4747
// the SignedData encapsulates something other than data (1.2.840.113549.1.7.1).
4848
func (sd *SignedData) GetData() ([]byte, error) {
49-
return sd.psd.EncapContentInfo.DataEContent()
49+
return sd.EncapContentInfo.DataEContent()
5050
}
5151

5252
// GetCertificates gets all the certificates stored in the SignedData.
5353
func (sd *SignedData) GetCertificates() ([]*x509.Certificate, error) {
54-
return sd.psd.X509Certificates()
54+
return sd.X509Certificates()
5555
}
5656

5757
// SetCertificates replaces the certificates stored in the SignedData with new
5858
// ones.
5959
func (sd *SignedData) SetCertificates(certs []*x509.Certificate) error {
60-
sd.psd.ClearCertificates()
60+
sd.ClearCertificates()
6161
for _, cert := range certs {
62-
if err := sd.psd.AddCertificate(cert); err != nil {
62+
if err := sd.AddCertificate(cert); err != nil {
6363
return err
6464
}
6565
}
@@ -69,15 +69,15 @@ func (sd *SignedData) SetCertificates(certs []*x509.Certificate) error {
6969
// Detached removes the data content from this SignedData. No more signatures
7070
// can be added after this method has been called.
7171
func (sd *SignedData) Detached() {
72-
sd.psd.EncapContentInfo.EContent = asn1.RawValue{}
72+
sd.EncapContentInfo.EContent = asn1.RawValue{}
7373
}
7474

7575
// IsDetached checks if this SignedData has data content.
7676
func (sd *SignedData) IsDetached() bool {
77-
return sd.psd.EncapContentInfo.EContent.Bytes == nil
77+
return sd.EncapContentInfo.EContent.Bytes == nil
7878
}
7979

8080
// ToDER encodes this SignedData message using DER.
8181
func (sd *SignedData) ToDER() ([]byte, error) {
82-
return sd.psd.ContentInfoDER()
82+
return sd.ContentInfoDER()
8383
}

ietf-cms/timestamp.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ import (
1616
// in old messages signed with revoked keys.
1717
func (sd *SignedData) AddTimestamps(url string) error {
1818
var (
19-
attrs = make([]protocol.Attribute, len(sd.psd.SignerInfos))
19+
attrs = make([]protocol.Attribute, len(sd.SignerInfos))
2020
err error
2121
)
2222

2323
// Fetch all timestamp tokens before adding any to sd. This avoids a partial
2424
// failure.
2525
for i := range attrs {
26-
if attrs[i], err = fetchTS(url, sd.psd.SignerInfos[i]); err != nil {
26+
if attrs[i], err = fetchTS(url, sd.SignerInfos[i]); err != nil {
2727
return err
2828
}
2929
}
3030

3131
for i := range attrs {
32-
sd.psd.SignerInfos[i].UnsignedAttrs = append(sd.psd.SignerInfos[i].UnsignedAttrs, attrs[i])
32+
sd.SignerInfos[i].UnsignedAttrs = append(sd.SignerInfos[i].UnsignedAttrs, attrs[i])
3333
}
3434

3535
return nil
@@ -88,7 +88,7 @@ func getTimestamp(si protocol.SignerInfo, opts x509.VerifyOptions) (timestamp.In
8888
return timestamp.Info{}, err
8989
}
9090

91-
tsti, err := timestamp.ParseInfo(tst.psd.EncapContentInfo)
91+
tsti, err := timestamp.ParseInfo(tst.EncapContentInfo)
9292
if err != nil {
9393
return timestamp.Info{}, err
9494
}

ietf-cms/timestamp_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestAddTimestamps(t *testing.T) {
2424
if _, err := sd.Verify(intermediateOpts); err != nil {
2525
t.Fatal(err)
2626
}
27-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
27+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != nil {
2828
t.Fatal(err)
2929
}
3030

@@ -68,17 +68,17 @@ func TestTimestampsVerifications(t *testing.T) {
6868
getTimestampedSignedData := func() *SignedData {
6969
sd, _ := NewSignedData([]byte("hi"))
7070
sd.Sign(leaf.Chain(), leaf.PrivateKey)
71-
tsReq, _ := tsRequest(sd.psd.SignerInfos[0])
71+
tsReq, _ := tsRequest(sd.SignerInfos[0])
7272
tsResp, _ := tsa.Do(tsReq)
7373
tsAttr, _ := protocol.NewAttribute(oid.AttributeTimeStampToken, tsResp.TimeStampToken)
74-
sd.psd.SignerInfos[0].UnsignedAttrs = append(sd.psd.SignerInfos[0].UnsignedAttrs, tsAttr)
74+
sd.SignerInfos[0].UnsignedAttrs = append(sd.SignerInfos[0].UnsignedAttrs, tsAttr)
7575
return sd
7676
}
7777

7878
// Good timestamp
7979
tsa.Clear()
8080
sd := getTimestampedSignedData()
81-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
81+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != nil {
8282
t.Fatal(err)
8383
}
8484
if _, err := sd.Verify(intermediateOpts); err != nil {
@@ -97,7 +97,7 @@ func TestTimestampsVerifications(t *testing.T) {
9797
return info
9898
})
9999
sd = getTimestampedSignedData()
100-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
100+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != nil {
101101
t.Fatal(err)
102102
}
103103
if _, err := sd.Verify(intermediateOpts); err == nil || !strings.HasPrefix(err.Error(), "x509: certificate has expired") {
@@ -116,7 +116,7 @@ func TestTimestampsVerifications(t *testing.T) {
116116
return info
117117
})
118118
sd = getTimestampedSignedData()
119-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
119+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != nil {
120120
t.Fatal(err)
121121
}
122122
if _, err := sd.Verify(intermediateOpts); err != nil {
@@ -135,7 +135,7 @@ func TestTimestampsVerifications(t *testing.T) {
135135
return info
136136
})
137137
sd = getTimestampedSignedData()
138-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
138+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != nil {
139139
t.Fatal(err)
140140
}
141141
if _, err := sd.Verify(intermediateOpts); err == nil || !strings.HasPrefix(err.Error(), "x509: certificate has expired") {
@@ -154,7 +154,7 @@ func TestTimestampsVerifications(t *testing.T) {
154154
return info
155155
})
156156
sd = getTimestampedSignedData()
157-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != nil {
157+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != nil {
158158
t.Fatal(err)
159159
}
160160
if _, err := sd.Verify(intermediateOpts); err != nil {
@@ -167,7 +167,7 @@ func TestTimestampsVerifications(t *testing.T) {
167167
return info
168168
})
169169
sd = getTimestampedSignedData()
170-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err == nil || err.Error() != "invalid message imprint" {
170+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err == nil || err.Error() != "invalid message imprint" {
171171
t.Fatalf("expected 'invalid message imprint', got %v", err)
172172
}
173173

@@ -179,7 +179,7 @@ func TestTimestampsVerifications(t *testing.T) {
179179
return tst
180180
})
181181
sd = getTimestampedSignedData()
182-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err == nil {
182+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err == nil {
183183
t.Fatal("expected error")
184184
} else if _, ok := err.(x509.UnknownAuthorityError); !ok {
185185
t.Fatalf("expected x509.UnknownAuthorityError, got %v", err)
@@ -191,7 +191,7 @@ func TestTimestampsVerifications(t *testing.T) {
191191
return tst
192192
})
193193
sd = getTimestampedSignedData()
194-
if _, err := getTimestamp(sd.psd.SignerInfos[0], intermediateOpts); err != rsa.ErrVerification {
194+
if _, err := getTimestamp(sd.SignerInfos[0], intermediateOpts); err != rsa.ErrVerification {
195195
t.Fatalf("expected %v, got %v", rsa.ErrVerification, err)
196196
}
197197
}

ietf-cms/verify.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
//
1717
// WARNING: this function doesn't do any revocation checking.
1818
func (sd *SignedData) Verify(opts x509.VerifyOptions) ([][][]*x509.Certificate, error) {
19-
econtent, err := sd.psd.EncapContentInfo.EContentValue()
19+
econtent, err := sd.EncapContentInfo.EContentValue()
2020
if err != nil {
2121
return nil, err
2222
}
@@ -35,18 +35,18 @@ func (sd *SignedData) Verify(opts x509.VerifyOptions) ([][][]*x509.Certificate,
3535
//
3636
// WARNING: this function doesn't do any revocation checking.
3737
func (sd *SignedData) VerifyDetached(message []byte, opts x509.VerifyOptions) ([][][]*x509.Certificate, error) {
38-
if sd.psd.EncapContentInfo.EContent.Bytes != nil {
38+
if sd.EncapContentInfo.EContent.Bytes != nil {
3939
return nil, errors.New("signature not detached")
4040
}
4141
return sd.verify(message, opts)
4242
}
4343

4444
func (sd *SignedData) verify(econtent []byte, opts x509.VerifyOptions) ([][][]*x509.Certificate, error) {
45-
if len(sd.psd.SignerInfos) == 0 {
45+
if len(sd.SignerInfos) == 0 {
4646
return nil, protocol.ASN1Error{Message: "no signatures found"}
4747
}
4848

49-
certs, err := sd.psd.X509Certificates()
49+
certs, err := sd.X509Certificates()
5050
if err != nil {
5151
return nil, err
5252
}
@@ -64,16 +64,16 @@ func (sd *SignedData) verify(econtent []byte, opts x509.VerifyOptions) ([][][]*x
6464
tsOpts := opts
6565
tsOpts.KeyUsages = []x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping}
6666

67-
chains := make([][][]*x509.Certificate, 0, len(sd.psd.SignerInfos))
67+
chains := make([][][]*x509.Certificate, 0, len(sd.SignerInfos))
6868

69-
for _, si := range sd.psd.SignerInfos {
69+
for _, si := range sd.SignerInfos {
7070
var signedMessage []byte
7171

7272
// SignedAttrs is optional if EncapContentInfo eContentType isn't id-data.
7373
if si.SignedAttrs == nil {
7474
// SignedAttrs may only be absent if EncapContentInfo eContentType is
7575
// id-data.
76-
if !sd.psd.EncapContentInfo.IsTypeData() {
76+
if !sd.EncapContentInfo.IsTypeData() {
7777
return nil, protocol.ASN1Error{Message: "missing SignedAttrs"}
7878
}
7979

@@ -87,7 +87,7 @@ func (sd *SignedData) verify(econtent []byte, opts x509.VerifyOptions) ([][][]*x
8787
if err != nil {
8888
return nil, err
8989
}
90-
if !siContentType.Equal(sd.psd.EncapContentInfo.EContentType) {
90+
if !siContentType.Equal(sd.EncapContentInfo.EContentType) {
9191
return nil, protocol.ASN1Error{Message: "invalid SignerInfo ContentType attribute"}
9292
}
9393

ietf-cms/verify_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func ExampleSignedData() {
3636

3737
func verifyOptionsForSignedData(sd *SignedData) (opts x509.VerifyOptions) {
3838
// add self-signed cert as trusted root
39-
certs, err := sd.psd.X509Certificates()
39+
certs, err := sd.X509Certificates()
4040
if err != nil {
4141
panic(err)
4242
}
@@ -46,7 +46,7 @@ func verifyOptionsForSignedData(sd *SignedData) (opts x509.VerifyOptions) {
4646
}
4747

4848
// trust signing time
49-
signingTime, err := sd.psd.SignerInfos[0].GetSigningTimeAttribute()
49+
signingTime, err := sd.SignerInfos[0].GetSigningTimeAttribute()
5050
if err != nil {
5151
panic(err)
5252
}

0 commit comments

Comments
 (0)