2 * Copyright 2007-2020 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 /* Get current certificate store containing trusted root CA certs */
24 X509_STORE *OSSL_CMP_CTX_get0_trustedStore(const OSSL_CMP_CTX *ctx)
27 CMPerr(0, CMP_R_NULL_ARGUMENT);
34 * Set certificate store containing trusted (root) CA certs and possibly CRLs
35 * and a cert verification callback function used for CMP server authentication.
36 * Any already existing store entry is freed. Given NULL, the entry is reset.
38 int OSSL_CMP_CTX_set0_trustedStore(OSSL_CMP_CTX *ctx, X509_STORE *store)
41 CMPerr(0, CMP_R_NULL_ARGUMENT);
44 X509_STORE_free(ctx->trusted);
49 /* Get current list of non-trusted intermediate certs */
50 STACK_OF(X509) *OSSL_CMP_CTX_get0_untrusted_certs(const OSSL_CMP_CTX *ctx)
53 CMPerr(0, CMP_R_NULL_ARGUMENT);
56 return ctx->untrusted_certs;
60 * Set untrusted certificates for path construction in authentication of
61 * the CMP server and potentially others (TLS server, newly enrolled cert).
63 int OSSL_CMP_CTX_set1_untrusted_certs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
65 STACK_OF(X509) *untrusted_certs;
67 CMPerr(0, CMP_R_NULL_ARGUMENT);
70 if ((untrusted_certs = sk_X509_new_null()) == NULL)
72 if (ossl_cmp_sk_X509_add1_certs(untrusted_certs, certs, 0, 1, 0) != 1)
74 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
75 ctx->untrusted_certs = untrusted_certs;
78 sk_X509_pop_free(untrusted_certs, X509_free);
83 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
84 * Returns new context on success, NULL on error
86 OSSL_CMP_CTX *OSSL_CMP_CTX_new(void)
88 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
93 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
96 ctx->failInfoCode = -1;
98 ctx->msg_timeout = 2 * 60;
100 if ((ctx->untrusted_certs = sk_X509_new_null()) == NULL)
104 ctx->pbm_owf = NID_sha256;
105 ctx->pbm_itercnt = 500;
106 ctx->pbm_mac = NID_hmac_sha1;
108 ctx->digest = NID_sha256;
109 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
110 ctx->revocationReason = CRL_REASON_NONE;
112 /* all other elements are initialized to 0 or NULL, respectively */
116 OSSL_CMP_CTX_free(ctx);
120 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
121 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
124 CMPerr(0, CMP_R_NULL_ARGUMENT);
129 ctx->failInfoCode = -1;
131 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
132 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
133 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
134 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
135 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
136 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
137 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
138 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
141 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
142 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
147 OPENSSL_free(ctx->serverPath);
148 OPENSSL_free(ctx->server);
149 OPENSSL_free(ctx->proxy);
150 OPENSSL_free(ctx->no_proxy);
152 X509_free(ctx->srvCert);
153 X509_free(ctx->validatedSrvCert);
154 X509_NAME_free(ctx->expected_sender);
155 X509_STORE_free(ctx->trusted);
156 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
158 X509_free(ctx->clCert);
159 EVP_PKEY_free(ctx->pkey);
160 ASN1_OCTET_STRING_free(ctx->referenceValue);
161 if (ctx->secretValue != NULL)
162 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
163 ASN1_OCTET_STRING_free(ctx->secretValue);
165 X509_NAME_free(ctx->recipient);
166 ASN1_OCTET_STRING_free(ctx->transactionID);
167 ASN1_OCTET_STRING_free(ctx->senderNonce);
168 ASN1_OCTET_STRING_free(ctx->recipNonce);
169 sk_OSSL_CMP_ITAV_pop_free(ctx->geninfo_ITAVs, OSSL_CMP_ITAV_free);
170 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
172 EVP_PKEY_free(ctx->newPkey);
173 X509_NAME_free(ctx->issuer);
174 X509_NAME_free(ctx->subjectName);
175 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
176 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
177 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
178 X509_free(ctx->oldCert);
179 X509_REQ_free(ctx->p10CSR);
181 sk_OSSL_CMP_ITAV_pop_free(ctx->genm_ITAVs, OSSL_CMP_ITAV_free);
183 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
184 X509_free(ctx->newCert);
185 sk_X509_pop_free(ctx->caPubs, X509_free);
186 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
191 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX *ctx, int status)
193 if (!ossl_assert(ctx != NULL))
195 ctx->status = status;
200 * Returns the PKIStatus from the last CertRepMessage
201 * or Revocation Response or error message, -1 on error
203 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX *ctx)
206 CMPerr(0, CMP_R_NULL_ARGUMENT);
213 * Returns the statusString from the last CertRepMessage
214 * or Revocation Response or error message, NULL on error
216 OSSL_CMP_PKIFREETEXT *OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX *ctx)
219 CMPerr(0, CMP_R_NULL_ARGUMENT);
222 return ctx->statusString;
225 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX *ctx,
226 OSSL_CMP_PKIFREETEXT *text)
228 if (!ossl_assert(ctx != NULL))
230 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
231 ctx->statusString = text;
235 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
237 if (!ossl_assert(ctx != NULL))
239 X509_free(ctx->validatedSrvCert);
240 ctx->validatedSrvCert = cert;
244 /* Set callback function for checking if the cert is ok or should be rejected */
245 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_certConf_cb_t cb)
248 CMPerr(0, CMP_R_NULL_ARGUMENT);
251 ctx->certConf_cb = cb;
256 * Set argument, respectively a pointer to a structure containing arguments,
257 * optionally to be used by the certConf callback.
259 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
262 CMPerr(0, CMP_R_NULL_ARGUMENT);
265 ctx->certConf_cb_arg = arg;
270 * Get argument, respectively the pointer to a structure containing arguments,
271 * optionally to be used by certConf callback.
272 * Returns callback argument set previously (NULL if not set or on error)
274 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX *ctx)
277 CMPerr(0, CMP_R_NULL_ARGUMENT);
280 return ctx->certConf_cb_arg;
283 #ifndef OPENSSL_NO_TRACE
284 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
285 int category, int cmd, void *vdata)
287 OSSL_CMP_CTX *ctx = vdata;
289 OSSL_CMP_severity level = -1;
294 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
296 if (ctx->log_cb == NULL)
297 return 1; /* silently drop message */
299 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
301 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
302 goto end; /* suppress output since severity is not sufficient */
304 if (!ctx->log_cb(func != NULL ? func : "(no func)",
305 file != NULL ? file : "(no file)",
316 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
317 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
318 const char *func, const char *file, int line,
319 const char *level_str, const char *format, ...)
322 char hugebuf[1024 * 2];
325 if (ctx == NULL || ctx->log_cb == NULL)
326 return 1; /* silently drop message */
328 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
329 return 1; /* suppress output since severity is not sufficient */
334 va_start(args, format);
337 func = "(unset function name)";
339 file = "(unset file name)";
340 if (level_str == NULL)
341 level_str = "(unset level string)";
343 #ifndef OPENSSL_NO_TRACE
344 if (OSSL_TRACE_ENABLED(CMP)) {
345 OSSL_TRACE_BEGIN(CMP) {
347 BIO_snprintf(hugebuf, sizeof(hugebuf),
348 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
349 func, file, line, level_str);
350 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
351 if (BIO_vsnprintf(hugebuf + printed,
352 sizeof(hugebuf) - printed, format, args) > 0)
353 res = BIO_puts(trc_out, hugebuf) > 0;
355 } OSSL_TRACE_END(CMP);
357 #else /* compensate for disabled trace API */
359 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
360 res = ctx->log_cb(func, file, line, level, hugebuf);
367 /* Set a callback function for error reporting and logging messages */
368 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
371 CMPerr(0, CMP_R_NULL_ARGUMENT);
376 #ifndef OPENSSL_NO_TRACE
377 /* do also in case cb == NULL, to switch off logging output: */
378 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
379 ossl_cmp_log_trace_cb, ctx))
386 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
387 void OSSL_CMP_CTX_print_errors(OSSL_CMP_CTX *ctx)
389 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
393 * Set or clear the reference value to be used for identification
394 * (i.e., the user name) when using PBMAC.
396 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
397 const unsigned char *ref, int len)
400 CMPerr(0, CMP_R_NULL_ARGUMENT);
403 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref,
407 /* Set or clear the password to be used for protecting messages with PBMAC */
408 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
411 ASN1_OCTET_STRING *secretValue = NULL;
413 CMPerr(0, CMP_R_NULL_ARGUMENT);
416 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
418 if (ctx->secretValue != NULL) {
419 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
420 ASN1_OCTET_STRING_free(ctx->secretValue);
422 ctx->secretValue = secretValue;
427 * Returns the stack of certificates received in a response message.
428 * The stack is duplicated so the caller must handle freeing it!
429 * Returns pointer to created stack on success, NULL on error
431 STACK_OF(X509) *OSSL_CMP_CTX_get1_extraCertsIn(const OSSL_CMP_CTX *ctx)
434 CMPerr(0, CMP_R_NULL_ARGUMENT);
437 if (ctx->extraCertsIn == NULL)
438 return sk_X509_new_null();
439 return X509_chain_up_ref(ctx->extraCertsIn);
443 * Copies any given stack of inbound X509 certificates to extraCertsIn
444 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
446 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX *ctx,
447 STACK_OF(X509) *extraCertsIn)
449 if (!ossl_assert(ctx != NULL))
452 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
453 ctx->extraCertsIn = NULL;
454 if (extraCertsIn == NULL)
456 return (ctx->extraCertsIn = X509_chain_up_ref(extraCertsIn)) != NULL;
460 * Duplicate and set the given stack as the new stack of X509
461 * certificates to send out in the extraCerts field.
463 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX *ctx,
464 STACK_OF(X509) *extraCertsOut)
467 CMPerr(0, CMP_R_NULL_ARGUMENT);
471 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
472 ctx->extraCertsOut = NULL;
473 if (extraCertsOut == NULL)
475 return (ctx->extraCertsOut = X509_chain_up_ref(extraCertsOut)) != NULL;
479 * Add the given policy info object
480 * to the X509_EXTENSIONS of the requested certificate template.
482 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
484 if (ctx == NULL || pinfo == NULL) {
485 CMPerr(0, CMP_R_NULL_ARGUMENT);
489 if (ctx->policies == NULL
490 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
493 return sk_POLICYINFO_push(ctx->policies, pinfo);
496 /* Add an ITAV for geninfo of the PKI message header */
497 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
500 CMPerr(0, CMP_R_NULL_ARGUMENT);
503 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
506 /* Add an itav for the body of outgoing general messages */
507 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
510 CMPerr(0, CMP_R_NULL_ARGUMENT);
513 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
517 * Returns a duplicate of the stack of X509 certificates that
518 * were received in the caPubs field of the last CertRepMessage.
519 * Returns NULL on error
521 STACK_OF(X509) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX *ctx)
524 CMPerr(0, CMP_R_NULL_ARGUMENT);
527 if (ctx->caPubs == NULL)
528 return sk_X509_new_null();
529 return X509_chain_up_ref(ctx->caPubs);
533 * Duplicate and copy the given stack of certificates to the given
534 * OSSL_CMP_CTX structure so that they may be retrieved later.
536 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *caPubs)
538 if (!ossl_assert(ctx != NULL))
541 sk_X509_pop_free(ctx->caPubs, X509_free);
545 return (ctx->caPubs = X509_chain_up_ref(caPubs)) != NULL;
548 #define char_dup OPENSSL_strdup
549 #define char_free OPENSSL_free
550 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
551 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
553 TYPE *val_dup = NULL; \
556 CMPerr(0, CMP_R_NULL_ARGUMENT); \
560 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
562 TYPE##_free(ctx->FIELD); \
563 ctx->FIELD = val_dup; \
567 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
568 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
571 CMPerr(0, CMP_R_NULL_ARGUMENT); \
575 if (val != NULL && !TYPE##_up_ref(val)) \
577 TYPE##_free(ctx->FIELD); \
583 * Pins the server certificate to be directly trusted (even if it is expired)
584 * for verifying response messages.
585 * Cert pointer is not consumed. It may be NULL to clear the entry.
587 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert, X509)
589 /* Set the X509 name of the recipient. Set in the PKIHeader */
590 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
592 /* Store the X509 name of the expected sender in the PKIHeader of responses */
593 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
595 /* Set the X509 name of the issuer. Set in the PKIHeader */
596 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
599 * Set the subject name that will be placed in the certificate
600 * request. This will be the subject name on the received certificate.
602 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
604 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
605 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
608 CMPerr(0, CMP_R_NULL_ARGUMENT);
612 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
613 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
614 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
617 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
618 ctx->reqExtensions = exts;
622 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
623 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
626 CMPerr(0, CMP_R_NULL_ARGUMENT);
629 /* if one of the following conditions 'fail' this is not an error */
630 return ctx->reqExtensions != NULL
631 && X509v3_get_ext_by_NID(ctx->reqExtensions,
632 NID_subject_alt_name, -1) >= 0;
636 * Add a GENERAL_NAME structure that will be added to the CRMF
637 * request's extensions field to request subject alternative names.
639 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
640 const GENERAL_NAME *name)
642 GENERAL_NAME *name_dup;
644 if (ctx == NULL || name == NULL) {
645 CMPerr(0, CMP_R_NULL_ARGUMENT);
649 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
650 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
654 if (ctx->subjectAltNames == NULL
655 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
657 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
659 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
660 GENERAL_NAME_free(name_dup);
667 * Set our own client certificate, used for example in KUR and when
668 * doing the IR with existing certificate.
670 DEFINE_OSSL_CMP_CTX_set1_up_ref(clCert, X509)
673 * Set the old certificate that we are updating in KUR
674 * or the certificate to be revoked in RR, respectively.
675 * Also used as reference cert (defaulting to clCert) for deriving subject DN
676 * and SANs. Its issuer is used as default recipient in the CMP message header.
678 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert, X509)
680 /* Set the PKCS#10 CSR to be sent in P10CR */
681 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
684 * Set the (newly received in IP/KUP/CP) certificate in the context.
685 * TODO: this only permits for one cert to be enrolled at a time.
687 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX *ctx, X509 *cert)
689 if (!ossl_assert(ctx != NULL))
692 X509_free(ctx->newCert);
698 * Get the (newly received in IP/KUP/CP) client certificate from the context
699 * TODO: this only permits for one client cert to be received...
701 X509 *OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX *ctx)
704 CMPerr(0, CMP_R_NULL_ARGUMENT);
710 /* Set the client's current private key */
711 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey, EVP_PKEY)
713 /* Set new key pair. Used e.g. when doing Key Update */
714 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
717 CMPerr(0, CMP_R_NULL_ARGUMENT);
721 EVP_PKEY_free(ctx->newPkey);
723 ctx->newPkey_priv = priv;
727 /* Get the private/public key to use for cert enrollment, or NULL on error */
728 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
731 CMPerr(0, CMP_R_NULL_ARGUMENT);
735 if (ctx->newPkey != NULL)
736 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
737 if (ctx->p10CSR != NULL)
738 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
739 return ctx->pkey; /* may be NULL */
742 /* Set the given transactionID to the context */
743 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX *ctx,
744 const ASN1_OCTET_STRING *id)
747 CMPerr(0, CMP_R_NULL_ARGUMENT);
750 return ossl_cmp_asn1_octet_string_set1(&ctx->transactionID, id);
753 /* Set the nonce to be used for the recipNonce in the message created next */
754 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX *ctx,
755 const ASN1_OCTET_STRING *nonce)
757 if (!ossl_assert(ctx != NULL))
759 return ossl_cmp_asn1_octet_string_set1(&ctx->recipNonce, nonce);
762 /* Stores the given nonce as the last senderNonce sent out */
763 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
764 const ASN1_OCTET_STRING *nonce)
767 CMPerr(0, CMP_R_NULL_ARGUMENT);
770 return ossl_cmp_asn1_octet_string_set1(&ctx->senderNonce, nonce);
773 /* Set the proxy server to use for HTTP(S) connections */
774 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
776 /* Set the (HTTP) host name of the CMP server */
777 DEFINE_OSSL_CMP_CTX_set1(server, char)
779 /* Set the server exclusion list of the HTTP proxy server */
780 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
782 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
783 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX *ctx, OSSL_HTTP_bio_cb_t cb)
786 CMPerr(0, CMP_R_NULL_ARGUMENT);
793 /* Set argument optionally to be used by the http connect/disconnect callback */
794 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
797 CMPerr(0, CMP_R_NULL_ARGUMENT);
800 ctx->http_cb_arg = arg;
805 * Get argument optionally to be used by the http connect/disconnect callback
806 * Returns callback argument set previously (NULL if not set or on error)
808 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX *ctx)
811 CMPerr(0, CMP_R_NULL_ARGUMENT);
814 return ctx->http_cb_arg;
817 /* Set callback function for sending CMP request and receiving response */
818 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_transfer_cb_t cb)
821 CMPerr(0, CMP_R_NULL_ARGUMENT);
824 ctx->transfer_cb = cb;
828 /* Set argument optionally to be used by the transfer callback */
829 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
832 CMPerr(0, CMP_R_NULL_ARGUMENT);
835 ctx->transfer_cb_arg = arg;
840 * Get argument optionally to be used by the transfer callback.
841 * Returns callback argument set previously (NULL if not set or on error)
843 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX *ctx)
846 CMPerr(0, CMP_R_NULL_ARGUMENT);
849 return ctx->transfer_cb_arg;
852 /** Set the HTTP server port to be used */
853 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX *ctx, int port)
856 CMPerr(0, CMP_R_NULL_ARGUMENT);
859 ctx->serverPort = port;
863 /* Set the HTTP path to be used on the server (e.g "pkix/") */
864 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
866 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
867 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX *ctx, int fail_info)
869 if (!ossl_assert(ctx != NULL))
871 ctx->failInfoCode = fail_info;
876 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
877 * Returns bit string as integer on success, -1 on error
879 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX *ctx)
882 CMPerr(0, CMP_R_NULL_ARGUMENT);
885 return ctx->failInfoCode;
888 /* Set a Boolean or integer option of the context to the "val" arg */
889 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
894 CMPerr(0, CMP_R_NULL_ARGUMENT);
899 case OSSL_CMP_OPT_REVOCATION_REASON:
900 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
902 case OSSL_CMP_OPT_POPO_METHOD:
903 min_val = OSSL_CRMF_POPO_NONE;
910 CMPerr(0, CMP_R_INVALID_ARGS);
915 case OSSL_CMP_OPT_LOG_VERBOSITY:
916 if (val > OSSL_CMP_LOG_DEBUG) {
917 CMPerr(0, CMP_R_INVALID_ARGS);
920 ctx->log_verbosity = val;
922 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
923 ctx->implicitConfirm = val;
925 case OSSL_CMP_OPT_DISABLE_CONFIRM:
926 ctx->disableConfirm = val;
928 case OSSL_CMP_OPT_UNPROTECTED_SEND:
929 ctx->unprotectedSend = val;
931 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
932 ctx->unprotectedErrors = val;
934 case OSSL_CMP_OPT_VALIDITY_DAYS:
937 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
938 ctx->SubjectAltName_nodefault = val;
940 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
941 ctx->setSubjectAltNameCritical = val;
943 case OSSL_CMP_OPT_POLICIES_CRITICAL:
944 ctx->setPoliciesCritical = val;
946 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
947 ctx->ignore_keyusage = val;
949 case OSSL_CMP_OPT_POPO_METHOD:
950 if (val > OSSL_CRMF_POPO_KEYAGREE) {
951 CMPerr(0, CMP_R_INVALID_ARGS);
954 ctx->popoMethod = val;
956 case OSSL_CMP_OPT_DIGEST_ALGNID:
959 case OSSL_CMP_OPT_OWF_ALGNID:
962 case OSSL_CMP_OPT_MAC_ALGNID:
965 case OSSL_CMP_OPT_MSG_TIMEOUT:
966 ctx->msg_timeout = val;
968 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
969 ctx->total_timeout = val;
971 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
972 ctx->permitTAInExtraCertsForIR = val;
974 case OSSL_CMP_OPT_REVOCATION_REASON:
975 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
976 CMPerr(0, CMP_R_INVALID_ARGS);
979 ctx->revocationReason = val;
982 CMPerr(0, CMP_R_INVALID_ARGS);
990 * Reads a Boolean or integer option value from the context.
991 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
993 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
996 CMPerr(0, CMP_R_NULL_ARGUMENT);
1001 case OSSL_CMP_OPT_LOG_VERBOSITY:
1002 return ctx->log_verbosity;
1003 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1004 return ctx->implicitConfirm;
1005 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1006 return ctx->disableConfirm;
1007 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1008 return ctx->unprotectedSend;
1009 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1010 return ctx->unprotectedErrors;
1011 case OSSL_CMP_OPT_VALIDITY_DAYS:
1013 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1014 return ctx->SubjectAltName_nodefault;
1015 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1016 return ctx->setSubjectAltNameCritical;
1017 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1018 return ctx->setPoliciesCritical;
1019 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1020 return ctx->ignore_keyusage;
1021 case OSSL_CMP_OPT_POPO_METHOD:
1022 return ctx->popoMethod;
1023 case OSSL_CMP_OPT_DIGEST_ALGNID:
1025 case OSSL_CMP_OPT_OWF_ALGNID:
1026 return ctx->pbm_owf;
1027 case OSSL_CMP_OPT_MAC_ALGNID:
1028 return ctx->pbm_mac;
1029 case OSSL_CMP_OPT_MSG_TIMEOUT:
1030 return ctx->msg_timeout;
1031 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1032 return ctx->total_timeout;
1033 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1034 return ctx->permitTAInExtraCertsForIR;
1035 case OSSL_CMP_OPT_REVOCATION_REASON:
1036 return ctx->revocationReason;
1038 CMPerr(0, CMP_R_INVALID_ARGS);