/src/openssl36/crypto/cmp/cmp_hdr.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright Nokia 2007-2019 |
4 | | * Copyright Siemens AG 2015-2019 |
5 | | * |
6 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
7 | | * this file except in compliance with the License. You can obtain a copy |
8 | | * in the file LICENSE in the source distribution or at |
9 | | * https://www.openssl.org/source/license.html |
10 | | */ |
11 | | |
12 | | /* CMP functions for PKIHeader handling */ |
13 | | |
14 | | #include "cmp_local.h" |
15 | | #include <openssl/rand.h> /* for RAND_bytes_ex() */ |
16 | | |
17 | | int ossl_cmp_hdr_set_pvno(OSSL_CMP_PKIHEADER *hdr, int pvno) |
18 | 43.0k | { |
19 | 43.0k | if (!ossl_assert(hdr != NULL)) |
20 | 0 | return 0; |
21 | 43.0k | return ASN1_INTEGER_set(hdr->pvno, pvno); |
22 | 43.0k | } |
23 | | |
24 | | int ossl_cmp_hdr_get_pvno(const OSSL_CMP_PKIHEADER *hdr) |
25 | 73.3k | { |
26 | 73.3k | int64_t pvno; |
27 | | |
28 | 73.3k | if (!ossl_assert(hdr != NULL)) |
29 | 0 | return -1; |
30 | 73.3k | if (!ASN1_INTEGER_get_int64(&pvno, hdr->pvno) || pvno < 0 || pvno > INT_MAX) |
31 | 24.5k | return -1; |
32 | 48.7k | return (int)pvno; |
33 | 73.3k | } |
34 | | |
35 | | int ossl_cmp_hdr_get_protection_nid(const OSSL_CMP_PKIHEADER *hdr) |
36 | 58.3k | { |
37 | 58.3k | if (!ossl_assert(hdr != NULL) |
38 | 58.3k | || hdr->protectionAlg == NULL) |
39 | 29.6k | return NID_undef; |
40 | 28.7k | return OBJ_obj2nid(hdr->protectionAlg->algorithm); |
41 | 58.3k | } |
42 | | |
43 | | ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_transactionID(const OSSL_CMP_PKIHEADER *hdr) |
44 | 0 | { |
45 | 0 | if (hdr == NULL) { |
46 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
47 | 0 | return NULL; |
48 | 0 | } |
49 | 0 | return hdr->transactionID; |
50 | 0 | } |
51 | | |
52 | | ASN1_OCTET_STRING *ossl_cmp_hdr_get0_senderNonce(const OSSL_CMP_PKIHEADER *hdr) |
53 | 0 | { |
54 | 0 | if (!ossl_assert(hdr != NULL)) |
55 | 0 | return NULL; |
56 | 0 | return hdr->senderNonce; |
57 | 0 | } |
58 | | |
59 | | ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_recipNonce(const OSSL_CMP_PKIHEADER *hdr) |
60 | 0 | { |
61 | 0 | if (hdr == NULL) { |
62 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
63 | 0 | return NULL; |
64 | 0 | } |
65 | 0 | return hdr->recipNonce; |
66 | 0 | } |
67 | | |
68 | | STACK_OF(OSSL_CMP_ITAV) |
69 | | *OSSL_CMP_HDR_get0_geninfo_ITAVs(const OSSL_CMP_PKIHEADER *hdr) |
70 | 0 | { |
71 | 0 | if (hdr == NULL) { |
72 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | 0 | return hdr->generalInfo; |
76 | 0 | } |
77 | | |
78 | | /* a NULL-DN as an empty sequence of RDNs */ |
79 | | int ossl_cmp_general_name_is_NULL_DN(GENERAL_NAME *name) |
80 | 2.78k | { |
81 | 2.78k | return name == NULL |
82 | 2.78k | || (name->type == GEN_DIRNAME && IS_NULL_DN(name->d.directoryName)); |
83 | 2.78k | } |
84 | | |
85 | | /* |
86 | | * Set the sender name in PKIHeader. |
87 | | * when nm is NULL, sender is set to an empty string |
88 | | * returns 1 on success, 0 on error |
89 | | */ |
90 | | int ossl_cmp_hdr_set1_sender(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm) |
91 | 43.0k | { |
92 | 43.0k | if (!ossl_assert(hdr != NULL)) |
93 | 0 | return 0; |
94 | 43.0k | return GENERAL_NAME_set1_X509_NAME(&hdr->sender, nm); |
95 | 43.0k | } |
96 | | |
97 | | int ossl_cmp_hdr_set1_recipient(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm) |
98 | 43.0k | { |
99 | 43.0k | if (!ossl_assert(hdr != NULL)) |
100 | 0 | return 0; |
101 | 43.0k | return GENERAL_NAME_set1_X509_NAME(&hdr->recipient, nm); |
102 | 43.0k | } |
103 | | |
104 | | int ossl_cmp_hdr_update_messageTime(OSSL_CMP_PKIHEADER *hdr) |
105 | 43.0k | { |
106 | 43.0k | if (!ossl_assert(hdr != NULL)) |
107 | 0 | return 0; |
108 | 43.0k | if (hdr->messageTime == NULL |
109 | 43.0k | && (hdr->messageTime = ASN1_GENERALIZEDTIME_new()) == NULL) |
110 | 0 | return 0; |
111 | 43.0k | return ASN1_GENERALIZEDTIME_set(hdr->messageTime, time(NULL)) != NULL; |
112 | 43.0k | } |
113 | | |
114 | | /* assign to *tgt a random byte array of given length */ |
115 | | static int set_random(ASN1_OCTET_STRING **tgt, OSSL_CMP_CTX *ctx, int len) |
116 | 75.9k | { |
117 | 75.9k | unsigned char *bytes = OPENSSL_malloc(len); |
118 | 75.9k | int res = 0; |
119 | | |
120 | 75.9k | if (bytes == NULL || RAND_bytes_ex(ctx->libctx, bytes, len, 0) <= 0) |
121 | 75.9k | ERR_raise(ERR_LIB_CMP, CMP_R_FAILURE_OBTAINING_RANDOM); |
122 | 75.9k | else |
123 | 75.9k | res = ossl_cmp_asn1_octet_string_set1_bytes(tgt, bytes, len); |
124 | 75.9k | OPENSSL_free(bytes); |
125 | 75.9k | return res; |
126 | 75.9k | } |
127 | | |
128 | | int ossl_cmp_hdr_set1_senderKID(OSSL_CMP_PKIHEADER *hdr, |
129 | | const ASN1_OCTET_STRING *senderKID) |
130 | 0 | { |
131 | 0 | if (!ossl_assert(hdr != NULL)) |
132 | 0 | return 0; |
133 | 0 | return ossl_cmp_asn1_octet_string_set1(&hdr->senderKID, senderKID); |
134 | 0 | } |
135 | | |
136 | | /* push the given text string to the given PKIFREETEXT ft */ |
137 | | int ossl_cmp_hdr_push0_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text) |
138 | 0 | { |
139 | 0 | if (!ossl_assert(hdr != NULL && text != NULL)) |
140 | 0 | return 0; |
141 | | |
142 | 0 | if (hdr->freeText == NULL |
143 | 0 | && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL) |
144 | 0 | return 0; |
145 | | |
146 | 0 | return sk_ASN1_UTF8STRING_push(hdr->freeText, text); |
147 | 0 | } |
148 | | |
149 | | int ossl_cmp_hdr_push1_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text) |
150 | 0 | { |
151 | 0 | if (!ossl_assert(hdr != NULL && text != NULL)) |
152 | 0 | return 0; |
153 | | |
154 | 0 | if (hdr->freeText == NULL |
155 | 0 | && (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL) |
156 | 0 | return 0; |
157 | | |
158 | 0 | return ossl_cmp_sk_ASN1_UTF8STRING_push_str(hdr->freeText, (char *)text->data, |
159 | 0 | text->length); |
160 | 0 | } |
161 | | |
162 | | int ossl_cmp_hdr_generalInfo_push0_item(OSSL_CMP_PKIHEADER *hdr, |
163 | | OSSL_CMP_ITAV *itav) |
164 | 0 | { |
165 | 0 | if (!ossl_assert(hdr != NULL && itav != NULL)) |
166 | 0 | return 0; |
167 | 0 | return OSSL_CMP_ITAV_push0_stack_item(&hdr->generalInfo, itav); |
168 | 0 | } |
169 | | |
170 | | int ossl_cmp_hdr_generalInfo_push1_items(OSSL_CMP_PKIHEADER *hdr, |
171 | | const STACK_OF(OSSL_CMP_ITAV) *itavs) |
172 | 0 | { |
173 | 0 | int i; |
174 | 0 | OSSL_CMP_ITAV *itav; |
175 | |
|
176 | 0 | if (!ossl_assert(hdr != NULL)) |
177 | 0 | return 0; |
178 | | |
179 | 0 | for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) { |
180 | 0 | itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i)); |
181 | 0 | if (itav == NULL) |
182 | 0 | return 0; |
183 | | |
184 | 0 | if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) { |
185 | 0 | OSSL_CMP_ITAV_free(itav); |
186 | 0 | return 0; |
187 | 0 | } |
188 | 0 | } |
189 | 0 | return 1; |
190 | 0 | } |
191 | | |
192 | | int ossl_cmp_hdr_set_implicitConfirm(OSSL_CMP_PKIHEADER *hdr) |
193 | 0 | { |
194 | 0 | OSSL_CMP_ITAV *itav; |
195 | 0 | ASN1_TYPE *asn1null; |
196 | |
|
197 | 0 | if (!ossl_assert(hdr != NULL)) |
198 | 0 | return 0; |
199 | 0 | asn1null = (ASN1_TYPE *)ASN1_NULL_new(); |
200 | 0 | if (asn1null == NULL) |
201 | 0 | return 0; |
202 | 0 | if ((itav = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), |
203 | 0 | asn1null)) |
204 | 0 | == NULL) |
205 | 0 | goto err; |
206 | 0 | if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) |
207 | 0 | goto err; |
208 | 0 | return 1; |
209 | | |
210 | 0 | err: |
211 | 0 | ASN1_TYPE_free(asn1null); |
212 | 0 | OSSL_CMP_ITAV_free(itav); |
213 | 0 | return 0; |
214 | 0 | } |
215 | | |
216 | | /* return 1 if implicitConfirm in the generalInfo field of the header is set */ |
217 | | int ossl_cmp_hdr_has_implicitConfirm(const OSSL_CMP_PKIHEADER *hdr) |
218 | 0 | { |
219 | 0 | int itavCount; |
220 | 0 | int i; |
221 | 0 | OSSL_CMP_ITAV *itav; |
222 | |
|
223 | 0 | if (!ossl_assert(hdr != NULL)) |
224 | 0 | return 0; |
225 | | |
226 | 0 | itavCount = sk_OSSL_CMP_ITAV_num(hdr->generalInfo); |
227 | 0 | for (i = 0; i < itavCount; i++) { |
228 | 0 | itav = sk_OSSL_CMP_ITAV_value(hdr->generalInfo, i); |
229 | 0 | if (itav != NULL |
230 | 0 | && OBJ_obj2nid(itav->infoType) == NID_id_it_implicitConfirm) |
231 | 0 | return 1; |
232 | 0 | } |
233 | | |
234 | 0 | return 0; |
235 | 0 | } |
236 | | |
237 | | /* |
238 | | * set ctx->transactionID in CMP header |
239 | | * if ctx->transactionID is NULL, a random one is created with 128 bit |
240 | | * according to section 5.1.1: |
241 | | * |
242 | | * It is RECOMMENDED that the clients fill the transactionID field with |
243 | | * 128 bits of (pseudo-) random data for the start of a transaction to |
244 | | * reduce the probability of having the transactionID in use at the server. |
245 | | */ |
246 | | int ossl_cmp_hdr_set_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr) |
247 | 43.0k | { |
248 | 43.0k | if (ctx->transactionID == NULL) { |
249 | 32.9k | char *tid; |
250 | | |
251 | 32.9k | if (!set_random(&ctx->transactionID, ctx, |
252 | 32.9k | OSSL_CMP_TRANSACTIONID_LENGTH)) |
253 | 0 | return 0; |
254 | 32.9k | tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID); |
255 | 32.9k | if (tid != NULL) |
256 | 32.9k | ossl_cmp_log1(DEBUG, ctx, |
257 | 32.9k | "Starting new transaction with ID=%s", tid); |
258 | 32.9k | OPENSSL_free(tid); |
259 | 32.9k | } |
260 | | |
261 | 43.0k | return ossl_cmp_asn1_octet_string_set1(&hdr->transactionID, |
262 | 43.0k | ctx->transactionID); |
263 | 43.0k | } |
264 | | |
265 | | /* fill in all fields of the hdr according to the info given in ctx */ |
266 | | int ossl_cmp_hdr_init(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr) |
267 | 35.2k | { |
268 | 35.2k | const X509_NAME *sender; |
269 | 35.2k | const X509_NAME *rcp = NULL; |
270 | | |
271 | 35.2k | if (!ossl_assert(ctx != NULL && hdr != NULL)) |
272 | 0 | return 0; |
273 | | |
274 | | /* set the CMP version */ |
275 | 35.2k | if (!ossl_cmp_hdr_set_pvno(hdr, OSSL_CMP_PVNO)) |
276 | 0 | return 0; |
277 | | |
278 | | /* |
279 | | * If no protection cert nor oldCert nor CSR nor subject is given, |
280 | | * sender name is not known to the client and thus set to NULL-DN |
281 | | */ |
282 | 35.2k | sender = ctx->cert != NULL ? X509_get_subject_name(ctx->cert) : ctx->oldCert != NULL ? X509_get_subject_name(ctx->oldCert) |
283 | 35.2k | : ctx->p10CSR != NULL ? X509_REQ_get_subject_name(ctx->p10CSR) |
284 | 33.0k | : ctx->subjectName; |
285 | 35.2k | if (!ossl_cmp_hdr_set1_sender(hdr, sender)) |
286 | 0 | return 0; |
287 | | |
288 | | /* determine recipient entry in PKIHeader */ |
289 | 35.2k | if (ctx->recipient != NULL) |
290 | 11.5k | rcp = ctx->recipient; |
291 | 23.6k | else if (ctx->srvCert != NULL) |
292 | 0 | rcp = X509_get_subject_name(ctx->srvCert); |
293 | 23.6k | else if (ctx->issuer != NULL) |
294 | 0 | rcp = ctx->issuer; |
295 | 23.6k | else if (ctx->oldCert != NULL) |
296 | 2.25k | rcp = X509_get_issuer_name(ctx->oldCert); |
297 | 21.4k | else if (ctx->cert != NULL) |
298 | 0 | rcp = X509_get_issuer_name(ctx->cert); |
299 | 35.2k | if (!ossl_cmp_hdr_set1_recipient(hdr, rcp)) |
300 | 0 | return 0; |
301 | | |
302 | | /* set current time as message time */ |
303 | 35.2k | if (!ossl_cmp_hdr_update_messageTime(hdr)) |
304 | 0 | return 0; |
305 | | |
306 | 35.2k | if (ctx->recipNonce != NULL |
307 | 9.11k | && !ossl_cmp_asn1_octet_string_set1(&hdr->recipNonce, |
308 | 9.11k | ctx->recipNonce)) |
309 | 0 | return 0; |
310 | | |
311 | 35.2k | if (!ossl_cmp_hdr_set_transactionID(ctx, hdr)) |
312 | 0 | return 0; |
313 | | |
314 | | /*- |
315 | | * set random senderNonce |
316 | | * according to section 5.1.1: |
317 | | * |
318 | | * senderNonce present |
319 | | * -- 128 (pseudo-)random bits |
320 | | * The senderNonce and recipNonce fields protect the PKIMessage against |
321 | | * replay attacks. The senderNonce will typically be 128 bits of |
322 | | * (pseudo-) random data generated by the sender, whereas the recipNonce |
323 | | * is copied from the senderNonce of the previous message in the |
324 | | * transaction. |
325 | | */ |
326 | 35.2k | if (!set_random(&hdr->senderNonce, ctx, OSSL_CMP_SENDERNONCE_LENGTH)) |
327 | 0 | return 0; |
328 | | |
329 | | /* store senderNonce - for cmp with recipNonce in next outgoing msg */ |
330 | 35.2k | if (!OSSL_CMP_CTX_set1_senderNonce(ctx, hdr->senderNonce)) |
331 | 0 | return 0; |
332 | | |
333 | | /*- |
334 | | * freeText [7] PKIFreeText OPTIONAL, |
335 | | * -- this may be used to indicate context-specific instructions |
336 | | * -- (this field is intended for human consumption) |
337 | | */ |
338 | 35.2k | if (ctx->freeText != NULL |
339 | 0 | && !ossl_cmp_hdr_push1_freeText(hdr, ctx->freeText)) |
340 | 0 | return 0; |
341 | | |
342 | 35.2k | return 1; |
343 | 35.2k | } |