2 * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
12 #include <openssl/trace.h>
13 #include <openssl/bio.h>
14 #include <openssl/ocsp.h> /* for OCSP_REVOKED_STATUS_* */
16 #include "cmp_local.h"
18 /* explicit #includes not strictly needed since implied by the above: */
19 #include <openssl/cmp.h>
20 #include <openssl/crmf.h>
21 #include <openssl/err.h>
24 * Get current certificate store containing trusted root CA certs
26 X509_STORE
*OSSL_CMP_CTX_get0_trustedStore(const OSSL_CMP_CTX
*ctx
)
29 ERR_raise(ERR_LIB_CMP
, 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.
40 int OSSL_CMP_CTX_set0_trustedStore(OSSL_CMP_CTX
*ctx
, X509_STORE
*store
)
43 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
46 X509_STORE_free(ctx
->trusted
);
51 /* Get current list of non-trusted intermediate certs */
52 STACK_OF(X509
) *OSSL_CMP_CTX_get0_untrusted(const OSSL_CMP_CTX
*ctx
)
55 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
58 return ctx
->untrusted
;
62 * Set untrusted certificates for path construction in authentication of
63 * the CMP server and potentially others (TLS server, newly enrolled cert).
65 int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX
*ctx
, STACK_OF(X509
) *certs
)
67 STACK_OF(X509
) *untrusted
= NULL
;
70 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
73 if (!ossl_x509_add_certs_new(&untrusted
, certs
,
74 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
))
76 sk_X509_pop_free(ctx
->untrusted
, X509_free
);
77 ctx
->untrusted
= untrusted
;
80 sk_X509_pop_free(untrusted
, X509_free
);
84 static int cmp_ctx_set_md(OSSL_CMP_CTX
*ctx
, EVP_MD
**pmd
, int nid
)
86 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, OBJ_nid2sn(nid
), ctx
->propq
);
87 /* fetching in advance to be able to throw error early if unsupported */
90 ERR_raise(ERR_LIB_CMP
, CMP_R_UNSUPPORTED_ALGORITHM
);
99 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
100 * Returns new context on success, NULL on error
102 OSSL_CMP_CTX
*OSSL_CMP_CTX_new(OSSL_LIB_CTX
*libctx
, const char *propq
)
104 OSSL_CMP_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
109 ctx
->libctx
= libctx
;
110 if (propq
!= NULL
&& (ctx
->propq
= OPENSSL_strdup(propq
)) == NULL
)
113 ctx
->log_verbosity
= OSSL_CMP_LOG_INFO
;
116 ctx
->failInfoCode
= -1;
119 ctx
->msg_timeout
= -1;
121 if ((ctx
->untrusted
= sk_X509_new_null()) == NULL
)
125 if (!cmp_ctx_set_md(ctx
, &ctx
->pbm_owf
, NID_sha256
))
127 ctx
->pbm_itercnt
= 500;
128 ctx
->pbm_mac
= NID_hmac_sha1
;
130 if (!cmp_ctx_set_md(ctx
, &ctx
->digest
, NID_sha256
))
132 ctx
->popoMethod
= OSSL_CRMF_POPO_SIGNATURE
;
133 ctx
->revocationReason
= CRL_REASON_NONE
;
135 /* all other elements are initialized to 0 or NULL, respectively */
139 ERR_raise(ERR_LIB_X509
, ERR_R_MALLOC_FAILURE
);
141 OSSL_CMP_CTX_free(ctx
);
145 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
146 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX
*ctx
)
149 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
153 if (ctx
->http_ctx
!= NULL
) {
154 (void)OSSL_HTTP_close(ctx
->http_ctx
, 1);
155 ossl_cmp_debug(ctx
, "disconnected from CMP server");
156 ctx
->http_ctx
= NULL
;
159 ctx
->failInfoCode
= -1;
161 return ossl_cmp_ctx_set0_statusString(ctx
, NULL
)
162 && ossl_cmp_ctx_set0_newCert(ctx
, NULL
)
163 && ossl_cmp_ctx_set1_newChain(ctx
, NULL
)
164 && ossl_cmp_ctx_set1_caPubs(ctx
, NULL
)
165 && ossl_cmp_ctx_set1_extraCertsIn(ctx
, NULL
)
166 && ossl_cmp_ctx_set0_validatedSrvCert(ctx
, NULL
)
167 && OSSL_CMP_CTX_set1_transactionID(ctx
, NULL
)
168 && OSSL_CMP_CTX_set1_senderNonce(ctx
, NULL
)
169 && ossl_cmp_ctx_set1_recipNonce(ctx
, NULL
);
172 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
173 void OSSL_CMP_CTX_free(OSSL_CMP_CTX
*ctx
)
178 if (ctx
->http_ctx
!= NULL
) {
179 (void)OSSL_HTTP_close(ctx
->http_ctx
, 1);
180 ossl_cmp_debug(ctx
, "disconnected from CMP server");
182 OPENSSL_free(ctx
->propq
);
183 OPENSSL_free(ctx
->serverPath
);
184 OPENSSL_free(ctx
->server
);
185 OPENSSL_free(ctx
->proxy
);
186 OPENSSL_free(ctx
->no_proxy
);
188 X509_free(ctx
->srvCert
);
189 X509_free(ctx
->validatedSrvCert
);
190 X509_NAME_free(ctx
->expected_sender
);
191 X509_STORE_free(ctx
->trusted
);
192 sk_X509_pop_free(ctx
->untrusted
, X509_free
);
194 X509_free(ctx
->cert
);
195 sk_X509_pop_free(ctx
->chain
, X509_free
);
196 EVP_PKEY_free(ctx
->pkey
);
197 ASN1_OCTET_STRING_free(ctx
->referenceValue
);
198 if (ctx
->secretValue
!= NULL
)
199 OPENSSL_cleanse(ctx
->secretValue
->data
, ctx
->secretValue
->length
);
200 ASN1_OCTET_STRING_free(ctx
->secretValue
);
201 EVP_MD_free(ctx
->pbm_owf
);
203 X509_NAME_free(ctx
->recipient
);
204 EVP_MD_free(ctx
->digest
);
205 ASN1_OCTET_STRING_free(ctx
->transactionID
);
206 ASN1_OCTET_STRING_free(ctx
->senderNonce
);
207 ASN1_OCTET_STRING_free(ctx
->recipNonce
);
208 sk_OSSL_CMP_ITAV_pop_free(ctx
->geninfo_ITAVs
, OSSL_CMP_ITAV_free
);
209 sk_X509_pop_free(ctx
->extraCertsOut
, X509_free
);
211 EVP_PKEY_free(ctx
->newPkey
);
212 X509_NAME_free(ctx
->issuer
);
213 X509_NAME_free(ctx
->subjectName
);
214 sk_GENERAL_NAME_pop_free(ctx
->subjectAltNames
, GENERAL_NAME_free
);
215 sk_X509_EXTENSION_pop_free(ctx
->reqExtensions
, X509_EXTENSION_free
);
216 sk_POLICYINFO_pop_free(ctx
->policies
, POLICYINFO_free
);
217 X509_free(ctx
->oldCert
);
218 X509_REQ_free(ctx
->p10CSR
);
220 sk_OSSL_CMP_ITAV_pop_free(ctx
->genm_ITAVs
, OSSL_CMP_ITAV_free
);
222 sk_ASN1_UTF8STRING_pop_free(ctx
->statusString
, ASN1_UTF8STRING_free
);
223 X509_free(ctx
->newCert
);
224 sk_X509_pop_free(ctx
->newChain
, X509_free
);
225 sk_X509_pop_free(ctx
->caPubs
, X509_free
);
226 sk_X509_pop_free(ctx
->extraCertsIn
, X509_free
);
231 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX
*ctx
, int status
)
233 if (!ossl_assert(ctx
!= NULL
))
235 ctx
->status
= status
;
240 * Returns the PKIStatus from the last CertRepMessage
241 * or Revocation Response or error message, -1 on error
243 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX
*ctx
)
246 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
253 * Returns the statusString from the last CertRepMessage
254 * or Revocation Response or error message, NULL on error
256 OSSL_CMP_PKIFREETEXT
*OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX
*ctx
)
259 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
262 return ctx
->statusString
;
265 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX
*ctx
,
266 OSSL_CMP_PKIFREETEXT
*text
)
268 if (!ossl_assert(ctx
!= NULL
))
270 sk_ASN1_UTF8STRING_pop_free(ctx
->statusString
, ASN1_UTF8STRING_free
);
271 ctx
->statusString
= text
;
275 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX
*ctx
, X509
*cert
)
277 if (!ossl_assert(ctx
!= NULL
))
279 X509_free(ctx
->validatedSrvCert
);
280 ctx
->validatedSrvCert
= cert
;
284 /* Set callback function for checking if the cert is ok or should be rejected */
285 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX
*ctx
, OSSL_CMP_certConf_cb_t cb
)
288 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
291 ctx
->certConf_cb
= cb
;
296 * Set argument, respectively a pointer to a structure containing arguments,
297 * optionally to be used by the certConf callback.
299 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX
*ctx
, void *arg
)
302 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
305 ctx
->certConf_cb_arg
= arg
;
310 * Get argument, respectively the pointer to a structure containing arguments,
311 * optionally to be used by certConf callback.
312 * Returns callback argument set previously (NULL if not set or on error)
314 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX
*ctx
)
317 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
320 return ctx
->certConf_cb_arg
;
323 #ifndef OPENSSL_NO_TRACE
324 static size_t ossl_cmp_log_trace_cb(const char *buf
, size_t cnt
,
325 int category
, int cmd
, void *vdata
)
327 OSSL_CMP_CTX
*ctx
= vdata
;
329 OSSL_CMP_severity level
= -1;
334 if (buf
== NULL
|| cnt
== 0 || cmd
!= OSSL_TRACE_CTRL_WRITE
|| ctx
== NULL
)
336 if (ctx
->log_cb
== NULL
)
337 return 1; /* silently drop message */
339 msg
= ossl_cmp_log_parse_metadata(buf
, &level
, &func
, &file
, &line
);
341 if (level
> ctx
->log_verbosity
) /* excludes the case level is unknown */
342 goto end
; /* suppress output since severity is not sufficient */
344 if (!ctx
->log_cb(func
!= NULL
? func
: "(no func)",
345 file
!= NULL
? file
: "(no file)",
356 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
357 int ossl_cmp_print_log(OSSL_CMP_severity level
, const OSSL_CMP_CTX
*ctx
,
358 const char *func
, const char *file
, int line
,
359 const char *level_str
, const char *format
, ...)
362 char hugebuf
[1024 * 2];
365 if (ctx
== NULL
|| ctx
->log_cb
== NULL
)
366 return 1; /* silently drop message */
368 if (level
> ctx
->log_verbosity
) /* excludes the case level is unknown */
369 return 1; /* suppress output since severity is not sufficient */
374 va_start(args
, format
);
377 func
= "(unset function name)";
379 file
= "(unset file name)";
380 if (level_str
== NULL
)
381 level_str
= "(unset level string)";
383 #ifndef OPENSSL_NO_TRACE
384 if (OSSL_TRACE_ENABLED(CMP
)) {
385 OSSL_TRACE_BEGIN(CMP
) {
387 BIO_snprintf(hugebuf
, sizeof(hugebuf
),
388 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX
"%s: ",
389 func
, file
, line
, level_str
);
390 if (printed
> 0 && (size_t)printed
< sizeof(hugebuf
)) {
391 if (BIO_vsnprintf(hugebuf
+ printed
,
392 sizeof(hugebuf
) - printed
, format
, args
) > 0)
393 res
= BIO_puts(trc_out
, hugebuf
) > 0;
395 } OSSL_TRACE_END(CMP
);
397 #else /* compensate for disabled trace API */
399 if (BIO_vsnprintf(hugebuf
, sizeof(hugebuf
), format
, args
) > 0)
400 res
= ctx
->log_cb(func
, file
, line
, level
, hugebuf
);
407 /* Set a callback function for error reporting and logging messages */
408 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX
*ctx
, OSSL_CMP_log_cb_t cb
)
411 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
416 #ifndef OPENSSL_NO_TRACE
417 /* do also in case cb == NULL, to switch off logging output: */
418 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP
,
419 ossl_cmp_log_trace_cb
, ctx
))
426 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
427 void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX
*ctx
)
429 if (ctx
!= NULL
&& OSSL_CMP_LOG_ERR
> ctx
->log_verbosity
)
430 return; /* suppress output since severity is not sufficient */
431 OSSL_CMP_print_errors_cb(ctx
== NULL
? NULL
: ctx
->log_cb
);
435 * Set or clear the reference value to be used for identification
436 * (i.e., the user name) when using PBMAC.
438 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX
*ctx
,
439 const unsigned char *ref
, int len
)
442 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
445 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx
->referenceValue
, ref
,
449 /* Set or clear the password to be used for protecting messages with PBMAC */
450 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX
*ctx
, const unsigned char *sec
,
453 ASN1_OCTET_STRING
*secretValue
= NULL
;
455 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
458 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue
, sec
, len
) != 1)
460 if (ctx
->secretValue
!= NULL
) {
461 OPENSSL_cleanse(ctx
->secretValue
->data
, ctx
->secretValue
->length
);
462 ASN1_OCTET_STRING_free(ctx
->secretValue
);
464 ctx
->secretValue
= secretValue
;
468 /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
469 STACK_OF(X509
) *OSSL_CMP_CTX_get1_newChain(const OSSL_CMP_CTX
*ctx
)
472 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
475 return X509_chain_up_ref(ctx
->newChain
);
479 * Copies any given stack of inbound X509 certificates to newChain
480 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
482 int ossl_cmp_ctx_set1_newChain(OSSL_CMP_CTX
*ctx
, STACK_OF(X509
) *newChain
)
484 if (!ossl_assert(ctx
!= NULL
))
487 sk_X509_pop_free(ctx
->newChain
, X509_free
);
488 ctx
->newChain
= NULL
;
489 return newChain
== NULL
||
490 (ctx
->newChain
= X509_chain_up_ref(newChain
)) != NULL
;
493 /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
494 STACK_OF(X509
) *OSSL_CMP_CTX_get1_extraCertsIn(const OSSL_CMP_CTX
*ctx
)
497 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
500 return X509_chain_up_ref(ctx
->extraCertsIn
);
504 * Copies any given stack of inbound X509 certificates to extraCertsIn
505 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
507 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX
*ctx
,
508 STACK_OF(X509
) *extraCertsIn
)
510 if (!ossl_assert(ctx
!= NULL
))
513 sk_X509_pop_free(ctx
->extraCertsIn
, X509_free
);
514 ctx
->extraCertsIn
= NULL
;
515 return extraCertsIn
== NULL
516 || (ctx
->extraCertsIn
= X509_chain_up_ref(extraCertsIn
)) != NULL
;
520 * Copies any given stack as the new stack of X509
521 * certificates to send out in the extraCerts field.
523 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX
*ctx
,
524 STACK_OF(X509
) *extraCertsOut
)
527 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
531 sk_X509_pop_free(ctx
->extraCertsOut
, X509_free
);
532 ctx
->extraCertsOut
= NULL
;
533 return extraCertsOut
== NULL
534 || (ctx
->extraCertsOut
= X509_chain_up_ref(extraCertsOut
)) != NULL
;
538 * Add the given policy info object
539 * to the X509_EXTENSIONS of the requested certificate template.
541 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX
*ctx
, POLICYINFO
*pinfo
)
543 if (ctx
== NULL
|| pinfo
== NULL
) {
544 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
548 if (ctx
->policies
== NULL
549 && (ctx
->policies
= CERTIFICATEPOLICIES_new()) == NULL
)
552 return sk_POLICYINFO_push(ctx
->policies
, pinfo
);
555 /* Add an ITAV for geninfo of the PKI message header */
556 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX
*ctx
, OSSL_CMP_ITAV
*itav
)
559 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
562 return OSSL_CMP_ITAV_push0_stack_item(&ctx
->geninfo_ITAVs
, itav
);
565 /* Add an itav for the body of outgoing general messages */
566 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX
*ctx
, OSSL_CMP_ITAV
*itav
)
569 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
572 return OSSL_CMP_ITAV_push0_stack_item(&ctx
->genm_ITAVs
, itav
);
576 * Returns a duplicate of the stack of X509 certificates that
577 * were received in the caPubs field of the last CertRepMessage.
578 * Returns NULL on error
580 STACK_OF(X509
) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX
*ctx
)
583 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
586 return X509_chain_up_ref(ctx
->caPubs
);
590 * Copies any given stack of certificates to the given
591 * OSSL_CMP_CTX structure so that they may be retrieved later.
593 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX
*ctx
, STACK_OF(X509
) *caPubs
)
595 if (!ossl_assert(ctx
!= NULL
))
598 sk_X509_pop_free(ctx
->caPubs
, X509_free
);
600 return caPubs
== NULL
|| (ctx
->caPubs
= X509_chain_up_ref(caPubs
)) != NULL
;
603 #define char_dup OPENSSL_strdup
604 #define char_free OPENSSL_free
605 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
606 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
608 TYPE *val_dup = NULL; \
611 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
615 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
617 TYPE##_free(ctx->FIELD); \
618 ctx->FIELD = val_dup; \
622 #define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
623 #define EVP_PKEY_invalid(key) 0
624 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
625 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
628 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
632 /* prevent misleading error later on malformed cert or provider issue */ \
633 if (val != NULL && TYPE##_invalid(val)) { \
634 ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
637 if (val != NULL && !TYPE##_up_ref(val)) \
639 TYPE##_free(ctx->FIELD); \
645 * Pins the server certificate to be directly trusted (even if it is expired)
646 * for verifying response messages.
647 * Cert pointer is not consumed. It may be NULL to clear the entry.
649 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert
, X509
)
651 /* Set the X509 name of the recipient. Set in the PKIHeader */
652 DEFINE_OSSL_CMP_CTX_set1(recipient
, X509_NAME
)
654 /* Store the X509 name of the expected sender in the PKIHeader of responses */
655 DEFINE_OSSL_CMP_CTX_set1(expected_sender
, X509_NAME
)
657 /* Set the X509 name of the issuer. Set in the PKIHeader */
658 DEFINE_OSSL_CMP_CTX_set1(issuer
, X509_NAME
)
661 * Set the subject name that will be placed in the certificate
662 * request. This will be the subject name on the received certificate.
664 DEFINE_OSSL_CMP_CTX_set1(subjectName
, X509_NAME
)
666 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
667 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX
*ctx
, X509_EXTENSIONS
*exts
)
670 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
674 if (sk_GENERAL_NAME_num(ctx
->subjectAltNames
) > 0 && exts
!= NULL
675 && X509v3_get_ext_by_NID(exts
, NID_subject_alt_name
, -1) >= 0) {
676 ERR_raise(ERR_LIB_CMP
, CMP_R_MULTIPLE_SAN_SOURCES
);
679 sk_X509_EXTENSION_pop_free(ctx
->reqExtensions
, X509_EXTENSION_free
);
680 ctx
->reqExtensions
= exts
;
684 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
685 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX
*ctx
)
688 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
691 /* if one of the following conditions 'fail' this is not an error */
692 return ctx
->reqExtensions
!= NULL
693 && X509v3_get_ext_by_NID(ctx
->reqExtensions
,
694 NID_subject_alt_name
, -1) >= 0;
698 * Add a GENERAL_NAME structure that will be added to the CRMF
699 * request's extensions field to request subject alternative names.
701 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX
*ctx
,
702 const GENERAL_NAME
*name
)
704 GENERAL_NAME
*name_dup
;
706 if (ctx
== NULL
|| name
== NULL
) {
707 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
711 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx
) == 1) {
712 ERR_raise(ERR_LIB_CMP
, CMP_R_MULTIPLE_SAN_SOURCES
);
716 if (ctx
->subjectAltNames
== NULL
717 && (ctx
->subjectAltNames
= sk_GENERAL_NAME_new_null()) == NULL
)
719 if ((name_dup
= GENERAL_NAME_dup(name
)) == NULL
)
721 if (!sk_GENERAL_NAME_push(ctx
->subjectAltNames
, name_dup
)) {
722 GENERAL_NAME_free(name_dup
);
729 * Set our own client certificate, used for example in KUR and when
730 * doing the IR with existing certificate.
732 DEFINE_OSSL_CMP_CTX_set1_up_ref(cert
, X509
)
734 int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX
*ctx
, X509_STORE
*own_trusted
,
735 STACK_OF(X509
) *candidates
)
737 STACK_OF(X509
) *chain
;
740 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
744 if (!ossl_x509_add_certs_new(&ctx
->untrusted
, candidates
,
745 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
))
748 ossl_cmp_debug(ctx
, "trying to build chain for own CMP signer cert");
749 chain
= X509_build_chain(ctx
->cert
, ctx
->untrusted
, own_trusted
, 0,
750 ctx
->libctx
, ctx
->propq
);
752 ERR_raise(ERR_LIB_CMP
, CMP_R_FAILED_BUILDING_OWN_CHAIN
);
755 ossl_cmp_debug(ctx
, "success building chain for own CMP signer cert");
761 * Set the old certificate that we are updating in KUR
762 * or the certificate to be revoked in RR, respectively.
763 * Also used as reference cert (defaulting to cert) for deriving subject DN
764 * and SANs. Its issuer is used as default recipient in the CMP message header.
766 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert
, X509
)
768 /* Set the PKCS#10 CSR to be sent in P10CR */
769 DEFINE_OSSL_CMP_CTX_set1(p10CSR
, X509_REQ
)
772 * Set the (newly received in IP/KUP/CP) certificate in the context.
773 * TODO: this only permits for one cert to be enrolled at a time.
775 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX
*ctx
, X509
*cert
)
777 if (!ossl_assert(ctx
!= NULL
))
780 X509_free(ctx
->newCert
);
786 * Get the (newly received in IP/KUP/CP) client certificate from the context
787 * TODO: this only permits for one client cert to be received...
789 X509
*OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX
*ctx
)
792 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
798 /* Set the client's current private key */
799 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey
, EVP_PKEY
)
801 /* Set new key pair. Used e.g. when doing Key Update */
802 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX
*ctx
, int priv
, EVP_PKEY
*pkey
)
805 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
809 EVP_PKEY_free(ctx
->newPkey
);
811 ctx
->newPkey_priv
= priv
;
815 /* Get the private/public key to use for cert enrollment, or NULL on error */
816 EVP_PKEY
*OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX
*ctx
, int priv
)
819 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
823 if (ctx
->newPkey
!= NULL
)
824 return priv
&& !ctx
->newPkey_priv
? NULL
: ctx
->newPkey
;
825 if (ctx
->p10CSR
!= NULL
)
826 return priv
? NULL
: X509_REQ_get0_pubkey(ctx
->p10CSR
);
827 return ctx
->pkey
; /* may be NULL */
830 /* Set the given transactionID to the context */
831 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX
*ctx
,
832 const ASN1_OCTET_STRING
*id
)
835 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
838 return ossl_cmp_asn1_octet_string_set1(&ctx
->transactionID
, id
);
841 /* Set the nonce to be used for the recipNonce in the message created next */
842 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX
*ctx
,
843 const ASN1_OCTET_STRING
*nonce
)
845 if (!ossl_assert(ctx
!= NULL
))
847 return ossl_cmp_asn1_octet_string_set1(&ctx
->recipNonce
, nonce
);
850 /* Stores the given nonce as the last senderNonce sent out */
851 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX
*ctx
,
852 const ASN1_OCTET_STRING
*nonce
)
855 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
858 return ossl_cmp_asn1_octet_string_set1(&ctx
->senderNonce
, nonce
);
861 /* Set the proxy server to use for HTTP(S) connections */
862 DEFINE_OSSL_CMP_CTX_set1(proxy
, char)
864 /* Set the (HTTP) host name of the CMP server */
865 DEFINE_OSSL_CMP_CTX_set1(server
, char)
867 /* Set the server exclusion list of the HTTP proxy server */
868 DEFINE_OSSL_CMP_CTX_set1(no_proxy
, char)
870 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
871 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX
*ctx
, OSSL_HTTP_bio_cb_t cb
)
874 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
881 /* Set argument optionally to be used by the http connect/disconnect callback */
882 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX
*ctx
, void *arg
)
885 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
888 ctx
->http_cb_arg
= arg
;
893 * Get argument optionally to be used by the http connect/disconnect callback
894 * Returns callback argument set previously (NULL if not set or on error)
896 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX
*ctx
)
899 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
902 return ctx
->http_cb_arg
;
905 /* Set callback function for sending CMP request and receiving response */
906 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX
*ctx
, OSSL_CMP_transfer_cb_t cb
)
909 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
912 ctx
->transfer_cb
= cb
;
916 /* Set argument optionally to be used by the transfer callback */
917 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX
*ctx
, void *arg
)
920 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
923 ctx
->transfer_cb_arg
= arg
;
928 * Get argument optionally to be used by the transfer callback.
929 * Returns callback argument set previously (NULL if not set or on error)
931 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX
*ctx
)
934 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
937 return ctx
->transfer_cb_arg
;
940 /** Set the HTTP server port to be used */
941 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX
*ctx
, int port
)
944 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
947 ctx
->serverPort
= port
;
951 /* Set the HTTP path to be used on the server (e.g "pkix/") */
952 DEFINE_OSSL_CMP_CTX_set1(serverPath
, char)
954 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
955 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX
*ctx
, int fail_info
)
957 if (!ossl_assert(ctx
!= NULL
))
959 ctx
->failInfoCode
= fail_info
;
964 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
965 * Returns bit string as integer on success, -1 on error
967 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX
*ctx
)
970 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
973 return ctx
->failInfoCode
;
976 /* Set a Boolean or integer option of the context to the "val" arg */
977 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX
*ctx
, int opt
, int val
)
982 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
987 case OSSL_CMP_OPT_REVOCATION_REASON
:
988 min_val
= OCSP_REVOKED_STATUS_NOSTATUS
;
990 case OSSL_CMP_OPT_POPO_METHOD
:
991 min_val
= OSSL_CRMF_POPO_NONE
;
998 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_SMALL
);
1003 case OSSL_CMP_OPT_LOG_VERBOSITY
:
1004 if (val
> OSSL_CMP_LOG_MAX
) {
1005 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_LARGE
);
1008 ctx
->log_verbosity
= val
;
1010 case OSSL_CMP_OPT_IMPLICIT_CONFIRM
:
1011 ctx
->implicitConfirm
= val
;
1013 case OSSL_CMP_OPT_DISABLE_CONFIRM
:
1014 ctx
->disableConfirm
= val
;
1016 case OSSL_CMP_OPT_UNPROTECTED_SEND
:
1017 ctx
->unprotectedSend
= val
;
1019 case OSSL_CMP_OPT_UNPROTECTED_ERRORS
:
1020 ctx
->unprotectedErrors
= val
;
1022 case OSSL_CMP_OPT_VALIDITY_DAYS
:
1025 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT
:
1026 ctx
->SubjectAltName_nodefault
= val
;
1028 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL
:
1029 ctx
->setSubjectAltNameCritical
= val
;
1031 case OSSL_CMP_OPT_POLICIES_CRITICAL
:
1032 ctx
->setPoliciesCritical
= val
;
1034 case OSSL_CMP_OPT_IGNORE_KEYUSAGE
:
1035 ctx
->ignore_keyusage
= val
;
1037 case OSSL_CMP_OPT_POPO_METHOD
:
1038 if (val
> OSSL_CRMF_POPO_KEYAGREE
) {
1039 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_LARGE
);
1042 ctx
->popoMethod
= val
;
1044 case OSSL_CMP_OPT_DIGEST_ALGNID
:
1045 if (!cmp_ctx_set_md(ctx
, &ctx
->digest
, val
))
1048 case OSSL_CMP_OPT_OWF_ALGNID
:
1049 if (!cmp_ctx_set_md(ctx
, &ctx
->pbm_owf
, val
))
1052 case OSSL_CMP_OPT_MAC_ALGNID
:
1055 case OSSL_CMP_OPT_KEEP_ALIVE
:
1056 ctx
->keep_alive
= val
;
1058 case OSSL_CMP_OPT_MSG_TIMEOUT
:
1059 ctx
->msg_timeout
= val
;
1061 case OSSL_CMP_OPT_TOTAL_TIMEOUT
:
1062 ctx
->total_timeout
= val
;
1064 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR
:
1065 ctx
->permitTAInExtraCertsForIR
= val
;
1067 case OSSL_CMP_OPT_REVOCATION_REASON
:
1068 if (val
> OCSP_REVOKED_STATUS_AACOMPROMISE
) {
1069 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_LARGE
);
1072 ctx
->revocationReason
= val
;
1075 ERR_raise(ERR_LIB_CMP
, CMP_R_INVALID_OPTION
);
1083 * Reads a Boolean or integer option value from the context.
1084 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
1086 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX
*ctx
, int opt
)
1089 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
1094 case OSSL_CMP_OPT_LOG_VERBOSITY
:
1095 return ctx
->log_verbosity
;
1096 case OSSL_CMP_OPT_IMPLICIT_CONFIRM
:
1097 return ctx
->implicitConfirm
;
1098 case OSSL_CMP_OPT_DISABLE_CONFIRM
:
1099 return ctx
->disableConfirm
;
1100 case OSSL_CMP_OPT_UNPROTECTED_SEND
:
1101 return ctx
->unprotectedSend
;
1102 case OSSL_CMP_OPT_UNPROTECTED_ERRORS
:
1103 return ctx
->unprotectedErrors
;
1104 case OSSL_CMP_OPT_VALIDITY_DAYS
:
1106 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT
:
1107 return ctx
->SubjectAltName_nodefault
;
1108 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL
:
1109 return ctx
->setSubjectAltNameCritical
;
1110 case OSSL_CMP_OPT_POLICIES_CRITICAL
:
1111 return ctx
->setPoliciesCritical
;
1112 case OSSL_CMP_OPT_IGNORE_KEYUSAGE
:
1113 return ctx
->ignore_keyusage
;
1114 case OSSL_CMP_OPT_POPO_METHOD
:
1115 return ctx
->popoMethod
;
1116 case OSSL_CMP_OPT_DIGEST_ALGNID
:
1117 return EVP_MD_get_type(ctx
->digest
);
1118 case OSSL_CMP_OPT_OWF_ALGNID
:
1119 return EVP_MD_get_type(ctx
->pbm_owf
);
1120 case OSSL_CMP_OPT_MAC_ALGNID
:
1121 return ctx
->pbm_mac
;
1122 case OSSL_CMP_OPT_KEEP_ALIVE
:
1123 return ctx
->keep_alive
;
1124 case OSSL_CMP_OPT_MSG_TIMEOUT
:
1125 return ctx
->msg_timeout
;
1126 case OSSL_CMP_OPT_TOTAL_TIMEOUT
:
1127 return ctx
->total_timeout
;
1128 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR
:
1129 return ctx
->permitTAInExtraCertsForIR
;
1130 case OSSL_CMP_OPT_REVOCATION_REASON
:
1131 return ctx
->revocationReason
;
1133 ERR_raise(ERR_LIB_CMP
, CMP_R_INVALID_OPTION
);