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_* */
16 #include "cmp_local.h"
18 /* explicit #includes not strictly needed since implied by the above: */
19 #include <openssl/cmp.h>
20 #include <openssl/crmf.h>
21 #include <openssl/err.h>
23 #define DEFINE_OSSL_CMP_CTX_get0(FIELD, TYPE) \
24 DEFINE_OSSL_CMP_CTX_get0_NAME(FIELD, FIELD, TYPE)
25 #define DEFINE_OSSL_CMP_CTX_get0_NAME(NAME, FIELD, TYPE) \
26 TYPE *OSSL_CMP_CTX_get0_##NAME(const OSSL_CMP_CTX *ctx) \
29 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
36 * Get current certificate store containing trusted root CA certs
38 DEFINE_OSSL_CMP_CTX_get0_NAME(trustedStore, trusted, X509_STORE)
40 #define DEFINE_OSSL_set0(PREFIX, FIELD, TYPE) \
41 DEFINE_OSSL_set0_NAME(PREFIX, FIELD, FIELD, TYPE)
42 #define DEFINE_OSSL_set0_NAME(PREFIX, NAME, FIELD, TYPE) \
43 int PREFIX##_set0##_##NAME(OSSL_CMP_CTX *ctx, TYPE *val) \
46 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
49 TYPE##_free(ctx->FIELD); \
55 * Set certificate store containing trusted (root) CA certs and possibly CRLs
56 * and a cert verification callback function used for CMP server authentication.
57 * Any already existing store entry is freed. Given NULL, the entry is reset.
59 DEFINE_OSSL_set0_NAME(OSSL_CMP_CTX, trustedStore, trusted, X509_STORE)
61 /* Get current list of non-trusted intermediate certs */
62 DEFINE_OSSL_CMP_CTX_get0(untrusted, STACK_OF(X509))
65 * Set untrusted certificates for path construction in authentication of
66 * the CMP server and potentially others (TLS server, newly enrolled cert).
68 int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
70 STACK_OF(X509) *untrusted = NULL;
73 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
76 if (!ossl_x509_add_certs_new(&untrusted, certs,
77 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
79 OSSL_STACK_OF_X509_free(ctx->untrusted);
80 ctx->untrusted = untrusted;
83 OSSL_STACK_OF_X509_free(untrusted);
87 static int cmp_ctx_set_md(OSSL_CMP_CTX *ctx, EVP_MD **pmd, int nid)
89 EVP_MD *md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propq);
90 /* fetching in advance to be able to throw error early if unsupported */
93 ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_ALGORITHM);
102 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
103 * Returns new context on success, NULL on error
105 OSSL_CMP_CTX *OSSL_CMP_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
107 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
112 ctx->libctx = libctx;
113 if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
116 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
119 ctx->failInfoCode = -1;
122 ctx->msg_timeout = -1;
124 if ((ctx->untrusted = sk_X509_new_null()) == NULL)
128 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
130 ctx->pbm_itercnt = 500;
131 ctx->pbm_mac = NID_hmac_sha1;
133 if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
135 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
136 ctx->revocationReason = CRL_REASON_NONE;
138 /* all other elements are initialized to 0 or NULL, respectively */
142 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
144 OSSL_CMP_CTX_free(ctx);
148 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
149 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
152 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
156 if (ctx->http_ctx != NULL) {
157 (void)OSSL_HTTP_close(ctx->http_ctx, 1);
158 ossl_cmp_debug(ctx, "disconnected from CMP server");
159 ctx->http_ctx = NULL;
162 ctx->failInfoCode = -1;
164 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
165 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
166 && ossl_cmp_ctx_set1_newChain(ctx, NULL)
167 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
168 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
169 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
170 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
171 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
172 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
175 #define OSSL_CMP_ITAVs_free(itavs) \
176 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
177 #define X509_EXTENSIONS_free(exts) \
178 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free)
179 #define OSSL_CMP_PKIFREETEXT_free(text) \
180 sk_ASN1_UTF8STRING_pop_free(text, ASN1_UTF8STRING_free)
182 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
183 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
188 if (ctx->http_ctx != NULL) {
189 (void)OSSL_HTTP_close(ctx->http_ctx, 1);
190 ossl_cmp_debug(ctx, "disconnected from CMP server");
192 OPENSSL_free(ctx->propq);
193 OPENSSL_free(ctx->serverPath);
194 OPENSSL_free(ctx->server);
195 OPENSSL_free(ctx->proxy);
196 OPENSSL_free(ctx->no_proxy);
198 X509_free(ctx->srvCert);
199 X509_free(ctx->validatedSrvCert);
200 X509_NAME_free(ctx->expected_sender);
201 X509_STORE_free(ctx->trusted);
202 OSSL_STACK_OF_X509_free(ctx->untrusted);
204 X509_free(ctx->cert);
205 OSSL_STACK_OF_X509_free(ctx->chain);
206 EVP_PKEY_free(ctx->pkey);
207 ASN1_OCTET_STRING_free(ctx->referenceValue);
208 if (ctx->secretValue != NULL)
209 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
210 ASN1_OCTET_STRING_free(ctx->secretValue);
211 EVP_MD_free(ctx->pbm_owf);
213 X509_NAME_free(ctx->recipient);
214 EVP_MD_free(ctx->digest);
215 ASN1_OCTET_STRING_free(ctx->transactionID);
216 ASN1_OCTET_STRING_free(ctx->senderNonce);
217 ASN1_OCTET_STRING_free(ctx->recipNonce);
218 OSSL_CMP_ITAVs_free(ctx->geninfo_ITAVs);
219 OSSL_STACK_OF_X509_free(ctx->extraCertsOut);
221 EVP_PKEY_free(ctx->newPkey);
222 X509_NAME_free(ctx->issuer);
223 X509_NAME_free(ctx->subjectName);
224 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
225 X509_EXTENSIONS_free(ctx->reqExtensions);
226 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
227 X509_free(ctx->oldCert);
228 X509_REQ_free(ctx->p10CSR);
230 OSSL_CMP_ITAVs_free(ctx->genm_ITAVs);
232 OSSL_CMP_PKIFREETEXT_free(ctx->statusString);
233 X509_free(ctx->newCert);
234 OSSL_STACK_OF_X509_free(ctx->newChain);
235 OSSL_STACK_OF_X509_free(ctx->caPubs);
236 OSSL_STACK_OF_X509_free(ctx->extraCertsIn);
241 #define DEFINE_OSSL_set(PREFIX, FIELD, TYPE) \
242 int PREFIX##_set_##FIELD(OSSL_CMP_CTX *ctx, TYPE val) \
245 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
252 DEFINE_OSSL_set(ossl_cmp_ctx, status, int)
254 #define DEFINE_OSSL_get(PREFIX, FIELD, TYPE, ERR_RET) \
255 TYPE PREFIX##_get_##FIELD(const OSSL_CMP_CTX *ctx) \
258 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
265 * Returns the PKIStatus from the last CertRepMessage
266 * or Revocation Response or error message, -1 on error
268 DEFINE_OSSL_get(OSSL_CMP_CTX, status, int, -1)
271 * Returns the statusString from the last CertRepMessage
272 * or Revocation Response or error message, NULL on error
274 DEFINE_OSSL_CMP_CTX_get0(statusString, OSSL_CMP_PKIFREETEXT)
276 DEFINE_OSSL_set0(ossl_cmp_ctx, statusString, OSSL_CMP_PKIFREETEXT)
278 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
280 if (!ossl_assert(ctx != NULL))
282 X509_free(ctx->validatedSrvCert);
283 ctx->validatedSrvCert = cert;
287 /* Set callback function for checking if the cert is ok or should be rejected */
288 DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb, OSSL_CMP_certConf_cb_t)
291 * Set argument, respectively a pointer to a structure containing arguments,
292 * optionally to be used by the certConf callback.
294 DEFINE_OSSL_set(OSSL_CMP_CTX, certConf_cb_arg, void *)
297 * Get argument, respectively the pointer to a structure containing arguments,
298 * optionally to be used by certConf callback.
299 * Returns callback argument set previously (NULL if not set or on error)
301 DEFINE_OSSL_get(OSSL_CMP_CTX, certConf_cb_arg, void *, NULL)
303 #ifndef OPENSSL_NO_TRACE
304 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
305 int category, int cmd, void *vdata)
307 OSSL_CMP_CTX *ctx = vdata;
309 OSSL_CMP_severity level = -1;
314 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
316 if (ctx->log_cb == NULL)
317 return 1; /* silently drop message */
319 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
321 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
322 goto end; /* suppress output since severity is not sufficient */
324 if (!ctx->log_cb(func != NULL ? func : "(no func)",
325 file != NULL ? file : "(no file)",
336 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
337 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
338 const char *func, const char *file, int line,
339 const char *level_str, const char *format, ...)
342 char hugebuf[1024 * 2];
345 if (ctx == NULL || ctx->log_cb == NULL)
346 return 1; /* silently drop message */
348 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
349 return 1; /* suppress output since severity is not sufficient */
354 va_start(args, format);
357 func = "(unset function name)";
359 file = "(unset file name)";
360 if (level_str == NULL)
361 level_str = "(unset level string)";
363 #ifndef OPENSSL_NO_TRACE
364 if (OSSL_TRACE_ENABLED(CMP)) {
365 OSSL_TRACE_BEGIN(CMP) {
367 BIO_snprintf(hugebuf, sizeof(hugebuf),
368 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
369 func, file, line, level_str);
370 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
371 if (BIO_vsnprintf(hugebuf + printed,
372 sizeof(hugebuf) - printed, format, args) > 0)
373 res = BIO_puts(trc_out, hugebuf) > 0;
375 } OSSL_TRACE_END(CMP);
377 #else /* compensate for disabled trace API */
379 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
380 res = ctx->log_cb(func, file, line, level, hugebuf);
387 /* Set a callback function for error reporting and logging messages */
388 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
391 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
396 #ifndef OPENSSL_NO_TRACE
397 /* do also in case cb == NULL, to switch off logging output: */
398 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
399 ossl_cmp_log_trace_cb, ctx))
406 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
407 void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
409 if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
410 return; /* suppress output since severity is not sufficient */
411 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
415 * Set or clear the reference value to be used for identification
416 * (i.e., the user name) when using PBMAC.
418 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
419 const unsigned char *ref, int len)
422 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
426 ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref, len);
429 /* Set or clear the password to be used for protecting messages with PBMAC */
430 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
433 ASN1_OCTET_STRING *secretValue = NULL;
436 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
439 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
441 if (ctx->secretValue != NULL) {
442 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
443 ASN1_OCTET_STRING_free(ctx->secretValue);
445 ctx->secretValue = secretValue;
449 #define DEFINE_OSSL_CMP_CTX_get1_certs(FIELD) \
450 STACK_OF(X509) *OSSL_CMP_CTX_get1_##FIELD(const OSSL_CMP_CTX *ctx) \
453 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
456 return X509_chain_up_ref(ctx->FIELD); \
459 /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
460 DEFINE_OSSL_CMP_CTX_get1_certs(newChain)
462 #define DEFINE_OSSL_set1_certs(PREFIX, FIELD) \
463 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs) \
466 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
469 OSSL_STACK_OF_X509_free(ctx->FIELD); \
471 return certs == NULL || (ctx->FIELD = X509_chain_up_ref(certs)) != NULL; \
475 * Copies any given stack of inbound X509 certificates to newChain
476 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
478 DEFINE_OSSL_set1_certs(ossl_cmp_ctx, newChain)
480 /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
481 DEFINE_OSSL_CMP_CTX_get1_certs(extraCertsIn)
484 * Copies any given stack of inbound X509 certificates to extraCertsIn
485 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
487 DEFINE_OSSL_set1_certs(ossl_cmp_ctx, extraCertsIn)
490 * Copies any given stack as the new stack of X509
491 * certificates to send out in the extraCerts field.
493 DEFINE_OSSL_set1_certs(OSSL_CMP_CTX, extraCertsOut)
496 * Add the given policy info object
497 * to the X509_EXTENSIONS of the requested certificate template.
499 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
501 if (ctx == NULL || pinfo == NULL) {
502 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
506 if (ctx->policies == NULL
507 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
510 return sk_POLICYINFO_push(ctx->policies, pinfo);
513 /* Add an ITAV for geninfo of the PKI message header */
514 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
517 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
520 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
523 /* Add an itav for the body of outgoing general messages */
524 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
527 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
530 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
534 * Returns a duplicate of the stack of X509 certificates that
535 * were received in the caPubs field of the last CertRepMessage.
536 * Returns NULL on error
538 DEFINE_OSSL_CMP_CTX_get1_certs(caPubs)
541 * Copies any given stack of certificates to the given
542 * OSSL_CMP_CTX structure so that they may be retrieved later.
544 DEFINE_OSSL_set1_certs(ossl_cmp_ctx, caPubs)
546 #define char_dup OPENSSL_strdup
547 #define char_free OPENSSL_free
548 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
549 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
551 TYPE *val_dup = NULL; \
554 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
558 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
560 TYPE##_free(ctx->FIELD); \
561 ctx->FIELD = val_dup; \
565 #define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
566 #define EVP_PKEY_invalid(key) 0
568 #define DEFINE_OSSL_set1_up_ref(PREFIX, FIELD, TYPE) \
569 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
572 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
576 /* prevent misleading error later on malformed cert or provider issue */ \
577 if (val != NULL && TYPE##_invalid(val)) { \
578 ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
581 if (val != NULL && !TYPE##_up_ref(val)) \
583 TYPE##_free(ctx->FIELD); \
589 * Pins the server certificate to be directly trusted (even if it is expired)
590 * for verifying response messages.
591 * Cert pointer is not consumed. It may be NULL to clear the entry.
593 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, srvCert, X509)
595 /* Set the X509 name of the recipient. Set in the PKIHeader */
596 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
598 /* Store the X509 name of the expected sender in the PKIHeader of responses */
599 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
601 /* Set the X509 name of the issuer. Set in the PKIHeader */
602 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
605 * Set the subject name that will be placed in the certificate
606 * request. This will be the subject name on the received certificate.
608 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
610 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
611 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
614 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
618 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
619 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
620 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
623 X509_EXTENSIONS_free(ctx->reqExtensions);
624 ctx->reqExtensions = exts;
628 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
629 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
632 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
635 /* if one of the following conditions 'fail' this is not an error */
636 return ctx->reqExtensions != NULL
637 && X509v3_get_ext_by_NID(ctx->reqExtensions,
638 NID_subject_alt_name, -1) >= 0;
642 * Add a GENERAL_NAME structure that will be added to the CRMF
643 * request's extensions field to request subject alternative names.
645 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
646 const GENERAL_NAME *name)
648 GENERAL_NAME *name_dup;
650 if (ctx == NULL || name == NULL) {
651 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
655 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
656 ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_SAN_SOURCES);
660 if (ctx->subjectAltNames == NULL
661 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
663 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
665 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
666 GENERAL_NAME_free(name_dup);
673 * Set our own client certificate, used for example in KUR and when
674 * doing the IR with existing certificate.
676 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, cert, X509)
678 int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
679 STACK_OF(X509) *candidates)
681 STACK_OF(X509) *chain;
684 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
688 if (!ossl_x509_add_certs_new(&ctx->untrusted, candidates,
689 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
692 ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
693 chain = X509_build_chain(ctx->cert, ctx->untrusted, own_trusted, 0,
694 ctx->libctx, ctx->propq);
696 ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_BUILDING_OWN_CHAIN);
699 ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
705 * Set the old certificate that we are updating in KUR
706 * or the certificate to be revoked in RR, respectively.
707 * Also used as reference cert (defaulting to cert) for deriving subject DN
708 * and SANs. Its issuer is used as default recipient in the CMP message header.
710 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, oldCert, X509)
712 /* Set the PKCS#10 CSR to be sent in P10CR */
713 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
716 * Set the (newly received in IP/KUP/CP) certificate in the context.
717 * This only permits for one cert to be enrolled at a time.
719 DEFINE_OSSL_set0(ossl_cmp_ctx, newCert, X509)
722 * Get the (newly received in IP/KUP/CP) client certificate from the context
723 * This only permits for one client cert to be received...
725 DEFINE_OSSL_CMP_CTX_get0(newCert, X509)
727 /* Set the client's current private key */
728 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX, pkey, EVP_PKEY)
730 /* Set new key pair. Used e.g. when doing Key Update */
731 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
734 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
738 EVP_PKEY_free(ctx->newPkey);
740 ctx->newPkey_priv = priv;
744 /* Get the private/public key to use for cert enrollment, or NULL on error */
745 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
748 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
752 if (ctx->newPkey != NULL)
753 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
754 if (ctx->p10CSR != NULL)
755 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
756 return ctx->pkey; /* may be NULL */
759 #define DEFINE_set1_ASN1_OCTET_STRING(PREFIX, FIELD) \
760 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, const ASN1_OCTET_STRING *id) \
763 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
766 return ossl_cmp_asn1_octet_string_set1(&ctx->FIELD, id); \
769 /* Set the given transactionID to the context */
770 DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, transactionID)
772 /* Set the nonce to be used for the recipNonce in the message created next */
773 DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx, recipNonce)
775 /* Stores the given nonce as the last senderNonce sent out */
776 DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX, senderNonce)
778 /* Set the proxy server to use for HTTP(S) connections */
779 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
781 /* Set the (HTTP) host name of the CMP server */
782 DEFINE_OSSL_CMP_CTX_set1(server, char)
784 /* Set the server exclusion list of the HTTP proxy server */
785 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
787 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
788 DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb, OSSL_HTTP_bio_cb_t)
790 /* Set argument optionally to be used by the http connect/disconnect callback */
791 DEFINE_OSSL_set(OSSL_CMP_CTX, http_cb_arg, void *)
794 * Get argument optionally to be used by the http connect/disconnect callback
795 * Returns callback argument set previously (NULL if not set or on error)
797 DEFINE_OSSL_get(OSSL_CMP_CTX, http_cb_arg, void *, NULL)
799 /* Set callback function for sending CMP request and receiving response */
800 DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb, OSSL_CMP_transfer_cb_t)
802 /* Set argument optionally to be used by the transfer callback */
803 DEFINE_OSSL_set(OSSL_CMP_CTX, transfer_cb_arg, void *)
806 * Get argument optionally to be used by the transfer callback.
807 * Returns callback argument set previously (NULL if not set or on error)
809 DEFINE_OSSL_get(OSSL_CMP_CTX, transfer_cb_arg, void *, NULL)
811 /** Set the HTTP server port to be used */
812 DEFINE_OSSL_set(OSSL_CMP_CTX, serverPort, int)
814 /* Set the HTTP path to be used on the server (e.g "pkix/") */
815 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
817 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
818 DEFINE_OSSL_set(ossl_cmp_ctx, failInfoCode, int)
821 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
822 * Returns bit string as integer on success, -1 on error
824 DEFINE_OSSL_get(OSSL_CMP_CTX, failInfoCode, int, -1)
826 /* Set a Boolean or integer option of the context to the "val" arg */
827 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
832 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
837 case OSSL_CMP_OPT_REVOCATION_REASON:
838 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
840 case OSSL_CMP_OPT_POPO_METHOD:
841 min_val = OSSL_CRMF_POPO_NONE;
848 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_SMALL);
853 case OSSL_CMP_OPT_LOG_VERBOSITY:
854 if (val > OSSL_CMP_LOG_MAX) {
855 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
858 ctx->log_verbosity = val;
860 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
861 ctx->implicitConfirm = val;
863 case OSSL_CMP_OPT_DISABLE_CONFIRM:
864 ctx->disableConfirm = val;
866 case OSSL_CMP_OPT_UNPROTECTED_SEND:
867 ctx->unprotectedSend = val;
869 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
870 ctx->unprotectedErrors = val;
872 case OSSL_CMP_OPT_VALIDITY_DAYS:
875 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
876 ctx->SubjectAltName_nodefault = val;
878 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
879 ctx->setSubjectAltNameCritical = val;
881 case OSSL_CMP_OPT_POLICIES_CRITICAL:
882 ctx->setPoliciesCritical = val;
884 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
885 ctx->ignore_keyusage = val;
887 case OSSL_CMP_OPT_POPO_METHOD:
888 if (val > OSSL_CRMF_POPO_KEYAGREE) {
889 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
892 ctx->popoMethod = val;
894 case OSSL_CMP_OPT_DIGEST_ALGNID:
895 if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
898 case OSSL_CMP_OPT_OWF_ALGNID:
899 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
902 case OSSL_CMP_OPT_MAC_ALGNID:
905 case OSSL_CMP_OPT_KEEP_ALIVE:
906 ctx->keep_alive = val;
908 case OSSL_CMP_OPT_MSG_TIMEOUT:
909 ctx->msg_timeout = val;
911 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
912 ctx->total_timeout = val;
914 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
915 ctx->permitTAInExtraCertsForIR = val;
917 case OSSL_CMP_OPT_REVOCATION_REASON:
918 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
919 ERR_raise(ERR_LIB_CMP, CMP_R_VALUE_TOO_LARGE);
922 ctx->revocationReason = val;
925 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);
933 * Reads a Boolean or integer option value from the context.
934 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
936 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
939 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
944 case OSSL_CMP_OPT_LOG_VERBOSITY:
945 return ctx->log_verbosity;
946 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
947 return ctx->implicitConfirm;
948 case OSSL_CMP_OPT_DISABLE_CONFIRM:
949 return ctx->disableConfirm;
950 case OSSL_CMP_OPT_UNPROTECTED_SEND:
951 return ctx->unprotectedSend;
952 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
953 return ctx->unprotectedErrors;
954 case OSSL_CMP_OPT_VALIDITY_DAYS:
956 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
957 return ctx->SubjectAltName_nodefault;
958 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
959 return ctx->setSubjectAltNameCritical;
960 case OSSL_CMP_OPT_POLICIES_CRITICAL:
961 return ctx->setPoliciesCritical;
962 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
963 return ctx->ignore_keyusage;
964 case OSSL_CMP_OPT_POPO_METHOD:
965 return ctx->popoMethod;
966 case OSSL_CMP_OPT_DIGEST_ALGNID:
967 return EVP_MD_get_type(ctx->digest);
968 case OSSL_CMP_OPT_OWF_ALGNID:
969 return EVP_MD_get_type(ctx->pbm_owf);
970 case OSSL_CMP_OPT_MAC_ALGNID:
972 case OSSL_CMP_OPT_KEEP_ALIVE:
973 return ctx->keep_alive;
974 case OSSL_CMP_OPT_MSG_TIMEOUT:
975 return ctx->msg_timeout;
976 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
977 return ctx->total_timeout;
978 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
979 return ctx->permitTAInExtraCertsForIR;
980 case OSSL_CMP_OPT_REVOCATION_REASON:
981 return ctx->revocationReason;
983 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_OPTION);