rasn_cms/tsp.rs
1//! [RFC 3161](https://www.rfc-editor.org/rfc/rfc3161) Time Stamp Protocol (TSP).
2
3use crate::ContentInfo;
4use rasn::prelude::*;
5use rasn::types::OctetString;
6use rasn::{AsnType, Decode, Encode};
7use rasn_pkix::{AlgorithmIdentifier, Extensions, GeneralName};
8
9/// Time-stamp token eContentType of EncapsulatedContentInfo in the SignedData.
10pub const TST_INFO: &Oid = Oid::ISO_MEMBER_BODY_US_RSADSI_PKCS9_SMIME_CT_TSTINFO;
11
12fn default_false() -> bool {
13 false
14}
15
16/** Time-stamp request.
17
18[RFC 3161 2.4.1](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.1):
19
20```text
21 TimeStampReq ::= SEQUENCE {
22 version INTEGER { v1(1) },
23 messageImprint MessageImprint,
24 --a hash algorithm OID and the hash value of the data to be
25 --time-stamped
26 reqPolicy TSAPolicyId OPTIONAL,
27 nonce INTEGER OPTIONAL,
28 certReq BOOLEAN DEFAULT FALSE,
29 extensions [0] IMPLICIT Extensions OPTIONAL }
30```
31*/
32#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct TimeStampReq {
34 pub version: u64,
35 pub message_imprint: MessageImprint,
36 pub req_policy: Option<TsaPolicyId>,
37 pub nonce: Option<Integer>,
38 #[rasn(default = "default_false")]
39 pub cert_req: bool,
40 #[rasn(tag(0))]
41 pub extensions: Option<Extensions>,
42}
43
44/** Fingerprint of data to protect with a time-stamp.
45
46[RFC 3161 2.4.1](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.1):
47
48```text
49 MessageImprint ::= SEQUENCE {
50 hashAlgorithm AlgorithmIdentifier,
51 hashedMessage OCTET STRING }
52```
53*/
54#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
55pub struct MessageImprint {
56 pub hash_algorithm: AlgorithmIdentifier,
57 pub hashed_message: OctetString,
58}
59
60/** A policy that applies to a time-stamp.
61
62[RFC 3161 2.4.1](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.1):
63
64```text
65 TSAPolicyId ::= OBJECT IDENTIFIER
66```
67*/
68pub type TsaPolicyId = ObjectIdentifier;
69
70/** Time-stamp response.
71
72[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
73
74```text
75 TimeStampResp ::= SEQUENCE {
76 status PKIStatusInfo,
77 timeStampToken TimeStampToken OPTIONAL }
78```
79*/
80#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
81pub struct TimeStampResp {
82 pub status: PkiStatusInfo,
83 pub time_stamp_token: Option<TimeStampToken>,
84}
85
86/** Time-stamp response status.
87
88[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
89
90```text
91 PKIStatusInfo ::= SEQUENCE {
92 status PKIStatus,
93 statusString PKIFreeText OPTIONAL,
94 failInfo PKIFailureInfo OPTIONAL }
95```
96*/
97#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
98pub struct PkiStatusInfo {
99 pub status: PkiStatus,
100 pub status_string: Option<PkiFreeText>,
101 pub fail_info: Option<PkiFailureInfo>,
102}
103
104/** Time-stamp response status code.
105
106[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
107
108```text
109 PKIStatus ::= INTEGER {
110 granted (0),
111 -- when the PKIStatus contains the value zero a TimeStampToken, as
112 requested, is present.
113 grantedWithMods (1),
114 -- when the PKIStatus contains the value one a TimeStampToken,
115 with modifications, is present.
116 rejection (2),
117 waiting (3),
118 revocationWarning (4),
119 -- this message contains a warning that a revocation is
120 -- imminent
121 revocationNotification (5)
122 -- notification that a revocation has occurred }
123```
124*/
125#[derive(AsnType, Clone, Copy, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
126#[rasn(enumerated)]
127pub enum PkiStatus {
128 /// A TimeStampToken, as requested, is present.
129 Granted = 0,
130 /// A TimeStampToken, with modifications, is present.
131 GrantedWithMods = 1,
132 Rejection = 2,
133 Waiting = 3,
134 /// This message contains a warning that a revocation is imminent.
135 RevocationWarning = 4,
136 /// Notification that a revocation has occurred.
137 RevocationNotification = 5,
138}
139
140/** Time-stamp response status free text.
141
142[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
143
144```text
145 PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
146 -- text encoded as UTF-8 String (note: each UTF8String SHOULD
147 -- include an RFC 1766 language tag to indicate the language
148 -- of the contained text)
149```
150*/
151pub type PkiFreeText = SequenceOf<Utf8String>;
152
153/** Time-stamp response status failure reason.
154
155[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
156
157```text
158 PKIFailureInfo ::= BIT STRING {
159 badAlg (0),
160 -- unrecognized or unsupported Algorithm Identifier
161 badRequest (2),
162 -- transaction not permitted or supported
163 badDataFormat (5),
164 -- the data submitted has the wrong format
165 timeNotAvailable (14),
166 -- the TSA's time source is not available
167 unacceptedPolicy (15),
168 -- the requested TSA policy is not supported by the TSA
169 unacceptedExtension (16),
170 -- the requested extension is not supported by the TSA
171 addInfoNotAvailable (17)
172 -- the additional information requested could not be understood
173 -- or is not available
174 systemFailure (25)
175 -- the request cannot be handled due to system failure }
176```
177*/
178pub type PkiFailureInfo = BitString;
179
180/** Time-stamp token.
181
182[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
183
184```text
185 TimeStampToken ::= ContentInfo
186 -- contentType is id-signedData ([CMS])
187 -- content is SignedData ([CMS])
188```
189*/
190pub type TimeStampToken = ContentInfo;
191
192/** Time-stamp token information.
193
194[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
195
196```text
197 TSTInfo ::= SEQUENCE {
198 version INTEGER { v1(1) },
199 policy TSAPolicyId,
200 messageImprint MessageImprint,
201 -- MUST have the same value as the similar field in
202 -- TimeStampReq
203 serialNumber INTEGER,
204 -- Time-Stamping users MUST be ready to accommodate integers
205 -- up to 160 bits.
206 genTime GeneralizedTime,
207 accuracy Accuracy OPTIONAL,
208 ordering BOOLEAN DEFAULT FALSE,
209 nonce INTEGER OPTIONAL,
210 -- MUST be present if the similar field was present
211 -- in TimeStampReq. In that case it MUST have the same value.
212 tsa [0] GeneralName OPTIONAL,
213 extensions [1] IMPLICIT Extensions OPTIONAL }
214```
215*/
216#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, Hash)]
217pub struct TstInfo {
218 pub version: Integer,
219 pub policy: TsaPolicyId,
220 pub message_imprint: MessageImprint,
221 pub serial_number: Integer,
222 pub gen_time: GeneralizedTime,
223 pub accuracy: Option<Accuracy>,
224 #[rasn(default = "default_false")]
225 pub ordering: bool,
226 pub nonce: Option<Integer>,
227 #[rasn(tag(explicit(0)))]
228 pub tsa: Option<GeneralName>,
229 #[rasn(tag(1))]
230 pub extensions: Option<Extensions>,
231}
232
233/** Accuracy of the time (`genTime`) in [TstInfo].
234
235[RFC 3161 2.4.2](https://www.rfc-editor.org/rfc/rfc3161#section-2.4.2):
236
237```text
238 Accuracy ::= SEQUENCE {
239 seconds INTEGER OPTIONAL,
240 millis [0] INTEGER (1..999) OPTIONAL,
241 micros [1] INTEGER (1..999) OPTIONAL }
242```
243*/
244#[derive(AsnType, Clone, Debug, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash)]
245pub struct Accuracy {
246 pub seconds: Option<Integer>,
247 #[rasn(tag(0))]
248 pub millis: Option<Integer>,
249 #[rasn(tag(1))]
250 pub micros: Option<Integer>,
251}