2 * Copyright 2007-2024 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 #define DEFINE_OSSL_CMP_CTX_get0(FIELD, TYPE) \
24 DEFINE_OSSL_CMP_CTX_get0_NAME(FIELD, FIELD, TYPE)
25 #define DEFINE_OSSL_CMP_CTX_get0_NAME(NAME, FIELD, TYPE) \
26 TYPE *OSSL_CMP_CTX_get0_##NAME(const OSSL_CMP_CTX *ctx) \
29 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
36 * Get current certificate store containing trusted root CA certs
38 DEFINE_OSSL_CMP_CTX_get0_NAME(trusted
, trusted
, X509_STORE
)
40 #define DEFINE_OSSL_set0(PREFIX, FIELD, TYPE) \
41 DEFINE_OSSL_set0_NAME(PREFIX, FIELD, FIELD, TYPE)
42 #define DEFINE_OSSL_set0_NAME(PREFIX, NAME, FIELD, TYPE) \
43 int PREFIX##_set0##_##NAME(OSSL_CMP_CTX *ctx, TYPE *val) \
46 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
49 TYPE##_free(ctx->FIELD); \
55 * Set certificate store containing trusted (root) CA certs and possibly CRLs
56 * and a cert verification callback function used for CMP server authentication.
57 * Any already existing store entry is freed. Given NULL, the entry is reset.
59 DEFINE_OSSL_set0_NAME(OSSL_CMP_CTX
, trusted
, trusted
, X509_STORE
)
61 DEFINE_OSSL_CMP_CTX_get0(libctx
, OSSL_LIB_CTX
)
62 DEFINE_OSSL_CMP_CTX_get0(propq
, const char)
64 /* Get current list of non-trusted intermediate certs */
65 DEFINE_OSSL_CMP_CTX_get0(untrusted
, STACK_OF(X509
))
68 * Set untrusted certificates for path construction in authentication of
69 * the CMP server and potentially others (TLS server, newly enrolled cert).
71 int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX
*ctx
, STACK_OF(X509
) *certs
)
73 STACK_OF(X509
) *untrusted
= NULL
;
76 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
79 if (!ossl_x509_add_certs_new(&untrusted
, certs
,
80 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
))
82 OSSL_STACK_OF_X509_free(ctx
->untrusted
);
83 ctx
->untrusted
= untrusted
;
86 OSSL_STACK_OF_X509_free(untrusted
);
90 static int cmp_ctx_set_md(OSSL_CMP_CTX
*ctx
, EVP_MD
**pmd
, int nid
)
92 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, OBJ_nid2sn(nid
), ctx
->propq
);
93 /* fetching in advance to be able to throw error early if unsupported */
96 ERR_raise(ERR_LIB_CMP
, CMP_R_UNSUPPORTED_ALGORITHM
);
105 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
106 * Returns new context on success, NULL on error
108 OSSL_CMP_CTX
*OSSL_CMP_CTX_new(OSSL_LIB_CTX
*libctx
, const char *propq
)
110 OSSL_CMP_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
115 ctx
->libctx
= libctx
;
116 if (propq
!= NULL
&& (ctx
->propq
= OPENSSL_strdup(propq
)) == NULL
)
119 ctx
->log_verbosity
= OSSL_CMP_LOG_INFO
;
121 ctx
->status
= OSSL_CMP_PKISTATUS_unspecified
;
122 ctx
->failInfoCode
= -1;
125 ctx
->msg_timeout
= -1;
126 ctx
->tls_used
= -1; /* default for backward compatibility */
128 if ((ctx
->untrusted
= sk_X509_new_null()) == NULL
) {
129 ERR_raise(ERR_LIB_X509
, ERR_R_CRYPTO_LIB
);
134 if (!cmp_ctx_set_md(ctx
, &ctx
->pbm_owf
, NID_sha256
))
136 ctx
->pbm_itercnt
= 500;
137 ctx
->pbm_mac
= NID_hmac_sha1
;
139 if (!cmp_ctx_set_md(ctx
, &ctx
->digest
, NID_sha256
))
141 ctx
->popoMethod
= OSSL_CRMF_POPO_SIGNATURE
;
142 ctx
->revocationReason
= CRL_REASON_NONE
;
144 /* all other elements are initialized to 0 or NULL, respectively */
148 OSSL_CMP_CTX_free(ctx
);
152 #define OSSL_CMP_ITAVs_free(itavs) \
153 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
154 #define X509_EXTENSIONS_free(exts) \
155 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free)
156 #define OSSL_CMP_PKIFREETEXT_free(text) \
157 sk_ASN1_UTF8STRING_pop_free(text, ASN1_UTF8STRING_free)
159 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
160 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX
*ctx
)
163 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
167 #ifndef OPENSSL_NO_HTTP
168 if (ctx
->http_ctx
!= NULL
) {
169 (void)OSSL_HTTP_close(ctx
->http_ctx
, 1);
170 ossl_cmp_debug(ctx
, "disconnected from CMP server");
171 ctx
->http_ctx
= NULL
;
174 ctx
->status
= OSSL_CMP_PKISTATUS_unspecified
;
175 ctx
->failInfoCode
= -1;
177 OSSL_CMP_ITAVs_free(ctx
->genm_ITAVs
);
178 ctx
->genm_ITAVs
= NULL
;
180 return ossl_cmp_ctx_set0_statusString(ctx
, NULL
)
181 && ossl_cmp_ctx_set0_newCert(ctx
, NULL
)
182 && ossl_cmp_ctx_set1_newChain(ctx
, NULL
)
183 && ossl_cmp_ctx_set1_caPubs(ctx
, NULL
)
184 && ossl_cmp_ctx_set1_extraCertsIn(ctx
, NULL
)
185 && ossl_cmp_ctx_set1_validatedSrvCert(ctx
, NULL
)
186 && ossl_cmp_ctx_set1_first_senderNonce(ctx
, NULL
)
187 && OSSL_CMP_CTX_set1_transactionID(ctx
, NULL
)
188 && OSSL_CMP_CTX_set1_senderNonce(ctx
, NULL
)
189 && ossl_cmp_ctx_set1_recipNonce(ctx
, NULL
);
192 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
193 void OSSL_CMP_CTX_free(OSSL_CMP_CTX
*ctx
)
198 #ifndef OPENSSL_NO_HTTP
199 if (ctx
->http_ctx
!= NULL
) {
200 (void)OSSL_HTTP_close(ctx
->http_ctx
, 1);
201 ossl_cmp_debug(ctx
, "disconnected from CMP server");
204 OPENSSL_free(ctx
->propq
);
205 OPENSSL_free(ctx
->serverPath
);
206 OPENSSL_free(ctx
->server
);
207 OPENSSL_free(ctx
->proxy
);
208 OPENSSL_free(ctx
->no_proxy
);
210 X509_free(ctx
->srvCert
);
211 X509_free(ctx
->validatedSrvCert
);
212 X509_NAME_free(ctx
->expected_sender
);
213 X509_STORE_free(ctx
->trusted
);
214 OSSL_STACK_OF_X509_free(ctx
->untrusted
);
216 X509_free(ctx
->cert
);
217 OSSL_STACK_OF_X509_free(ctx
->chain
);
218 EVP_PKEY_free(ctx
->pkey
);
219 ASN1_OCTET_STRING_free(ctx
->referenceValue
);
220 if (ctx
->secretValue
!= NULL
)
221 OPENSSL_cleanse(ctx
->secretValue
->data
, ctx
->secretValue
->length
);
222 ASN1_OCTET_STRING_free(ctx
->secretValue
);
223 EVP_MD_free(ctx
->pbm_owf
);
225 X509_NAME_free(ctx
->recipient
);
226 EVP_MD_free(ctx
->digest
);
227 ASN1_OCTET_STRING_free(ctx
->transactionID
);
228 ASN1_OCTET_STRING_free(ctx
->senderNonce
);
229 ASN1_OCTET_STRING_free(ctx
->recipNonce
);
230 ASN1_OCTET_STRING_free(ctx
->first_senderNonce
);
231 OSSL_CMP_ITAVs_free(ctx
->geninfo_ITAVs
);
232 OSSL_STACK_OF_X509_free(ctx
->extraCertsOut
);
234 EVP_PKEY_free(ctx
->newPkey
);
235 X509_NAME_free(ctx
->issuer
);
236 ASN1_INTEGER_free(ctx
->serialNumber
);
237 X509_NAME_free(ctx
->subjectName
);
238 sk_GENERAL_NAME_pop_free(ctx
->subjectAltNames
, GENERAL_NAME_free
);
239 X509_EXTENSIONS_free(ctx
->reqExtensions
);
240 sk_POLICYINFO_pop_free(ctx
->policies
, POLICYINFO_free
);
241 X509_free(ctx
->oldCert
);
242 X509_REQ_free(ctx
->p10CSR
);
244 OSSL_CMP_ITAVs_free(ctx
->genm_ITAVs
);
246 OSSL_CMP_PKIFREETEXT_free(ctx
->statusString
);
247 X509_free(ctx
->newCert
);
248 OSSL_STACK_OF_X509_free(ctx
->newChain
);
249 OSSL_STACK_OF_X509_free(ctx
->caPubs
);
250 OSSL_STACK_OF_X509_free(ctx
->extraCertsIn
);
255 #define DEFINE_OSSL_set(PREFIX, FIELD, TYPE) \
256 int PREFIX##_set_##FIELD(OSSL_CMP_CTX *ctx, TYPE val) \
259 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
266 DEFINE_OSSL_set(ossl_cmp_ctx
, status
, int)
268 #define DEFINE_OSSL_get(PREFIX, FIELD, TYPE, ERR_RET) \
269 TYPE PREFIX##_get_##FIELD(const OSSL_CMP_CTX *ctx) \
272 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
279 * Returns the PKIStatus from the last CertRepMessage
280 * or Revocation Response or error message, -1 on error
282 DEFINE_OSSL_get(OSSL_CMP_CTX
, status
, int, -1)
285 * Returns the statusString from the last CertRepMessage
286 * or Revocation Response or error message, NULL on error
288 DEFINE_OSSL_CMP_CTX_get0(statusString
, OSSL_CMP_PKIFREETEXT
)
290 DEFINE_OSSL_set0(ossl_cmp_ctx
, statusString
, OSSL_CMP_PKIFREETEXT
)
292 /* Set callback function for checking if the cert is ok or should be rejected */
293 DEFINE_OSSL_set(OSSL_CMP_CTX
, certConf_cb
, OSSL_CMP_certConf_cb_t
)
296 * Set argument, respectively a pointer to a structure containing arguments,
297 * optionally to be used by the certConf callback.
299 DEFINE_OSSL_set(OSSL_CMP_CTX
, certConf_cb_arg
, void *)
302 * Get argument, respectively the pointer to a structure containing arguments,
303 * optionally to be used by certConf callback.
304 * Returns callback argument set previously (NULL if not set or on error)
306 DEFINE_OSSL_get(OSSL_CMP_CTX
, certConf_cb_arg
, void *, NULL
)
308 #ifndef OPENSSL_NO_TRACE
309 static size_t ossl_cmp_log_trace_cb(const char *buf
, size_t cnt
,
310 int category
, int cmd
, void *vdata
)
312 OSSL_CMP_CTX
*ctx
= vdata
;
314 OSSL_CMP_severity level
= -1;
319 if (buf
== NULL
|| cnt
== 0 || cmd
!= OSSL_TRACE_CTRL_WRITE
|| ctx
== NULL
)
321 if (ctx
->log_cb
== NULL
)
322 return 1; /* silently drop message */
324 msg
= ossl_cmp_log_parse_metadata(buf
, &level
, &func
, &file
, &line
);
326 if (level
> ctx
->log_verbosity
) /* excludes the case level is unknown */
327 goto end
; /* suppress output since severity is not sufficient */
329 if (!ctx
->log_cb(func
!= NULL
? func
: "(no func)",
330 file
!= NULL
? file
: "(no file)",
341 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
342 int ossl_cmp_print_log(OSSL_CMP_severity level
, const OSSL_CMP_CTX
*ctx
,
343 const char *func
, const char *file
, int line
,
344 const char *level_str
, const char *format
, ...)
347 char hugebuf
[1024 * 2];
350 if (ctx
== NULL
|| ctx
->log_cb
== NULL
)
351 return 1; /* silently drop message */
353 if (level
> ctx
->log_verbosity
) /* excludes the case level is unknown */
354 return 1; /* suppress output since severity is not sufficient */
359 va_start(args
, format
);
362 func
= "(unset function name)";
364 file
= "(unset file name)";
365 if (level_str
== NULL
)
366 level_str
= "(unset level string)";
368 #ifndef OPENSSL_NO_TRACE
369 if (OSSL_TRACE_ENABLED(CMP
)) {
370 OSSL_TRACE_BEGIN(CMP
) {
372 BIO_snprintf(hugebuf
, sizeof(hugebuf
),
373 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX
"%s: ",
374 func
, file
, line
, level_str
);
375 if (printed
> 0 && (size_t)printed
< sizeof(hugebuf
)) {
376 if (BIO_vsnprintf(hugebuf
+ printed
,
377 sizeof(hugebuf
) - printed
, format
, args
) > 0)
378 res
= BIO_puts(trc_out
, hugebuf
) > 0;
380 } OSSL_TRACE_END(CMP
);
382 #else /* compensate for disabled trace API */
384 if (BIO_vsnprintf(hugebuf
, sizeof(hugebuf
), format
, args
) > 0)
385 res
= ctx
->log_cb(func
, file
, line
, level
, hugebuf
);
392 /* Set a callback function for error reporting and logging messages */
393 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX
*ctx
, OSSL_CMP_log_cb_t cb
)
396 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
401 #ifndef OPENSSL_NO_TRACE
402 /* do also in case cb == NULL, to switch off logging output: */
403 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP
,
404 ossl_cmp_log_trace_cb
, ctx
))
411 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
412 void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX
*ctx
)
414 if (ctx
!= NULL
&& OSSL_CMP_LOG_ERR
> ctx
->log_verbosity
)
415 return; /* suppress output since severity is not sufficient */
416 OSSL_CMP_print_errors_cb(ctx
== NULL
? NULL
: ctx
->log_cb
);
420 * Set or clear the reference value to be used for identification
421 * (i.e., the user name) when using PBMAC.
423 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX
*ctx
,
424 const unsigned char *ref
, int len
)
427 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
431 ossl_cmp_asn1_octet_string_set1_bytes(&ctx
->referenceValue
, ref
, len
);
434 /* Set or clear the password to be used for protecting messages with PBMAC */
435 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX
*ctx
,
436 const unsigned char *sec
, int len
)
438 ASN1_OCTET_STRING
*secretValue
= NULL
;
441 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
444 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue
, sec
, len
) != 1)
446 if (ctx
->secretValue
!= NULL
) {
447 OPENSSL_cleanse(ctx
->secretValue
->data
, ctx
->secretValue
->length
);
448 ASN1_OCTET_STRING_free(ctx
->secretValue
);
450 ctx
->secretValue
= secretValue
;
454 #define DEFINE_OSSL_CMP_CTX_get1_certs(FIELD) \
455 STACK_OF(X509) *OSSL_CMP_CTX_get1_##FIELD(const OSSL_CMP_CTX *ctx) \
458 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
461 return X509_chain_up_ref(ctx->FIELD); \
464 /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
465 DEFINE_OSSL_CMP_CTX_get1_certs(newChain
)
467 #define DEFINE_OSSL_set1_certs(PREFIX, FIELD) \
468 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs) \
471 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
474 OSSL_STACK_OF_X509_free(ctx->FIELD); \
476 return certs == NULL || (ctx->FIELD = X509_chain_up_ref(certs)) != NULL; \
480 * Copies any given stack of inbound X509 certificates to newChain
481 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
483 DEFINE_OSSL_set1_certs(ossl_cmp_ctx
, newChain
)
485 /* Returns the stack of extraCerts received in CertRepMessage, NULL on error */
486 DEFINE_OSSL_CMP_CTX_get1_certs(extraCertsIn
)
489 * Copies any given stack of inbound X509 certificates to extraCertsIn
490 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
492 DEFINE_OSSL_set1_certs(ossl_cmp_ctx
, extraCertsIn
)
495 * Copies any given stack as the new stack of X509
496 * certificates to send out in the extraCerts field.
498 DEFINE_OSSL_set1_certs(OSSL_CMP_CTX
, extraCertsOut
)
501 * Add the given policy info object
502 * to the X509_EXTENSIONS of the requested certificate template.
504 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX
*ctx
, POLICYINFO
*pinfo
)
506 if (ctx
== NULL
|| pinfo
== NULL
) {
507 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
511 if (ctx
->policies
== NULL
512 && (ctx
->policies
= CERTIFICATEPOLICIES_new()) == NULL
)
515 return sk_POLICYINFO_push(ctx
->policies
, pinfo
);
518 /* Add an ITAV for geninfo of the PKI message header */
519 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX
*ctx
, OSSL_CMP_ITAV
*itav
)
522 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
525 return OSSL_CMP_ITAV_push0_stack_item(&ctx
->geninfo_ITAVs
, itav
);
528 int OSSL_CMP_CTX_reset_geninfo_ITAVs(OSSL_CMP_CTX
*ctx
)
531 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
534 OSSL_CMP_ITAVs_free(ctx
->geninfo_ITAVs
);
535 ctx
->geninfo_ITAVs
= NULL
;
539 DEFINE_OSSL_CMP_CTX_get0(geninfo_ITAVs
, STACK_OF(OSSL_CMP_ITAV
))
541 /* Add an itav for the body of outgoing general messages */
542 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX
*ctx
, OSSL_CMP_ITAV
*itav
)
545 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
548 return OSSL_CMP_ITAV_push0_stack_item(&ctx
->genm_ITAVs
, itav
);
552 * Returns a duplicate of the stack of X509 certificates that
553 * were received in the caPubs field of the last CertRepMessage.
554 * Returns NULL on error
556 DEFINE_OSSL_CMP_CTX_get1_certs(caPubs
)
559 * Copies any given stack of certificates to the given
560 * OSSL_CMP_CTX structure so that they may be retrieved later.
562 DEFINE_OSSL_set1_certs(ossl_cmp_ctx
, caPubs
)
564 #define char_dup OPENSSL_strdup
565 #define char_free OPENSSL_free
566 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
567 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
569 TYPE *val_dup = NULL; \
572 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
576 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
578 TYPE##_free(ctx->FIELD); \
579 ctx->FIELD = val_dup; \
583 #define X509_invalid(cert) (!ossl_x509v3_cache_extensions(cert))
584 #define EVP_PKEY_invalid(key) 0
586 #define DEFINE_OSSL_set1_up_ref(PREFIX, FIELD, TYPE) \
587 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
590 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
594 /* prevent misleading error later on malformed cert or provider issue */ \
595 if (val != NULL && TYPE##_invalid(val)) { \
596 ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
599 if (val != NULL && !TYPE##_up_ref(val)) \
601 TYPE##_free(ctx->FIELD); \
606 DEFINE_OSSL_set1_up_ref(ossl_cmp_ctx
, validatedSrvCert
, X509
)
609 * Pins the server certificate to be directly trusted (even if it is expired)
610 * for verifying response messages.
611 * Cert pointer is not consumed. It may be NULL to clear the entry.
613 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX
, srvCert
, X509
)
615 /* Set the X509 name of the recipient to be placed in the PKIHeader */
616 DEFINE_OSSL_CMP_CTX_set1(recipient
, X509_NAME
)
618 /* Store the X509 name of the expected sender in the PKIHeader of responses */
619 DEFINE_OSSL_CMP_CTX_set1(expected_sender
, X509_NAME
)
621 /* Set the X509 name of the issuer to be placed in the certTemplate */
622 DEFINE_OSSL_CMP_CTX_set1(issuer
, X509_NAME
)
624 /* Set the ASN1_INTEGER serial to be placed in the certTemplate for rr */
625 DEFINE_OSSL_CMP_CTX_set1(serialNumber
, ASN1_INTEGER
)
627 * Set the subject name that will be placed in the certificate
628 * request. This will be the subject name on the received certificate.
630 DEFINE_OSSL_CMP_CTX_set1(subjectName
, X509_NAME
)
632 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
633 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX
*ctx
, X509_EXTENSIONS
*exts
)
636 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
640 if (sk_GENERAL_NAME_num(ctx
->subjectAltNames
) > 0 && exts
!= NULL
641 && X509v3_get_ext_by_NID(exts
, NID_subject_alt_name
, -1) >= 0) {
642 ERR_raise(ERR_LIB_CMP
, CMP_R_MULTIPLE_SAN_SOURCES
);
645 X509_EXTENSIONS_free(ctx
->reqExtensions
);
646 ctx
->reqExtensions
= exts
;
650 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
651 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX
*ctx
)
654 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
657 /* if one of the following conditions 'fail' this is not an error */
658 return ctx
->reqExtensions
!= NULL
659 && X509v3_get_ext_by_NID(ctx
->reqExtensions
,
660 NID_subject_alt_name
, -1) >= 0;
664 * Add a GENERAL_NAME structure that will be added to the CRMF
665 * request's extensions field to request subject alternative names.
667 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX
*ctx
,
668 const GENERAL_NAME
*name
)
670 GENERAL_NAME
*name_dup
;
672 if (ctx
== NULL
|| name
== NULL
) {
673 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
677 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx
) == 1) {
678 ERR_raise(ERR_LIB_CMP
, CMP_R_MULTIPLE_SAN_SOURCES
);
682 if (ctx
->subjectAltNames
== NULL
683 && (ctx
->subjectAltNames
= sk_GENERAL_NAME_new_null()) == NULL
)
685 if ((name_dup
= GENERAL_NAME_dup(name
)) == NULL
)
687 if (!sk_GENERAL_NAME_push(ctx
->subjectAltNames
, name_dup
)) {
688 GENERAL_NAME_free(name_dup
);
695 * Set our own client certificate, used for example in KUR and when
696 * doing the IR with existing certificate.
698 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX
, cert
, X509
)
700 int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX
*ctx
, X509_STORE
*own_trusted
,
701 STACK_OF(X509
) *candidates
)
703 STACK_OF(X509
) *chain
;
706 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
710 if (!ossl_x509_add_certs_new(&ctx
->untrusted
, candidates
,
711 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
))
714 ossl_cmp_debug(ctx
, "trying to build chain for own CMP signer cert");
715 chain
= X509_build_chain(ctx
->cert
, ctx
->untrusted
, own_trusted
, 0,
716 ctx
->libctx
, ctx
->propq
);
718 ERR_raise(ERR_LIB_CMP
, CMP_R_FAILED_BUILDING_OWN_CHAIN
);
721 ossl_cmp_debug(ctx
, "success building chain for own CMP signer cert");
727 * Set the old certificate that we are updating in KUR
728 * or the certificate to be revoked in RR, respectively.
729 * Also used as reference cert (defaulting to cert) for deriving subject DN
730 * and SANs. Its issuer is used as default recipient in the CMP message header.
732 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX
, oldCert
, X509
)
734 /* Set the PKCS#10 CSR to be sent in P10CR */
735 DEFINE_OSSL_CMP_CTX_set1(p10CSR
, X509_REQ
)
738 * Set the (newly received in IP/KUP/CP) certificate in the context.
739 * This only permits for one cert to be enrolled at a time.
741 DEFINE_OSSL_set0(ossl_cmp_ctx
, newCert
, X509
)
743 /* Get successfully validated server cert, if any, of current transaction */
744 DEFINE_OSSL_CMP_CTX_get0(validatedSrvCert
, X509
)
747 * Get the (newly received in IP/KUP/CP) client certificate from the context
748 * This only permits for one client cert to be received...
750 DEFINE_OSSL_CMP_CTX_get0(newCert
, X509
)
752 /* Set the client's current private key */
753 DEFINE_OSSL_set1_up_ref(OSSL_CMP_CTX
, pkey
, EVP_PKEY
)
755 /* Set new key pair. Used e.g. when doing Key Update */
756 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX
*ctx
, int priv
, EVP_PKEY
*pkey
)
759 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
763 EVP_PKEY_free(ctx
->newPkey
);
765 ctx
->newPkey_priv
= priv
;
769 /* Get the private/public key to use for cert enrollment, or NULL on error */
770 /* In case |priv| == 0, better use ossl_cmp_ctx_get0_newPubkey() below */
771 EVP_PKEY
*OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX
*ctx
, int priv
)
774 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
778 if (ctx
->newPkey
!= NULL
)
779 return priv
&& !ctx
->newPkey_priv
? NULL
: ctx
->newPkey
;
780 if (ctx
->p10CSR
!= NULL
)
781 return priv
? NULL
: X509_REQ_get0_pubkey(ctx
->p10CSR
);
782 return ctx
->pkey
; /* may be NULL */
785 EVP_PKEY
*ossl_cmp_ctx_get0_newPubkey(const OSSL_CMP_CTX
*ctx
)
787 if (!ossl_assert(ctx
!= NULL
))
789 if (ctx
->newPkey
!= NULL
)
791 if (ctx
->p10CSR
!= NULL
)
792 return X509_REQ_get0_pubkey(ctx
->p10CSR
);
793 if (ctx
->oldCert
!= NULL
)
794 return X509_get0_pubkey(ctx
->oldCert
);
795 if (ctx
->cert
!= NULL
)
796 return X509_get0_pubkey(ctx
->cert
);
800 #define DEFINE_set1_ASN1_OCTET_STRING(PREFIX, FIELD) \
801 int PREFIX##_set1_##FIELD(OSSL_CMP_CTX *ctx, const ASN1_OCTET_STRING *id) \
804 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); \
807 return ossl_cmp_asn1_octet_string_set1(&ctx->FIELD, id); \
810 /* Set the given transactionID to the context */
811 DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX
, transactionID
)
813 /* Set the nonce to be used for the recipNonce in the message created next */
814 DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx
, recipNonce
)
816 /* Stores the given nonce as the last senderNonce sent out */
817 DEFINE_set1_ASN1_OCTET_STRING(OSSL_CMP_CTX
, senderNonce
)
819 /* store the first req sender nonce for verifying delayed delivery */
820 DEFINE_set1_ASN1_OCTET_STRING(ossl_cmp_ctx
, first_senderNonce
)
822 /* Set the proxy server to use for HTTP(S) connections */
823 DEFINE_OSSL_CMP_CTX_set1(proxy
, char)
825 /* Set the (HTTP) hostname of the CMP server */
826 DEFINE_OSSL_CMP_CTX_set1(server
, char)
828 /* Set the server exclusion list of the HTTP proxy server */
829 DEFINE_OSSL_CMP_CTX_set1(no_proxy
, char)
831 #ifndef OPENSSL_NO_HTTP
832 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
833 DEFINE_OSSL_set(OSSL_CMP_CTX
, http_cb
, OSSL_HTTP_bio_cb_t
)
835 /* Set argument optionally to be used by the http connect/disconnect callback */
836 DEFINE_OSSL_set(OSSL_CMP_CTX
, http_cb_arg
, void *)
839 * Get argument optionally to be used by the http connect/disconnect callback
840 * Returns callback argument set previously (NULL if not set or on error)
842 DEFINE_OSSL_get(OSSL_CMP_CTX
, http_cb_arg
, void *, NULL
)
845 /* Set callback function for sending CMP request and receiving response */
846 DEFINE_OSSL_set(OSSL_CMP_CTX
, transfer_cb
, OSSL_CMP_transfer_cb_t
)
848 /* Set argument optionally to be used by the transfer callback */
849 DEFINE_OSSL_set(OSSL_CMP_CTX
, transfer_cb_arg
, void *)
852 * Get argument optionally to be used by the transfer callback.
853 * Returns callback argument set previously (NULL if not set or on error)
855 DEFINE_OSSL_get(OSSL_CMP_CTX
, transfer_cb_arg
, void *, NULL
)
857 /** Set the HTTP server port to be used */
858 DEFINE_OSSL_set(OSSL_CMP_CTX
, serverPort
, int)
860 /* Set the HTTP path to be used on the server (e.g "pkix/") */
861 DEFINE_OSSL_CMP_CTX_set1(serverPath
, char)
863 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
864 DEFINE_OSSL_set(ossl_cmp_ctx
, failInfoCode
, int)
867 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
868 * Returns bit string as integer on success, -1 on error
870 DEFINE_OSSL_get(OSSL_CMP_CTX
, failInfoCode
, int, -1)
872 /* Set a Boolean or integer option of the context to the "val" arg */
873 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX
*ctx
, int opt
, int val
)
878 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
883 case OSSL_CMP_OPT_REVOCATION_REASON
:
884 min_val
= OCSP_REVOKED_STATUS_NOSTATUS
;
886 case OSSL_CMP_OPT_POPO_METHOD
:
887 min_val
= OSSL_CRMF_POPO_NONE
;
894 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_SMALL
);
899 case OSSL_CMP_OPT_LOG_VERBOSITY
:
900 if (val
> OSSL_CMP_LOG_MAX
) {
901 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_LARGE
);
904 ctx
->log_verbosity
= val
;
906 case OSSL_CMP_OPT_IMPLICIT_CONFIRM
:
907 ctx
->implicitConfirm
= val
;
909 case OSSL_CMP_OPT_DISABLE_CONFIRM
:
910 ctx
->disableConfirm
= val
;
912 case OSSL_CMP_OPT_UNPROTECTED_SEND
:
913 ctx
->unprotectedSend
= val
;
915 case OSSL_CMP_OPT_UNPROTECTED_ERRORS
:
916 ctx
->unprotectedErrors
= val
;
918 case OSSL_CMP_OPT_NO_CACHE_EXTRACERTS
:
919 ctx
->noCacheExtraCerts
= val
;
921 case OSSL_CMP_OPT_VALIDITY_DAYS
:
924 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT
:
925 ctx
->SubjectAltName_nodefault
= val
;
927 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL
:
928 ctx
->setSubjectAltNameCritical
= val
;
930 case OSSL_CMP_OPT_POLICIES_CRITICAL
:
931 ctx
->setPoliciesCritical
= val
;
933 case OSSL_CMP_OPT_IGNORE_KEYUSAGE
:
934 ctx
->ignore_keyusage
= val
;
936 case OSSL_CMP_OPT_POPO_METHOD
:
937 if (val
> OSSL_CRMF_POPO_KEYAGREE
) {
938 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_LARGE
);
941 ctx
->popoMethod
= val
;
943 case OSSL_CMP_OPT_DIGEST_ALGNID
:
944 if (!cmp_ctx_set_md(ctx
, &ctx
->digest
, val
))
947 case OSSL_CMP_OPT_OWF_ALGNID
:
948 if (!cmp_ctx_set_md(ctx
, &ctx
->pbm_owf
, val
))
951 case OSSL_CMP_OPT_MAC_ALGNID
:
954 case OSSL_CMP_OPT_KEEP_ALIVE
:
955 ctx
->keep_alive
= val
;
957 case OSSL_CMP_OPT_MSG_TIMEOUT
:
958 ctx
->msg_timeout
= val
;
960 case OSSL_CMP_OPT_TOTAL_TIMEOUT
:
961 ctx
->total_timeout
= val
;
963 case OSSL_CMP_OPT_USE_TLS
:
966 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR
:
967 ctx
->permitTAInExtraCertsForIR
= val
;
969 case OSSL_CMP_OPT_REVOCATION_REASON
:
970 if (val
> OCSP_REVOKED_STATUS_AACOMPROMISE
) {
971 ERR_raise(ERR_LIB_CMP
, CMP_R_VALUE_TOO_LARGE
);
974 ctx
->revocationReason
= val
;
977 ERR_raise(ERR_LIB_CMP
, CMP_R_INVALID_OPTION
);
985 * Reads a Boolean or integer option value from the context.
986 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
988 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX
*ctx
, int opt
)
991 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
996 case OSSL_CMP_OPT_LOG_VERBOSITY
:
997 return ctx
->log_verbosity
;
998 case OSSL_CMP_OPT_IMPLICIT_CONFIRM
:
999 return ctx
->implicitConfirm
;
1000 case OSSL_CMP_OPT_DISABLE_CONFIRM
:
1001 return ctx
->disableConfirm
;
1002 case OSSL_CMP_OPT_UNPROTECTED_SEND
:
1003 return ctx
->unprotectedSend
;
1004 case OSSL_CMP_OPT_UNPROTECTED_ERRORS
:
1005 return ctx
->unprotectedErrors
;
1006 case OSSL_CMP_OPT_NO_CACHE_EXTRACERTS
:
1007 return ctx
->noCacheExtraCerts
;
1008 case OSSL_CMP_OPT_VALIDITY_DAYS
:
1010 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT
:
1011 return ctx
->SubjectAltName_nodefault
;
1012 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL
:
1013 return ctx
->setSubjectAltNameCritical
;
1014 case OSSL_CMP_OPT_POLICIES_CRITICAL
:
1015 return ctx
->setPoliciesCritical
;
1016 case OSSL_CMP_OPT_IGNORE_KEYUSAGE
:
1017 return ctx
->ignore_keyusage
;
1018 case OSSL_CMP_OPT_POPO_METHOD
:
1019 return ctx
->popoMethod
;
1020 case OSSL_CMP_OPT_DIGEST_ALGNID
:
1021 return EVP_MD_get_type(ctx
->digest
);
1022 case OSSL_CMP_OPT_OWF_ALGNID
:
1023 return EVP_MD_get_type(ctx
->pbm_owf
);
1024 case OSSL_CMP_OPT_MAC_ALGNID
:
1025 return ctx
->pbm_mac
;
1026 case OSSL_CMP_OPT_KEEP_ALIVE
:
1027 return ctx
->keep_alive
;
1028 case OSSL_CMP_OPT_MSG_TIMEOUT
:
1029 return ctx
->msg_timeout
;
1030 case OSSL_CMP_OPT_TOTAL_TIMEOUT
:
1031 return ctx
->total_timeout
;
1032 case OSSL_CMP_OPT_USE_TLS
:
1033 return ctx
->tls_used
;
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 ERR_raise(ERR_LIB_CMP
, CMP_R_INVALID_OPTION
);