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