VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/crypto/cmp/cmp_client.c

Last change on this file was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette