Debian Codegraph @

ietf-cms/timestamp.go

raw symbols SCIP
1 package cms
2
3 import (
4 "bytes"
5 "crypto/x509"
6 "errors"
7
8 "github.com/github/smimesign/ietf-cms/oid"
9 "github.com/github/smimesign/ietf-cms/protocol"
10 "github.com/github/smimesign/ietf-cms/timestamp"
11 )
12
13 // AddTimestamps adds a timestamp to the SignedData using the RFC3161
14 // timestamping service at the given URL. This timestamp proves that the signed
15 // message existed the time of generation, allowing verifiers to have more trust
16 // in old messages signed with revoked keys.
17 func (sd *SignedData) AddTimestamps(url string) error {
18 var (
19 attrs = make([]protocol.Attribute, len(sd.psd.SignerInfos))
20 err error
21 )
22
23 // Fetch all timestamp tokens before adding any to sd. This avoids a partial
24 // failure.
25 for i := range attrs {
26 if attrs[i], err = fetchTS(url, sd.psd.SignerInfos[i]); err != nil {
27 return err
28 }
29 }
30
31 for i := range attrs {
32 sd.psd.SignerInfos[i].UnsignedAttrs = append(sd.psd.SignerInfos[i].UnsignedAttrs, attrs[i])
33 }
34
35 return nil
36 }
37
38 func fetchTS(url string, si protocol.SignerInfo) (protocol.Attribute, error) {
39 nilAttr := protocol.Attribute{}
40
41 req, err := tsRequest(si)
42 if err != nil {
43 return nilAttr, err
44 }
45
46 resp, err := req.Do(url)
47 if err != nil {
48 return nilAttr, err
49 }
50
51 if tsti, err := resp.Info(); err != nil {
52 return nilAttr, err
53 } else if !req.Matches(tsti) {
54 return nilAttr, errors.New("invalid message imprint")
55 }
56
57 return protocol.NewAttribute(oid.AttributeTimeStampToken, resp.TimeStampToken)
58 }
59
60 func tsRequest(si protocol.SignerInfo) (timestamp.Request, error) {
61 hash, err := si.Hash()
62 if err != nil {
63 return timestamp.Request{}, err
64 }
65
66 mi, err := timestamp.NewMessageImprint(hash, bytes.NewReader(si.Signature))
67 if err != nil {
68 return timestamp.Request{}, err
69 }
70
71 return timestamp.Request{
72 Version: 1,
73 CertReq: true,
74 Nonce: timestamp.GenerateNonce(),
75 MessageImprint: mi,
76 }, nil
77 }
78
79 // getTimestamp verifies and returns the timestamp.Info from the SignerInfo.
80 func getTimestamp(si protocol.SignerInfo, opts x509.VerifyOptions) (timestamp.Info, error) {
81 rawValue, err := si.UnsignedAttrs.GetOnlyAttributeValueBytes(oid.AttributeTimeStampToken)
82 if err != nil {
83 return timestamp.Info{}, err
84 }
85
86 tst, err := ParseSignedData(rawValue.FullBytes)
87 if err != nil {
88 return timestamp.Info{}, err
89 }
90
91 tsti, err := timestamp.ParseInfo(tst.psd.EncapContentInfo)
92 if err != nil {
93 return timestamp.Info{}, err
94 }
95
96 if tsti.Version != 1 {
97 return timestamp.Info{}, protocol.ErrUnsupported
98 }
99
100 // verify timestamp signature and certificate chain..
101 if _, err = tst.Verify(opts); err != nil {
102 return timestamp.Info{}, err
103 }
104
105 // verify timestamp token matches SignerInfo.
106 hash, err := tsti.MessageImprint.Hash()
107 if err != nil {
108 return timestamp.Info{}, err
109 }
110 mi, err := timestamp.NewMessageImprint(hash, bytes.NewReader(si.Signature))
111 if err != nil {
112 return timestamp.Info{}, err
113 }
114 if !mi.Equal(tsti.MessageImprint) {
115 return timestamp.Info{}, errors.New("invalid message imprint")
116 }
117
118 return tsti, nil
119 }
120
121 // hasTimestamp checks if si has a timestamp.
122 func hasTimestamp(si protocol.SignerInfo) (bool, error) {
123 vals, err := si.UnsignedAttrs.GetValues(oid.AttributeTimeStampToken)
124 if err != nil {
125 return false, err
126 }
127
128 return len(vals) > 0, nil
129 }