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>
24 * Get current certificate store containing trusted root CA certs
26 X509_STORE *OSSL_CMP_CTX_get0_trustedStore(const OSSL_CMP_CTX *ctx)
29 CMPerr(0, CMP_R_NULL_ARGUMENT);
36 * Set certificate store containing trusted (root) CA certs and possibly CRLs
37 * and a cert verification callback function used for CMP server authentication.
38 * Any already existing store entry is freed. Given NULL, the entry is reset.
39 * returns 1 on success, 0 on error
41 int OSSL_CMP_CTX_set0_trustedStore(OSSL_CMP_CTX *ctx, X509_STORE *store)
44 CMPerr(0, CMP_R_NULL_ARGUMENT);
47 X509_STORE_free(ctx->trusted);
53 * Get current list of non-trusted intermediate certs
55 STACK_OF(X509) *OSSL_CMP_CTX_get0_untrusted_certs(const OSSL_CMP_CTX *ctx)
58 CMPerr(0, CMP_R_NULL_ARGUMENT);
61 return ctx->untrusted_certs;
65 * Set untrusted certificates for path construction in authentication of
66 * the CMP server and potentially others (TLS server, newly enrolled cert).
67 * returns 1 on success, 0 on error
69 int OSSL_CMP_CTX_set1_untrusted_certs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
71 STACK_OF(X509) *untrusted_certs;
73 CMPerr(0, CMP_R_NULL_ARGUMENT);
76 if ((untrusted_certs = sk_X509_new_null()) == NULL)
78 if (ossl_cmp_sk_X509_add1_certs(untrusted_certs, certs, 0, 1, 0) != 1)
80 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
81 ctx->untrusted_certs = untrusted_certs;
84 sk_X509_pop_free(untrusted_certs, X509_free);
89 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
90 * Returns new context on success, NULL on error
92 OSSL_CMP_CTX *OSSL_CMP_CTX_new(void)
94 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
99 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
102 ctx->failInfoCode = -1;
104 ctx->serverPort = OSSL_CMP_DEFAULT_PORT;
105 ctx->proxyPort = OSSL_CMP_DEFAULT_PORT;
106 ctx->msgtimeout = 2 * 60;
108 if ((ctx->untrusted_certs = sk_X509_new_null()) == NULL)
112 ctx->pbm_owf = NID_sha256;
113 ctx->pbm_itercnt = 500;
114 ctx->pbm_mac = NID_hmac_sha1;
116 ctx->digest = NID_sha256;
117 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
118 ctx->revocationReason = CRL_REASON_NONE;
120 /* all other elements are initialized to 0 or NULL, respectively */
124 OSSL_CMP_CTX_free(ctx);
129 * Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX
131 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
134 CMPerr(0, CMP_R_NULL_ARGUMENT);
139 ctx->failInfoCode = -1;
141 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
142 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
143 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
144 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
145 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
146 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
147 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
148 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
152 * Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new()
154 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
159 OPENSSL_free(ctx->serverPath);
160 OPENSSL_free(ctx->serverName);
161 OPENSSL_free(ctx->proxyName);
163 X509_free(ctx->srvCert);
164 X509_free(ctx->validatedSrvCert);
165 X509_NAME_free(ctx->expected_sender);
166 X509_STORE_free(ctx->trusted);
167 sk_X509_pop_free(ctx->untrusted_certs, X509_free);
169 X509_free(ctx->clCert);
170 EVP_PKEY_free(ctx->pkey);
171 ASN1_OCTET_STRING_free(ctx->referenceValue);
172 if (ctx->secretValue != NULL)
173 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
174 ASN1_OCTET_STRING_free(ctx->secretValue);
176 X509_NAME_free(ctx->recipient);
177 ASN1_OCTET_STRING_free(ctx->transactionID);
178 ASN1_OCTET_STRING_free(ctx->senderNonce);
179 ASN1_OCTET_STRING_free(ctx->recipNonce);
180 sk_OSSL_CMP_ITAV_pop_free(ctx->geninfo_ITAVs, OSSL_CMP_ITAV_free);
181 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
183 EVP_PKEY_free(ctx->newPkey);
184 X509_NAME_free(ctx->issuer);
185 X509_NAME_free(ctx->subjectName);
186 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
187 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
188 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
189 X509_free(ctx->oldCert);
190 X509_REQ_free(ctx->p10CSR);
192 sk_OSSL_CMP_ITAV_pop_free(ctx->genm_ITAVs, OSSL_CMP_ITAV_free);
194 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
195 X509_free(ctx->newCert);
196 sk_X509_pop_free(ctx->caPubs, X509_free);
197 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
202 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX *ctx, int status)
204 if (!ossl_assert(ctx != NULL))
206 ctx->status = status;
211 * Returns the PKIStatus from the last CertRepMessage
212 * or Revocation Response or error message, -1 on error
214 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX *ctx)
217 CMPerr(0, CMP_R_NULL_ARGUMENT);
224 * Returns the statusString from the last CertRepMessage
225 * or Revocation Response or error message, NULL on error
227 OSSL_CMP_PKIFREETEXT *OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX *ctx)
230 CMPerr(0, CMP_R_NULL_ARGUMENT);
233 return ctx->statusString;
236 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX *ctx,
237 OSSL_CMP_PKIFREETEXT *text)
239 if (!ossl_assert(ctx != NULL))
241 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
242 ctx->statusString = text;
246 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
248 if (!ossl_assert(ctx != NULL))
250 X509_free(ctx->validatedSrvCert);
251 ctx->validatedSrvCert = cert;
256 * Set callback function for checking if the cert is ok or should
258 * Returns 1 on success, 0 on error
260 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX *ctx, OSSL_cmp_certConf_cb_t cb)
263 CMPerr(0, CMP_R_NULL_ARGUMENT);
266 ctx->certConf_cb = cb;
271 * Set argument, respectively a pointer to a structure containing arguments,
272 * optionally to be used by the certConf callback.
273 * Returns 1 on success, 0 on error
275 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
278 CMPerr(0, CMP_R_NULL_ARGUMENT);
281 ctx->certConf_cb_arg = arg;
286 * Get argument, respectively the pointer to a structure containing arguments,
287 * optionally to be used by certConf callback.
288 * Returns callback argument set previously (NULL if not set or on error)
290 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX *ctx)
293 CMPerr(0, CMP_R_NULL_ARGUMENT);
296 return ctx->certConf_cb_arg;
299 #ifndef OPENSSL_NO_TRACE
300 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
301 int category, int cmd, void *vdata)
303 OSSL_CMP_CTX *ctx = vdata;
305 OSSL_CMP_severity level = -1;
310 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
312 if (ctx->log_cb == NULL)
313 return 1; /* silently drop message */
315 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
317 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
318 goto end; /* suppress output since severity is not sufficient */
320 if (!ctx->log_cb(func != NULL ? func : "(no func)",
321 file != NULL ? file : "(no file)",
332 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
333 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
334 const char *func, const char *file, int line,
335 const char *level_str, const char *format, ...)
338 char hugebuf[1024 * 2];
341 if (ctx == NULL || ctx->log_cb == NULL)
342 return 1; /* silently drop message */
344 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
345 return 1; /* suppress output since severity is not sufficient */
350 va_start(args, format);
353 func = "(unset function name)";
355 file = "(unset file name)";
356 if (level_str == NULL)
357 level_str = "(unset level string)";
359 #ifndef OPENSSL_NO_TRACE
360 if (OSSL_TRACE_ENABLED(CMP)) {
361 OSSL_TRACE_BEGIN(CMP) {
363 BIO_snprintf(hugebuf, sizeof(hugebuf),
364 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
365 func, file, line, level_str);
366 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
367 if (BIO_vsnprintf(hugebuf + printed,
368 sizeof(hugebuf) - printed, format, args) > 0)
369 res = BIO_puts(trc_out, hugebuf) > 0;
371 } OSSL_TRACE_END(CMP);
373 #else /* compensate for disabled trace API */
375 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
376 res = ctx->log_cb(func, file, line, level, hugebuf);
384 * Set a callback function for error reporting and logging messages.
385 * Returns 1 on success, 0 on error
387 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_cmp_log_cb_t cb)
390 CMPerr(0, CMP_R_NULL_ARGUMENT);
395 #ifndef OPENSSL_NO_TRACE
396 /* do also in case cb == NULL, to switch off logging output: */
397 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
398 ossl_cmp_log_trace_cb, ctx))
405 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
406 void OSSL_CMP_CTX_print_errors(OSSL_CMP_CTX *ctx)
408 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
412 * Set or clear the reference value to be used for identification
413 * (i.e., the user name) when using PBMAC.
414 * Returns 1 on success, 0 on error
416 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
417 const unsigned char *ref, int len)
420 CMPerr(0, CMP_R_NULL_ARGUMENT);
423 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref,
428 * Set or clear the password to be used for protecting messages with PBMAC.
429 * Returns 1 on success, 0 on error
431 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
434 ASN1_OCTET_STRING *secretValue = NULL;
436 CMPerr(0, 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;
450 * Returns the stack of certificates received in a response message.
451 * The stack is duplicated so the caller must handle freeing it!
452 * Returns pointer to created stack on success, NULL on error
454 STACK_OF(X509) *OSSL_CMP_CTX_get1_extraCertsIn(const OSSL_CMP_CTX *ctx)
457 CMPerr(0, CMP_R_NULL_ARGUMENT);
460 if (ctx->extraCertsIn == NULL)
461 return sk_X509_new_null();
462 return X509_chain_up_ref(ctx->extraCertsIn);
466 * Copies any given stack of inbound X509 certificates to extraCertsIn
467 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
468 * Returns 1 on success, 0 on error.
470 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX *ctx,
471 STACK_OF(X509) *extraCertsIn)
473 if (!ossl_assert(ctx != NULL))
476 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
477 ctx->extraCertsIn = NULL;
478 if (extraCertsIn == NULL)
480 return (ctx->extraCertsIn = X509_chain_up_ref(extraCertsIn)) != NULL;
484 * Duplicate and set the given stack as the new stack of X509
485 * certificates to send out in the extraCerts field.
486 * Returns 1 on success, 0 on error
488 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX *ctx,
489 STACK_OF(X509) *extraCertsOut)
492 CMPerr(0, CMP_R_NULL_ARGUMENT);
496 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
497 ctx->extraCertsOut = NULL;
498 if (extraCertsOut == NULL)
500 return (ctx->extraCertsOut = X509_chain_up_ref(extraCertsOut)) != NULL;
504 * Add the given policy info object
505 * to the X509_EXTENSIONS of the requested certificate template.
506 * Returns 1 on success, 0 on error.
508 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
510 if (ctx == NULL || pinfo == NULL) {
511 CMPerr(0, CMP_R_NULL_ARGUMENT);
515 if (ctx->policies == NULL
516 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
519 return sk_POLICYINFO_push(ctx->policies, pinfo);
523 * Add an ITAV for geninfo of the PKI message header
525 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
528 CMPerr(0, CMP_R_NULL_ARGUMENT);
531 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
535 * Add an itav for the body of outgoing general messages
537 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
540 CMPerr(0, CMP_R_NULL_ARGUMENT);
543 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
547 * Returns a duplicate of the stack of X509 certificates that
548 * were received in the caPubs field of the last CertRepMessage.
549 * Returns NULL on error
551 STACK_OF(X509) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX *ctx)
554 CMPerr(0, CMP_R_NULL_ARGUMENT);
557 if (ctx->caPubs == NULL)
558 return sk_X509_new_null();
559 return X509_chain_up_ref(ctx->caPubs);
563 * Duplicate and copy the given stack of certificates to the given
564 * OSSL_CMP_CTX structure so that they may be retrieved later.
565 * Returns 1 on success, 0 on error
567 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *caPubs)
569 if (!ossl_assert(ctx != NULL))
572 sk_X509_pop_free(ctx->caPubs, X509_free);
576 return (ctx->caPubs = X509_chain_up_ref(caPubs)) != NULL;
579 #define char_dup OPENSSL_strdup
580 #define char_free OPENSSL_free
581 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
582 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
584 TYPE *val_dup = NULL; \
587 CMPerr(0, CMP_R_NULL_ARGUMENT); \
591 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
593 TYPE##_free(ctx->FIELD); \
594 ctx->FIELD = val_dup; \
598 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
599 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
602 CMPerr(0, CMP_R_NULL_ARGUMENT); \
606 if (val != NULL && !TYPE##_up_ref(val)) \
608 TYPE##_free(ctx->FIELD); \
614 * Pins the server certificate to be directly trusted (even if it is expired)
615 * for verifying response messages.
616 * Cert pointer is not consumed. It may be NULL to clear the entry.
617 * Returns 1 on success, 0 on error
619 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert, X509)
622 * Set the X509 name of the recipient. Set in the PKIHeader.
623 * returns 1 on success, 0 on error
625 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
628 * Store the X509 name of the expected sender in the PKIHeader of responses.
629 * Returns 1 on success, 0 on error
631 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
634 * Set the X509 name of the issuer. Set in the PKIHeader.
635 * Returns 1 on success, 0 on error
637 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
640 * Set the subject name that will be placed in the certificate
641 * request. This will be the subject name on the received certificate.
642 * Returns 1 on success, 0 on error
644 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
647 * Set the X.509v3 certificate request extensions to be used in IR/CR/KUR.
648 * Returns 1 on success, 0 on error
650 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
653 CMPerr(0, CMP_R_NULL_ARGUMENT);
657 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
658 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
659 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
662 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
663 ctx->reqExtensions = exts;
667 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
668 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
671 CMPerr(0, CMP_R_NULL_ARGUMENT);
674 /* if one of the following conditions 'fail' this is not an error */
675 return ctx->reqExtensions != NULL
676 && X509v3_get_ext_by_NID(ctx->reqExtensions,
677 NID_subject_alt_name, -1) >= 0;
681 * Add a GENERAL_NAME structure that will be added to the CRMF
682 * request's extensions field to request subject alternative names.
683 * Returns 1 on success, 0 on error
685 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
686 const GENERAL_NAME *name)
688 GENERAL_NAME *name_dup;
690 if (ctx == NULL || name == NULL) {
691 CMPerr(0, CMP_R_NULL_ARGUMENT);
695 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
696 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
700 if (ctx->subjectAltNames == NULL
701 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
703 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
705 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
706 GENERAL_NAME_free(name_dup);
713 * Set our own client certificate, used for example in KUR and when
714 * doing the IR with existing certificate.
715 * Returns 1 on success, 0 on error
717 DEFINE_OSSL_CMP_CTX_set1_up_ref(clCert, X509)
720 * Set the old certificate that we are updating in KUR
721 * or the certificate to be revoked in RR, respectively.
722 * Also used as reference cert (defaulting to clCert) for deriving subject DN
723 * and SANs. Its issuer is used as default recipient in the CMP message header.
724 * Returns 1 on success, 0 on error
726 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert, X509)
729 * Set the PKCS#10 CSR to be sent in P10CR.
730 * Returns 1 on success, 0 on error
732 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
735 * Sets the (newly received in IP/KUP/CP) certificate in the context.
736 * Returns 1 on success, 0 on error
737 * TODO: this only permits for one cert to be enrolled at a time.
739 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX *ctx, X509 *cert)
741 if (!ossl_assert(ctx != NULL))
744 X509_free(ctx->newCert);
750 * Get the (newly received in IP/KUP/CP) client certificate from the context
751 * TODO: this only permits for one client cert to be received...
753 X509 *OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX *ctx)
756 CMPerr(0, CMP_R_NULL_ARGUMENT);
763 * Set the client's current private key.
764 * Returns 1 on success, 0 on error
766 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey, EVP_PKEY)
769 * Set new key pair. Used e.g. when doing Key Update.
770 * Returns 1 on success, 0 on error
772 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
775 CMPerr(0, CMP_R_NULL_ARGUMENT);
779 EVP_PKEY_free(ctx->newPkey);
781 ctx->newPkey_priv = priv;
786 * gets the private/public key to use for certificate enrollment, NULL on error
788 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
791 CMPerr(0, CMP_R_NULL_ARGUMENT);
795 if (ctx->newPkey != NULL)
796 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
797 if (ctx->p10CSR != NULL)
798 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
799 return ctx->pkey; /* may be NULL */
803 * Sets the given transactionID to the context.
804 * Returns 1 on success, 0 on error
806 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX *ctx,
807 const ASN1_OCTET_STRING *id)
810 CMPerr(0, CMP_R_NULL_ARGUMENT);
813 return ossl_cmp_asn1_octet_string_set1(&ctx->transactionID, id);
817 * sets the given nonce to be used for the recipNonce in the next message to be
819 * returns 1 on success, 0 on error
821 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX *ctx,
822 const ASN1_OCTET_STRING *nonce)
824 if (!ossl_assert(ctx != NULL))
826 return ossl_cmp_asn1_octet_string_set1(&ctx->recipNonce, nonce);
830 * Stores the given nonce as the last senderNonce sent out.
831 * Returns 1 on success, 0 on error
833 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
834 const ASN1_OCTET_STRING *nonce)
837 CMPerr(0, CMP_R_NULL_ARGUMENT);
840 return ossl_cmp_asn1_octet_string_set1(&ctx->senderNonce, nonce);
844 * Set the host name of the (HTTP) proxy server to use for all connections
845 * returns 1 on success, 0 on error
847 DEFINE_OSSL_CMP_CTX_set1(proxyName, char)
850 * Set the (HTTP) host name of the CA server.
851 * Returns 1 on success, 0 on error
853 DEFINE_OSSL_CMP_CTX_set1(serverName, char)
856 * Sets the (HTTP) proxy port to be used.
857 * Returns 1 on success, 0 on error
859 int OSSL_CMP_CTX_set_proxyPort(OSSL_CMP_CTX *ctx, int port)
862 CMPerr(0, CMP_R_NULL_ARGUMENT);
865 ctx->proxyPort = port;
870 * sets the http connect/disconnect callback function to be used for HTTP(S)
871 * returns 1 on success, 0 on error
873 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX *ctx, OSSL_HTTP_bio_cb_t cb)
876 CMPerr(0, CMP_R_NULL_ARGUMENT);
884 * Set argument optionally to be used by the http connect/disconnect callback.
885 * Returns 1 on success, 0 on error
887 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
890 CMPerr(0, CMP_R_NULL_ARGUMENT);
893 ctx->http_cb_arg = arg;
898 * Get argument optionally to be used by the http connect/disconnect callback
899 * Returns callback argument set previously (NULL if not set or on error)
901 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX *ctx)
904 CMPerr(0, CMP_R_NULL_ARGUMENT);
907 return ctx->http_cb_arg;
911 * Set callback function for sending CMP request and receiving response.
912 * Returns 1 on success, 0 on error
914 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX *ctx, OSSL_cmp_transfer_cb_t cb)
917 CMPerr(0, CMP_R_NULL_ARGUMENT);
920 ctx->transfer_cb = cb;
925 * Set argument optionally to be used by the transfer callback.
926 * Returns 1 on success, 0 on error
928 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
931 CMPerr(0, CMP_R_NULL_ARGUMENT);
934 ctx->transfer_cb_arg = arg;
939 * Get argument optionally to be used by the transfer callback.
940 * Returns callback argument set previously (NULL if not set or on error)
942 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX *ctx)
945 CMPerr(0, CMP_R_NULL_ARGUMENT);
948 return ctx->transfer_cb_arg;
952 * Sets the (HTTP) server port to be used.
953 * Returns 1 on success, 0 on error
955 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX *ctx, int port)
958 CMPerr(0, CMP_R_NULL_ARGUMENT);
961 ctx->serverPort = port;
966 * Sets the HTTP path to be used on the server (e.g "pkix/").
967 * Returns 1 on success, 0 on error
969 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
972 * Set the failInfo error code as bit encoding in OSSL_CMP_CTX.
973 * Returns 1 on success, 0 on error
975 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX *ctx, int fail_info)
977 if (!ossl_assert(ctx != NULL))
979 ctx->failInfoCode = fail_info;
984 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
985 * Returns bit string as integer on success, -1 on error
987 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX *ctx)
990 CMPerr(0, CMP_R_NULL_ARGUMENT);
993 return ctx->failInfoCode;
997 * Sets a Boolean or integer option of the context to the "val" arg.
998 * Returns 1 on success, 0 on error
1000 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
1005 CMPerr(0, CMP_R_NULL_ARGUMENT);
1010 case OSSL_CMP_OPT_REVOCATION_REASON:
1011 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
1013 case OSSL_CMP_OPT_POPOMETHOD:
1014 min_val = OSSL_CRMF_POPO_NONE;
1020 if (val < min_val) {
1021 CMPerr(0, CMP_R_INVALID_ARGS);
1026 case OSSL_CMP_OPT_LOG_VERBOSITY:
1027 if (val > OSSL_CMP_LOG_DEBUG) {
1028 CMPerr(0, CMP_R_INVALID_ARGS);
1031 ctx->log_verbosity = val;
1033 case OSSL_CMP_OPT_IMPLICITCONFIRM:
1034 ctx->implicitConfirm = val;
1036 case OSSL_CMP_OPT_DISABLECONFIRM:
1037 ctx->disableConfirm = val;
1039 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1040 ctx->unprotectedSend = val;
1042 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1043 ctx->unprotectedErrors = val;
1045 case OSSL_CMP_OPT_VALIDITYDAYS:
1048 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1049 ctx->SubjectAltName_nodefault = val;
1051 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1052 ctx->setSubjectAltNameCritical = val;
1054 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1055 ctx->setPoliciesCritical = val;
1057 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1058 ctx->ignore_keyusage = val;
1060 case OSSL_CMP_OPT_POPOMETHOD:
1061 if (val > OSSL_CRMF_POPO_KEYAGREE) {
1062 CMPerr(0, CMP_R_INVALID_ARGS);
1065 ctx->popoMethod = val;
1067 case OSSL_CMP_OPT_DIGEST_ALGNID:
1070 case OSSL_CMP_OPT_OWF_ALGNID:
1073 case OSSL_CMP_OPT_MAC_ALGNID:
1076 case OSSL_CMP_OPT_MSGTIMEOUT:
1077 ctx->msgtimeout = val;
1079 case OSSL_CMP_OPT_TOTALTIMEOUT:
1080 ctx->totaltimeout = val;
1082 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1083 ctx->permitTAInExtraCertsForIR = val;
1085 case OSSL_CMP_OPT_REVOCATION_REASON:
1086 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
1087 CMPerr(0, CMP_R_INVALID_ARGS);
1090 ctx->revocationReason = val;
1093 CMPerr(0, CMP_R_INVALID_ARGS);
1101 * Reads a Boolean or integer option value from the context.
1102 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
1104 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
1107 CMPerr(0, CMP_R_NULL_ARGUMENT);
1112 case OSSL_CMP_OPT_LOG_VERBOSITY:
1113 return ctx->log_verbosity;
1114 case OSSL_CMP_OPT_IMPLICITCONFIRM:
1115 return ctx->implicitConfirm;
1116 case OSSL_CMP_OPT_DISABLECONFIRM:
1117 return ctx->disableConfirm;
1118 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1119 return ctx->unprotectedSend;
1120 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1121 return ctx->unprotectedErrors;
1122 case OSSL_CMP_OPT_VALIDITYDAYS:
1124 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1125 return ctx->SubjectAltName_nodefault;
1126 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1127 return ctx->setSubjectAltNameCritical;
1128 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1129 return ctx->setPoliciesCritical;
1130 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1131 return ctx->ignore_keyusage;
1132 case OSSL_CMP_OPT_POPOMETHOD:
1133 return ctx->popoMethod;
1134 case OSSL_CMP_OPT_DIGEST_ALGNID:
1136 case OSSL_CMP_OPT_OWF_ALGNID:
1137 return ctx->pbm_owf;
1138 case OSSL_CMP_OPT_MAC_ALGNID:
1139 return ctx->pbm_mac;
1140 case OSSL_CMP_OPT_MSGTIMEOUT:
1141 return ctx->msgtimeout;
1142 case OSSL_CMP_OPT_TOTALTIMEOUT:
1143 return ctx->totaltimeout;
1144 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1145 return ctx->permitTAInExtraCertsForIR;
1146 case OSSL_CMP_OPT_REVOCATION_REASON:
1147 return ctx->revocationReason;
1149 CMPerr(0, CMP_R_INVALID_ARGS);