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