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