2 * Copyright 2007-2019 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->serverPort = OSSL_CMP_DEFAULT_PORT;
99 ctx->msg_timeout = 2 * 60;
101 if ((ctx->untrusted_certs = sk_X509_new_null()) == NULL)
105 ctx->pbm_owf = NID_sha256;
106 ctx->pbm_itercnt = 500;
107 ctx->pbm_mac = NID_hmac_sha1;
109 ctx->digest = NID_sha256;
110 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
111 ctx->revocationReason = CRL_REASON_NONE;
113 /* all other elements are initialized to 0 or NULL, respectively */
117 OSSL_CMP_CTX_free(ctx);
121 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
122 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
125 CMPerr(0, CMP_R_NULL_ARGUMENT);
130 ctx->failInfoCode = -1;
132 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
133 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
134 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
135 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
136 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
137 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
138 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
139 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
142 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
143 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
148 OPENSSL_free(ctx->serverPath);
149 OPENSSL_free(ctx->serverName);
150 OPENSSL_free(ctx->proxy);
151 OPENSSL_free(ctx->no_proxy);
153 X509_free(ctx->srvCert);
154 X509_free(ctx->validatedSrvCert);
155 X509_NAME_free(ctx->expected_sender);
156 X509_STORE_free(ctx->trusted);
157 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
159 X509_free(ctx->clCert);
160 EVP_PKEY_free(ctx->pkey);
161 ASN1_OCTET_STRING_free(ctx->referenceValue);
162 if (ctx->secretValue != NULL)
163 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
164 ASN1_OCTET_STRING_free(ctx->secretValue);
166 X509_NAME_free(ctx->recipient);
167 ASN1_OCTET_STRING_free(ctx->transactionID);
168 ASN1_OCTET_STRING_free(ctx->senderNonce);
169 ASN1_OCTET_STRING_free(ctx->recipNonce);
170 sk_OSSL_CMP_ITAV_pop_free(ctx->geninfo_ITAVs, OSSL_CMP_ITAV_free);
171 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
173 EVP_PKEY_free(ctx->newPkey);
174 X509_NAME_free(ctx->issuer);
175 X509_NAME_free(ctx->subjectName);
176 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
177 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
178 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
179 X509_free(ctx->oldCert);
180 X509_REQ_free(ctx->p10CSR);
182 sk_OSSL_CMP_ITAV_pop_free(ctx->genm_ITAVs, OSSL_CMP_ITAV_free);
184 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
185 X509_free(ctx->newCert);
186 sk_X509_pop_free(ctx->caPubs, X509_free);
187 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
192 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX *ctx, int status)
194 if (!ossl_assert(ctx != NULL))
196 ctx->status = status;
201 * Returns the PKIStatus from the last CertRepMessage
202 * or Revocation Response or error message, -1 on error
204 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX *ctx)
207 CMPerr(0, CMP_R_NULL_ARGUMENT);
214 * Returns the statusString from the last CertRepMessage
215 * or Revocation Response or error message, NULL on error
217 OSSL_CMP_PKIFREETEXT *OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX *ctx)
220 CMPerr(0, CMP_R_NULL_ARGUMENT);
223 return ctx->statusString;
226 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX *ctx,
227 OSSL_CMP_PKIFREETEXT *text)
229 if (!ossl_assert(ctx != NULL))
231 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
232 ctx->statusString = text;
236 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
238 if (!ossl_assert(ctx != NULL))
240 X509_free(ctx->validatedSrvCert);
241 ctx->validatedSrvCert = cert;
245 /* Set callback function for checking if the cert is ok or should be rejected */
246 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_certConf_cb_t cb)
249 CMPerr(0, CMP_R_NULL_ARGUMENT);
252 ctx->certConf_cb = cb;
257 * Set argument, respectively a pointer to a structure containing arguments,
258 * optionally to be used by the certConf callback.
260 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
263 CMPerr(0, CMP_R_NULL_ARGUMENT);
266 ctx->certConf_cb_arg = arg;
271 * Get argument, respectively the pointer to a structure containing arguments,
272 * optionally to be used by certConf callback.
273 * Returns callback argument set previously (NULL if not set or on error)
275 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX *ctx)
278 CMPerr(0, CMP_R_NULL_ARGUMENT);
281 return ctx->certConf_cb_arg;
284 #ifndef OPENSSL_NO_TRACE
285 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
286 int category, int cmd, void *vdata)
288 OSSL_CMP_CTX *ctx = vdata;
290 OSSL_CMP_severity level = -1;
295 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
297 if (ctx->log_cb == NULL)
298 return 1; /* silently drop message */
300 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
302 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
303 goto end; /* suppress output since severity is not sufficient */
305 if (!ctx->log_cb(func != NULL ? func : "(no func)",
306 file != NULL ? file : "(no file)",
317 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
318 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
319 const char *func, const char *file, int line,
320 const char *level_str, const char *format, ...)
323 char hugebuf[1024 * 2];
326 if (ctx == NULL || ctx->log_cb == NULL)
327 return 1; /* silently drop message */
329 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
330 return 1; /* suppress output since severity is not sufficient */
335 va_start(args, format);
338 func = "(unset function name)";
340 file = "(unset file name)";
341 if (level_str == NULL)
342 level_str = "(unset level string)";
344 #ifndef OPENSSL_NO_TRACE
345 if (OSSL_TRACE_ENABLED(CMP)) {
346 OSSL_TRACE_BEGIN(CMP) {
348 BIO_snprintf(hugebuf, sizeof(hugebuf),
349 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
350 func, file, line, level_str);
351 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
352 if (BIO_vsnprintf(hugebuf + printed,
353 sizeof(hugebuf) - printed, format, args) > 0)
354 res = BIO_puts(trc_out, hugebuf) > 0;
356 } OSSL_TRACE_END(CMP);
358 #else /* compensate for disabled trace API */
360 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
361 res = ctx->log_cb(func, file, line, level, hugebuf);
368 /* Set a callback function for error reporting and logging messages */
369 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
372 CMPerr(0, CMP_R_NULL_ARGUMENT);
377 #ifndef OPENSSL_NO_TRACE
378 /* do also in case cb == NULL, to switch off logging output: */
379 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
380 ossl_cmp_log_trace_cb, ctx))
387 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
388 void OSSL_CMP_CTX_print_errors(OSSL_CMP_CTX *ctx)
390 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
394 * Set or clear the reference value to be used for identification
395 * (i.e., the user name) when using PBMAC.
397 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
398 const unsigned char *ref, int len)
401 CMPerr(0, CMP_R_NULL_ARGUMENT);
404 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref,
408 /* Set or clear the password to be used for protecting messages with PBMAC */
409 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
412 ASN1_OCTET_STRING *secretValue = NULL;
414 CMPerr(0, CMP_R_NULL_ARGUMENT);
417 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
419 if (ctx->secretValue != NULL) {
420 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
421 ASN1_OCTET_STRING_free(ctx->secretValue);
423 ctx->secretValue = secretValue;
428 * Returns the stack of certificates received in a response message.
429 * The stack is duplicated so the caller must handle freeing it!
430 * Returns pointer to created stack on success, NULL on error
432 STACK_OF(X509) *OSSL_CMP_CTX_get1_extraCertsIn(const OSSL_CMP_CTX *ctx)
435 CMPerr(0, CMP_R_NULL_ARGUMENT);
438 if (ctx->extraCertsIn == NULL)
439 return sk_X509_new_null();
440 return X509_chain_up_ref(ctx->extraCertsIn);
444 * Copies any given stack of inbound X509 certificates to extraCertsIn
445 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
447 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX *ctx,
448 STACK_OF(X509) *extraCertsIn)
450 if (!ossl_assert(ctx != NULL))
453 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
454 ctx->extraCertsIn = NULL;
455 if (extraCertsIn == NULL)
457 return (ctx->extraCertsIn = X509_chain_up_ref(extraCertsIn)) != NULL;
461 * Duplicate and set the given stack as the new stack of X509
462 * certificates to send out in the extraCerts field.
464 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX *ctx,
465 STACK_OF(X509) *extraCertsOut)
468 CMPerr(0, CMP_R_NULL_ARGUMENT);
472 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
473 ctx->extraCertsOut = NULL;
474 if (extraCertsOut == NULL)
476 return (ctx->extraCertsOut = X509_chain_up_ref(extraCertsOut)) != NULL;
480 * Add the given policy info object
481 * to the X509_EXTENSIONS of the requested certificate template.
483 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
485 if (ctx == NULL || pinfo == NULL) {
486 CMPerr(0, CMP_R_NULL_ARGUMENT);
490 if (ctx->policies == NULL
491 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
494 return sk_POLICYINFO_push(ctx->policies, pinfo);
497 /* Add an ITAV for geninfo of the PKI message header */
498 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
501 CMPerr(0, CMP_R_NULL_ARGUMENT);
504 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
507 /* Add an itav for the body of outgoing general messages */
508 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
511 CMPerr(0, CMP_R_NULL_ARGUMENT);
514 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
518 * Returns a duplicate of the stack of X509 certificates that
519 * were received in the caPubs field of the last CertRepMessage.
520 * Returns NULL on error
522 STACK_OF(X509) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX *ctx)
525 CMPerr(0, CMP_R_NULL_ARGUMENT);
528 if (ctx->caPubs == NULL)
529 return sk_X509_new_null();
530 return X509_chain_up_ref(ctx->caPubs);
534 * Duplicate and copy the given stack of certificates to the given
535 * OSSL_CMP_CTX structure so that they may be retrieved later.
537 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *caPubs)
539 if (!ossl_assert(ctx != NULL))
542 sk_X509_pop_free(ctx->caPubs, X509_free);
546 return (ctx->caPubs = X509_chain_up_ref(caPubs)) != NULL;
549 #define char_dup OPENSSL_strdup
550 #define char_free OPENSSL_free
551 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
552 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
554 TYPE *val_dup = NULL; \
557 CMPerr(0, CMP_R_NULL_ARGUMENT); \
561 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
563 TYPE##_free(ctx->FIELD); \
564 ctx->FIELD = val_dup; \
568 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
569 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
572 CMPerr(0, CMP_R_NULL_ARGUMENT); \
576 if (val != NULL && !TYPE##_up_ref(val)) \
578 TYPE##_free(ctx->FIELD); \
584 * Pins the server certificate to be directly trusted (even if it is expired)
585 * for verifying response messages.
586 * Cert pointer is not consumed. It may be NULL to clear the entry.
588 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert, X509)
590 /* Set the X509 name of the recipient. Set in the PKIHeader */
591 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
593 /* Store the X509 name of the expected sender in the PKIHeader of responses */
594 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
596 /* Set the X509 name of the issuer. Set in the PKIHeader */
597 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
600 * Set the subject name that will be placed in the certificate
601 * request. This will be the subject name on the received certificate.
603 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
605 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
606 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
609 CMPerr(0, CMP_R_NULL_ARGUMENT);
613 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
614 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
615 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
618 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
619 ctx->reqExtensions = exts;
623 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
624 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
627 CMPerr(0, CMP_R_NULL_ARGUMENT);
630 /* if one of the following conditions 'fail' this is not an error */
631 return ctx->reqExtensions != NULL
632 && X509v3_get_ext_by_NID(ctx->reqExtensions,
633 NID_subject_alt_name, -1) >= 0;
637 * Add a GENERAL_NAME structure that will be added to the CRMF
638 * request's extensions field to request subject alternative names.
640 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
641 const GENERAL_NAME *name)
643 GENERAL_NAME *name_dup;
645 if (ctx == NULL || name == NULL) {
646 CMPerr(0, CMP_R_NULL_ARGUMENT);
650 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
651 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
655 if (ctx->subjectAltNames == NULL
656 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
658 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
660 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
661 GENERAL_NAME_free(name_dup);
668 * Set our own client certificate, used for example in KUR and when
669 * doing the IR with existing certificate.
671 DEFINE_OSSL_CMP_CTX_set1_up_ref(clCert, X509)
674 * Set the old certificate that we are updating in KUR
675 * or the certificate to be revoked in RR, respectively.
676 * Also used as reference cert (defaulting to clCert) for deriving subject DN
677 * and SANs. Its issuer is used as default recipient in the CMP message header.
679 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert, X509)
681 /* Set the PKCS#10 CSR to be sent in P10CR */
682 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
685 * Set the (newly received in IP/KUP/CP) certificate in the context.
686 * TODO: this only permits for one cert to be enrolled at a time.
688 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX *ctx, X509 *cert)
690 if (!ossl_assert(ctx != NULL))
693 X509_free(ctx->newCert);
699 * Get the (newly received in IP/KUP/CP) client certificate from the context
700 * TODO: this only permits for one client cert to be received...
702 X509 *OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX *ctx)
705 CMPerr(0, CMP_R_NULL_ARGUMENT);
711 /* Set the client's current private key */
712 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey, EVP_PKEY)
714 /* Set new key pair. Used e.g. when doing Key Update */
715 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
718 CMPerr(0, CMP_R_NULL_ARGUMENT);
722 EVP_PKEY_free(ctx->newPkey);
724 ctx->newPkey_priv = priv;
728 /* Get the private/public key to use for cert enrollment, or NULL on error */
729 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
732 CMPerr(0, CMP_R_NULL_ARGUMENT);
736 if (ctx->newPkey != NULL)
737 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
738 if (ctx->p10CSR != NULL)
739 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
740 return ctx->pkey; /* may be NULL */
743 /* Set the given transactionID to the context */
744 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX *ctx,
745 const ASN1_OCTET_STRING *id)
748 CMPerr(0, CMP_R_NULL_ARGUMENT);
751 return ossl_cmp_asn1_octet_string_set1(&ctx->transactionID, id);
754 /* Set the nonce to be used for the recipNonce in the message created next */
755 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX *ctx,
756 const ASN1_OCTET_STRING *nonce)
758 if (!ossl_assert(ctx != NULL))
760 return ossl_cmp_asn1_octet_string_set1(&ctx->recipNonce, nonce);
763 /* Stores the given nonce as the last senderNonce sent out */
764 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
765 const ASN1_OCTET_STRING *nonce)
768 CMPerr(0, CMP_R_NULL_ARGUMENT);
771 return ossl_cmp_asn1_octet_string_set1(&ctx->senderNonce, nonce);
774 /* Set the proxy server to use for HTTP(S) connections */
775 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
777 /* Set the (HTTP) host name of the CMP server */
778 DEFINE_OSSL_CMP_CTX_set1(serverName, char)
780 /* Set the server exclusion list of the HTTP proxy server */
781 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
783 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
784 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX *ctx, OSSL_HTTP_bio_cb_t cb)
787 CMPerr(0, CMP_R_NULL_ARGUMENT);
794 /* Set argument optionally to be used by the http connect/disconnect callback */
795 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
798 CMPerr(0, CMP_R_NULL_ARGUMENT);
801 ctx->http_cb_arg = arg;
806 * Get argument optionally to be used by the http connect/disconnect callback
807 * Returns callback argument set previously (NULL if not set or on error)
809 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX *ctx)
812 CMPerr(0, CMP_R_NULL_ARGUMENT);
815 return ctx->http_cb_arg;
818 /* Set callback function for sending CMP request and receiving response */
819 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_transfer_cb_t cb)
822 CMPerr(0, CMP_R_NULL_ARGUMENT);
825 ctx->transfer_cb = cb;
829 /* Set argument optionally to be used by the transfer callback */
830 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
833 CMPerr(0, CMP_R_NULL_ARGUMENT);
836 ctx->transfer_cb_arg = arg;
841 * Get argument optionally to be used by the transfer callback.
842 * Returns callback argument set previously (NULL if not set or on error)
844 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX *ctx)
847 CMPerr(0, CMP_R_NULL_ARGUMENT);
850 return ctx->transfer_cb_arg;
853 /** Set the HTTP server port to be used */
854 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX *ctx, int port)
857 CMPerr(0, CMP_R_NULL_ARGUMENT);
860 ctx->serverPort = port;
864 /* Set the HTTP path to be used on the server (e.g "pkix/") */
865 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
867 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
868 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX *ctx, int fail_info)
870 if (!ossl_assert(ctx != NULL))
872 ctx->failInfoCode = fail_info;
877 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
878 * Returns bit string as integer on success, -1 on error
880 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX *ctx)
883 CMPerr(0, CMP_R_NULL_ARGUMENT);
886 return ctx->failInfoCode;
889 /* Set a Boolean or integer option of the context to the "val" arg */
890 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
895 CMPerr(0, CMP_R_NULL_ARGUMENT);
900 case OSSL_CMP_OPT_REVOCATION_REASON:
901 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
903 case OSSL_CMP_OPT_POPO_METHOD:
904 min_val = OSSL_CRMF_POPO_NONE;
911 CMPerr(0, CMP_R_INVALID_ARGS);
916 case OSSL_CMP_OPT_LOG_VERBOSITY:
917 if (val > OSSL_CMP_LOG_DEBUG) {
918 CMPerr(0, CMP_R_INVALID_ARGS);
921 ctx->log_verbosity = val;
923 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
924 ctx->implicitConfirm = val;
926 case OSSL_CMP_OPT_DISABLE_CONFIRM:
927 ctx->disableConfirm = val;
929 case OSSL_CMP_OPT_UNPROTECTED_SEND:
930 ctx->unprotectedSend = val;
932 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
933 ctx->unprotectedErrors = val;
935 case OSSL_CMP_OPT_VALIDITY_DAYS:
938 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
939 ctx->SubjectAltName_nodefault = val;
941 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
942 ctx->setSubjectAltNameCritical = val;
944 case OSSL_CMP_OPT_POLICIES_CRITICAL:
945 ctx->setPoliciesCritical = val;
947 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
948 ctx->ignore_keyusage = val;
950 case OSSL_CMP_OPT_POPO_METHOD:
951 if (val > OSSL_CRMF_POPO_KEYAGREE) {
952 CMPerr(0, CMP_R_INVALID_ARGS);
955 ctx->popoMethod = val;
957 case OSSL_CMP_OPT_DIGEST_ALGNID:
960 case OSSL_CMP_OPT_OWF_ALGNID:
963 case OSSL_CMP_OPT_MAC_ALGNID:
966 case OSSL_CMP_OPT_MSG_TIMEOUT:
967 ctx->msg_timeout = val;
969 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
970 ctx->total_timeout = val;
972 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
973 ctx->permitTAInExtraCertsForIR = val;
975 case OSSL_CMP_OPT_REVOCATION_REASON:
976 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
977 CMPerr(0, CMP_R_INVALID_ARGS);
980 ctx->revocationReason = val;
983 CMPerr(0, CMP_R_INVALID_ARGS);
991 * Reads a Boolean or integer option value from the context.
992 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
994 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
997 CMPerr(0, CMP_R_NULL_ARGUMENT);
1002 case OSSL_CMP_OPT_LOG_VERBOSITY:
1003 return ctx->log_verbosity;
1004 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1005 return ctx->implicitConfirm;
1006 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1007 return ctx->disableConfirm;
1008 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1009 return ctx->unprotectedSend;
1010 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1011 return ctx->unprotectedErrors;
1012 case OSSL_CMP_OPT_VALIDITY_DAYS:
1014 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1015 return ctx->SubjectAltName_nodefault;
1016 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1017 return ctx->setSubjectAltNameCritical;
1018 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1019 return ctx->setPoliciesCritical;
1020 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1021 return ctx->ignore_keyusage;
1022 case OSSL_CMP_OPT_POPO_METHOD:
1023 return ctx->popoMethod;
1024 case OSSL_CMP_OPT_DIGEST_ALGNID:
1026 case OSSL_CMP_OPT_OWF_ALGNID:
1027 return ctx->pbm_owf;
1028 case OSSL_CMP_OPT_MAC_ALGNID:
1029 return ctx->pbm_mac;
1030 case OSSL_CMP_OPT_MSG_TIMEOUT:
1031 return ctx->msg_timeout;
1032 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1033 return ctx->total_timeout;
1034 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1035 return ctx->permitTAInExtraCertsForIR;
1036 case OSSL_CMP_OPT_REVOCATION_REASON:
1037 return ctx->revocationReason;
1039 CMPerr(0, CMP_R_INVALID_ARGS);