/src/openssl34/crypto/cmp/cmp_client.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2007-2025 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 "internal/cryptlib.h" |
14 | | |
15 | | /* explicit #includes not strictly needed since implied by the above: */ |
16 | | #include <openssl/bio.h> |
17 | | #include <openssl/cmp.h> |
18 | | #include <openssl/err.h> |
19 | | #include <openssl/evp.h> |
20 | | #include <openssl/x509v3.h> |
21 | | #include <openssl/cmp_util.h> |
22 | | |
23 | 0 | #define IS_CREP(t) ((t) == OSSL_CMP_PKIBODY_IP || (t) == OSSL_CMP_PKIBODY_CP \ |
24 | 0 | || (t) == OSSL_CMP_PKIBODY_KUP) |
25 | | |
26 | | /*- |
27 | | * Evaluate whether there's an exception (violating the standard) configured for |
28 | | * handling negative responses without protection or with invalid protection. |
29 | | * Returns 1 on acceptance, 0 on rejection, or -1 on (internal) error. |
30 | | */ |
31 | | static int unprotected_exception(const OSSL_CMP_CTX *ctx, |
32 | | const OSSL_CMP_MSG *rep, |
33 | | int invalid_protection, |
34 | | ossl_unused int expected_type) |
35 | 0 | { |
36 | 0 | int rcvd_type = OSSL_CMP_MSG_get_bodytype(rep /* may be NULL */); |
37 | 0 | const char *msg_type = NULL; |
38 | |
|
39 | 0 | if (!ossl_assert(ctx != NULL && rep != NULL)) |
40 | 0 | return -1; |
41 | | |
42 | 0 | if (!ctx->unprotectedErrors) |
43 | 0 | return 0; |
44 | | |
45 | 0 | switch (rcvd_type) { |
46 | 0 | case OSSL_CMP_PKIBODY_ERROR: |
47 | 0 | msg_type = "error response"; |
48 | 0 | break; |
49 | 0 | case OSSL_CMP_PKIBODY_RP: |
50 | 0 | { |
51 | 0 | OSSL_CMP_PKISI *si = |
52 | 0 | ossl_cmp_revrepcontent_get_pkisi(rep->body->value.rp, |
53 | 0 | OSSL_CMP_REVREQSID); |
54 | |
|
55 | 0 | if (si == NULL) |
56 | 0 | return -1; |
57 | 0 | if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_rejection) |
58 | 0 | msg_type = "revocation response message with rejection status"; |
59 | 0 | break; |
60 | 0 | } |
61 | 0 | case OSSL_CMP_PKIBODY_PKICONF: |
62 | 0 | msg_type = "PKI Confirmation message"; |
63 | 0 | break; |
64 | 0 | default: |
65 | 0 | if (IS_CREP(rcvd_type)) { |
66 | 0 | int any_rid = OSSL_CMP_CERTREQID_NONE; |
67 | 0 | OSSL_CMP_CERTREPMESSAGE *crepmsg = rep->body->value.ip; |
68 | 0 | OSSL_CMP_CERTRESPONSE *crep = |
69 | 0 | ossl_cmp_certrepmessage_get0_certresponse(crepmsg, any_rid); |
70 | |
|
71 | 0 | if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) |
72 | 0 | return -1; |
73 | 0 | if (crep == NULL) |
74 | 0 | return -1; |
75 | 0 | if (ossl_cmp_pkisi_get_status(crep->status) |
76 | 0 | == OSSL_CMP_PKISTATUS_rejection) |
77 | 0 | msg_type = "CertRepMessage with rejection status"; |
78 | 0 | } |
79 | 0 | } |
80 | 0 | if (msg_type == NULL) |
81 | 0 | return 0; |
82 | 0 | ossl_cmp_log2(WARN, ctx, "ignoring %s protection of %s", |
83 | 0 | invalid_protection ? "invalid" : "missing", msg_type); |
84 | 0 | return 1; |
85 | 0 | } |
86 | | |
87 | | /* Save error info from PKIStatusInfo field of a certresponse into ctx */ |
88 | | static int save_statusInfo(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si) |
89 | 0 | { |
90 | 0 | int i; |
91 | 0 | OSSL_CMP_PKIFREETEXT *ss; |
92 | |
|
93 | 0 | if (!ossl_assert(ctx != NULL && si != NULL)) |
94 | 0 | return 0; |
95 | | |
96 | 0 | ctx->status = ossl_cmp_pkisi_get_status(si); |
97 | 0 | if (ctx->status < OSSL_CMP_PKISTATUS_accepted) |
98 | 0 | return 0; |
99 | | |
100 | 0 | ctx->failInfoCode = ossl_cmp_pkisi_get_pkifailureinfo(si); |
101 | |
|
102 | 0 | if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null()) |
103 | 0 | || (ctx->statusString == NULL)) |
104 | 0 | return 0; |
105 | | |
106 | 0 | ss = si->statusString; /* may be NULL */ |
107 | 0 | for (i = 0; i < sk_ASN1_UTF8STRING_num(ss); i++) { |
108 | 0 | ASN1_UTF8STRING *str = sk_ASN1_UTF8STRING_value(ss, i); |
109 | 0 | ASN1_UTF8STRING *dup = ASN1_STRING_dup(str); |
110 | |
|
111 | 0 | if (dup == NULL || !sk_ASN1_UTF8STRING_push(ctx->statusString, dup)) { |
112 | 0 | ASN1_UTF8STRING_free(dup); |
113 | 0 | return 0; |
114 | 0 | } |
115 | 0 | } |
116 | 0 | return 1; |
117 | 0 | } |
118 | | |
119 | | static int is_crep_with_waiting(const OSSL_CMP_MSG *resp, int rid) |
120 | 0 | { |
121 | 0 | OSSL_CMP_CERTREPMESSAGE *crepmsg; |
122 | 0 | OSSL_CMP_CERTRESPONSE *crep; |
123 | 0 | int bt = OSSL_CMP_MSG_get_bodytype(resp); |
124 | |
|
125 | 0 | if (!IS_CREP(bt)) |
126 | 0 | return 0; |
127 | | |
128 | 0 | crepmsg = resp->body->value.ip; /* same for cp and kup */ |
129 | 0 | crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid); |
130 | |
|
131 | 0 | return (crep != NULL |
132 | 0 | && ossl_cmp_pkisi_get_status(crep->status) |
133 | 0 | == OSSL_CMP_PKISTATUS_waiting); |
134 | 0 | } |
135 | | |
136 | | /*- |
137 | | * Perform the generic aspects of sending a request and receiving a response. |
138 | | * Returns 1 on success and provides the received PKIMESSAGE in *rep. |
139 | | * Returns 0 on error. |
140 | | * Regardless of success, caller is responsible for freeing *rep (unless NULL). |
141 | | */ |
142 | | static int send_receive_check(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req, |
143 | | OSSL_CMP_MSG **rep, int expected_type) |
144 | 0 | { |
145 | 0 | int begin_transaction = |
146 | 0 | expected_type != OSSL_CMP_PKIBODY_POLLREP |
147 | 0 | && expected_type != OSSL_CMP_PKIBODY_PKICONF; |
148 | 0 | const char *req_type_str = |
149 | 0 | ossl_cmp_bodytype_to_string(OSSL_CMP_MSG_get_bodytype(req)); |
150 | 0 | const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type); |
151 | 0 | int bak_msg_timeout = ctx->msg_timeout; |
152 | 0 | int bt; |
153 | 0 | time_t now = time(NULL); |
154 | 0 | int time_left; |
155 | 0 | OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb; |
156 | |
|
157 | 0 | #ifndef OPENSSL_NO_HTTP |
158 | 0 | if (transfer_cb == NULL) |
159 | 0 | transfer_cb = OSSL_CMP_MSG_http_perform; |
160 | 0 | #endif |
161 | 0 | *rep = NULL; |
162 | |
|
163 | 0 | if (ctx->total_timeout != 0 /* not waiting indefinitely */) { |
164 | 0 | if (begin_transaction) |
165 | 0 | ctx->end_time = now + ctx->total_timeout; |
166 | 0 | if (now >= ctx->end_time) { |
167 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT); |
168 | 0 | return 0; |
169 | 0 | } |
170 | 0 | if (!ossl_assert(ctx->end_time - now < INT_MAX)) { |
171 | | /* actually cannot happen due to assignment in initial_certreq() */ |
172 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
173 | 0 | return 0; |
174 | 0 | } |
175 | 0 | time_left = (int)(ctx->end_time - now); |
176 | 0 | if (ctx->msg_timeout == 0 || time_left < ctx->msg_timeout) |
177 | 0 | ctx->msg_timeout = time_left; |
178 | 0 | } |
179 | | |
180 | | /* should print error queue since transfer_cb may call ERR_clear_error() */ |
181 | 0 | OSSL_CMP_CTX_print_errors(ctx); |
182 | |
|
183 | 0 | if (ctx->server != NULL) |
184 | 0 | ossl_cmp_log1(INFO, ctx, "sending %s", req_type_str); |
185 | |
|
186 | 0 | *rep = (*transfer_cb)(ctx, req); |
187 | 0 | ctx->msg_timeout = bak_msg_timeout; |
188 | |
|
189 | 0 | if (*rep == NULL) { |
190 | 0 | ERR_raise_data(ERR_LIB_CMP, |
191 | 0 | ctx->total_timeout != 0 && time(NULL) >= ctx->end_time ? |
192 | 0 | CMP_R_TOTAL_TIMEOUT : CMP_R_TRANSFER_ERROR, |
193 | 0 | "request sent: %s, expected response: %s", |
194 | 0 | req_type_str, expected_type_str); |
195 | 0 | return 0; |
196 | 0 | } |
197 | | |
198 | 0 | bt = OSSL_CMP_MSG_get_bodytype(*rep); |
199 | | /* |
200 | | * The body type in the 'bt' variable is not yet verified. |
201 | | * Still we use this preliminary value already for a progress report because |
202 | | * the following msg verification may also produce log entries and may fail. |
203 | | */ |
204 | 0 | ossl_cmp_log2(INFO, ctx, "received %s%s", ossl_cmp_bodytype_to_string(bt), |
205 | 0 | ossl_cmp_is_error_with_waiting(*rep) ? " (waiting)" : ""); |
206 | | |
207 | | /* copy received extraCerts to ctx->extraCertsIn so they can be retrieved */ |
208 | 0 | if (bt != OSSL_CMP_PKIBODY_POLLREP && bt != OSSL_CMP_PKIBODY_PKICONF |
209 | 0 | && !ossl_cmp_ctx_set1_extraCertsIn(ctx, (*rep)->extraCerts)) |
210 | 0 | return 0; |
211 | | |
212 | 0 | if (!ossl_cmp_msg_check_update(ctx, *rep, unprotected_exception, |
213 | 0 | expected_type)) |
214 | 0 | return 0; |
215 | | |
216 | | /* |
217 | | * rep can have the expected response type, which during polling is pollRep. |
218 | | * When polling, also any other non-error response (the final response) |
219 | | * is fine here. When not yet polling, delayed delivery may be initiated |
220 | | * by the server returning an error message with 'waiting' status (or a |
221 | | * response message of expected type ip/cp/kup with 'waiting' status). |
222 | | */ |
223 | 0 | if (bt == expected_type |
224 | 0 | || (expected_type == OSSL_CMP_PKIBODY_POLLREP |
225 | 0 | ? bt != OSSL_CMP_PKIBODY_ERROR |
226 | 0 | : ossl_cmp_is_error_with_waiting(*rep))) |
227 | 0 | return 1; |
228 | | |
229 | | /* received message type is not one of the expected ones (e.g., error) */ |
230 | 0 | ERR_raise(ERR_LIB_CMP, bt == OSSL_CMP_PKIBODY_ERROR ? CMP_R_RECEIVED_ERROR : |
231 | 0 | CMP_R_UNEXPECTED_PKIBODY); /* in next line for mkerr.pl */ |
232 | |
|
233 | 0 | if (bt != OSSL_CMP_PKIBODY_ERROR) { |
234 | 0 | ERR_add_error_data(3, "message type is '", |
235 | 0 | ossl_cmp_bodytype_to_string(bt), "'"); |
236 | 0 | } else { |
237 | 0 | OSSL_CMP_ERRORMSGCONTENT *emc = (*rep)->body->value.error; |
238 | 0 | OSSL_CMP_PKISI *si = emc->pKIStatusInfo; |
239 | 0 | char buf[OSSL_CMP_PKISI_BUFLEN]; |
240 | |
|
241 | 0 | if (save_statusInfo(ctx, si) |
242 | 0 | && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, |
243 | 0 | sizeof(buf)) != NULL) |
244 | 0 | ERR_add_error_data(1, buf); |
245 | 0 | if (emc->errorCode != NULL |
246 | 0 | && BIO_snprintf(buf, sizeof(buf), "; errorCode: %08lX", |
247 | 0 | ASN1_INTEGER_get(emc->errorCode)) > 0) |
248 | 0 | ERR_add_error_data(1, buf); |
249 | 0 | if (emc->errorDetails != NULL) { |
250 | 0 | char *text = ossl_sk_ASN1_UTF8STRING2text(emc->errorDetails, ", ", |
251 | 0 | OSSL_CMP_PKISI_BUFLEN - 1); |
252 | |
|
253 | 0 | if (text != NULL && *text != '\0') |
254 | 0 | ERR_add_error_data(2, "; errorDetails: ", text); |
255 | 0 | OPENSSL_free(text); |
256 | 0 | } |
257 | 0 | if (ctx->status != OSSL_CMP_PKISTATUS_rejection) { |
258 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS); |
259 | 0 | if (ctx->status == OSSL_CMP_PKISTATUS_waiting) |
260 | 0 | ctx->status = OSSL_CMP_PKISTATUS_rejection; |
261 | 0 | } |
262 | 0 | } |
263 | 0 | return 0; |
264 | 0 | } |
265 | | |
266 | | /*- |
267 | | * When a 'waiting' PKIStatus has been received, this function is used to |
268 | | * poll, which should yield a pollRep or the final response. |
269 | | * On receiving a pollRep, which includes a checkAfter value, it return this |
270 | | * value if sleep == 0, else it sleeps as long as indicated and retries. |
271 | | * |
272 | | * A transaction timeout is enabled if ctx->total_timeout is != 0. |
273 | | * In this case polling will continue until the timeout is reached and then |
274 | | * polling is done a last time even if this is before the "checkAfter" time. |
275 | | * |
276 | | * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value. |
277 | | * Returns 1 on success and provides the received PKIMESSAGE in *rep. |
278 | | * In this case the caller is responsible for freeing *rep. |
279 | | * Returns 0 on error (which includes the cases that timeout has been reached |
280 | | * or a response with 'waiting' status has been received). |
281 | | */ |
282 | | static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid, |
283 | | OSSL_CMP_MSG **rep, int *checkAfter) |
284 | 110 | { |
285 | 110 | OSSL_CMP_MSG *preq = NULL; |
286 | 110 | OSSL_CMP_MSG *prep = NULL; |
287 | | |
288 | 110 | ossl_cmp_info(ctx, |
289 | 110 | "received 'waiting' PKIStatus, starting to poll for response"); |
290 | 110 | *rep = NULL; |
291 | 110 | for (;;) { |
292 | 110 | if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL) |
293 | 110 | goto err; |
294 | | |
295 | 0 | if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP)) |
296 | 0 | goto err; |
297 | | |
298 | | /* handle potential pollRep */ |
299 | 0 | if (OSSL_CMP_MSG_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) { |
300 | 0 | OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep; |
301 | 0 | OSSL_CMP_POLLREP *pollRep = NULL; |
302 | 0 | int64_t check_after; |
303 | 0 | char str[OSSL_CMP_PKISI_BUFLEN]; |
304 | 0 | int len; |
305 | |
|
306 | 0 | if (sk_OSSL_CMP_POLLREP_num(prc) > 1) { |
307 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED); |
308 | 0 | goto err; |
309 | 0 | } |
310 | 0 | pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid); |
311 | 0 | if (pollRep == NULL) |
312 | 0 | goto err; |
313 | | |
314 | 0 | if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) { |
315 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_BAD_CHECKAFTER_IN_POLLREP); |
316 | 0 | goto err; |
317 | 0 | } |
318 | 0 | if (check_after < 0 || (uint64_t)check_after |
319 | 0 | > (sleep ? ULONG_MAX / 1000 : INT_MAX)) { |
320 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CHECKAFTER_OUT_OF_RANGE); |
321 | 0 | if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd", |
322 | 0 | check_after) >= 0) |
323 | 0 | ERR_add_error_data(1, str); |
324 | 0 | goto err; |
325 | 0 | } |
326 | | |
327 | 0 | if (pollRep->reason == NULL |
328 | 0 | || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, |
329 | 0 | " with reason = '")) < 0) { |
330 | 0 | *str = '\0'; |
331 | 0 | } else { |
332 | 0 | char *text = ossl_sk_ASN1_UTF8STRING2text(pollRep->reason, ", ", |
333 | 0 | sizeof(str) - len - 2); |
334 | |
|
335 | 0 | if (text == NULL |
336 | 0 | || BIO_snprintf(str + len, sizeof(str) - len, |
337 | 0 | "%s'", text) < 0) |
338 | 0 | *str = '\0'; |
339 | 0 | OPENSSL_free(text); |
340 | 0 | } |
341 | 0 | ossl_cmp_log2(INFO, ctx, |
342 | 0 | "received polling response%s; checkAfter = %ld seconds", |
343 | 0 | str, check_after); |
344 | |
|
345 | 0 | if (ctx->total_timeout != 0) { /* timeout is not infinite */ |
346 | 0 | const int exp = OSSL_CMP_EXPECTED_RESP_TIME; |
347 | 0 | int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL)); |
348 | |
|
349 | 0 | if (time_left <= 0) { |
350 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT); |
351 | 0 | goto err; |
352 | 0 | } |
353 | 0 | if (time_left < check_after) |
354 | 0 | check_after = time_left; |
355 | | /* poll one last time just when timeout was reached */ |
356 | 0 | } |
357 | | |
358 | 0 | OSSL_CMP_MSG_free(preq); |
359 | 0 | preq = NULL; |
360 | 0 | OSSL_CMP_MSG_free(prep); |
361 | 0 | prep = NULL; |
362 | 0 | if (sleep) { |
363 | 0 | OSSL_sleep((unsigned long)(1000 * check_after)); |
364 | 0 | } else { |
365 | 0 | if (checkAfter != NULL) |
366 | 0 | *checkAfter = (int)check_after; |
367 | 0 | return -1; /* exits the loop */ |
368 | 0 | } |
369 | 0 | } else if (is_crep_with_waiting(prep, rid) |
370 | 0 | || ossl_cmp_is_error_with_waiting(prep)) { |
371 | | /* received status must not be 'waiting' */ |
372 | 0 | (void)ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection, |
373 | 0 | OSSL_CMP_CTX_FAILINFO_badRequest, |
374 | 0 | "polling already started", |
375 | 0 | 0 /* errorCode */, NULL); |
376 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS); |
377 | 0 | goto err; |
378 | 0 | } else { |
379 | 0 | ossl_cmp_info(ctx, "received final response after polling"); |
380 | 0 | if (!ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL)) |
381 | 0 | return 0; |
382 | 0 | break; |
383 | 0 | } |
384 | 0 | } |
385 | 0 | if (prep == NULL) |
386 | 0 | goto err; |
387 | | |
388 | 0 | OSSL_CMP_MSG_free(preq); |
389 | 0 | *rep = prep; |
390 | |
|
391 | 0 | return 1; |
392 | 110 | err: |
393 | 110 | (void)ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL); |
394 | 110 | OSSL_CMP_MSG_free(preq); |
395 | 110 | OSSL_CMP_MSG_free(prep); |
396 | 110 | return 0; |
397 | 0 | } |
398 | | |
399 | | static int save_senderNonce_if_waiting(OSSL_CMP_CTX *ctx, |
400 | | const OSSL_CMP_MSG *rep, int rid) |
401 | 0 | { |
402 | | /* |
403 | | * Lightweight CMP Profile section 4.4 states: the senderNonce of the |
404 | | * preceding request message because this value will be needed for checking |
405 | | * the recipNonce of the final response to be received after polling. |
406 | | */ |
407 | 0 | if ((is_crep_with_waiting(rep, rid) |
408 | 0 | || ossl_cmp_is_error_with_waiting(rep)) |
409 | 0 | && !ossl_cmp_ctx_set1_first_senderNonce(ctx, ctx->senderNonce)) |
410 | 0 | return 0; |
411 | | |
412 | 0 | return 1; |
413 | 0 | } |
414 | | |
415 | | /* |
416 | | * Send request and get response possibly with polling initiated by error msg. |
417 | | * Polling for ip/cp/kup/ with 'waiting' status is handled by cert_response(). |
418 | | */ |
419 | | static int send_receive_also_delayed(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req, |
420 | | OSSL_CMP_MSG **rep, int expected_type) |
421 | 0 | { |
422 | |
|
423 | 0 | if (!send_receive_check(ctx, req, rep, expected_type)) |
424 | 0 | return 0; |
425 | | |
426 | 0 | if (ossl_cmp_is_error_with_waiting(*rep)) { |
427 | 0 | if (!save_senderNonce_if_waiting(ctx, *rep, OSSL_CMP_CERTREQID_NONE)) |
428 | 0 | return 0; |
429 | | /* not modifying ctx->status during certConf and error exchanges */ |
430 | 0 | if (expected_type != OSSL_CMP_PKIBODY_PKICONF |
431 | 0 | && !save_statusInfo(ctx, (*rep)->body->value.error->pKIStatusInfo)) |
432 | 0 | return 0; |
433 | | |
434 | 0 | OSSL_CMP_MSG_free(*rep); |
435 | 0 | *rep = NULL; |
436 | |
|
437 | 0 | if (poll_for_response(ctx, 1 /* can sleep */, OSSL_CMP_CERTREQID_NONE, |
438 | 0 | rep, NULL /* checkAfter */) <= 0) { |
439 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED); |
440 | 0 | return 0; |
441 | 0 | } |
442 | 0 | } |
443 | 0 | if (OSSL_CMP_MSG_get_bodytype(*rep) != expected_type) { |
444 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
445 | 0 | return 0; |
446 | 0 | } |
447 | | |
448 | 0 | return 1; |
449 | 0 | } |
450 | | /* |
451 | | * Send certConf for IR, CR or KUR sequences and check response, |
452 | | * not modifying ctx->status during the certConf exchange |
453 | | */ |
454 | | int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int certReqId, |
455 | | int fail_info, const char *txt) |
456 | 0 | { |
457 | 0 | OSSL_CMP_MSG *certConf; |
458 | 0 | OSSL_CMP_MSG *PKIconf = NULL; |
459 | 0 | int res = 0; |
460 | | |
461 | | /* OSSL_CMP_certConf_new() also checks if all necessary options are set */ |
462 | 0 | certConf = ossl_cmp_certConf_new(ctx, certReqId, fail_info, txt); |
463 | 0 | if (certConf == NULL) |
464 | 0 | goto err; |
465 | | |
466 | 0 | res = send_receive_also_delayed(ctx, certConf, &PKIconf, |
467 | 0 | OSSL_CMP_PKIBODY_PKICONF); |
468 | |
|
469 | 0 | err: |
470 | 0 | OSSL_CMP_MSG_free(certConf); |
471 | 0 | OSSL_CMP_MSG_free(PKIconf); |
472 | 0 | return res; |
473 | 0 | } |
474 | | |
475 | | /* Send given error and check response */ |
476 | | int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info, |
477 | | const char *txt, int errorCode, const char *details) |
478 | 0 | { |
479 | 0 | OSSL_CMP_MSG *error = NULL; |
480 | 0 | OSSL_CMP_PKISI *si = NULL; |
481 | 0 | OSSL_CMP_MSG *PKIconf = NULL; |
482 | 0 | int res = 0; |
483 | | |
484 | | /* not overwriting ctx->status on error exchange */ |
485 | 0 | if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt)) == NULL) |
486 | 0 | goto err; |
487 | | /* ossl_cmp_error_new() also checks if all necessary options are set */ |
488 | 0 | if ((error = ossl_cmp_error_new(ctx, si, errorCode, details, 0)) == NULL) |
489 | 0 | goto err; |
490 | | |
491 | 0 | res = send_receive_also_delayed(ctx, error, |
492 | 0 | &PKIconf, OSSL_CMP_PKIBODY_PKICONF); |
493 | |
|
494 | 0 | err: |
495 | 0 | OSSL_CMP_MSG_free(error); |
496 | 0 | OSSL_CMP_PKISI_free(si); |
497 | 0 | OSSL_CMP_MSG_free(PKIconf); |
498 | 0 | return res; |
499 | 0 | } |
500 | | |
501 | | /*- |
502 | | * Retrieve a copy of the certificate, if any, from the given CertResponse. |
503 | | * Take into account PKIStatusInfo of CertResponse in ctx, report it on error. |
504 | | * Returns NULL if not found or on error. |
505 | | */ |
506 | | static X509 *get1_cert_status(OSSL_CMP_CTX *ctx, int bodytype, |
507 | | OSSL_CMP_CERTRESPONSE *crep) |
508 | 0 | { |
509 | 0 | char buf[OSSL_CMP_PKISI_BUFLEN]; |
510 | 0 | X509 *crt = NULL; |
511 | |
|
512 | 0 | if (!ossl_assert(ctx != NULL && crep != NULL)) |
513 | 0 | return NULL; |
514 | | |
515 | 0 | switch (ossl_cmp_pkisi_get_status(crep->status)) { |
516 | 0 | case OSSL_CMP_PKISTATUS_waiting: |
517 | 0 | ossl_cmp_err(ctx, |
518 | 0 | "received \"waiting\" status for cert when actually aiming to extract cert"); |
519 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_WAITING); |
520 | 0 | goto err; |
521 | 0 | case OSSL_CMP_PKISTATUS_grantedWithMods: |
522 | 0 | ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate"); |
523 | 0 | break; |
524 | 0 | case OSSL_CMP_PKISTATUS_accepted: |
525 | 0 | break; |
526 | | /* get all information in case of a rejection before going to error */ |
527 | 0 | case OSSL_CMP_PKISTATUS_rejection: |
528 | 0 | ossl_cmp_err(ctx, "received \"rejection\" status rather than cert"); |
529 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER); |
530 | 0 | goto err; |
531 | 0 | case OSSL_CMP_PKISTATUS_revocationWarning: |
532 | 0 | ossl_cmp_warn(ctx, |
533 | 0 | "received \"revocationWarning\" - a revocation of the cert is imminent"); |
534 | 0 | break; |
535 | 0 | case OSSL_CMP_PKISTATUS_revocationNotification: |
536 | 0 | ossl_cmp_warn(ctx, |
537 | 0 | "received \"revocationNotification\" - a revocation of the cert has occurred"); |
538 | 0 | break; |
539 | 0 | case OSSL_CMP_PKISTATUS_keyUpdateWarning: |
540 | 0 | if (bodytype != OSSL_CMP_PKIBODY_KUR) { |
541 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_KEYUPDATEWARNING); |
542 | 0 | goto err; |
543 | 0 | } |
544 | 0 | break; |
545 | 0 | default: |
546 | 0 | ossl_cmp_log1(ERROR, ctx, |
547 | 0 | "received unsupported PKIStatus %d for certificate", |
548 | 0 | ctx->status); |
549 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS); |
550 | 0 | goto err; |
551 | 0 | } |
552 | 0 | crt = ossl_cmp_certresponse_get1_cert(ctx, crep); |
553 | 0 | if (crt == NULL) /* according to PKIStatus, we can expect a cert */ |
554 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND); |
555 | |
|
556 | 0 | return crt; |
557 | | |
558 | 0 | err: |
559 | 0 | if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL) |
560 | 0 | ERR_add_error_data(1, buf); |
561 | 0 | return NULL; |
562 | 0 | } |
563 | | |
564 | | /*- |
565 | | * Callback fn validating that the new certificate can be verified, using |
566 | | * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and |
567 | | * ctx->untrusted, which at this point already contains msg->extraCerts. |
568 | | * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo. |
569 | | * Quoting from RFC 4210 section 5.1. Overall PKI Message: |
570 | | * The extraCerts field can contain certificates that may be useful to |
571 | | * the recipient. For example, this can be used by a CA or RA to |
572 | | * present an end entity with certificates that it needs to verify its |
573 | | * own new certificate (if, for example, the CA that issued the end |
574 | | * entity's certificate is not a root CA for the end entity). Note that |
575 | | * this field does not necessarily contain a certification path; the |
576 | | * recipient may have to sort, select from, or otherwise process the |
577 | | * extra certificates in order to use them. |
578 | | * Note: While often handy, there is no hard requirement by CMP that |
579 | | * an EE must be able to validate the certificates it gets enrolled. |
580 | | */ |
581 | | int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info, |
582 | | const char **text) |
583 | 0 | { |
584 | 0 | X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx); |
585 | 0 | STACK_OF(X509) *chain = NULL; |
586 | |
|
587 | 0 | (void)text; /* make (artificial) use of var to prevent compiler warning */ |
588 | |
|
589 | 0 | if (fail_info != 0) /* accept any error flagged by CMP core library */ |
590 | 0 | return fail_info; |
591 | | |
592 | 0 | if (out_trusted == NULL) { |
593 | 0 | ossl_cmp_debug(ctx, "trying to build chain for newly enrolled cert"); |
594 | 0 | chain = X509_build_chain(cert, ctx->untrusted, out_trusted, |
595 | 0 | 0, ctx->libctx, ctx->propq); |
596 | 0 | } else { |
597 | 0 | X509_STORE_CTX *csc = X509_STORE_CTX_new_ex(ctx->libctx, ctx->propq); |
598 | |
|
599 | 0 | ossl_cmp_debug(ctx, "validating newly enrolled cert"); |
600 | 0 | if (csc == NULL) |
601 | 0 | goto err; |
602 | 0 | if (!X509_STORE_CTX_init(csc, out_trusted, cert, ctx->untrusted)) |
603 | 0 | goto err; |
604 | | /* disable any cert status/revocation checking etc. */ |
605 | 0 | X509_VERIFY_PARAM_clear_flags(X509_STORE_CTX_get0_param(csc), |
606 | 0 | ~(X509_V_FLAG_USE_CHECK_TIME |
607 | 0 | | X509_V_FLAG_NO_CHECK_TIME |
608 | 0 | | X509_V_FLAG_PARTIAL_CHAIN |
609 | 0 | | X509_V_FLAG_POLICY_CHECK)); |
610 | 0 | if (X509_verify_cert(csc) <= 0) |
611 | 0 | goto err; |
612 | | |
613 | 0 | if (!ossl_x509_add_certs_new(&chain, X509_STORE_CTX_get0_chain(csc), |
614 | 0 | X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP |
615 | 0 | | X509_ADD_FLAG_NO_SS)) { |
616 | 0 | sk_X509_free(chain); |
617 | 0 | chain = NULL; |
618 | 0 | } |
619 | 0 | err: |
620 | 0 | X509_STORE_CTX_free(csc); |
621 | 0 | } |
622 | | |
623 | 0 | if (sk_X509_num(chain) > 0) |
624 | 0 | X509_free(sk_X509_shift(chain)); /* remove leaf (EE) cert */ |
625 | 0 | if (out_trusted != NULL) { |
626 | 0 | if (chain == NULL) { |
627 | 0 | ossl_cmp_err(ctx, "failed to validate newly enrolled cert"); |
628 | 0 | fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData; |
629 | 0 | } else { |
630 | 0 | ossl_cmp_debug(ctx, |
631 | 0 | "success validating newly enrolled cert"); |
632 | 0 | } |
633 | 0 | } else if (chain == NULL) { |
634 | 0 | ossl_cmp_warn(ctx, "could not build approximate chain for newly enrolled cert, resorting to received extraCerts"); |
635 | 0 | chain = OSSL_CMP_CTX_get1_extraCertsIn(ctx); |
636 | 0 | } else { |
637 | 0 | ossl_cmp_debug(ctx, |
638 | 0 | "success building approximate chain for newly enrolled cert"); |
639 | 0 | } |
640 | 0 | (void)ossl_cmp_ctx_set1_newChain(ctx, chain); |
641 | 0 | OSSL_STACK_OF_X509_free(chain); |
642 | |
|
643 | 0 | return fail_info; |
644 | 0 | } |
645 | | |
646 | | /*- |
647 | | * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR. |
648 | | * |rid| must be OSSL_CMP_CERTREQID_NONE if not available, namely for p10cr |
649 | | * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value. |
650 | | * Returns 1 on success and provides the received PKIMESSAGE in *resp. |
651 | | * Returns 0 on error (which includes the case that timeout has been reached). |
652 | | * Regardless of success, caller is responsible for freeing *resp (unless NULL). |
653 | | */ |
654 | | static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid, |
655 | | OSSL_CMP_MSG **resp, int *checkAfter, |
656 | | ossl_unused int req_type, |
657 | | ossl_unused int expected_type) |
658 | 0 | { |
659 | 0 | EVP_PKEY *rkey = ossl_cmp_ctx_get0_newPubkey(ctx); |
660 | 0 | int fail_info = 0; /* no failure */ |
661 | 0 | const char *txt = NULL; |
662 | 0 | OSSL_CMP_CERTREPMESSAGE *crepmsg = NULL; |
663 | 0 | OSSL_CMP_CERTRESPONSE *crep = NULL; |
664 | 0 | OSSL_CMP_certConf_cb_t cb; |
665 | 0 | X509 *cert; |
666 | 0 | char *subj = NULL; |
667 | 0 | int ret = 1; |
668 | 0 | int rcvd_type; |
669 | 0 | OSSL_CMP_PKISI *si; |
670 | |
|
671 | 0 | if (!ossl_assert(ctx != NULL)) |
672 | 0 | return 0; |
673 | | |
674 | 0 | retry: |
675 | 0 | rcvd_type = OSSL_CMP_MSG_get_bodytype(*resp); |
676 | 0 | if (IS_CREP(rcvd_type)) { |
677 | 0 | crepmsg = (*resp)->body->value.ip; /* same for cp and kup */ |
678 | 0 | if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) { |
679 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED); |
680 | 0 | return 0; |
681 | 0 | } |
682 | 0 | crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid); |
683 | 0 | if (crep == NULL) |
684 | 0 | return 0; |
685 | 0 | si = crep->status; |
686 | |
|
687 | 0 | if (rid == OSSL_CMP_CERTREQID_NONE) { |
688 | | /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */ |
689 | 0 | rid = ossl_cmp_asn1_get_int(crep->certReqId); |
690 | 0 | if (rid < OSSL_CMP_CERTREQID_NONE) { |
691 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID); |
692 | 0 | return 0; |
693 | 0 | } |
694 | 0 | } |
695 | 0 | } else if (rcvd_type == OSSL_CMP_PKIBODY_ERROR) { |
696 | 0 | si = (*resp)->body->value.error->pKIStatusInfo; |
697 | 0 | } else { |
698 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
699 | 0 | return 0; |
700 | 0 | } |
701 | | |
702 | 0 | if (!save_statusInfo(ctx, si)) |
703 | 0 | return 0; |
704 | | |
705 | 0 | if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting) { |
706 | | /* |
707 | | * Here we allow both and error message with waiting indication |
708 | | * as well as a certificate response with waiting indication, where |
709 | | * its flavor (ip, cp, or kup) may not strictly match ir/cr/p10cr/kur. |
710 | | */ |
711 | 0 | OSSL_CMP_MSG_free(*resp); |
712 | 0 | *resp = NULL; |
713 | 0 | if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) { |
714 | 0 | if (ret == -1) /* at this point implies sleep == 0 */ |
715 | 0 | return ret; /* waiting */ |
716 | 0 | goto retry; /* got some response other than pollRep */ |
717 | 0 | } else { |
718 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED); |
719 | 0 | return 0; |
720 | 0 | } |
721 | 0 | } |
722 | | |
723 | | /* at this point, we have received ip/cp/kup/error without waiting */ |
724 | 0 | if (rcvd_type == OSSL_CMP_PKIBODY_ERROR) { |
725 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_RECEIVED_ERROR); |
726 | 0 | return 0; |
727 | 0 | } |
728 | | /* here we are strict on the flavor of ip/cp/kup: must match request */ |
729 | 0 | if (rcvd_type != expected_type) { |
730 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
731 | 0 | return 0; |
732 | 0 | } |
733 | | |
734 | 0 | cert = get1_cert_status(ctx, (*resp)->body->type, crep); |
735 | 0 | if (cert == NULL) { |
736 | 0 | ERR_add_error_data(1, "; cannot extract certificate from response"); |
737 | 0 | return 0; |
738 | 0 | } |
739 | 0 | if (!ossl_cmp_ctx_set0_newCert(ctx, cert)) { |
740 | 0 | X509_free(cert); |
741 | 0 | return 0; |
742 | 0 | } |
743 | | |
744 | | /* |
745 | | * if the CMP server returned certificates in the caPubs field, copy them |
746 | | * to the context so that they can be retrieved if necessary |
747 | | */ |
748 | 0 | if (crepmsg != NULL && crepmsg->caPubs != NULL |
749 | 0 | && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs)) |
750 | 0 | return 0; |
751 | | |
752 | 0 | subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0); |
753 | 0 | if (rkey != NULL |
754 | | /* X509_check_private_key() also works if rkey is just public key */ |
755 | 0 | && !(X509_check_private_key(ctx->newCert, rkey))) { |
756 | 0 | fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData; |
757 | 0 | txt = "public key in new certificate does not match our enrollment key"; |
758 | | /*- |
759 | | * not calling (void)ossl_cmp_exchange_error(ctx, |
760 | | * OSSL_CMP_PKISTATUS_rejection, fail_info, txt) |
761 | | * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt |
762 | | * not returning 0 |
763 | | * since we better leave this for the certConf_cb to decide |
764 | | */ |
765 | 0 | } |
766 | | |
767 | | /* |
768 | | * Execute the certification checking callback function, |
769 | | * which can determine whether to accept a newly enrolled certificate. |
770 | | * It may overrule the pre-decision reflected in 'fail_info' and '*txt'. |
771 | | */ |
772 | 0 | cb = ctx->certConf_cb != NULL ? ctx->certConf_cb : OSSL_CMP_certConf_cb; |
773 | 0 | if ((fail_info = cb(ctx, ctx->newCert, fail_info, &txt)) != 0 |
774 | 0 | && txt == NULL) |
775 | 0 | txt = "CMP client did not accept it"; |
776 | 0 | if (fail_info != 0) /* immediately log error before any certConf exchange */ |
777 | 0 | ossl_cmp_log1(ERROR, ctx, |
778 | 0 | "rejecting newly enrolled cert with subject: %s", subj); |
779 | | /* |
780 | | * certConf exchange should better be moved to do_certreq_seq() such that |
781 | | * also more low-level errors with CertReqMessages get reported to server |
782 | | */ |
783 | 0 | if (!ctx->disableConfirm |
784 | 0 | && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) { |
785 | 0 | if (!ossl_cmp_exchange_certConf(ctx, rid, fail_info, txt)) |
786 | 0 | ret = 0; |
787 | 0 | } |
788 | | |
789 | | /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */ |
790 | 0 | if (fail_info != 0) { |
791 | 0 | ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_ACCEPTED, |
792 | 0 | "rejecting newly enrolled cert with subject: %s; %s", |
793 | 0 | subj, txt); |
794 | 0 | ctx->status = OSSL_CMP_PKISTATUS_rejection; |
795 | 0 | ret = 0; |
796 | 0 | } |
797 | 0 | OPENSSL_free(subj); |
798 | 0 | return ret; |
799 | 0 | } |
800 | | |
801 | | static int initial_certreq(OSSL_CMP_CTX *ctx, |
802 | | int req_type, const OSSL_CRMF_MSG *crm, |
803 | | OSSL_CMP_MSG **p_rep, int rep_type) |
804 | 1.32k | { |
805 | 1.32k | OSSL_CMP_MSG *req; |
806 | 1.32k | int res; |
807 | | |
808 | 1.32k | ctx->status = OSSL_CMP_PKISTATUS_request; |
809 | 1.32k | if (!ossl_cmp_ctx_set0_newCert(ctx, NULL)) |
810 | 0 | return 0; |
811 | | |
812 | | /* also checks if all necessary options are set */ |
813 | 1.32k | if ((req = ossl_cmp_certreq_new(ctx, req_type, crm)) == NULL) |
814 | 1.32k | return 0; |
815 | | |
816 | 0 | ctx->status = OSSL_CMP_PKISTATUS_trans; |
817 | 0 | res = send_receive_check(ctx, req, p_rep, rep_type); |
818 | 0 | OSSL_CMP_MSG_free(req); |
819 | 0 | return res; |
820 | 1.32k | } |
821 | | |
822 | | int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type, |
823 | | const OSSL_CRMF_MSG *crm, int *checkAfter) |
824 | 110 | { |
825 | 110 | OSSL_CMP_MSG *rep = NULL; |
826 | 110 | int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR; |
827 | 110 | int rid = is_p10 ? OSSL_CMP_CERTREQID_NONE : OSSL_CMP_CERTREQID; |
828 | 110 | int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1; |
829 | 110 | int res = 0; |
830 | | |
831 | 110 | if (ctx == NULL) { |
832 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
833 | 0 | return 0; |
834 | 0 | } |
835 | | |
836 | 110 | if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */ |
837 | 0 | if (!initial_certreq(ctx, req_type, crm, &rep, rep_type)) |
838 | 0 | goto err; |
839 | | |
840 | 0 | if (!save_senderNonce_if_waiting(ctx, rep, rid)) |
841 | 0 | return 0; |
842 | 110 | } else { |
843 | 110 | if (req_type < 0) |
844 | 0 | return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection, |
845 | 0 | 0, "polling aborted", |
846 | 0 | 0 /* errorCode */, "by application"); |
847 | 110 | res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter); |
848 | 110 | if (res <= 0) /* waiting or error */ |
849 | 110 | return res; |
850 | 110 | } |
851 | 0 | res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter, |
852 | 0 | req_type, rep_type); |
853 | |
|
854 | 0 | err: |
855 | 0 | OSSL_CMP_MSG_free(rep); |
856 | 0 | return res; |
857 | 0 | } |
858 | | |
859 | | /*- |
860 | | * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP, |
861 | | * certConf, PKIconf, and polling if required. |
862 | | * Will sleep as long as indicated by the server (according to checkAfter). |
863 | | * All enrollment options need to be present in the context. |
864 | | * Returns pointer to received certificate, or NULL if none was received. |
865 | | */ |
866 | | X509 *OSSL_CMP_exec_certreq(OSSL_CMP_CTX *ctx, int req_type, |
867 | | const OSSL_CRMF_MSG *crm) |
868 | 871 | { |
869 | 871 | OSSL_CMP_MSG *rep = NULL; |
870 | 871 | int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR; |
871 | 871 | int rid = is_p10 ? OSSL_CMP_CERTREQID_NONE : OSSL_CMP_CERTREQID; |
872 | 871 | int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1; |
873 | 871 | X509 *result = NULL; |
874 | | |
875 | 871 | if (ctx == NULL) { |
876 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
877 | 0 | return NULL; |
878 | 0 | } |
879 | | |
880 | 871 | if (!initial_certreq(ctx, req_type, crm, &rep, rep_type)) |
881 | 871 | goto err; |
882 | | |
883 | 0 | if (!save_senderNonce_if_waiting(ctx, rep, rid)) |
884 | 0 | return 0; |
885 | | |
886 | 0 | if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type) |
887 | 0 | <= 0) |
888 | 0 | goto err; |
889 | | |
890 | 0 | result = ctx->newCert; |
891 | 871 | err: |
892 | 871 | OSSL_CMP_MSG_free(rep); |
893 | 871 | return result; |
894 | 0 | } |
895 | | |
896 | | int OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx) |
897 | 162 | { |
898 | 162 | OSSL_CMP_MSG *rr = NULL; |
899 | 162 | OSSL_CMP_MSG *rp = NULL; |
900 | 162 | const int num_RevDetails = 1; |
901 | 162 | const int rsid = OSSL_CMP_REVREQSID; |
902 | 162 | OSSL_CMP_REVREPCONTENT *rrep = NULL; |
903 | 162 | OSSL_CMP_PKISI *si = NULL; |
904 | 162 | char buf[OSSL_CMP_PKISI_BUFLEN]; |
905 | 162 | int ret = 0; |
906 | | |
907 | 162 | if (ctx == NULL) { |
908 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
909 | 0 | return 0; |
910 | 0 | } |
911 | 162 | ctx->status = OSSL_CMP_PKISTATUS_request; |
912 | 162 | if (ctx->oldCert == NULL && ctx->p10CSR == NULL |
913 | 162 | && (ctx->serialNumber == NULL || ctx->issuer == NULL)) { |
914 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT); |
915 | 0 | return 0; |
916 | 0 | } |
917 | | |
918 | | /* OSSL_CMP_rr_new() also checks if all necessary options are set */ |
919 | 162 | if ((rr = ossl_cmp_rr_new(ctx)) == NULL) |
920 | 162 | goto end; |
921 | | |
922 | 0 | ctx->status = OSSL_CMP_PKISTATUS_trans; |
923 | 0 | if (!send_receive_also_delayed(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP)) |
924 | 0 | goto end; |
925 | | |
926 | 0 | rrep = rp->body->value.rp; |
927 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
928 | | if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) { |
929 | | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT); |
930 | | goto end; |
931 | | } |
932 | | #else |
933 | 0 | if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) { |
934 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT); |
935 | 0 | goto end; |
936 | 0 | } |
937 | 0 | #endif |
938 | | |
939 | | /* evaluate PKIStatus field */ |
940 | 0 | si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid); |
941 | 0 | if (!save_statusInfo(ctx, si)) |
942 | 0 | goto err; |
943 | 0 | switch (ossl_cmp_pkisi_get_status(si)) { |
944 | 0 | case OSSL_CMP_PKISTATUS_accepted: |
945 | 0 | ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)"); |
946 | 0 | ret = 1; |
947 | 0 | break; |
948 | 0 | case OSSL_CMP_PKISTATUS_grantedWithMods: |
949 | 0 | ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)"); |
950 | 0 | ret = 1; |
951 | 0 | break; |
952 | 0 | case OSSL_CMP_PKISTATUS_rejection: |
953 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER); |
954 | 0 | goto err; |
955 | 0 | case OSSL_CMP_PKISTATUS_revocationWarning: |
956 | 0 | ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)"); |
957 | 0 | ret = 1; |
958 | 0 | break; |
959 | 0 | case OSSL_CMP_PKISTATUS_revocationNotification: |
960 | | /* interpretation as warning or error depends on CA */ |
961 | 0 | ossl_cmp_warn(ctx, |
962 | 0 | "revocation accepted (PKIStatus=revocationNotification)"); |
963 | 0 | ret = 1; |
964 | 0 | break; |
965 | 0 | case OSSL_CMP_PKISTATUS_waiting: |
966 | 0 | case OSSL_CMP_PKISTATUS_keyUpdateWarning: |
967 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS); |
968 | 0 | goto err; |
969 | 0 | default: |
970 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS); |
971 | 0 | goto err; |
972 | 0 | } |
973 | | |
974 | | /* check any present CertId in optional revCerts field */ |
975 | 0 | if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) >= 1) { |
976 | 0 | OSSL_CRMF_CERTID *cid; |
977 | 0 | OSSL_CRMF_CERTTEMPLATE *tmpl = |
978 | 0 | sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails; |
979 | 0 | const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl); |
980 | 0 | const ASN1_INTEGER *serial = |
981 | 0 | OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl); |
982 | |
|
983 | 0 | if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) { |
984 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT); |
985 | 0 | ret = 0; |
986 | 0 | goto err; |
987 | 0 | } |
988 | 0 | if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) { |
989 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_CERTID); |
990 | 0 | ret = 0; |
991 | 0 | goto err; |
992 | 0 | } |
993 | 0 | if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) { |
994 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
995 | | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_CERTID_IN_RP); |
996 | | ret = 0; |
997 | | goto err; |
998 | | #endif |
999 | 0 | } |
1000 | 0 | if (ASN1_INTEGER_cmp(serial, |
1001 | 0 | OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) { |
1002 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1003 | | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_SERIAL_IN_RP); |
1004 | | ret = 0; |
1005 | | goto err; |
1006 | | #endif |
1007 | 0 | } |
1008 | 0 | } |
1009 | | |
1010 | | /* check number of any optionally present crls */ |
1011 | 0 | if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) { |
1012 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT); |
1013 | 0 | ret = 0; |
1014 | 0 | goto err; |
1015 | 0 | } |
1016 | | |
1017 | 0 | err: |
1018 | 0 | if (ret == 0 |
1019 | 0 | && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL) |
1020 | 0 | ERR_add_error_data(1, buf); |
1021 | |
|
1022 | 162 | end: |
1023 | 162 | OSSL_CMP_MSG_free(rr); |
1024 | 162 | OSSL_CMP_MSG_free(rp); |
1025 | 162 | return ret; |
1026 | 0 | } |
1027 | | |
1028 | | STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx) |
1029 | 1.11k | { |
1030 | 1.11k | OSSL_CMP_MSG *genm; |
1031 | 1.11k | OSSL_CMP_MSG *genp = NULL; |
1032 | 1.11k | STACK_OF(OSSL_CMP_ITAV) *itavs = NULL; |
1033 | | |
1034 | 1.11k | if (ctx == NULL) { |
1035 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
1036 | 0 | return NULL; |
1037 | 0 | } |
1038 | 1.11k | ctx->status = OSSL_CMP_PKISTATUS_request; |
1039 | | |
1040 | 1.11k | if ((genm = ossl_cmp_genm_new(ctx)) == NULL) |
1041 | 1.11k | goto err; |
1042 | | |
1043 | 0 | ctx->status = OSSL_CMP_PKISTATUS_trans; |
1044 | 0 | if (!send_receive_also_delayed(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP)) |
1045 | 0 | goto err; |
1046 | 0 | ctx->status = OSSL_CMP_PKISTATUS_accepted; |
1047 | |
|
1048 | 0 | itavs = genp->body->value.genp; |
1049 | 0 | if (itavs == NULL) |
1050 | 0 | itavs = sk_OSSL_CMP_ITAV_new_null(); |
1051 | | /* received stack of itavs not to be freed with the genp */ |
1052 | 0 | genp->body->value.genp = NULL; |
1053 | |
|
1054 | 1.11k | err: |
1055 | 1.11k | OSSL_CMP_MSG_free(genm); |
1056 | 1.11k | OSSL_CMP_MSG_free(genp); |
1057 | | |
1058 | 1.11k | return itavs; /* NULL indicates error case */ |
1059 | 0 | } |