]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cmp/cmp_ctx.c
Clean up CMP chain building for CMP signer, TLS client, and newly enrolled certs
[thirdparty/openssl.git] / crypto / cmp / cmp_ctx.c
1 /*
2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
5 *
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
10 */
11
12 #include <openssl/trace.h>
13 #include <openssl/bio.h>
14 #include <openssl/ocsp.h> /* for OCSP_REVOKED_STATUS_* */
15 #include "crypto/x509.h" /* for x509v3_cache_extensions() */
16
17 #include "cmp_local.h"
18
19 /* explicit #includes not strictly needed since implied by the above: */
20 #include <openssl/cmp.h>
21 #include <openssl/crmf.h>
22 #include <openssl/err.h>
23
24 DEFINE_STACK_OF(X509)
25 DEFINE_STACK_OF(X509_EXTENSION)
26 DEFINE_STACK_OF(POLICYINFO)
27 DEFINE_STACK_OF(ASN1_UTF8STRING)
28 DEFINE_STACK_OF(GENERAL_NAME)
29 DEFINE_STACK_OF(OSSL_CMP_ITAV)
30
31 /*
32 * Get current certificate store containing trusted root CA certs
33 */
34 X509_STORE *OSSL_CMP_CTX_get0_trustedStore(const OSSL_CMP_CTX *ctx)
35 {
36 if (ctx == NULL) {
37 CMPerr(0, CMP_R_NULL_ARGUMENT);
38 return NULL;
39 }
40 return ctx->trusted;
41 }
42
43 /*
44 * Set certificate store containing trusted (root) CA certs and possibly CRLs
45 * and a cert verification callback function used for CMP server authentication.
46 * Any already existing store entry is freed. Given NULL, the entry is reset.
47 */
48 int OSSL_CMP_CTX_set0_trustedStore(OSSL_CMP_CTX *ctx, X509_STORE *store)
49 {
50 if (ctx == NULL) {
51 CMPerr(0, CMP_R_NULL_ARGUMENT);
52 return 0;
53 }
54 X509_STORE_free(ctx->trusted);
55 ctx->trusted = store;
56 return 1;
57 }
58
59 /* Get current list of non-trusted intermediate certs */
60 STACK_OF(X509) *OSSL_CMP_CTX_get0_untrusted(const OSSL_CMP_CTX *ctx)
61 {
62 if (ctx == NULL) {
63 CMPerr(0, CMP_R_NULL_ARGUMENT);
64 return NULL;
65 }
66 return ctx->untrusted;
67 }
68
69 /*
70 * Set untrusted certificates for path construction in authentication of
71 * the CMP server and potentially others (TLS server, newly enrolled cert).
72 */
73 int OSSL_CMP_CTX_set1_untrusted(OSSL_CMP_CTX *ctx, STACK_OF(X509) *certs)
74 {
75 STACK_OF(X509) *untrusted;
76 if (ctx == NULL) {
77 CMPerr(0, CMP_R_NULL_ARGUMENT);
78 return 0;
79 }
80 if ((untrusted = sk_X509_new_null()) == NULL)
81 return 0;
82 if (X509_add_certs(untrusted, certs,
83 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP) != 1)
84 goto err;
85 sk_X509_pop_free(ctx->untrusted, X509_free);
86 ctx->untrusted = untrusted;
87 return 1;
88 err:
89 sk_X509_pop_free(untrusted, X509_free);
90 return 0;
91 }
92
93 static int cmp_ctx_set_md(OSSL_CMP_CTX *ctx, EVP_MD **pmd, int nid)
94 {
95 EVP_MD *md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(nid), ctx->propq);
96 /* fetching in advance to be able to throw error early if unsupported */
97
98 if (md == NULL) {
99 CMPerr(0, CMP_R_UNSUPPORTED_ALGORITHM);
100 return 0;
101 }
102 EVP_MD_free(*pmd);
103 *pmd = md;
104 return 1;
105 }
106
107 /*
108 * Allocates and initializes OSSL_CMP_CTX context structure with default values.
109 * Returns new context on success, NULL on error
110 */
111 OSSL_CMP_CTX *OSSL_CMP_CTX_new(OPENSSL_CTX *libctx, const char *propq)
112 {
113 OSSL_CMP_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
114
115 if (ctx == NULL)
116 goto err;
117
118 ctx->libctx = libctx;
119 if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
120 goto err;
121
122 ctx->log_verbosity = OSSL_CMP_LOG_INFO;
123
124 ctx->status = -1;
125 ctx->failInfoCode = -1;
126
127 ctx->msg_timeout = 2 * 60;
128
129 if ((ctx->untrusted = sk_X509_new_null()) == NULL)
130 goto err;
131
132 ctx->pbm_slen = 16;
133 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, NID_sha256))
134 goto err;
135 ctx->pbm_itercnt = 500;
136 ctx->pbm_mac = NID_hmac_sha1;
137
138 if (!cmp_ctx_set_md(ctx, &ctx->digest, NID_sha256))
139 goto err;
140 ctx->popoMethod = OSSL_CRMF_POPO_SIGNATURE;
141 ctx->revocationReason = CRL_REASON_NONE;
142
143 /* all other elements are initialized to 0 or NULL, respectively */
144 return ctx;
145
146 err:
147 OSSL_CMP_CTX_free(ctx);
148 X509err(0, ERR_R_MALLOC_FAILURE);
149 return NULL;
150 }
151
152 /* Prepare the OSSL_CMP_CTX for next use, partly re-initializing OSSL_CMP_CTX */
153 int OSSL_CMP_CTX_reinit(OSSL_CMP_CTX *ctx)
154 {
155 if (ctx == NULL) {
156 CMPerr(0, CMP_R_NULL_ARGUMENT);
157 return 0;
158 }
159
160 ctx->status = -1;
161 ctx->failInfoCode = -1;
162
163 return ossl_cmp_ctx_set0_statusString(ctx, NULL)
164 && ossl_cmp_ctx_set0_newCert(ctx, NULL)
165 && ossl_cmp_ctx_set1_newChain(ctx, NULL)
166 && ossl_cmp_ctx_set1_caPubs(ctx, NULL)
167 && ossl_cmp_ctx_set1_extraCertsIn(ctx, NULL)
168 && ossl_cmp_ctx_set0_validatedSrvCert(ctx, NULL)
169 && OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
170 && OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)
171 && ossl_cmp_ctx_set1_recipNonce(ctx, NULL);
172 }
173
174 /* Frees OSSL_CMP_CTX variables allocated in OSSL_CMP_CTX_new() */
175 void OSSL_CMP_CTX_free(OSSL_CMP_CTX *ctx)
176 {
177 if (ctx == NULL)
178 return;
179
180 OPENSSL_free(ctx->serverPath);
181 OPENSSL_free(ctx->server);
182 OPENSSL_free(ctx->proxy);
183 OPENSSL_free(ctx->no_proxy);
184
185 X509_free(ctx->srvCert);
186 X509_free(ctx->validatedSrvCert);
187 X509_NAME_free(ctx->expected_sender);
188 X509_STORE_free(ctx->trusted);
189 sk_X509_pop_free(ctx->untrusted, X509_free);
190
191 X509_free(ctx->cert);
192 sk_X509_pop_free(ctx->chain, X509_free);
193 EVP_PKEY_free(ctx->pkey);
194 ASN1_OCTET_STRING_free(ctx->referenceValue);
195 if (ctx->secretValue != NULL)
196 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
197 ASN1_OCTET_STRING_free(ctx->secretValue);
198 EVP_MD_free(ctx->pbm_owf);
199
200 X509_NAME_free(ctx->recipient);
201 EVP_MD_free(ctx->digest);
202 ASN1_OCTET_STRING_free(ctx->transactionID);
203 ASN1_OCTET_STRING_free(ctx->senderNonce);
204 ASN1_OCTET_STRING_free(ctx->recipNonce);
205 sk_OSSL_CMP_ITAV_pop_free(ctx->geninfo_ITAVs, OSSL_CMP_ITAV_free);
206 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
207
208 EVP_PKEY_free(ctx->newPkey);
209 X509_NAME_free(ctx->issuer);
210 X509_NAME_free(ctx->subjectName);
211 sk_GENERAL_NAME_pop_free(ctx->subjectAltNames, GENERAL_NAME_free);
212 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
213 sk_POLICYINFO_pop_free(ctx->policies, POLICYINFO_free);
214 X509_free(ctx->oldCert);
215 X509_REQ_free(ctx->p10CSR);
216
217 sk_OSSL_CMP_ITAV_pop_free(ctx->genm_ITAVs, OSSL_CMP_ITAV_free);
218
219 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
220 X509_free(ctx->newCert);
221 sk_X509_pop_free(ctx->newChain, X509_free);
222 sk_X509_pop_free(ctx->caPubs, X509_free);
223 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
224
225 OPENSSL_free(ctx);
226 }
227
228 int ossl_cmp_ctx_set_status(OSSL_CMP_CTX *ctx, int status)
229 {
230 if (!ossl_assert(ctx != NULL))
231 return 0;
232 ctx->status = status;
233 return 1;
234 }
235
236 /*
237 * Returns the PKIStatus from the last CertRepMessage
238 * or Revocation Response or error message, -1 on error
239 */
240 int OSSL_CMP_CTX_get_status(const OSSL_CMP_CTX *ctx)
241 {
242 if (ctx == NULL) {
243 CMPerr(0, CMP_R_NULL_ARGUMENT);
244 return -1;
245 }
246 return ctx->status;
247 }
248
249 /*
250 * Returns the statusString from the last CertRepMessage
251 * or Revocation Response or error message, NULL on error
252 */
253 OSSL_CMP_PKIFREETEXT *OSSL_CMP_CTX_get0_statusString(const OSSL_CMP_CTX *ctx)
254 {
255 if (ctx == NULL) {
256 CMPerr(0, CMP_R_NULL_ARGUMENT);
257 return NULL;
258 }
259 return ctx->statusString;
260 }
261
262 int ossl_cmp_ctx_set0_statusString(OSSL_CMP_CTX *ctx,
263 OSSL_CMP_PKIFREETEXT *text)
264 {
265 if (!ossl_assert(ctx != NULL))
266 return 0;
267 sk_ASN1_UTF8STRING_pop_free(ctx->statusString, ASN1_UTF8STRING_free);
268 ctx->statusString = text;
269 return 1;
270 }
271
272 int ossl_cmp_ctx_set0_validatedSrvCert(OSSL_CMP_CTX *ctx, X509 *cert)
273 {
274 if (!ossl_assert(ctx != NULL))
275 return 0;
276 X509_free(ctx->validatedSrvCert);
277 ctx->validatedSrvCert = cert;
278 return 1;
279 }
280
281 /* Set callback function for checking if the cert is ok or should be rejected */
282 int OSSL_CMP_CTX_set_certConf_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_certConf_cb_t cb)
283 {
284 if (ctx == NULL) {
285 CMPerr(0, CMP_R_NULL_ARGUMENT);
286 return 0;
287 }
288 ctx->certConf_cb = cb;
289 return 1;
290 }
291
292 /*
293 * Set argument, respectively a pointer to a structure containing arguments,
294 * optionally to be used by the certConf callback.
295 */
296 int OSSL_CMP_CTX_set_certConf_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
297 {
298 if (ctx == NULL) {
299 CMPerr(0, CMP_R_NULL_ARGUMENT);
300 return 0;
301 }
302 ctx->certConf_cb_arg = arg;
303 return 1;
304 }
305
306 /*
307 * Get argument, respectively the pointer to a structure containing arguments,
308 * optionally to be used by certConf callback.
309 * Returns callback argument set previously (NULL if not set or on error)
310 */
311 void *OSSL_CMP_CTX_get_certConf_cb_arg(const OSSL_CMP_CTX *ctx)
312 {
313 if (ctx == NULL) {
314 CMPerr(0, CMP_R_NULL_ARGUMENT);
315 return NULL;
316 }
317 return ctx->certConf_cb_arg;
318 }
319
320 #ifndef OPENSSL_NO_TRACE
321 static size_t ossl_cmp_log_trace_cb(const char *buf, size_t cnt,
322 int category, int cmd, void *vdata)
323 {
324 OSSL_CMP_CTX *ctx = vdata;
325 const char *msg;
326 OSSL_CMP_severity level = -1;
327 char *func = NULL;
328 char *file = NULL;
329 int line = 0;
330
331 if (buf == NULL || cnt == 0 || cmd != OSSL_TRACE_CTRL_WRITE || ctx == NULL)
332 return 0;
333 if (ctx->log_cb == NULL)
334 return 1; /* silently drop message */
335
336 msg = ossl_cmp_log_parse_metadata(buf, &level, &func, &file, &line);
337
338 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
339 goto end; /* suppress output since severity is not sufficient */
340
341 if (!ctx->log_cb(func != NULL ? func : "(no func)",
342 file != NULL ? file : "(no file)",
343 line, level, msg))
344 cnt = 0;
345
346 end:
347 OPENSSL_free(func);
348 OPENSSL_free(file);
349 return cnt;
350 }
351 #endif
352
353 /* Print CMP log messages (i.e., diagnostic info) via the log cb of the ctx */
354 int ossl_cmp_print_log(OSSL_CMP_severity level, const OSSL_CMP_CTX *ctx,
355 const char *func, const char *file, int line,
356 const char *level_str, const char *format, ...)
357 {
358 va_list args;
359 char hugebuf[1024 * 2];
360 int res = 0;
361
362 if (ctx == NULL || ctx->log_cb == NULL)
363 return 1; /* silently drop message */
364
365 if (level > ctx->log_verbosity) /* excludes the case level is unknown */
366 return 1; /* suppress output since severity is not sufficient */
367
368 if (format == NULL)
369 return 0;
370
371 va_start(args, format);
372
373 if (func == NULL)
374 func = "(unset function name)";
375 if (file == NULL)
376 file = "(unset file name)";
377 if (level_str == NULL)
378 level_str = "(unset level string)";
379
380 #ifndef OPENSSL_NO_TRACE
381 if (OSSL_TRACE_ENABLED(CMP)) {
382 OSSL_TRACE_BEGIN(CMP) {
383 int printed =
384 BIO_snprintf(hugebuf, sizeof(hugebuf),
385 "%s:%s:%d:" OSSL_CMP_LOG_PREFIX "%s: ",
386 func, file, line, level_str);
387 if (printed > 0 && (size_t)printed < sizeof(hugebuf)) {
388 if (BIO_vsnprintf(hugebuf + printed,
389 sizeof(hugebuf) - printed, format, args) > 0)
390 res = BIO_puts(trc_out, hugebuf) > 0;
391 }
392 } OSSL_TRACE_END(CMP);
393 }
394 #else /* compensate for disabled trace API */
395 {
396 if (BIO_vsnprintf(hugebuf, sizeof(hugebuf), format, args) > 0)
397 res = ctx->log_cb(func, file, line, level, hugebuf);
398 }
399 #endif
400 va_end(args);
401 return res;
402 }
403
404 /* Set a callback function for error reporting and logging messages */
405 int OSSL_CMP_CTX_set_log_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_log_cb_t cb)
406 {
407 if (ctx == NULL) {
408 CMPerr(0, CMP_R_NULL_ARGUMENT);
409 return 0;
410 }
411 ctx->log_cb = cb;
412
413 #ifndef OPENSSL_NO_TRACE
414 /* do also in case cb == NULL, to switch off logging output: */
415 if (!OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_CMP,
416 ossl_cmp_log_trace_cb, ctx))
417 return 0;
418 #endif
419
420 return 1;
421 }
422
423 /* Print OpenSSL and CMP errors via the log cb of the ctx or ERR_print_errors */
424 void OSSL_CMP_CTX_print_errors(const OSSL_CMP_CTX *ctx)
425 {
426 if (ctx != NULL && OSSL_CMP_LOG_ERR > ctx->log_verbosity)
427 return; /* suppress output since severity is not sufficient */
428 OSSL_CMP_print_errors_cb(ctx == NULL ? NULL : ctx->log_cb);
429 }
430
431 /*
432 * Set or clear the reference value to be used for identification
433 * (i.e., the user name) when using PBMAC.
434 */
435 int OSSL_CMP_CTX_set1_referenceValue(OSSL_CMP_CTX *ctx,
436 const unsigned char *ref, int len)
437 {
438 if (ctx == NULL) {
439 CMPerr(0, CMP_R_NULL_ARGUMENT);
440 return 0;
441 }
442 return ossl_cmp_asn1_octet_string_set1_bytes(&ctx->referenceValue, ref,
443 len);
444 }
445
446 /* Set or clear the password to be used for protecting messages with PBMAC */
447 int OSSL_CMP_CTX_set1_secretValue(OSSL_CMP_CTX *ctx, const unsigned char *sec,
448 const int len)
449 {
450 ASN1_OCTET_STRING *secretValue = NULL;
451 if (ctx == NULL) {
452 CMPerr(0, CMP_R_NULL_ARGUMENT);
453 return 0;
454 }
455 if (ossl_cmp_asn1_octet_string_set1_bytes(&secretValue, sec, len) != 1)
456 return 0;
457 if (ctx->secretValue != NULL) {
458 OPENSSL_cleanse(ctx->secretValue->data, ctx->secretValue->length);
459 ASN1_OCTET_STRING_free(ctx->secretValue);
460 }
461 ctx->secretValue = secretValue;
462 return 1;
463 }
464
465 /* Returns the cert chain computed by OSSL_CMP_certConf_cb(), NULL on error */
466 STACK_OF(X509) *OSSL_CMP_CTX_get1_newChain(const OSSL_CMP_CTX *ctx)
467 {
468 if (ctx == NULL) {
469 CMPerr(0, CMP_R_NULL_ARGUMENT);
470 return NULL;
471 }
472 if (ctx->newChain == NULL)
473 return sk_X509_new_null();
474 return X509_chain_up_ref(ctx->newChain);
475 }
476
477 /*
478 * Copies any given stack of inbound X509 certificates to newChain
479 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
480 */
481 int ossl_cmp_ctx_set1_newChain(OSSL_CMP_CTX *ctx, STACK_OF(X509) *newChain)
482 {
483 if (!ossl_assert(ctx != NULL))
484 return 0;
485
486 sk_X509_pop_free(ctx->newChain, X509_free);
487 ctx->newChain= NULL;
488 if (newChain == NULL)
489 return 1;
490 return (ctx->newChain = X509_chain_up_ref(newChain)) != NULL;
491 }
492
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)
495 {
496 if (ctx == NULL) {
497 CMPerr(0, CMP_R_NULL_ARGUMENT);
498 return NULL;
499 }
500 if (ctx->extraCertsIn == NULL)
501 return sk_X509_new_null();
502 return X509_chain_up_ref(ctx->extraCertsIn);
503 }
504
505 /*
506 * Copies any given stack of inbound X509 certificates to extraCertsIn
507 * of the OSSL_CMP_CTX structure so that they may be retrieved later.
508 */
509 int ossl_cmp_ctx_set1_extraCertsIn(OSSL_CMP_CTX *ctx,
510 STACK_OF(X509) *extraCertsIn)
511 {
512 if (!ossl_assert(ctx != NULL))
513 return 0;
514
515 sk_X509_pop_free(ctx->extraCertsIn, X509_free);
516 ctx->extraCertsIn = NULL;
517 if (extraCertsIn == NULL)
518 return 1;
519 return (ctx->extraCertsIn = X509_chain_up_ref(extraCertsIn)) != NULL;
520 }
521
522 /*
523 * Copies any given stack as the new stack of X509
524 * certificates to send out in the extraCerts field.
525 */
526 int OSSL_CMP_CTX_set1_extraCertsOut(OSSL_CMP_CTX *ctx,
527 STACK_OF(X509) *extraCertsOut)
528 {
529 if (ctx == NULL) {
530 CMPerr(0, CMP_R_NULL_ARGUMENT);
531 return 0;
532 }
533
534 sk_X509_pop_free(ctx->extraCertsOut, X509_free);
535 ctx->extraCertsOut = NULL;
536 if (extraCertsOut == NULL)
537 return 1;
538 return (ctx->extraCertsOut = X509_chain_up_ref(extraCertsOut)) != NULL;
539 }
540
541 /*
542 * Add the given policy info object
543 * to the X509_EXTENSIONS of the requested certificate template.
544 */
545 int OSSL_CMP_CTX_push0_policy(OSSL_CMP_CTX *ctx, POLICYINFO *pinfo)
546 {
547 if (ctx == NULL || pinfo == NULL) {
548 CMPerr(0, CMP_R_NULL_ARGUMENT);
549 return 0;
550 }
551
552 if (ctx->policies == NULL
553 && (ctx->policies = CERTIFICATEPOLICIES_new()) == NULL)
554 return 0;
555
556 return sk_POLICYINFO_push(ctx->policies, pinfo);
557 }
558
559 /* Add an ITAV for geninfo of the PKI message header */
560 int OSSL_CMP_CTX_push0_geninfo_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
561 {
562 if (ctx == NULL) {
563 CMPerr(0, CMP_R_NULL_ARGUMENT);
564 return 0;
565 }
566 return OSSL_CMP_ITAV_push0_stack_item(&ctx->geninfo_ITAVs, itav);
567 }
568
569 /* Add an itav for the body of outgoing general messages */
570 int OSSL_CMP_CTX_push0_genm_ITAV(OSSL_CMP_CTX *ctx, OSSL_CMP_ITAV *itav)
571 {
572 if (ctx == NULL) {
573 CMPerr(0, CMP_R_NULL_ARGUMENT);
574 return 0;
575 }
576 return OSSL_CMP_ITAV_push0_stack_item(&ctx->genm_ITAVs, itav);
577 }
578
579 /*
580 * Returns a duplicate of the stack of X509 certificates that
581 * were received in the caPubs field of the last CertRepMessage.
582 * Returns NULL on error
583 */
584 STACK_OF(X509) *OSSL_CMP_CTX_get1_caPubs(const OSSL_CMP_CTX *ctx)
585 {
586 if (ctx == NULL) {
587 CMPerr(0, CMP_R_NULL_ARGUMENT);
588 return NULL;
589 }
590 if (ctx->caPubs == NULL)
591 return sk_X509_new_null();
592 return X509_chain_up_ref(ctx->caPubs);
593 }
594
595 /*
596 * Copies any given stack of certificates to the given
597 * OSSL_CMP_CTX structure so that they may be retrieved later.
598 */
599 int ossl_cmp_ctx_set1_caPubs(OSSL_CMP_CTX *ctx, STACK_OF(X509) *caPubs)
600 {
601 if (!ossl_assert(ctx != NULL))
602 return 0;
603
604 sk_X509_pop_free(ctx->caPubs, X509_free);
605 ctx->caPubs = NULL;
606 if (caPubs == NULL)
607 return 1;
608 return (ctx->caPubs = X509_chain_up_ref(caPubs)) != NULL;
609 }
610
611 #define char_dup OPENSSL_strdup
612 #define char_free OPENSSL_free
613 #define DEFINE_OSSL_CMP_CTX_set1(FIELD, TYPE) /* this uses _dup */ \
614 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, const TYPE *val) \
615 { \
616 TYPE *val_dup = NULL; \
617 \
618 if (ctx == NULL) { \
619 CMPerr(0, CMP_R_NULL_ARGUMENT); \
620 return 0; \
621 } \
622 \
623 if (val != NULL && (val_dup = TYPE##_dup(val)) == NULL) \
624 return 0; \
625 TYPE##_free(ctx->FIELD); \
626 ctx->FIELD = val_dup; \
627 return 1; \
628 }
629
630 #define X509_invalid(cert) (!x509v3_cache_extensions(cert))
631 #define EVP_PKEY_invalid(key) 0
632 #define DEFINE_OSSL_CMP_CTX_set1_up_ref(FIELD, TYPE) \
633 int OSSL_CMP_CTX_set1_##FIELD(OSSL_CMP_CTX *ctx, TYPE *val) \
634 { \
635 if (ctx == NULL) { \
636 CMPerr(0, CMP_R_NULL_ARGUMENT); \
637 return 0; \
638 } \
639 \
640 /* prevent misleading error later on malformed cert or provider issue */ \
641 if (val != NULL && TYPE##_invalid(val)) { \
642 CMPerr(0, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); \
643 return 0; \
644 } \
645 if (val != NULL && !TYPE##_up_ref(val)) \
646 return 0; \
647 TYPE##_free(ctx->FIELD); \
648 ctx->FIELD = val; \
649 return 1; \
650 }
651
652 /*
653 * Pins the server certificate to be directly trusted (even if it is expired)
654 * for verifying response messages.
655 * Cert pointer is not consumed. It may be NULL to clear the entry.
656 */
657 DEFINE_OSSL_CMP_CTX_set1_up_ref(srvCert, X509)
658
659 /* Set the X509 name of the recipient. Set in the PKIHeader */
660 DEFINE_OSSL_CMP_CTX_set1(recipient, X509_NAME)
661
662 /* Store the X509 name of the expected sender in the PKIHeader of responses */
663 DEFINE_OSSL_CMP_CTX_set1(expected_sender, X509_NAME)
664
665 /* Set the X509 name of the issuer. Set in the PKIHeader */
666 DEFINE_OSSL_CMP_CTX_set1(issuer, X509_NAME)
667
668 /*
669 * Set the subject name that will be placed in the certificate
670 * request. This will be the subject name on the received certificate.
671 */
672 DEFINE_OSSL_CMP_CTX_set1(subjectName, X509_NAME)
673
674 /* Set the X.509v3 certificate request extensions to be used in IR/CR/KUR */
675 int OSSL_CMP_CTX_set0_reqExtensions(OSSL_CMP_CTX *ctx, X509_EXTENSIONS *exts)
676 {
677 if (ctx == NULL) {
678 CMPerr(0, CMP_R_NULL_ARGUMENT);
679 return 0;
680 }
681
682 if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 && exts != NULL
683 && X509v3_get_ext_by_NID(exts, NID_subject_alt_name, -1) >= 0) {
684 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
685 return 0;
686 }
687 sk_X509_EXTENSION_pop_free(ctx->reqExtensions, X509_EXTENSION_free);
688 ctx->reqExtensions = exts;
689 return 1;
690 }
691
692 /* returns 1 if ctx contains a Subject Alternative Name extension, else 0 */
693 int OSSL_CMP_CTX_reqExtensions_have_SAN(OSSL_CMP_CTX *ctx)
694 {
695 if (ctx == NULL) {
696 CMPerr(0, CMP_R_NULL_ARGUMENT);
697 return -1;
698 }
699 /* if one of the following conditions 'fail' this is not an error */
700 return ctx->reqExtensions != NULL
701 && X509v3_get_ext_by_NID(ctx->reqExtensions,
702 NID_subject_alt_name, -1) >= 0;
703 }
704
705 /*
706 * Add a GENERAL_NAME structure that will be added to the CRMF
707 * request's extensions field to request subject alternative names.
708 */
709 int OSSL_CMP_CTX_push1_subjectAltName(OSSL_CMP_CTX *ctx,
710 const GENERAL_NAME *name)
711 {
712 GENERAL_NAME *name_dup;
713
714 if (ctx == NULL || name == NULL) {
715 CMPerr(0, CMP_R_NULL_ARGUMENT);
716 return 0;
717 }
718
719 if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) {
720 CMPerr(0, CMP_R_MULTIPLE_SAN_SOURCES);
721 return 0;
722 }
723
724 if (ctx->subjectAltNames == NULL
725 && (ctx->subjectAltNames = sk_GENERAL_NAME_new_null()) == NULL)
726 return 0;
727 if ((name_dup = GENERAL_NAME_dup(name)) == NULL)
728 return 0;
729 if (!sk_GENERAL_NAME_push(ctx->subjectAltNames, name_dup)) {
730 GENERAL_NAME_free(name_dup);
731 return 0;
732 }
733 return 1;
734 }
735
736 /*
737 * Set our own client certificate, used for example in KUR and when
738 * doing the IR with existing certificate.
739 */
740 DEFINE_OSSL_CMP_CTX_set1_up_ref(cert, X509)
741
742 int OSSL_CMP_CTX_build_cert_chain(OSSL_CMP_CTX *ctx, X509_STORE *own_trusted,
743 STACK_OF(X509) *candidates)
744 {
745 STACK_OF(X509) *chain;
746
747 if (ctx == NULL) {
748 CMPerr(0, CMP_R_NULL_ARGUMENT);
749 return 0;
750 }
751
752 if (ctx->untrusted != NULL ?
753 !X509_add_certs(ctx->untrusted, candidates,
754 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP) :
755 !OSSL_CMP_CTX_set1_untrusted(ctx, candidates))
756 return 0;
757
758 ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert");
759 chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq, own_trusted,
760 ctx->untrusted, ctx->cert);
761 if (chain == NULL) {
762 CMPerr(0, CMP_R_FAILED_BUILDING_OWN_CHAIN);
763 return 0;
764 }
765 ossl_cmp_debug(ctx, "success building chain for own CMP signer cert");
766 ctx->chain = chain;
767 return 1;
768 }
769
770 /*
771 * Set the old certificate that we are updating in KUR
772 * or the certificate to be revoked in RR, respectively.
773 * Also used as reference cert (defaulting to cert) for deriving subject DN
774 * and SANs. Its issuer is used as default recipient in the CMP message header.
775 */
776 DEFINE_OSSL_CMP_CTX_set1_up_ref(oldCert, X509)
777
778 /* Set the PKCS#10 CSR to be sent in P10CR */
779 DEFINE_OSSL_CMP_CTX_set1(p10CSR, X509_REQ)
780
781 /*
782 * Set the (newly received in IP/KUP/CP) certificate in the context.
783 * TODO: this only permits for one cert to be enrolled at a time.
784 */
785 int ossl_cmp_ctx_set0_newCert(OSSL_CMP_CTX *ctx, X509 *cert)
786 {
787 if (!ossl_assert(ctx != NULL))
788 return 0;
789
790 X509_free(ctx->newCert);
791 ctx->newCert = cert;
792 return 1;
793 }
794
795 /*
796 * Get the (newly received in IP/KUP/CP) client certificate from the context
797 * TODO: this only permits for one client cert to be received...
798 */
799 X509 *OSSL_CMP_CTX_get0_newCert(const OSSL_CMP_CTX *ctx)
800 {
801 if (ctx == NULL) {
802 CMPerr(0, CMP_R_NULL_ARGUMENT);
803 return NULL;
804 }
805 return ctx->newCert;
806 }
807
808 /* Set the client's current private key */
809 DEFINE_OSSL_CMP_CTX_set1_up_ref(pkey, EVP_PKEY)
810
811 /* Set new key pair. Used e.g. when doing Key Update */
812 int OSSL_CMP_CTX_set0_newPkey(OSSL_CMP_CTX *ctx, int priv, EVP_PKEY *pkey)
813 {
814 if (ctx == NULL) {
815 CMPerr(0, CMP_R_NULL_ARGUMENT);
816 return 0;
817 }
818
819 EVP_PKEY_free(ctx->newPkey);
820 ctx->newPkey = pkey;
821 ctx->newPkey_priv = priv;
822 return 1;
823 }
824
825 /* Get the private/public key to use for cert enrollment, or NULL on error */
826 EVP_PKEY *OSSL_CMP_CTX_get0_newPkey(const OSSL_CMP_CTX *ctx, int priv)
827 {
828 if (ctx == NULL) {
829 CMPerr(0, CMP_R_NULL_ARGUMENT);
830 return NULL;
831 }
832
833 if (ctx->newPkey != NULL)
834 return priv && !ctx->newPkey_priv ? NULL : ctx->newPkey;
835 if (ctx->p10CSR != NULL)
836 return priv ? NULL : X509_REQ_get0_pubkey(ctx->p10CSR);
837 return ctx->pkey; /* may be NULL */
838 }
839
840 /* Set the given transactionID to the context */
841 int OSSL_CMP_CTX_set1_transactionID(OSSL_CMP_CTX *ctx,
842 const ASN1_OCTET_STRING *id)
843 {
844 if (ctx == NULL) {
845 CMPerr(0, CMP_R_NULL_ARGUMENT);
846 return 0;
847 }
848 return ossl_cmp_asn1_octet_string_set1(&ctx->transactionID, id);
849 }
850
851 /* Set the nonce to be used for the recipNonce in the message created next */
852 int ossl_cmp_ctx_set1_recipNonce(OSSL_CMP_CTX *ctx,
853 const ASN1_OCTET_STRING *nonce)
854 {
855 if (!ossl_assert(ctx != NULL))
856 return 0;
857 return ossl_cmp_asn1_octet_string_set1(&ctx->recipNonce, nonce);
858 }
859
860 /* Stores the given nonce as the last senderNonce sent out */
861 int OSSL_CMP_CTX_set1_senderNonce(OSSL_CMP_CTX *ctx,
862 const ASN1_OCTET_STRING *nonce)
863 {
864 if (ctx == NULL) {
865 CMPerr(0, CMP_R_NULL_ARGUMENT);
866 return 0;
867 }
868 return ossl_cmp_asn1_octet_string_set1(&ctx->senderNonce, nonce);
869 }
870
871 /* Set the proxy server to use for HTTP(S) connections */
872 DEFINE_OSSL_CMP_CTX_set1(proxy, char)
873
874 /* Set the (HTTP) host name of the CMP server */
875 DEFINE_OSSL_CMP_CTX_set1(server, char)
876
877 /* Set the server exclusion list of the HTTP proxy server */
878 DEFINE_OSSL_CMP_CTX_set1(no_proxy, char)
879
880 /* Set the http connect/disconnect callback function to be used for HTTP(S) */
881 int OSSL_CMP_CTX_set_http_cb(OSSL_CMP_CTX *ctx, OSSL_HTTP_bio_cb_t cb)
882 {
883 if (ctx == NULL) {
884 CMPerr(0, CMP_R_NULL_ARGUMENT);
885 return 0;
886 }
887 ctx->http_cb = cb;
888 return 1;
889 }
890
891 /* Set argument optionally to be used by the http connect/disconnect callback */
892 int OSSL_CMP_CTX_set_http_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
893 {
894 if (ctx == NULL) {
895 CMPerr(0, CMP_R_NULL_ARGUMENT);
896 return 0;
897 }
898 ctx->http_cb_arg = arg;
899 return 1;
900 }
901
902 /*
903 * Get argument optionally to be used by the http connect/disconnect callback
904 * Returns callback argument set previously (NULL if not set or on error)
905 */
906 void *OSSL_CMP_CTX_get_http_cb_arg(const OSSL_CMP_CTX *ctx)
907 {
908 if (ctx == NULL) {
909 CMPerr(0, CMP_R_NULL_ARGUMENT);
910 return NULL;
911 }
912 return ctx->http_cb_arg;
913 }
914
915 /* Set callback function for sending CMP request and receiving response */
916 int OSSL_CMP_CTX_set_transfer_cb(OSSL_CMP_CTX *ctx, OSSL_CMP_transfer_cb_t cb)
917 {
918 if (ctx == NULL) {
919 CMPerr(0, CMP_R_NULL_ARGUMENT);
920 return 0;
921 }
922 ctx->transfer_cb = cb;
923 return 1;
924 }
925
926 /* Set argument optionally to be used by the transfer callback */
927 int OSSL_CMP_CTX_set_transfer_cb_arg(OSSL_CMP_CTX *ctx, void *arg)
928 {
929 if (ctx == NULL) {
930 CMPerr(0, CMP_R_NULL_ARGUMENT);
931 return 0;
932 }
933 ctx->transfer_cb_arg = arg;
934 return 1;
935 }
936
937 /*
938 * Get argument optionally to be used by the transfer callback.
939 * Returns callback argument set previously (NULL if not set or on error)
940 */
941 void *OSSL_CMP_CTX_get_transfer_cb_arg(const OSSL_CMP_CTX *ctx)
942 {
943 if (ctx == NULL) {
944 CMPerr(0, CMP_R_NULL_ARGUMENT);
945 return NULL;
946 }
947 return ctx->transfer_cb_arg;
948 }
949
950 /** Set the HTTP server port to be used */
951 int OSSL_CMP_CTX_set_serverPort(OSSL_CMP_CTX *ctx, int port)
952 {
953 if (ctx == NULL) {
954 CMPerr(0, CMP_R_NULL_ARGUMENT);
955 return 0;
956 }
957 ctx->serverPort = port;
958 return 1;
959 }
960
961 /* Set the HTTP path to be used on the server (e.g "pkix/") */
962 DEFINE_OSSL_CMP_CTX_set1(serverPath, char)
963
964 /* Set the failInfo error code as bit encoding in OSSL_CMP_CTX */
965 int ossl_cmp_ctx_set_failInfoCode(OSSL_CMP_CTX *ctx, int fail_info)
966 {
967 if (!ossl_assert(ctx != NULL))
968 return 0;
969 ctx->failInfoCode = fail_info;
970 return 1;
971 }
972
973 /*
974 * Get the failInfo error code in OSSL_CMP_CTX as bit encoding.
975 * Returns bit string as integer on success, -1 on error
976 */
977 int OSSL_CMP_CTX_get_failInfoCode(const OSSL_CMP_CTX *ctx)
978 {
979 if (ctx == NULL) {
980 CMPerr(0, CMP_R_NULL_ARGUMENT);
981 return -1;
982 }
983 return ctx->failInfoCode;
984 }
985
986 /* Set a Boolean or integer option of the context to the "val" arg */
987 int OSSL_CMP_CTX_set_option(OSSL_CMP_CTX *ctx, int opt, int val)
988 {
989 int min_val;
990
991 if (ctx == NULL) {
992 CMPerr(0, CMP_R_NULL_ARGUMENT);
993 return 0;
994 }
995
996 switch (opt) {
997 case OSSL_CMP_OPT_REVOCATION_REASON:
998 min_val = OCSP_REVOKED_STATUS_NOSTATUS;
999 break;
1000 case OSSL_CMP_OPT_POPO_METHOD:
1001 min_val = OSSL_CRMF_POPO_NONE;
1002 break;
1003 default:
1004 min_val = 0;
1005 break;
1006 }
1007 if (val < min_val) {
1008 CMPerr(0, CMP_R_VALUE_TOO_SMALL);
1009 return 0;
1010 }
1011
1012 switch (opt) {
1013 case OSSL_CMP_OPT_LOG_VERBOSITY:
1014 if (val > OSSL_CMP_LOG_MAX) {
1015 CMPerr(0, CMP_R_VALUE_TOO_LARGE);
1016 return 0;
1017 }
1018 ctx->log_verbosity = val;
1019 break;
1020 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1021 ctx->implicitConfirm = val;
1022 break;
1023 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1024 ctx->disableConfirm = val;
1025 break;
1026 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1027 ctx->unprotectedSend = val;
1028 break;
1029 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1030 ctx->unprotectedErrors = val;
1031 break;
1032 case OSSL_CMP_OPT_VALIDITY_DAYS:
1033 ctx->days = val;
1034 break;
1035 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1036 ctx->SubjectAltName_nodefault = val;
1037 break;
1038 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1039 ctx->setSubjectAltNameCritical = val;
1040 break;
1041 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1042 ctx->setPoliciesCritical = val;
1043 break;
1044 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1045 ctx->ignore_keyusage = val;
1046 break;
1047 case OSSL_CMP_OPT_POPO_METHOD:
1048 if (val > OSSL_CRMF_POPO_KEYAGREE) {
1049 CMPerr(0, CMP_R_VALUE_TOO_LARGE);
1050 return 0;
1051 }
1052 ctx->popoMethod = val;
1053 break;
1054 case OSSL_CMP_OPT_DIGEST_ALGNID:
1055 if (!cmp_ctx_set_md(ctx, &ctx->digest, val))
1056 return 0;
1057 break;
1058 case OSSL_CMP_OPT_OWF_ALGNID:
1059 if (!cmp_ctx_set_md(ctx, &ctx->pbm_owf, val))
1060 return 0;
1061 break;
1062 case OSSL_CMP_OPT_MAC_ALGNID:
1063 ctx->pbm_mac = val;
1064 break;
1065 case OSSL_CMP_OPT_MSG_TIMEOUT:
1066 ctx->msg_timeout = val;
1067 break;
1068 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1069 ctx->total_timeout = val;
1070 break;
1071 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1072 ctx->permitTAInExtraCertsForIR = val;
1073 break;
1074 case OSSL_CMP_OPT_REVOCATION_REASON:
1075 if (val > OCSP_REVOKED_STATUS_AACOMPROMISE) {
1076 CMPerr(0, CMP_R_VALUE_TOO_LARGE);
1077 return 0;
1078 }
1079 ctx->revocationReason = val;
1080 break;
1081 default:
1082 CMPerr(0, CMP_R_INVALID_OPTION);
1083 return 0;
1084 }
1085
1086 return 1;
1087 }
1088
1089 /*
1090 * Reads a Boolean or integer option value from the context.
1091 * Returns -1 on error (which is the default OSSL_CMP_OPT_REVOCATION_REASON)
1092 */
1093 int OSSL_CMP_CTX_get_option(const OSSL_CMP_CTX *ctx, int opt)
1094 {
1095 if (ctx == NULL) {
1096 CMPerr(0, CMP_R_NULL_ARGUMENT);
1097 return -1;
1098 }
1099
1100 switch (opt) {
1101 case OSSL_CMP_OPT_LOG_VERBOSITY:
1102 return ctx->log_verbosity;
1103 case OSSL_CMP_OPT_IMPLICIT_CONFIRM:
1104 return ctx->implicitConfirm;
1105 case OSSL_CMP_OPT_DISABLE_CONFIRM:
1106 return ctx->disableConfirm;
1107 case OSSL_CMP_OPT_UNPROTECTED_SEND:
1108 return ctx->unprotectedSend;
1109 case OSSL_CMP_OPT_UNPROTECTED_ERRORS:
1110 return ctx->unprotectedErrors;
1111 case OSSL_CMP_OPT_VALIDITY_DAYS:
1112 return ctx->days;
1113 case OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT:
1114 return ctx->SubjectAltName_nodefault;
1115 case OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL:
1116 return ctx->setSubjectAltNameCritical;
1117 case OSSL_CMP_OPT_POLICIES_CRITICAL:
1118 return ctx->setPoliciesCritical;
1119 case OSSL_CMP_OPT_IGNORE_KEYUSAGE:
1120 return ctx->ignore_keyusage;
1121 case OSSL_CMP_OPT_POPO_METHOD:
1122 return ctx->popoMethod;
1123 case OSSL_CMP_OPT_DIGEST_ALGNID:
1124 return EVP_MD_type(ctx->digest);
1125 case OSSL_CMP_OPT_OWF_ALGNID:
1126 return EVP_MD_type(ctx->pbm_owf);
1127 case OSSL_CMP_OPT_MAC_ALGNID:
1128 return ctx->pbm_mac;
1129 case OSSL_CMP_OPT_MSG_TIMEOUT:
1130 return ctx->msg_timeout;
1131 case OSSL_CMP_OPT_TOTAL_TIMEOUT:
1132 return ctx->total_timeout;
1133 case OSSL_CMP_OPT_PERMIT_TA_IN_EXTRACERTS_FOR_IR:
1134 return ctx->permitTAInExtraCertsForIR;
1135 case OSSL_CMP_OPT_REVOCATION_REASON:
1136 return ctx->revocationReason;
1137 default:
1138 CMPerr(0, CMP_R_INVALID_OPTION);
1139 return -1;
1140 }
1141 }