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