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