2 * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include <openssl/trace.h>
13 #include <openssl/bio.h>
14 #include <openssl/ocsp.h> /* for OCSP_REVOKED_STATUS_* */
15 #include "crypto/x509.h" /* for x509v3_cache_extensions() */
17 #include "cmp_local.h"
19 /* explicit #includes not strictly needed since implied by the above: */
20 #include <openssl/cmp.h>
21 #include <openssl/crmf.h>
22 #include <openssl/err.h>
25 * Get current certificate store containing trusted root CA certs
27 X509_STORE *OSSL_CMP_CTX_get0_trustedStore(const OSSL_CMP_CTX *ctx)
30 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
37 * Set certificate store containing trusted (root) CA certs and possibly CRLs
38 * and a cert verification callback function used for CMP server authentication.
39 * Any already existing store entry is freed. Given NULL, the entry is reset.
41 int OSSL_CMP_CTX_set0_trustedStore(OSSL_CMP_CTX *ctx, X509_STORE *store)
44 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
47 X509_STORE_free(ctx->trusted);
52 /* Get current list of non-trusted intermediate certs */
53 STACK_OF(X509) *OSSL_CMP_CTX_get0_untrusted(const OSSL_CMP_CTX *ctx)
56 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
59 return ctx->untrusted;
63 * Set untrusted certificates for path construction in authentication of
64 * the CMP server and potentially others (TLS server, newly enrolled cert).
66 int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
68 STACK_OF(X509) *untrusted;
70 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
73 if ((untrusted = sk_X509_new_null()) == NULL)
75 if (X509_add_certs(untrusted, certs,
76 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP) != 1)
78 sk_X509_pop_free(ctx->untrusted, X509_free);
79 ctx->untrusted = untrusted;
82 sk_X509_pop_free(untrusted, X509_free);
86 static int cmp_ctx_set_md(OSSL_CMP_CTX *ctx, EVP_MD **pmd, int nid)
88 EVP_MD *md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propq);
89 /* fetching in advance to be able to throw error early if unsupported */
92 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_ALGORITHM);
101 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
102 * Returns new context on success, NULL on error
104 OSSL_CMP_CTX *OSSL_CMP_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
106 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
111 ctx->libctx = libctx;
112 if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
115 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
118 ctx->failInfoCode = -1;
120 ctx->msg_timeout = 2 * 60;
122 if ((ctx->untrusted = sk_X509_new_null()) == NULL)
126 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
128 ctx->pbm_itercnt = 500;
129 ctx->pbm_mac = NID_hmac_sha1;
131 if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
133 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
134 ctx->revocationReason = CRL_REASON_NONE;
136 /* all other elements are initialized to 0 or NULL, respectively */
140 OSSL_CMP_CTX_free(ctx);
141 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
145 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
146 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
149 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
154 ctx->failInfoCode = -1;
156 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
157 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
158 && ossl_cmp_ctx_set1_newChain(ctx, NULL)
159 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
160 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
161 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
162 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
163 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
164 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
167 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
168 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
173 OPENSSL_free(ctx->serverPath);
174 OPENSSL_free(ctx->server);
175 OPENSSL_free(ctx->proxy);
176 OPENSSL_free(ctx->no_proxy);
178 X509_free(ctx->srvCert);
179 X509_free(ctx->validatedSrvCert);
180 X509_NAME_free(ctx->expected_sender);
181 X509_STORE_free(ctx->trusted);
182 sk_X509_pop_free(ctx->untrusted, X509_free);
184 X509_free(ctx->cert);
185 sk_X509_pop_free(ctx->chain, X509_free);
186 EVP_PKEY_free(ctx->pkey);
187 ASN1_OCTET_STRING_free(ctx->referenceValue);
188 if (ctx->secretValue != NULL)
189 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
190 ASN1_OCTET_STRING_free(ctx->secretValue);
191 EVP_MD_free(ctx->pbm_owf);
193 X509_NAME_free(ctx->recipient);
194 EVP_MD_free(ctx->digest);
195 ASN1_OCTET_STRING_free(ctx->transactionID);
196 ASN1_OCTET_STRING_free(ctx->senderNonce);
197 ASN1_OCTET_STRING_free(ctx->recipNonce);
198 sk_OSSL_CMP_ITAV_pop_free(ctx->geninfo_ITAVs, OSSL_CMP_ITAV_free);
199 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
201 EVP_PKEY_free(ctx->newPkey);
202 X509_NAME_free(ctx->issuer);
203 X509_NAME_free(ctx->subjectName);
204 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
205 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
206 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
207 X509_free(ctx->oldCert);
208 X509_REQ_free(ctx->p10CSR);
210 sk_OSSL_CMP_ITAV_pop_free(ctx->genm_ITAVs, OSSL_CMP_ITAV_free);
212 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
213 X509_free(ctx->newCert);
214 sk_X509_pop_free(ctx->newChain, X509_free);
215 sk_X509_pop_free(ctx->caPubs, X509_free);
216 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
221 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX *ctx, int status)
223 if (!ossl_assert(ctx != NULL))
225 ctx->status = status;
230 * Returns the PKIStatus from the last CertRepMessage
231 * or Revocation Response or error message, -1 on error
233 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX *ctx)
236 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
243 * Returns the statusString from the last CertRepMessage
244 * or Revocation Response or error message, NULL on error
246 OSSL_CMP_PKIFREETEXT *OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX *ctx)
249 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
252 return ctx->statusString;
255 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX *ctx,
256 OSSL_CMP_PKIFREETEXT *text)
258 if (!ossl_assert(ctx != NULL))
260 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
261 ctx->statusString = text;
265 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
267 if (!ossl_assert(ctx != NULL))
269 X509_free(ctx->validatedSrvCert);
270 ctx->validatedSrvCert = cert;
274 /* Set callback function for checking if the cert is ok or should be rejected */
275 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_certConf_cb_t cb)
278 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
281 ctx->certConf_cb = cb;
286 * Set argument, respectively a pointer to a structure containing arguments,
287 * optionally to be used by the certConf callback.
289 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
292 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
295 ctx->certConf_cb_arg = arg;
300 * Get argument, respectively the pointer to a structure containing arguments,
301 * optionally to be used by certConf callback.
302 * Returns callback argument set previously (NULL if not set or on error)
304 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX *ctx)
307 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
310 return ctx->certConf_cb_arg;
313 #ifndef OPENSSL_NO_TRACE
314 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
315 int category, int cmd, void *vdata)
317 OSSL_CMP_CTX *ctx = vdata;
319 OSSL_CMP_severity level = -1;
324 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
326 if (ctx->log_cb == NULL)
327 return 1; /* silently drop message */
329 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
331 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
332 goto end; /* suppress output since severity is not sufficient */
334 if (!ctx->log_cb(func != NULL ? func : "(no func)",
335 file != NULL ? file : "(no file)",
346 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
347 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
348 const char *func, const char *file, int line,
349 const char *level_str, const char *format, ...)
352 char hugebuf[1024 * 2];
355 if (ctx == NULL || ctx->log_cb == NULL)
356 return 1; /* silently drop message */
358 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
359 return 1; /* suppress output since severity is not sufficient */
364 va_start(args, format);
367 func = "(unset function name)";
369 file = "(unset file name)";
370 if (level_str == NULL)
371 level_str = "(unset level string)";
373 #ifndef OPENSSL_NO_TRACE
374 if (OSSL_TRACE_ENABLED(CMP)) {
375 OSSL_TRACE_BEGIN(CMP) {
377 BIO_snprintf(hugebuf, sizeof(hugebuf),
378 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
379 func, file, line, level_str);
380 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
381 if (BIO_vsnprintf(hugebuf + printed,
382 sizeof(hugebuf) - printed, format, args) > 0)
383 res = BIO_puts(trc_out, hugebuf) > 0;
385 } OSSL_TRACE_END(CMP);
387 #else /* compensate for disabled trace API */
389 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
390 res = ctx->log_cb(func, file, line, level, hugebuf);
397 /* Set a callback function for error reporting and logging messages */
398 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
401 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
406 #ifndef OPENSSL_NO_TRACE
407 /* do also in case cb == NULL, to switch off logging output: */
408 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
409 ossl_cmp_log_trace_cb, ctx))
416 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
417 void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
419 if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
420 return; /* suppress output since severity is not sufficient */
421 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
425 * Set or clear the reference value to be used for identification
426 * (i.e., the user name) when using PBMAC.
428 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
429 const unsigned char *ref, int len)
432 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
435 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref,
439 /* Set or clear the password to be used for protecting messages with PBMAC */
440 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
443 ASN1_OCTET_STRING *secretValue = NULL;
445 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
448 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
450 if (ctx->secretValue != NULL) {
451 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
452 ASN1_OCTET_STRING_free(ctx->secretValue);
454 ctx->secretValue = secretValue;
458 /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
459 STACK_OF(X509) *OSSL_CMP_CTX_get1_newChain(const OSSL_CMP_CTX *ctx)
462 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
465 return X509_chain_up_ref(ctx->newChain);
469 * Copies any given stack of inbound X509 certificates to newChain
470 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
472 int ossl_cmp_ctx_set1_newChain(OSSL_CMP_CTX *ctx, STACK_OF(X509) *newChain)
474 if (!ossl_assert(ctx != NULL))
477 sk_X509_pop_free(ctx->newChain, X509_free);
478 ctx->newChain = NULL;
479 return newChain == NULL ||
480 (ctx->newChain = X509_chain_up_ref(newChain)) != NULL;
483 /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
484 STACK_OF(X509) *OSSL_CMP_CTX_get1_extraCertsIn(const OSSL_CMP_CTX *ctx)
487 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
490 return X509_chain_up_ref(ctx->extraCertsIn);
494 * Copies any given stack of inbound X509 certificates to extraCertsIn
495 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
497 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX *ctx,
498 STACK_OF(X509) *extraCertsIn)
500 if (!ossl_assert(ctx != NULL))
503 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
504 ctx->extraCertsIn = NULL;
505 return extraCertsIn == NULL
506 || (ctx->extraCertsIn = X509_chain_up_ref(extraCertsIn)) != NULL;
510 * Copies any given stack as the new stack of X509
511 * certificates to send out in the extraCerts field.
513 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX *ctx,
514 STACK_OF(X509) *extraCertsOut)
517 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
521 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
522 ctx->extraCertsOut = NULL;
523 return extraCertsOut == NULL
524 || (ctx->extraCertsOut = X509_chain_up_ref(extraCertsOut)) != NULL;
528 * Add the given policy info object
529 * to the X509_EXTENSIONS of the requested certificate template.
531 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
533 if (ctx == NULL || pinfo == NULL) {
534 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
538 if (ctx->policies == NULL
539 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
542 return sk_POLICYINFO_push(ctx->policies, pinfo);
545 /* Add an ITAV for geninfo of the PKI message header */
546 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
549 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
552 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
555 /* Add an itav for the body of outgoing general messages */
556 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
559 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
562 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
566 * Returns a duplicate of the stack of X509 certificates that
567 * were received in the caPubs field of the last CertRepMessage.
568 * Returns NULL on error
570 STACK_OF(X509) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX *ctx)
573 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
576 return X509_chain_up_ref(ctx->caPubs);
580 * Copies any given stack of certificates to the given
581 * OSSL_CMP_CTX structure so that they may be retrieved later.
583 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *caPubs)
585 if (!ossl_assert(ctx != NULL))
588 sk_X509_pop_free(ctx->caPubs, X509_free);
590 return caPubs == NULL || (ctx->caPubs = X509_chain_up_ref(caPubs)) != NULL;
593 #define char_dup OPENSSL_strdup
594 #define char_free OPENSSL_free
595 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
596 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
598 TYPE *val_dup = NULL; \
601 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
605 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
607 TYPE##_free(ctx->FIELD); \
608 ctx->FIELD = val_dup; \
612 #define X509_invalid(cert) (!x509v3_cache_extensions(cert))
613 #define EVP_PKEY_invalid(key) 0
614 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
615 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
618 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
622 /* prevent misleading error later on malformed cert or provider issue */ \
623 if (val != NULL && TYPE##_invalid(val)) { \
624 ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
627 if (val != NULL && !TYPE##_up_ref(val)) \
629 TYPE##_free(ctx->FIELD); \
635 * Pins the server certificate to be directly trusted (even if it is expired)
636 * for verifying response messages.
637 * Cert pointer is not consumed. It may be NULL to clear the entry.
639 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert, X509)
641 /* Set the X509 name of the recipient. Set in the PKIHeader */
642 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
644 /* Store the X509 name of the expected sender in the PKIHeader of responses */
645 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
647 /* Set the X509 name of the issuer. Set in the PKIHeader */
648 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
651 * Set the subject name that will be placed in the certificate
652 * request. This will be the subject name on the received certificate.
654 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
656 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
657 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
660 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
664 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
665 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
666 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
669 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
670 ctx->reqExtensions = exts;
674 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
675 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
678 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
681 /* if one of the following conditions 'fail' this is not an error */
682 return ctx->reqExtensions != NULL
683 && X509v3_get_ext_by_NID(ctx->reqExtensions,
684 NID_subject_alt_name, -1) >= 0;
688 * Add a GENERAL_NAME structure that will be added to the CRMF
689 * request's extensions field to request subject alternative names.
691 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
692 const GENERAL_NAME *name)
694 GENERAL_NAME *name_dup;
696 if (ctx == NULL || name == NULL) {
697 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
701 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
702 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
706 if (ctx->subjectAltNames == NULL
707 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
709 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
711 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
712 GENERAL_NAME_free(name_dup);
719 * Set our own client certificate, used for example in KUR and when
720 * doing the IR with existing certificate.
722 DEFINE_OSSL_CMP_CTX_set1_up_ref(cert, X509)
724 int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
725 STACK_OF(X509) *candidates)
727 STACK_OF(X509) *chain;
730 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
734 if (ctx->untrusted != NULL ?
735 !X509_add_certs(ctx->untrusted, candidates,
736 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP) :
737 !OSSL_CMP_CTX_set1_untrusted(ctx, candidates))
740 ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
741 chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq, own_trusted,
742 ctx->untrusted, ctx->cert);
744 ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_BUILDING_OWN_CHAIN);
747 ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
753 * Set the old certificate that we are updating in KUR
754 * or the certificate to be revoked in RR, respectively.
755 * Also used as reference cert (defaulting to cert) for deriving subject DN
756 * and SANs. Its issuer is used as default recipient in the CMP message header.
758 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert, X509)
760 /* Set the PKCS#10 CSR to be sent in P10CR */
761 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
764 * Set the (newly received in IP/KUP/CP) certificate in the context.
765 * TODO: this only permits for one cert to be enrolled at a time.
767 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX *ctx, X509 *cert)
769 if (!ossl_assert(ctx != NULL))
772 X509_free(ctx->newCert);
778 * Get the (newly received in IP/KUP/CP) client certificate from the context
779 * TODO: this only permits for one client cert to be received...
781 X509 *OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX *ctx)
784 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
790 /* Set the client's current private key */
791 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey, EVP_PKEY)
793 /* Set new key pair. Used e.g. when doing Key Update */
794 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
797 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
801 EVP_PKEY_free(ctx->newPkey);
803 ctx->newPkey_priv = priv;
807 /* Get the private/public key to use for cert enrollment, or NULL on error */
808 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
811 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
815 if (ctx->newPkey != NULL)
816 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
817 if (ctx->p10CSR != NULL)
818 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
819 return ctx->pkey; /* may be NULL */
822 /* Set the given transactionID to the context */
823 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX *ctx,
824 const ASN1_OCTET_STRING *id)
827 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
830 return ossl_cmp_asn1_octet_string_set1(&ctx->transactionID, id);
833 /* Set the nonce to be used for the recipNonce in the message created next */
834 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX *ctx,
835 const ASN1_OCTET_STRING *nonce)
837 if (!ossl_assert(ctx != NULL))
839 return ossl_cmp_asn1_octet_string_set1(&ctx->recipNonce, nonce);
842 /* Stores the given nonce as the last senderNonce sent out */
843 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
844 const ASN1_OCTET_STRING *nonce)
847 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
850 return ossl_cmp_asn1_octet_string_set1(&ctx->senderNonce, nonce);
853 /* Set the proxy server to use for HTTP(S) connections */
854 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
856 /* Set the (HTTP) host name of the CMP server */
857 DEFINE_OSSL_CMP_CTX_set1(server, char)
859 /* Set the server exclusion list of the HTTP proxy server */
860 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
862 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
863 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX *ctx, OSSL_HTTP_bio_cb_t cb)
866 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
873 /* Set argument optionally to be used by the http connect/disconnect callback */
874 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
877 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
880 ctx->http_cb_arg = arg;
885 * Get argument optionally to be used by the http connect/disconnect callback
886 * Returns callback argument set previously (NULL if not set or on error)
888 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX *ctx)
891 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
894 return ctx->http_cb_arg;
897 /* Set callback function for sending CMP request and receiving response */
898 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_transfer_cb_t cb)
901 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
904 ctx->transfer_cb = cb;
908 /* Set argument optionally to be used by the transfer callback */
909 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
912 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
915 ctx->transfer_cb_arg = arg;
920 * Get argument optionally to be used by the transfer callback.
921 * Returns callback argument set previously (NULL if not set or on error)
923 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX *ctx)
926 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
929 return ctx->transfer_cb_arg;
932 /** Set the HTTP server port to be used */
933 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX *ctx, int port)
936 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
939 ctx->serverPort = port;
943 /* Set the HTTP path to be used on the server (e.g "pkix/") */
944 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
946 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
947 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX *ctx, int fail_info)
949 if (!ossl_assert(ctx != NULL))
951 ctx->failInfoCode = fail_info;
956 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
957 * Returns bit string as integer on success, -1 on error
959 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX *ctx)
962 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
965 return ctx->failInfoCode;
968 /* Set a Boolean or integer option of the context to the "val" arg */
969 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
974 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
979 case OSSL_CMP_OPT_REVOCATION_REASON:
980 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
982 case OSSL_CMP_OPT_POPO_METHOD:
983 min_val = OSSL_CRMF_POPO_NONE;
990 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_SMALL);
995 case OSSL_CMP_OPT_LOG_VERBOSITY:
996 if (val > OSSL_CMP_LOG_MAX) {
997 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
1000 ctx->log_verbosity = val;
1002 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1003 ctx->implicitConfirm = val;
1005 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1006 ctx->disableConfirm = val;
1008 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1009 ctx->unprotectedSend = val;
1011 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1012 ctx->unprotectedErrors = val;
1014 case OSSL_CMP_OPT_VALIDITY_DAYS:
1017 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1018 ctx->SubjectAltName_nodefault = val;
1020 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1021 ctx->setSubjectAltNameCritical = val;
1023 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1024 ctx->setPoliciesCritical = val;
1026 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1027 ctx->ignore_keyusage = val;
1029 case OSSL_CMP_OPT_POPO_METHOD:
1030 if (val > OSSL_CRMF_POPO_KEYAGREE) {
1031 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
1034 ctx->popoMethod = val;
1036 case OSSL_CMP_OPT_DIGEST_ALGNID:
1037 if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
1040 case OSSL_CMP_OPT_OWF_ALGNID:
1041 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
1044 case OSSL_CMP_OPT_MAC_ALGNID:
1047 case OSSL_CMP_OPT_MSG_TIMEOUT:
1048 ctx->msg_timeout = val;
1050 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1051 ctx->total_timeout = val;
1053 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1054 ctx->permitTAInExtraCertsForIR = val;
1056 case OSSL_CMP_OPT_REVOCATION_REASON:
1057 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
1058 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
1061 ctx->revocationReason = val;
1064 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
1072 * Reads a Boolean or integer option value from the context.
1073 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
1075 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
1078 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
1083 case OSSL_CMP_OPT_LOG_VERBOSITY:
1084 return ctx->log_verbosity;
1085 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1086 return ctx->implicitConfirm;
1087 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1088 return ctx->disableConfirm;
1089 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1090 return ctx->unprotectedSend;
1091 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1092 return ctx->unprotectedErrors;
1093 case OSSL_CMP_OPT_VALIDITY_DAYS:
1095 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1096 return ctx->SubjectAltName_nodefault;
1097 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1098 return ctx->setSubjectAltNameCritical;
1099 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1100 return ctx->setPoliciesCritical;
1101 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1102 return ctx->ignore_keyusage;
1103 case OSSL_CMP_OPT_POPO_METHOD:
1104 return ctx->popoMethod;
1105 case OSSL_CMP_OPT_DIGEST_ALGNID:
1106 return EVP_MD_type(ctx->digest);
1107 case OSSL_CMP_OPT_OWF_ALGNID:
1108 return EVP_MD_type(ctx->pbm_owf);
1109 case OSSL_CMP_OPT_MAC_ALGNID:
1110 return ctx->pbm_mac;
1111 case OSSL_CMP_OPT_MSG_TIMEOUT:
1112 return ctx->msg_timeout;
1113 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1114 return ctx->total_timeout;
1115 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1116 return ctx->permitTAInExtraCertsForIR;
1117 case OSSL_CMP_OPT_REVOCATION_REASON:
1118 return ctx->revocationReason;
1120 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);