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