-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
115 lines (103 loc) · 3.56 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
package timestamp
import (
"crypto/x509/pkix"
"encoding/asn1"
"errors"
"math/big"
"time"
asn1util "github.com/shizhMSFT/go-timestamp/asn1"
)
// Response is a time-stamping response.
// TimeStampResp ::= SEQUENCE {
// status PKIStatusInfo,
// timeStampToken TimeStampToken OPTIONAL }
type Response struct {
Status PKIStatusInfo
TimeStampToken asn1.RawValue `asn1:"optional"`
}
func (r *Response) MarshalBinary() ([]byte, error) {
if r == nil {
return nil, errors.New("null response")
}
return asn1.Marshal(*r)
}
func (r *Response) UnmarshalBinary(data []byte) error {
_, err := asn1.Unmarshal(data, r)
return err
}
func (r *Response) SignedData() (*ParsedSignedData, error) {
raw, err := asn1util.ConvertToDER(r.TimeStampToken.FullBytes)
if err != nil {
return nil, err
}
signedData, err := ParseSignedData(raw)
if err != nil {
return nil, err
}
if err := signedData.Verify(nil); err != nil {
return nil, err
}
return signedData, nil
}
func (r *Response) TimeStampTokenInfo() (*TSTInfo, error) {
signed, err := r.SignedData()
if err != nil {
return nil, err
}
if !OIDCTTSTInfo.Equal(signed.ContentType) {
return nil, errors.New("content is not of type TST info")
}
info := &TSTInfo{}
if _, err := asn1.Unmarshal(signed.Content, info); err != nil {
return nil, err
}
return info, nil
}
// PKIStatusInfo contains status codes and failure information for PKI messages.
// PKIStatusInfo ::= SEQUENCE {
// status PKIStatus,
// statusString PKIFreeText OPTIONAL,
// failInfo PKIFailureInfo OPTIONAL }
type PKIStatusInfo struct {
Status PKIStatus
StatusString string `asn1:"optional"`
FailInfo PKIFailureInfo `asn1:"optional"`
}
// TSTInfo ::= SEQUENCE {
// version INTEGER { v1(1) },
// policy TSAPolicyId,
// messageImprint MessageImprint,
// -- MUST have the same value as the similar field in
// -- TimeStampReq
// serialNumber INTEGER,
// -- Time-Stamping users MUST be ready to accommodate integers
// -- up to 160 bits.
// genTime GeneralizedTime,
// accuracy Accuracy OPTIONAL,
// ordering BOOLEAN DEFAULT FALSE,
// nonce INTEGER OPTIONAL,
// -- MUST be present if the similar field was present
// -- in TimeStampReq. In that case it MUST have the same value.
// tsa [0] GeneralName OPTIONAL,
// extensions [1] IMPLICIT Extensions OPTIONAL }
type TSTInfo struct {
Version int
Policy TSAPolicyID
MessageImprint MessageImprint
SerialNumber *big.Int
GenTime time.Time `asn1:"generalized"`
Accuracy Accuracy `asn1:"optional"`
Ordering bool `asn1:"optional,default:false"`
Nonce *big.Int `asn1:"optional"`
TSA asn1.RawValue `asn1:"optional,tag:0"`
Extensions []pkix.Extension `asn1:"optional,tag:1"`
}
// Accuracy ::= SEQUENCE {
// seconds INTEGER OPTIONAL,
// millis [0] INTEGER (1..999) OPTIONAL,
// micros [1] INTEGER (1..999) OPTIONAL }
type Accuracy struct {
Seconds int `asn1:"optional"`
Milliseconds int `asn1:"optional,tag:0"`
Microseconds int `asn1:"optional,tag:1"`
}