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>
24 DEFINE_STACK_OF(X509_EXTENSION)
25 DEFINE_STACK_OF(POLICYINFO)
26 DEFINE_STACK_OF(ASN1_UTF8STRING)
27 DEFINE_STACK_OF(GENERAL_NAME)
28 DEFINE_STACK_OF(OSSL_CMP_ITAV)
31 * Get current certificate store containing trusted root CA certs
33 X509_STORE *OSSL_CMP_CTX_get0_trustedStore(const OSSL_CMP_CTX *ctx)
36 CMPerr(0, CMP_R_NULL_ARGUMENT);
43 * Set certificate store containing trusted (root) CA certs and possibly CRLs
44 * and a cert verification callback function used for CMP server authentication.
45 * Any already existing store entry is freed. Given NULL, the entry is reset.
47 int OSSL_CMP_CTX_set0_trustedStore(OSSL_CMP_CTX *ctx, X509_STORE *store)
50 CMPerr(0, CMP_R_NULL_ARGUMENT);
53 X509_STORE_free(ctx->trusted);
58 /* Get current list of non-trusted intermediate certs */
59 STACK_OF(X509) *OSSL_CMP_CTX_get0_untrusted_certs(const OSSL_CMP_CTX *ctx)
62 CMPerr(0, CMP_R_NULL_ARGUMENT);
65 return ctx->untrusted_certs;
69 * Set untrusted certificates for path construction in authentication of
70 * the CMP server and potentially others (TLS server, newly enrolled cert).
72 int OSSL_CMP_CTX_set1_untrusted_certs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
74 STACK_OF(X509) *untrusted_certs;
76 CMPerr(0, CMP_R_NULL_ARGUMENT);
79 if ((untrusted_certs = sk_X509_new_null()) == NULL)
81 if (ossl_cmp_sk_X509_add1_certs(untrusted_certs, certs, 0, 1, 0) != 1)
83 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
84 ctx->untrusted_certs = untrusted_certs;
87 sk_X509_pop_free(untrusted_certs, X509_free);
92 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
93 * Returns new context on success, NULL on error
95 OSSL_CMP_CTX *OSSL_CMP_CTX_new(void)
97 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
102 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
105 ctx->failInfoCode = -1;
107 ctx->msg_timeout = 2 * 60;
109 if ((ctx->untrusted_certs = sk_X509_new_null()) == NULL)
113 ctx->pbm_owf = NID_sha256;
114 ctx->pbm_itercnt = 500;
115 ctx->pbm_mac = NID_hmac_sha1;
117 ctx->digest = NID_sha256;
118 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
119 ctx->revocationReason = CRL_REASON_NONE;
121 /* all other elements are initialized to 0 or NULL, respectively */
125 OSSL_CMP_CTX_free(ctx);
129 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
130 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
133 CMPerr(0, CMP_R_NULL_ARGUMENT);
138 ctx->failInfoCode = -1;
140 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
141 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
142 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
143 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
144 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
145 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
146 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
147 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
150 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
151 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
156 OPENSSL_free(ctx->serverPath);
157 OPENSSL_free(ctx->server);
158 OPENSSL_free(ctx->proxy);
159 OPENSSL_free(ctx->no_proxy);
161 X509_free(ctx->srvCert);
162 X509_free(ctx->validatedSrvCert);
163 X509_NAME_free(ctx->expected_sender);
164 X509_STORE_free(ctx->trusted);
165 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
167 X509_free(ctx->clCert);
168 EVP_PKEY_free(ctx->pkey);
169 ASN1_OCTET_STRING_free(ctx->referenceValue);
170 if (ctx->secretValue != NULL)
171 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
172 ASN1_OCTET_STRING_free(ctx->secretValue);
174 X509_NAME_free(ctx->recipient);
175 ASN1_OCTET_STRING_free(ctx->transactionID);
176 ASN1_OCTET_STRING_free(ctx->senderNonce);
177 ASN1_OCTET_STRING_free(ctx->recipNonce);
178 sk_OSSL_CMP_ITAV_pop_free(ctx->geninfo_ITAVs, OSSL_CMP_ITAV_free);
179 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
181 EVP_PKEY_free(ctx->newPkey);
182 X509_NAME_free(ctx->issuer);
183 X509_NAME_free(ctx->subjectName);
184 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
185 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
186 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
187 X509_free(ctx->oldCert);
188 X509_REQ_free(ctx->p10CSR);
190 sk_OSSL_CMP_ITAV_pop_free(ctx->genm_ITAVs, OSSL_CMP_ITAV_free);
192 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
193 X509_free(ctx->newCert);
194 sk_X509_pop_free(ctx->caPubs, X509_free);
195 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
200 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX *ctx, int status)
202 if (!ossl_assert(ctx != NULL))
204 ctx->status = status;
209 * Returns the PKIStatus from the last CertRepMessage
210 * or Revocation Response or error message, -1 on error
212 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX *ctx)
215 CMPerr(0, CMP_R_NULL_ARGUMENT);
222 * Returns the statusString from the last CertRepMessage
223 * or Revocation Response or error message, NULL on error
225 OSSL_CMP_PKIFREETEXT *OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX *ctx)
228 CMPerr(0, CMP_R_NULL_ARGUMENT);
231 return ctx->statusString;
234 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX *ctx,
235 OSSL_CMP_PKIFREETEXT *text)
237 if (!ossl_assert(ctx != NULL))
239 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
240 ctx->statusString = text;
244 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
246 if (!ossl_assert(ctx != NULL))
248 X509_free(ctx->validatedSrvCert);
249 ctx->validatedSrvCert = cert;
253 /* Set callback function for checking if the cert is ok or should be rejected */
254 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_certConf_cb_t cb)
257 CMPerr(0, CMP_R_NULL_ARGUMENT);
260 ctx->certConf_cb = cb;
265 * Set argument, respectively a pointer to a structure containing arguments,
266 * optionally to be used by the certConf callback.
268 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
271 CMPerr(0, CMP_R_NULL_ARGUMENT);
274 ctx->certConf_cb_arg = arg;
279 * Get argument, respectively the pointer to a structure containing arguments,
280 * optionally to be used by certConf callback.
281 * Returns callback argument set previously (NULL if not set or on error)
283 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX *ctx)
286 CMPerr(0, CMP_R_NULL_ARGUMENT);
289 return ctx->certConf_cb_arg;
292 #ifndef OPENSSL_NO_TRACE
293 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
294 int category, int cmd, void *vdata)
296 OSSL_CMP_CTX *ctx = vdata;
298 OSSL_CMP_severity level = -1;
303 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
305 if (ctx->log_cb == NULL)
306 return 1; /* silently drop message */
308 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
310 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
311 goto end; /* suppress output since severity is not sufficient */
313 if (!ctx->log_cb(func != NULL ? func : "(no func)",
314 file != NULL ? file : "(no file)",
325 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
326 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
327 const char *func, const char *file, int line,
328 const char *level_str, const char *format, ...)
331 char hugebuf[1024 * 2];
334 if (ctx == NULL || ctx->log_cb == NULL)
335 return 1; /* silently drop message */
337 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
338 return 1; /* suppress output since severity is not sufficient */
343 va_start(args, format);
346 func = "(unset function name)";
348 file = "(unset file name)";
349 if (level_str == NULL)
350 level_str = "(unset level string)";
352 #ifndef OPENSSL_NO_TRACE
353 if (OSSL_TRACE_ENABLED(CMP)) {
354 OSSL_TRACE_BEGIN(CMP) {
356 BIO_snprintf(hugebuf, sizeof(hugebuf),
357 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
358 func, file, line, level_str);
359 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
360 if (BIO_vsnprintf(hugebuf + printed,
361 sizeof(hugebuf) - printed, format, args) > 0)
362 res = BIO_puts(trc_out, hugebuf) > 0;
364 } OSSL_TRACE_END(CMP);
366 #else /* compensate for disabled trace API */
368 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
369 res = ctx->log_cb(func, file, line, level, hugebuf);
376 /* Set a callback function for error reporting and logging messages */
377 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
380 CMPerr(0, CMP_R_NULL_ARGUMENT);
385 #ifndef OPENSSL_NO_TRACE
386 /* do also in case cb == NULL, to switch off logging output: */
387 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
388 ossl_cmp_log_trace_cb, ctx))
395 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
396 void OSSL_CMP_CTX_print_errors(OSSL_CMP_CTX *ctx)
398 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
402 * Set or clear the reference value to be used for identification
403 * (i.e., the user name) when using PBMAC.
405 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
406 const unsigned char *ref, int len)
409 CMPerr(0, CMP_R_NULL_ARGUMENT);
412 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref,
416 /* Set or clear the password to be used for protecting messages with PBMAC */
417 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
420 ASN1_OCTET_STRING *secretValue = NULL;
422 CMPerr(0, CMP_R_NULL_ARGUMENT);
425 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
427 if (ctx->secretValue != NULL) {
428 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
429 ASN1_OCTET_STRING_free(ctx->secretValue);
431 ctx->secretValue = secretValue;
436 * Returns the stack of certificates received in a response message.
437 * The stack is duplicated so the caller must handle freeing it!
438 * Returns pointer to created stack on success, NULL on error
440 STACK_OF(X509) *OSSL_CMP_CTX_get1_extraCertsIn(const OSSL_CMP_CTX *ctx)
443 CMPerr(0, CMP_R_NULL_ARGUMENT);
446 if (ctx->extraCertsIn == NULL)
447 return sk_X509_new_null();
448 return X509_chain_up_ref(ctx->extraCertsIn);
452 * Copies any given stack of inbound X509 certificates to extraCertsIn
453 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
455 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX *ctx,
456 STACK_OF(X509) *extraCertsIn)
458 if (!ossl_assert(ctx != NULL))
461 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
462 ctx->extraCertsIn = NULL;
463 if (extraCertsIn == NULL)
465 return (ctx->extraCertsIn = X509_chain_up_ref(extraCertsIn)) != NULL;
469 * Duplicate and set the given stack as the new stack of X509
470 * certificates to send out in the extraCerts field.
472 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX *ctx,
473 STACK_OF(X509) *extraCertsOut)
476 CMPerr(0, CMP_R_NULL_ARGUMENT);
480 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
481 ctx->extraCertsOut = NULL;
482 if (extraCertsOut == NULL)
484 return (ctx->extraCertsOut = X509_chain_up_ref(extraCertsOut)) != NULL;
488 * Add the given policy info object
489 * to the X509_EXTENSIONS of the requested certificate template.
491 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
493 if (ctx == NULL || pinfo == NULL) {
494 CMPerr(0, CMP_R_NULL_ARGUMENT);
498 if (ctx->policies == NULL
499 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
502 return sk_POLICYINFO_push(ctx->policies, pinfo);
505 /* Add an ITAV for geninfo of the PKI message header */
506 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
509 CMPerr(0, CMP_R_NULL_ARGUMENT);
512 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
515 /* Add an itav for the body of outgoing general messages */
516 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
519 CMPerr(0, CMP_R_NULL_ARGUMENT);
522 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
526 * Returns a duplicate of the stack of X509 certificates that
527 * were received in the caPubs field of the last CertRepMessage.
528 * Returns NULL on error
530 STACK_OF(X509) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX *ctx)
533 CMPerr(0, CMP_R_NULL_ARGUMENT);
536 if (ctx->caPubs == NULL)
537 return sk_X509_new_null();
538 return X509_chain_up_ref(ctx->caPubs);
542 * Duplicate and copy the given stack of certificates to the given
543 * OSSL_CMP_CTX structure so that they may be retrieved later.
545 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *caPubs)
547 if (!ossl_assert(ctx != NULL))
550 sk_X509_pop_free(ctx->caPubs, X509_free);
554 return (ctx->caPubs = X509_chain_up_ref(caPubs)) != NULL;
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) \
562 TYPE *val_dup = NULL; \
565 CMPerr(0, CMP_R_NULL_ARGUMENT); \
569 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
571 TYPE##_free(ctx->FIELD); \
572 ctx->FIELD = val_dup; \
576 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
577 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
580 CMPerr(0, CMP_R_NULL_ARGUMENT); \
584 if (val != NULL && !TYPE##_up_ref(val)) \
586 TYPE##_free(ctx->FIELD); \
592 * Pins the server certificate to be directly trusted (even if it is expired)
593 * for verifying response messages.
594 * Cert pointer is not consumed. It may be NULL to clear the entry.
596 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert, X509)
598 /* Set the X509 name of the recipient. Set in the PKIHeader */
599 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
601 /* Store the X509 name of the expected sender in the PKIHeader of responses */
602 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
604 /* Set the X509 name of the issuer. Set in the PKIHeader */
605 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
608 * Set the subject name that will be placed in the certificate
609 * request. This will be the subject name on the received certificate.
611 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
613 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
614 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
617 CMPerr(0, CMP_R_NULL_ARGUMENT);
621 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
622 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
623 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
626 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
627 ctx->reqExtensions = exts;
631 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
632 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
635 CMPerr(0, CMP_R_NULL_ARGUMENT);
638 /* if one of the following conditions 'fail' this is not an error */
639 return ctx->reqExtensions != NULL
640 && X509v3_get_ext_by_NID(ctx->reqExtensions,
641 NID_subject_alt_name, -1) >= 0;
645 * Add a GENERAL_NAME structure that will be added to the CRMF
646 * request's extensions field to request subject alternative names.
648 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
649 const GENERAL_NAME *name)
651 GENERAL_NAME *name_dup;
653 if (ctx == NULL || name == NULL) {
654 CMPerr(0, CMP_R_NULL_ARGUMENT);
658 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
659 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
663 if (ctx->subjectAltNames == NULL
664 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
666 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
668 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
669 GENERAL_NAME_free(name_dup);
676 * Set our own client certificate, used for example in KUR and when
677 * doing the IR with existing certificate.
679 DEFINE_OSSL_CMP_CTX_set1_up_ref(clCert, X509)
682 * Set the old certificate that we are updating in KUR
683 * or the certificate to be revoked in RR, respectively.
684 * Also used as reference cert (defaulting to clCert) for deriving subject DN
685 * and SANs. Its issuer is used as default recipient in the CMP message header.
687 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert, X509)
689 /* Set the PKCS#10 CSR to be sent in P10CR */
690 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
693 * Set the (newly received in IP/KUP/CP) certificate in the context.
694 * TODO: this only permits for one cert to be enrolled at a time.
696 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX *ctx, X509 *cert)
698 if (!ossl_assert(ctx != NULL))
701 X509_free(ctx->newCert);
707 * Get the (newly received in IP/KUP/CP) client certificate from the context
708 * TODO: this only permits for one client cert to be received...
710 X509 *OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX *ctx)
713 CMPerr(0, CMP_R_NULL_ARGUMENT);
719 /* Set the client's current private key */
720 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey, EVP_PKEY)
722 /* Set new key pair. Used e.g. when doing Key Update */
723 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
726 CMPerr(0, CMP_R_NULL_ARGUMENT);
730 EVP_PKEY_free(ctx->newPkey);
732 ctx->newPkey_priv = priv;
736 /* Get the private/public key to use for cert enrollment, or NULL on error */
737 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
740 CMPerr(0, CMP_R_NULL_ARGUMENT);
744 if (ctx->newPkey != NULL)
745 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
746 if (ctx->p10CSR != NULL)
747 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
748 return ctx->pkey; /* may be NULL */
751 /* Set the given transactionID to the context */
752 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX *ctx,
753 const ASN1_OCTET_STRING *id)
756 CMPerr(0, CMP_R_NULL_ARGUMENT);
759 return ossl_cmp_asn1_octet_string_set1(&ctx->transactionID, id);
762 /* Set the nonce to be used for the recipNonce in the message created next */
763 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX *ctx,
764 const ASN1_OCTET_STRING *nonce)
766 if (!ossl_assert(ctx != NULL))
768 return ossl_cmp_asn1_octet_string_set1(&ctx->recipNonce, nonce);
771 /* Stores the given nonce as the last senderNonce sent out */
772 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
773 const ASN1_OCTET_STRING *nonce)
776 CMPerr(0, CMP_R_NULL_ARGUMENT);
779 return ossl_cmp_asn1_octet_string_set1(&ctx->senderNonce, nonce);
782 /* Set the proxy server to use for HTTP(S) connections */
783 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
785 /* Set the (HTTP) host name of the CMP server */
786 DEFINE_OSSL_CMP_CTX_set1(server, char)
788 /* Set the server exclusion list of the HTTP proxy server */
789 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
791 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
792 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX *ctx, OSSL_HTTP_bio_cb_t cb)
795 CMPerr(0, CMP_R_NULL_ARGUMENT);
802 /* Set argument optionally to be used by the http connect/disconnect callback */
803 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
806 CMPerr(0, CMP_R_NULL_ARGUMENT);
809 ctx->http_cb_arg = arg;
814 * Get argument optionally to be used by the http connect/disconnect callback
815 * Returns callback argument set previously (NULL if not set or on error)
817 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX *ctx)
820 CMPerr(0, CMP_R_NULL_ARGUMENT);
823 return ctx->http_cb_arg;
826 /* Set callback function for sending CMP request and receiving response */
827 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_transfer_cb_t cb)
830 CMPerr(0, CMP_R_NULL_ARGUMENT);
833 ctx->transfer_cb = cb;
837 /* Set argument optionally to be used by the transfer callback */
838 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
841 CMPerr(0, CMP_R_NULL_ARGUMENT);
844 ctx->transfer_cb_arg = arg;
849 * Get argument optionally to be used by the transfer callback.
850 * Returns callback argument set previously (NULL if not set or on error)
852 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX *ctx)
855 CMPerr(0, CMP_R_NULL_ARGUMENT);
858 return ctx->transfer_cb_arg;
861 /** Set the HTTP server port to be used */
862 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX *ctx, int port)
865 CMPerr(0, CMP_R_NULL_ARGUMENT);
868 ctx->serverPort = port;
872 /* Set the HTTP path to be used on the server (e.g "pkix/") */
873 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
875 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
876 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX *ctx, int fail_info)
878 if (!ossl_assert(ctx != NULL))
880 ctx->failInfoCode = fail_info;
885 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
886 * Returns bit string as integer on success, -1 on error
888 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX *ctx)
891 CMPerr(0, CMP_R_NULL_ARGUMENT);
894 return ctx->failInfoCode;
897 /* Set a Boolean or integer option of the context to the "val" arg */
898 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
903 CMPerr(0, CMP_R_NULL_ARGUMENT);
908 case OSSL_CMP_OPT_REVOCATION_REASON:
909 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
911 case OSSL_CMP_OPT_POPO_METHOD:
912 min_val = OSSL_CRMF_POPO_NONE;
919 CMPerr(0, CMP_R_INVALID_ARGS);
924 case OSSL_CMP_OPT_LOG_VERBOSITY:
925 if (val > OSSL_CMP_LOG_DEBUG) {
926 CMPerr(0, CMP_R_INVALID_ARGS);
929 ctx->log_verbosity = val;
931 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
932 ctx->implicitConfirm = val;
934 case OSSL_CMP_OPT_DISABLE_CONFIRM:
935 ctx->disableConfirm = val;
937 case OSSL_CMP_OPT_UNPROTECTED_SEND:
938 ctx->unprotectedSend = val;
940 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
941 ctx->unprotectedErrors = val;
943 case OSSL_CMP_OPT_VALIDITY_DAYS:
946 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
947 ctx->SubjectAltName_nodefault = val;
949 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
950 ctx->setSubjectAltNameCritical = val;
952 case OSSL_CMP_OPT_POLICIES_CRITICAL:
953 ctx->setPoliciesCritical = val;
955 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
956 ctx->ignore_keyusage = val;
958 case OSSL_CMP_OPT_POPO_METHOD:
959 if (val > OSSL_CRMF_POPO_KEYAGREE) {
960 CMPerr(0, CMP_R_INVALID_ARGS);
963 ctx->popoMethod = val;
965 case OSSL_CMP_OPT_DIGEST_ALGNID:
968 case OSSL_CMP_OPT_OWF_ALGNID:
971 case OSSL_CMP_OPT_MAC_ALGNID:
974 case OSSL_CMP_OPT_MSG_TIMEOUT:
975 ctx->msg_timeout = val;
977 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
978 ctx->total_timeout = val;
980 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
981 ctx->permitTAInExtraCertsForIR = val;
983 case OSSL_CMP_OPT_REVOCATION_REASON:
984 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
985 CMPerr(0, CMP_R_INVALID_ARGS);
988 ctx->revocationReason = val;
991 CMPerr(0, CMP_R_INVALID_ARGS);
999 * Reads a Boolean or integer option value from the context.
1000 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
1002 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
1005 CMPerr(0, CMP_R_NULL_ARGUMENT);
1010 case OSSL_CMP_OPT_LOG_VERBOSITY:
1011 return ctx->log_verbosity;
1012 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1013 return ctx->implicitConfirm;
1014 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1015 return ctx->disableConfirm;
1016 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1017 return ctx->unprotectedSend;
1018 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1019 return ctx->unprotectedErrors;
1020 case OSSL_CMP_OPT_VALIDITY_DAYS:
1022 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1023 return ctx->SubjectAltName_nodefault;
1024 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1025 return ctx->setSubjectAltNameCritical;
1026 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1027 return ctx->setPoliciesCritical;
1028 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1029 return ctx->ignore_keyusage;
1030 case OSSL_CMP_OPT_POPO_METHOD:
1031 return ctx->popoMethod;
1032 case OSSL_CMP_OPT_DIGEST_ALGNID:
1034 case OSSL_CMP_OPT_OWF_ALGNID:
1035 return ctx->pbm_owf;
1036 case OSSL_CMP_OPT_MAC_ALGNID:
1037 return ctx->pbm_mac;
1038 case OSSL_CMP_OPT_MSG_TIMEOUT:
1039 return ctx->msg_timeout;
1040 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1041 return ctx->total_timeout;
1042 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1043 return ctx->permitTAInExtraCertsForIR;
1044 case OSSL_CMP_OPT_REVOCATION_REASON:
1045 return ctx->revocationReason;
1047 CMPerr(0, CMP_R_INVALID_ARGS);