-
Notifications
You must be signed in to change notification settings - Fork 4
/
response.go
214 lines (198 loc) · 7 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tspclient
import (
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"fmt"
"math/big"
"time"
"github.com/notaryproject/tspclient-go/pki"
)
// signingCertificateV2 contains certificate hash and identifier of the
// TSA signing certificate.
//
// Reference: RFC 5035 3 signingCertificateV2
//
// signingCertificateV2 ::= SEQUENCE {
// certs SEQUENCE OF ESSCertIDv2,
// policies SEQUENCE OF PolicyInformation OPTIONAL }
type signingCertificateV2 struct {
// Certificates contains the list of certificates. The first certificate
// MUST be the signing certificate used to verify the timestamp token.
Certificates []eSSCertIDv2
// Policies suggests policy values to be used in the certification path
// validation.
Policies asn1.RawValue `asn1:"optional"`
}
// eSSCertIDv2 uniquely identifies a certificate.
//
// Reference: RFC 5035 4
//
// eSSCertIDv2 ::= SEQUENCE {
// hashAlgorithm AlgorithmIdentifier
// DEFAULT {algorithm id-sha256},
// certHash Hash,
// issuerSerial IssuerSerial OPTIONAL }
type eSSCertIDv2 struct {
// HashAlgorithm is the hashing algorithm used to hash certificate.
// When it is not present, the default value is SHA256 (id-sha256).
// Supported values are SHA256, SHA384, and SHA512
HashAlgorithm pkix.AlgorithmIdentifier `asn1:"optional"`
// CertHash is the certificate hash using algorithm specified
// by HashAlgorithm. It is computed over the entire DER-encoded
// certificate (including the signature)
CertHash []byte
// IssuerSerial holds the issuer and serialNumber of the certificate.
// When it is not present, the SignerIdentifier field in the SignerInfo
// will be used.
IssuerSerial issuerAndSerial `asn1:"optional"`
}
// issuerAndSerial holds the issuer name and serialNumber of the certificate
//
// Reference: RFC 5035 4
//
// IssuerSerial ::= SEQUENCE {
// issuer GeneralNames,
// serialNumber CertificateSerialNumber }
type issuerAndSerial struct {
IssuerName generalNames
SerialNumber *big.Int
}
// generalNames holds the issuer name of the certificate.
//
// Reference: RFC 3280 4.2.1.7
//
// GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
//
// GeneralName ::= CHOICE {
// otherName [0] OtherName,
// rfc822Name [1] IA5String,
// dNSName [2] IA5String,
// x400Address [3] ORAddress,
// directoryName [4] Name,
// ediPartyName [5] EDIPartyName,
// uniformResourceIdentifier [6] IA5String,
// iPAddress [7] OCTET STRING,
// registeredID [8] OBJECT IDENTIFIER }
type generalNames struct {
Name asn1.RawValue `asn1:"optional,tag:4"`
}
// Response is a time-stamping response.
//
// TimeStampResp ::= SEQUENCE {
// status PKIStatusInfo,
// timeStampToken TimeStampToken OPTIONAL }
type Response struct {
Status pki.StatusInfo
TimestampToken asn1.RawValue `asn1:"optional"`
}
// MarshalBinary encodes the response to binary form.
// This method implements encoding.BinaryMarshaler.
//
// Reference: https://pkg.go.dev/encoding#BinaryMarshaler
func (r *Response) MarshalBinary() ([]byte, error) {
if r == nil {
return nil, errors.New("nil response")
}
return asn1.Marshal(*r)
}
// UnmarshalBinary decodes the response from binary form.
// This method implements encoding.BinaryUnmarshaler.
//
// Reference: https://pkg.go.dev/encoding#BinaryUnmarshaler
func (r *Response) UnmarshalBinary(data []byte) error {
_, err := asn1.Unmarshal(data, r)
return err
}
// SignedToken returns the timestamp token with signatures.
//
// Callers should invoke SignedToken.Verify to verify the content before
// comsumption.
func (r *Response) SignedToken() (*SignedToken, error) {
if err := r.validateStatus(); err != nil {
return nil, err
}
return ParseSignedToken(r.TimestampToken.FullBytes)
}
// Validate checks if resp is a successful timestamp response against
// its corresponding request based on RFC 3161.
// It is used when a timestamp requestor receives the response from TSA.
func (r *Response) Validate(req *Request) error {
if req == nil {
return &InvalidResponseError{Msg: "missing corresponding request"}
}
if r == nil {
return &InvalidResponseError{Msg: "response cannot be nil"}
}
if err := r.validateStatus(); err != nil {
return err
}
token, err := r.SignedToken()
if err != nil {
return &InvalidResponseError{Detail: err}
}
info, err := token.Info()
if err != nil {
return &InvalidResponseError{Detail: err}
}
if info.Version != 1 {
return &InvalidResponseError{Msg: fmt.Sprintf("timestamp token info version must be 1, but got %d", info.Version)}
}
// check policy
if req.ReqPolicy != nil && !req.ReqPolicy.Equal(info.Policy) {
return &InvalidResponseError{Msg: fmt.Sprintf("policy in response %v does not match policy in request %v", info.Policy, req.ReqPolicy)}
}
// check MessageImprint
if !info.MessageImprint.Equal(req.MessageImprint) {
return &InvalidResponseError{Msg: fmt.Sprintf("message imprint in response %+v does not match with request %+v", info.MessageImprint, req.MessageImprint)}
}
// check gen time to be UTC
// reference: https://datatracker.ietf.org/doc/html/rfc3161#section-2.4.2
genTime := info.GenTime
if genTime.Location() != time.UTC {
return &InvalidResponseError{Msg: "TSTInfo genTime must be in UTC"}
}
// check nonce
if req.Nonce != nil {
responseNonce := info.Nonce
if responseNonce == nil || responseNonce.Cmp(req.Nonce) != 0 {
return &InvalidResponseError{Msg: fmt.Sprintf("nonce in response %s does not match nonce in request %s", responseNonce, req.Nonce)}
}
}
// check certReq
if req.CertReq {
for _, signerInfo := range token.SignerInfos {
if _, err := token.SigningCertificate(&signerInfo); err == nil {
// found at least one signing certificate
return nil
}
}
// no signing certificate was found
return &InvalidResponseError{Msg: "certReq is True in request, but did not find any TSA signing certificate in the response"}
}
if len(token.Certificates) != 0 {
return &InvalidResponseError{Msg: "certReq is False in request, but certificates field is included in the response"}
}
return nil
}
// validateStatus validates the response.Status
//
// Reference: RFC 3161 2.4.2
func (r *Response) validateStatus() error {
if err := r.Status.Err(); err != nil {
return &InvalidResponseError{Detail: err}
}
return nil
}