Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/cmp/cmp_ctx.c

raw
1 /*
2 * Copyright 2007-2026 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 #include "cmp_local.h"
13 #include <openssl/ocsp.h> /* for OCSP_REVOKED_STATUS_* */
14
15 #define DEFINE_OSSL_CMP_CTX_get0(FIELD, TYPE) \
16 DEFINE_OSSL_CMP_CTX_get0_NAME(FIELD, FIELD, TYPE)
17 #define DEFINE_OSSL_CMP_CTX_get0_NAME(NAME, FIELD, TYPE) \
18 TYPE *OSSL_CMP_CTX_get0_##NAME(const OSSL_CMP_CTX *ctx) \
19 { \
20 if (ctx == NULL) { \
21 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
22 return NULL; \
23 } \
24 return ctx->FIELD; \
25 }
26
27 /*
28 * Get current certificate store containing trusted root CA certs
29 */
30 DEFINE_OSSL_CMP_CTX_get0_NAME(trusted, trusted, X509_STORE)
31
32 #define DEFINE_OSSL_set0(PREFIX, FIELD, TYPE) \
33 DEFINE_OSSL_set0_NAME(PREFIX, FIELD, FIELD, TYPE)
34 #define DEFINE_OSSL_set0_NAME(PREFIX, NAME, FIELD, TYPE) \
35 int PREFIX##_set0##_##NAME(OSSL_CMP_CTX *ctx, TYPE *val) \
36 { \
37 if (ctx == NULL) { \
38 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
39 return 0; \
40 } \
41 TYPE##_free(ctx->FIELD); \
42 ctx->FIELD = val; \
43 return 1; \
44 }
45
46 /*
47 * Set certificate store containing trusted (root) CA certs and possibly CRLs
48 * and a cert verification callback function used for CMP server authentication.
49 * Any already existing store entry is freed. Given NULL, the entry is reset.
50 */
51 DEFINE_OSSL_set0_NAME(OSSL_CMP_CTX, trusted, trusted, X509_STORE)
52
53 DEFINE_OSSL_CMP_CTX_get0(libctx, OSSL_LIB_CTX)
54 DEFINE_OSSL_CMP_CTX_get0(propq, const char)
55
56 /* Get current list of non-trusted intermediate certs */
57 DEFINE_OSSL_CMP_CTX_get0(untrusted, STACK_OF(X509))
58
59 /*
60 * Set untrusted certificates for path construction in authentication of
61 * the CMP server and potentially others (TLS server, newly enrolled cert).
62 */
63 int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
64 {
65 STACK_OF(X509) *untrusted = NULL;
66
67 if (ctx == NULL) {
68 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
69 return 0;
70 }
71 if (!ossl_x509_add_certs_new(&untrusted, certs,
72 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
73 goto err;
74 OSSL_STACK_OF_X509_free(ctx->untrusted);
75 ctx->untrusted = untrusted;
76 return 1;
77 err:
78 OSSL_STACK_OF_X509_free(untrusted);
79 return 0;
80 }
81
82 static int cmp_ctx_set_md(OSSL_CMP_CTX *ctx, EVP_MD **pmd, int nid)
83 {
84 EVP_MD *md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propq);
85 /* fetching in advance to be able to throw error early if unsupported */
86
87 if (md == NULL) {
88 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_ALGORITHM);
89 return 0;
90 }
91 EVP_MD_free(*pmd);
92 *pmd = md;
93 return 1;
94 }
95
96 /*
97 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
98 * Returns new context on success, NULL on error
99 */
100 OSSL_CMP_CTX *OSSL_CMP_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
101 {
102 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
103
104 if (ctx == NULL)
105 goto err;
106
107 ctx->libctx = libctx;
108 if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
109 goto err;
110
111 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
112
113 ctx->status = OSSL_CMP_PKISTATUS_unspecified;
114 ctx->failInfoCode = -1;
115
116 ctx->keep_alive = 1;
117 ctx->msg_timeout = -1;
118 ctx->tls_used = -1; /* default for backward compatibility */
119
120 if ((ctx->untrusted = sk_X509_new_null()) == NULL) {
121 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
122 goto err;
123 }
124
125 ctx->pbm_slen = 16;
126 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
127 goto err;
128 ctx->pbm_itercnt = 500;
129 ctx->pbm_mac = NID_hmac_sha1;
130
131 if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
132 goto err;
133 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
134 ctx->revocationReason = CRL_REASON_NONE;
135
136 /* all other elements are initialized to 0 or NULL, respectively */
137 return ctx;
138
139 err:
140 OSSL_CMP_CTX_free(ctx);
141 return NULL;
142 }
143
144 #define OSSL_CMP_ITAVs_free(itavs) \
145 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
146 #define X509_EXTENSIONS_free(exts) \
147 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free)
148 #define OSSL_CMP_PKIFREETEXT_free(text) \
149 sk_ASN1_UTF8STRING_pop_free(text, ASN1_UTF8STRING_free)
150
151 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
152 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
153 {
154 if (ctx == NULL) {
155 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
156 return 0;
157 }
158
159 #ifndef OPENSSL_NO_HTTP
160 if (ctx->http_ctx != NULL) {
161 (void)OSSL_HTTP_close(ctx->http_ctx, 1);
162 ossl_cmp_debug(ctx, "disconnected from CMP server");
163 ctx->http_ctx = NULL;
164 }
165 #endif
166 ctx->status = OSSL_CMP_PKISTATUS_unspecified;
167 ctx->failInfoCode = -1;
168
169 OSSL_CMP_ITAVs_free(ctx->genm_ITAVs);
170 ctx->genm_ITAVs = NULL;
171
172 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
173 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
174 && ossl_cmp_ctx_set1_newChain(ctx, NULL)
175 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
176 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
177 && ossl_cmp_ctx_set1_validatedSrvCert(ctx, NULL)
178 && ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL)
179 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
180 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
181 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
182 }
183
184 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
185 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
186 {
187 if (ctx == NULL)
188 return;
189
190 #ifndef OPENSSL_NO_HTTP
191 if (ctx->http_ctx != NULL) {
192 (void)OSSL_HTTP_close(ctx->http_ctx, 1);
193 ossl_cmp_debug(ctx, "disconnected from CMP server");
194 }
195 #endif
196 OPENSSL_free(ctx->propq);
197 OPENSSL_free(ctx->serverPath);
198 OPENSSL_free(ctx->server);
199 OPENSSL_free(ctx->proxy);
200 OPENSSL_free(ctx->no_proxy);
201
202 X509_free(ctx->srvCert);
203 X509_free(ctx->validatedSrvCert);
204 X509_NAME_free(ctx->expected_sender);
205 X509_STORE_free(ctx->trusted);
206 OSSL_STACK_OF_X509_free(ctx->untrusted);
207
208 X509_free(ctx->cert);
209 OSSL_STACK_OF_X509_free(ctx->chain);
210 EVP_PKEY_free(ctx->pkey);
211 ASN1_OCTET_STRING_free(ctx->referenceValue);
212 if (ctx->secretValue != NULL)
213 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
214 ASN1_OCTET_STRING_free(ctx->secretValue);
215 EVP_MD_free(ctx->pbm_owf);
216
217 X509_NAME_free(ctx->recipient);
218 EVP_MD_free(ctx->digest);
219 ASN1_OCTET_STRING_free(ctx->transactionID);
220 ASN1_OCTET_STRING_free(ctx->senderNonce);
221 ASN1_OCTET_STRING_free(ctx->recipNonce);
222 ASN1_OCTET_STRING_free(ctx->first_senderNonce);
223 OSSL_CMP_ITAVs_free(ctx->geninfo_ITAVs);
224 OSSL_STACK_OF_X509_free(ctx->extraCertsOut);
225
226 EVP_PKEY_free(ctx->newPkey);
227 X509_NAME_free(ctx->issuer);
228 ASN1_INTEGER_free(ctx->serialNumber);
229 X509_NAME_free(ctx->subjectName);
230 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
231 X509_EXTENSIONS_free(ctx->reqExtensions);
232 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
233 X509_free(ctx->oldCert);
234 X509_REQ_free(ctx->p10CSR);
235
236 OSSL_CMP_ITAVs_free(ctx->genm_ITAVs);
237
238 OSSL_CMP_PKIFREETEXT_free(ctx->statusString);
239 X509_free(ctx->newCert);
240 OSSL_STACK_OF_X509_free(ctx->newChain);
241 OSSL_STACK_OF_X509_free(ctx->caPubs);
242 OSSL_STACK_OF_X509_free(ctx->extraCertsIn);
243
244 OPENSSL_free(ctx);
245 }
246
247 #define DEFINE_OSSL_set(PREFIX, FIELD, TYPE) \
248 int PREFIX##_set_##FIELD(OSSL_CMP_CTX *ctx, TYPE val) \
249 { \
250 if (ctx == NULL) { \
251 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
252 return 0; \
253 } \
254 ctx->FIELD = val; \
255 return 1; \
256 }
257
258 DEFINE_OSSL_set(ossl_cmp_ctx, status, int)
259
260 #define DEFINE_OSSL_get(PREFIX, FIELD, TYPE, ERR_RET) \
261 TYPE PREFIX##_get_##FIELD(const OSSL_CMP_CTX *ctx) \
262 { \
263 if (ctx == NULL) { \
264 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
265 return ERR_RET; \
266 } \
267 return ctx->FIELD; \
268 }
269
270 /*
271 * Returns the PKIStatus from the last CertRepMessage
272 * or Revocation Response or error message, -1 on error
273 */
274 DEFINE_OSSL_get(OSSL_CMP_CTX, status, int, -1)
275
276 /*
277 * Returns the statusString from the last CertRepMessage
278 * or Revocation Response or error message, NULL on error
279 */
280 DEFINE_OSSL_CMP_CTX_get0(statusString, OSSL_CMP_PKIFREETEXT)
281
282 DEFINE_OSSL_set0(ossl_cmp_ctx, statusString, OSSL_CMP_PKIFREETEXT)
283
284 /* Set callback function for checking if the cert is ok or should be rejected */
285 DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb, OSSL_CMP_certConf_cb_t)
286
287 /*
288 * Set argument, respectively a pointer to a structure containing arguments,
289 * optionally to be used by the certConf callback.
290 */
291 DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb_arg, void *)
292
293 /*
294 * Get argument, respectively the pointer to a structure containing arguments,
295 * optionally to be used by certConf callback.
296 * Returns callback argument set previously (NULL if not set or on error)
297 */
298 DEFINE_OSSL_get(OSSL_CMP_CTX, certConf_cb_arg, void *, NULL)
299
300 #ifndef OPENSSL_NO_TRACE
301 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
302 int category, int cmd, void *vdata)
303 {
304 OSSL_CMP_CTX *ctx = vdata;
305 const char *msg;
306 OSSL_CMP_severity level = -1;
307 char *func = NULL;
308 char *file = NULL;
309 int line = 0;
310
311 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
312 return 0;
313 if (ctx->log_cb == NULL)
314 return 1; /* silently drop message */
315
316 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
317
318 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
319 goto end; /* suppress output since severity is not sufficient */
320
321 if (!ctx->log_cb(func != NULL ? func : "(no func)",
322 file != NULL ? file : "(no file)",
323 line, level, msg))
324 cnt = 0;
325
326 end:
327 OPENSSL_free(func);
328 OPENSSL_free(file);
329 return cnt;
330 }
331 #endif
332
333 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
334 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
335 const char *func, const char *file, int line,
336 const char *level_str, const char *format, ...)
337 {
338 va_list args;
339 char hugebuf[1024 * 2];
340 int res = 0;
341
342 if (ctx == NULL || ctx->log_cb == NULL)
343 return 1; /* silently drop message */
344
345 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
346 return 1; /* suppress output since severity is not sufficient */
347
348 if (format == NULL)
349 return 0;
350
351 va_start(args, format);
352
353 if (func == NULL)
354 func = "(unset function name)";
355 if (file == NULL)
356 file = "(unset file name)";
357 if (level_str == NULL)
358 level_str = "(unset level string)";
359
360 #ifndef OPENSSL_NO_TRACE
361 if (OSSL_TRACE_ENABLED(CMP)) {
362 OSSL_TRACE_BEGIN(CMP)
363 {
364 int printed = BIO_snprintf(hugebuf, sizeof(hugebuf),
365 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
366 func, file, line, level_str);
367 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
368 if (BIO_vsnprintf(hugebuf + printed,
369 sizeof(hugebuf) - printed, format, args)
370 > 0)
371 res = BIO_puts(trc_out, hugebuf) > 0;
372 }
373 }
374 OSSL_TRACE_END(CMP);
375 }
376 #else /* compensate for disabled trace API */
377 {
378 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
379 res = ctx->log_cb(func, file, line, level, hugebuf);
380 }
381 #endif
382 va_end(args);
383 return res;
384 }
385
386 /* Set a callback function for error reporting and logging messages */
387 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
388 {
389 if (ctx == NULL) {
390 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
391 return 0;
392 }
393 ctx->log_cb = cb;
394
395 #ifndef OPENSSL_NO_TRACE
396 /* do also in case cb == NULL, to switch off logging output: */
397 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
398 ossl_cmp_log_trace_cb, ctx))
399 return 0;
400 #endif
401
402 return 1;
403 }
404
405 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
406 void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
407 {
408 if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
409 return; /* suppress output since severity is not sufficient */
410 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
411 }
412
413 /*
414 * Set or clear the reference value to be used for identification
415 * (i.e., the user name) when using PBMAC.
416 */
417 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
418 const unsigned char *ref, int len)
419 {
420 if (ctx == NULL) {
421 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
422 return 0;
423 }
424 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref, len);
425 }
426
427 /* Set or clear the password to be used for protecting messages with PBMAC */
428 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx,
429 const unsigned char *sec, int len)
430 {
431 ASN1_OCTET_STRING *secretValue = NULL;
432
433 if (ctx == NULL) {
434 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
435 return 0;
436 }
437 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
438 return 0;
439 if (ctx->secretValue != NULL) {
440 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
441 ASN1_OCTET_STRING_free(ctx->secretValue);
442 }
443 ctx->secretValue = secretValue;
444 return 1;
445 }
446
447 #define DEFINE_OSSL_CMP_CTX_get1_certs(FIELD) \
448 STACK_OF(X509) *OSSL_CMP_CTX_get1_##FIELD(const OSSL_CMP_CTX *ctx) \
449 { \
450 if (ctx == NULL) { \
451 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
452 return NULL; \
453 } \
454 return X509_chain_up_ref(ctx->FIELD); \
455 }
456
457 /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
458 DEFINE_OSSL_CMP_CTX_get1_certs(newChain)
459
460 #define DEFINE_OSSL_set1_certs(PREFIX, FIELD) \
461 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs) \
462 { \
463 if (ctx == NULL) { \
464 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
465 return 0; \
466 } \
467 OSSL_STACK_OF_X509_free(ctx->FIELD); \
468 ctx->FIELD = NULL; \
469 return certs == NULL || (ctx->FIELD = X509_chain_up_ref(certs)) != NULL; \
470 }
471
472 /*
473 * Copies any given stack of inbound X509 certificates to newChain
474 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
475 */
476 DEFINE_OSSL_set1_certs(ossl_cmp_ctx, newChain)
477
478 /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
479 DEFINE_OSSL_CMP_CTX_get1_certs(extraCertsIn)
480
481 /*
482 * Copies any given stack of inbound X509 certificates to extraCertsIn
483 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
484 */
485 DEFINE_OSSL_set1_certs(ossl_cmp_ctx, extraCertsIn)
486
487 /*
488 * Copies any given stack as the new stack of X509
489 * certificates to send out in the extraCerts field.
490 */
491 DEFINE_OSSL_set1_certs(OSSL_CMP_CTX, extraCertsOut)
492
493 /*
494 * Add the given policy info object
495 * to the X509_EXTENSIONS of the requested certificate template.
496 */
497 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
498 {
499 if (ctx == NULL || pinfo == NULL) {
500 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
501 return 0;
502 }
503
504 if (ctx->policies == NULL
505 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
506 return 0;
507
508 return sk_POLICYINFO_push(ctx->policies, pinfo);
509 }
510
511 /* Add an ITAV for geninfo of the PKI message header */
512 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
513 {
514 if (ctx == NULL) {
515 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
516 return 0;
517 }
518 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
519 }
520
521 int OSSL_CMP_CTX_reset_geninfo_ITAVs(OSSL_CMP_CTX *ctx)
522 {
523 if (ctx == NULL) {
524 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
525 return 0;
526 }
527 OSSL_CMP_ITAVs_free(ctx->geninfo_ITAVs);
528 ctx->geninfo_ITAVs = NULL;
529 return 1;
530 }
531
532 DEFINE_OSSL_CMP_CTX_get0(geninfo_ITAVs, STACK_OF(OSSL_CMP_ITAV))
533
534 /* Add an itav for the body of outgoing general messages */
535 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
536 {
537 if (ctx == NULL) {
538 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
539 return 0;
540 }
541 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
542 }
543
544 /*
545 * Returns a duplicate of the stack of X509 certificates that
546 * were received in the caPubs field of the last CertRepMessage.
547 * Returns NULL on error
548 */
549 DEFINE_OSSL_CMP_CTX_get1_certs(caPubs)
550
551 /*
552 * Copies any given stack of certificates to the given
553 * OSSL_CMP_CTX structure so that they may be retrieved later.
554 */
555 DEFINE_OSSL_set1_certs(ossl_cmp_ctx, caPubs)
556
557 #define char_dup OPENSSL_strdup
558 #define char_free OPENSSL_free
559 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
560 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
561 { \
562 TYPE *val_dup = NULL; \
563 \
564 if (ctx == NULL) { \
565 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
566 return 0; \
567 } \
568 \
569 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
570 return 0; \
571 TYPE##_free(ctx->FIELD); \
572 ctx->FIELD = val_dup; \
573 return 1; \
574 }
575
576 #define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
577 #define EVP_PKEY_invalid(key) 0
578
579 #define DEFINE_OSSL_set1_up_ref(PREFIX, FIELD, TYPE) \
580 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
581 { \
582 if (ctx == NULL) { \
583 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
584 return 0; \
585 } \
586 \
587 /* prevent misleading error later on malformed cert or provider issue */ \
588 if (val != NULL && TYPE##_invalid(val)) { \
589 ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
590 return 0; \
591 } \
592 if (val != NULL && !TYPE##_up_ref(val)) \
593 return 0; \
594 TYPE##_free(ctx->FIELD); \
595 ctx->FIELD = val; \
596 return 1; \
597 }
598
599 DEFINE_OSSL_set1_up_ref(ossl_cmp_ctx, validatedSrvCert, X509)
600
601 /*
602 * Pins the server certificate to be directly trusted (even if it is expired)
603 * for verifying response messages.
604 * Cert pointer is not consumed. It may be NULL to clear the entry.
605 */
606 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, srvCert, X509)
607
608 /* Set the X509 name of the recipient to be placed in the PKIHeader */
609 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
610
611 /* Store the X509 name of the expected sender in the PKIHeader of responses */
612 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
613
614 /* Set the X509 name of the issuer to be placed in the certTemplate */
615 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
616
617 /* Set the ASN1_INTEGER serial to be placed in the certTemplate for rr */
618 DEFINE_OSSL_CMP_CTX_set1(serialNumber, ASN1_INTEGER)
619 /*
620 * Set the subject name that will be placed in the certificate
621 * request. This will be the subject name on the received certificate.
622 */
623 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
624
625 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
626 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
627 {
628 if (ctx == NULL) {
629 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
630 return 0;
631 }
632
633 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
634 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
635 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
636 return 0;
637 }
638 X509_EXTENSIONS_free(ctx->reqExtensions);
639 ctx->reqExtensions = exts;
640 return 1;
641 }
642
643 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
644 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
645 {
646 if (ctx == NULL) {
647 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
648 return -1;
649 }
650 /* if one of the following conditions 'fail' this is not an error */
651 return ctx->reqExtensions != NULL
652 && X509v3_get_ext_by_NID(ctx->reqExtensions,
653 NID_subject_alt_name, -1)
654 >= 0;
655 }
656
657 /*
658 * Add a GENERAL_NAME structure that will be added to the CRMF
659 * request's extensions field to request subject alternative names.
660 */
661 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
662 const GENERAL_NAME *name)
663 {
664 GENERAL_NAME *name_dup;
665
666 if (ctx == NULL || name == NULL) {
667 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
668 return 0;
669 }
670
671 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
672 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
673 return 0;
674 }
675
676 if (ctx->subjectAltNames == NULL
677 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
678 return 0;
679 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
680 return 0;
681 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
682 GENERAL_NAME_free(name_dup);
683 return 0;
684 }
685 return 1;
686 }
687
688 /*
689 * Set our own client certificate, used for example in KUR and when
690 * doing the IR with existing certificate.
691 */
692 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, cert, X509)
693
694 int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
695 STACK_OF(X509) *candidates)
696 {
697 STACK_OF(X509) *chain;
698
699 if (ctx == NULL) {
700 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
701 return 0;
702 }
703
704 if (!ossl_x509_add_certs_new(&ctx->untrusted, candidates,
705 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
706 return 0;
707
708 ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
709 chain = X509_build_chain(ctx->cert, ctx->untrusted, own_trusted, 0,
710 ctx->libctx, ctx->propq);
711 if (chain == NULL) {
712 ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_BUILDING_OWN_CHAIN);
713 return 0;
714 }
715 ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
716 ctx->chain = chain;
717 return 1;
718 }
719
720 /*
721 * Set the old certificate that we are updating in KUR
722 * or the certificate to be revoked in RR, respectively.
723 * Also used as reference cert (defaulting to cert) for deriving subject DN
724 * and SANs. Its issuer is used as default recipient in the CMP message header.
725 */
726 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, oldCert, X509)
727
728 /* Set the PKCS#10 CSR to be sent in P10CR */
729 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
730
731 /*
732 * Set the (newly received in IP/KUP/CP) certificate in the context.
733 * This only permits for one cert to be enrolled at a time.
734 */
735 DEFINE_OSSL_set0(ossl_cmp_ctx, newCert, X509)
736
737 /* Get successfully validated sender cert, if any, of current transaction */
738 DEFINE_OSSL_CMP_CTX_get0(validatedSrvCert, X509)
739
740 /*
741 * Get the (newly received in IP/KUP/CP) client certificate from the context
742 * This only permits for one client cert to be received...
743 */
744 DEFINE_OSSL_CMP_CTX_get0(newCert, X509)
745
746 /* Set the client's current private key */
747 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, pkey, EVP_PKEY)
748
749 /* Set new key pair. Used e.g. when doing Key Update */
750 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
751 {
752 if (ctx == NULL) {
753 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
754 return 0;
755 }
756
757 EVP_PKEY_free(ctx->newPkey);
758 ctx->newPkey = pkey;
759 ctx->newPkey_priv = priv;
760 return 1;
761 }
762
763 /* Get the private/public key to use for cert enrollment, or NULL on error */
764 /* In case |priv| == 0, better use ossl_cmp_ctx_get0_newPubkey() below */
765 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
766 {
767 if (ctx == NULL) {
768 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
769 return NULL;
770 }
771
772 if (ctx->newPkey != NULL)
773 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
774 if (ctx->p10CSR != NULL)
775 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
776 return ctx->pkey; /* may be NULL */
777 }
778
779 EVP_PKEY *ossl_cmp_ctx_get0_newPubkey(const OSSL_CMP_CTX *ctx)
780 {
781 if (!ossl_assert(ctx != NULL))
782 return NULL;
783 if (ctx->newPkey != NULL)
784 return ctx->newPkey;
785 if (ctx->p10CSR != NULL)
786 return X509_REQ_get0_pubkey(ctx->p10CSR);
787 if (ctx->oldCert != NULL)
788 return X509_get0_pubkey(ctx->oldCert);
789 if (ctx->cert != NULL)
790 return X509_get0_pubkey(ctx->cert);
791 return ctx->pkey;
792 }
793
794 #define DEFINE_set1_ASN1_OCTET_STRING(PREFIX, FIELD) \
795 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, const ASN1_OCTET_STRING *id) \
796 { \
797 if (ctx == NULL) { \
798 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
799 return 0; \
800 } \
801 return ossl_cmp_asn1_octet_string_set1(&ctx->FIELD, id); \
802 }
803
804 /* Set the given transactionID to the context */
805 DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, transactionID)
806
807 /* Set the nonce to be used for the recipNonce in the message created next */
808 DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, recipNonce)
809
810 /* Stores the given nonce as the last senderNonce sent out */
811 DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, senderNonce)
812
813 /* store the first req sender nonce for verifying delayed delivery */
814 DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, first_senderNonce)
815
816 /* Set the proxy server to use for HTTP(S) connections */
817 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
818
819 /* Set the (HTTP) hostname of the CMP server */
820 DEFINE_OSSL_CMP_CTX_set1(server, char)
821
822 /* Set the server exclusion list of the HTTP proxy server */
823 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
824
825 #ifndef OPENSSL_NO_HTTP
826 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
827 DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb, OSSL_HTTP_bio_cb_t)
828
829 /* Set argument optionally to be used by the http connect/disconnect callback */
830 DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb_arg, void *)
831
832 /*
833 * Get argument optionally to be used by the http connect/disconnect callback
834 * Returns callback argument set previously (NULL if not set or on error)
835 */
836 DEFINE_OSSL_get(OSSL_CMP_CTX, http_cb_arg, void *, NULL)
837 #endif
838
839 /* Set callback function for sending CMP request and receiving response */
840 DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb, OSSL_CMP_transfer_cb_t)
841
842 /* Set argument optionally to be used by the transfer callback */
843 DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb_arg, void *)
844
845 /*
846 * Get argument optionally to be used by the transfer callback.
847 * Returns callback argument set previously (NULL if not set or on error)
848 */
849 DEFINE_OSSL_get(OSSL_CMP_CTX, transfer_cb_arg, void *, NULL)
850
851 /** Set the HTTP server port to be used */
852 DEFINE_OSSL_set(OSSL_CMP_CTX, serverPort, int)
853
854 /* Set the HTTP path to be used on the server (e.g "pkix/") */
855 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
856
857 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
858 DEFINE_OSSL_set(ossl_cmp_ctx, failInfoCode, int)
859
860 /*
861 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
862 * Returns bit string as integer on success, -1 on error
863 */
864 DEFINE_OSSL_get(OSSL_CMP_CTX, failInfoCode, int, -1)
865
866 /* Set a Boolean or integer option of the context to the "val" arg */
867 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
868 {
869 int min_val;
870
871 if (ctx == NULL) {
872 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
873 return 0;
874 }
875
876 switch (opt) {
877 case OSSL_CMP_OPT_REVOCATION_REASON:
878 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
879 break;
880 case OSSL_CMP_OPT_POPO_METHOD:
881 min_val = OSSL_CRMF_POPO_NONE;
882 break;
883 default:
884 min_val = 0;
885 break;
886 }
887 if (val < min_val) {
888 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_SMALL);
889 return 0;
890 }
891
892 switch (opt) {
893 case OSSL_CMP_OPT_LOG_VERBOSITY:
894 if (val > OSSL_CMP_LOG_MAX) {
895 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
896 return 0;
897 }
898 ctx->log_verbosity = val;
899 break;
900 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
901 ctx->implicitConfirm = val;
902 break;
903 case OSSL_CMP_OPT_DISABLE_CONFIRM:
904 ctx->disableConfirm = val;
905 break;
906 case OSSL_CMP_OPT_UNPROTECTED_SEND:
907 ctx->unprotectedSend = val;
908 break;
909 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
910 ctx->unprotectedErrors = val;
911 break;
912 case OSSL_CMP_OPT_NO_CACHE_EXTRACERTS:
913 ctx->noCacheExtraCerts = val;
914 break;
915 case OSSL_CMP_OPT_VALIDITY_DAYS:
916 ctx->days = val;
917 break;
918 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
919 ctx->SubjectAltName_nodefault = val;
920 break;
921 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
922 ctx->setSubjectAltNameCritical = val;
923 break;
924 case OSSL_CMP_OPT_POLICIES_CRITICAL:
925 ctx->setPoliciesCritical = val;
926 break;
927 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
928 ctx->ignore_keyusage = val;
929 break;
930 case OSSL_CMP_OPT_POPO_METHOD:
931 if (val > OSSL_CRMF_POPO_KEYAGREE) {
932 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
933 return 0;
934 }
935 ctx->popoMethod = val;
936 break;
937 case OSSL_CMP_OPT_DIGEST_ALGNID:
938 if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
939 return 0;
940 break;
941 case OSSL_CMP_OPT_OWF_ALGNID:
942 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
943 return 0;
944 break;
945 case OSSL_CMP_OPT_MAC_ALGNID:
946 ctx->pbm_mac = val;
947 break;
948 case OSSL_CMP_OPT_KEEP_ALIVE:
949 ctx->keep_alive = val;
950 break;
951 case OSSL_CMP_OPT_MSG_TIMEOUT:
952 ctx->msg_timeout = val;
953 break;
954 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
955 ctx->total_timeout = val;
956 break;
957 case OSSL_CMP_OPT_USE_TLS:
958 ctx->tls_used = val;
959 break;
960 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
961 ctx->permitTAInExtraCertsForIR = val;
962 break;
963 case OSSL_CMP_OPT_REVOCATION_REASON:
964 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
965 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
966 return 0;
967 }
968 ctx->revocationReason = val;
969 break;
970 default:
971 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
972 return 0;
973 }
974
975 return 1;
976 }
977
978 /*
979 * Reads a Boolean or integer option value from the context.
980 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
981 */
982 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
983 {
984 if (ctx == NULL) {
985 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
986 return -1;
987 }
988
989 switch (opt) {
990 case OSSL_CMP_OPT_LOG_VERBOSITY:
991 return ctx->log_verbosity;
992 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
993 return ctx->implicitConfirm;
994 case OSSL_CMP_OPT_DISABLE_CONFIRM:
995 return ctx->disableConfirm;
996 case OSSL_CMP_OPT_UNPROTECTED_SEND:
997 return ctx->unprotectedSend;
998 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
999 return ctx->unprotectedErrors;
1000 case OSSL_CMP_OPT_NO_CACHE_EXTRACERTS:
1001 return ctx->noCacheExtraCerts;
1002 case OSSL_CMP_OPT_VALIDITY_DAYS:
1003 return ctx->days;
1004 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1005 return ctx->SubjectAltName_nodefault;
1006 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1007 return ctx->setSubjectAltNameCritical;
1008 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1009 return ctx->setPoliciesCritical;
1010 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1011 return ctx->ignore_keyusage;
1012 case OSSL_CMP_OPT_POPO_METHOD:
1013 return ctx->popoMethod;
1014 case OSSL_CMP_OPT_DIGEST_ALGNID:
1015 return EVP_MD_get_type(ctx->digest);
1016 case OSSL_CMP_OPT_OWF_ALGNID:
1017 return EVP_MD_get_type(ctx->pbm_owf);
1018 case OSSL_CMP_OPT_MAC_ALGNID:
1019 return ctx->pbm_mac;
1020 case OSSL_CMP_OPT_KEEP_ALIVE:
1021 return ctx->keep_alive;
1022 case OSSL_CMP_OPT_MSG_TIMEOUT:
1023 return ctx->msg_timeout;
1024 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1025 return ctx->total_timeout;
1026 case OSSL_CMP_OPT_USE_TLS:
1027 return ctx->tls_used;
1028 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1029 return ctx->permitTAInExtraCertsForIR;
1030 case OSSL_CMP_OPT_REVOCATION_REASON:
1031 return ctx->revocationReason;
1032 default:
1033 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
1034 return -1;
1035 }
1036 }