]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cmp/cmp_client.c
Update copyright year
[thirdparty/openssl.git] / crypto / cmp / cmp_client.c
1 /*
2 * Copyright 2007-2020 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
22 #include "openssl/cmp_util.h"
23
24 #define IS_CREP(t) ((t) == OSSL_CMP_PKIBODY_IP || (t) == OSSL_CMP_PKIBODY_CP \
25 || (t) == OSSL_CMP_PKIBODY_KUP)
26
27 /*-
28 * Evaluate whether there's an exception (violating the standard) configured for
29 * handling negative responses without protection or with invalid protection.
30 * Returns 1 on acceptance, 0 on rejection, or -1 on (internal) error.
31 */
32 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
33 const OSSL_CMP_MSG *rep,
34 int invalid_protection,
35 int expected_type /* ignored here */)
36 {
37 int rcvd_type = ossl_cmp_msg_get_bodytype(rep /* may be NULL */);
38 const char *msg_type = NULL;
39
40 if (!ossl_assert(ctx != NULL && rep != NULL))
41 return -1;
42
43 if (!ctx->unprotectedErrors)
44 return 0;
45
46 switch (rcvd_type) {
47 case OSSL_CMP_PKIBODY_ERROR:
48 msg_type = "error response";
49 break;
50 case OSSL_CMP_PKIBODY_RP:
51 {
52 OSSL_CMP_PKISI *si =
53 ossl_cmp_revrepcontent_get_pkisi(rep->body->value.rp,
54 OSSL_CMP_REVREQSID);
55
56 if (si == NULL)
57 return -1;
58 if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_rejection)
59 msg_type = "revocation response message with rejection status";
60 break;
61 }
62 case OSSL_CMP_PKIBODY_PKICONF:
63 msg_type = "PKI Confirmation message";
64 break;
65 default:
66 if (IS_CREP(rcvd_type)) {
67 OSSL_CMP_CERTREPMESSAGE *crepmsg = rep->body->value.ip;
68 OSSL_CMP_CERTRESPONSE *crep =
69 ossl_cmp_certrepmessage_get0_certresponse(crepmsg,
70 -1 /* any rid */);
71
72 if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1)
73 return -1;
74 /* TODO: handle potentially multiple CertResponses in CertRepMsg */
75 if (crep == NULL)
76 return -1;
77 if (ossl_cmp_pkisi_get_status(crep->status)
78 == OSSL_CMP_PKISTATUS_rejection)
79 msg_type = "CertRepMessage with rejection status";
80 }
81 }
82 if (msg_type == NULL)
83 return 0;
84 ossl_cmp_log2(WARN, ctx, "ignoring %s protection of %s",
85 invalid_protection ? "invalid" : "missing", msg_type);
86 return 1;
87 }
88
89
90 /* Save error info from PKIStatusInfo field of a certresponse into ctx */
91 static int save_statusInfo(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si)
92 {
93 int i;
94 OSSL_CMP_PKIFREETEXT *ss;
95
96 if (!ossl_assert(ctx != NULL && si != NULL))
97 return 0;
98
99 if ((ctx->status = ossl_cmp_pkisi_get_status(si)) < 0)
100 return 0;
101
102 ctx->failInfoCode = 0;
103 if (si->failInfo != NULL) {
104 for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++) {
105 if (ASN1_BIT_STRING_get_bit(si->failInfo, i))
106 ctx->failInfoCode |= (1 << i);
107 }
108 }
109
110 if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
111 || (ctx->statusString == NULL))
112 return 0;
113
114 ss = si->statusString; /* may be NULL */
115 for (i = 0; i < sk_ASN1_UTF8STRING_num(ss); i++) {
116 ASN1_UTF8STRING *str = sk_ASN1_UTF8STRING_value(ss, i);
117
118 if (!sk_ASN1_UTF8STRING_push(ctx->statusString, ASN1_STRING_dup(str)))
119 return 0;
120 }
121 return 1;
122 }
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 {
133 const char *req_type_str =
134 ossl_cmp_bodytype_to_string(ossl_cmp_msg_get_bodytype(req));
135 const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type);
136 int msg_timeout;
137 int bt;
138 time_t now = time(NULL);
139 int time_left;
140 OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb;
141
142 if (transfer_cb == NULL)
143 transfer_cb = OSSL_CMP_MSG_http_perform;
144
145 *rep = NULL;
146 msg_timeout = ctx->msg_timeout; /* backup original value */
147 if ((IS_CREP(expected_type) || expected_type == OSSL_CMP_PKIBODY_POLLREP)
148 && ctx->total_timeout > 0 /* timeout is not infinite */) {
149 if (now >= ctx->end_time) {
150 CMPerr(0, CMP_R_TOTAL_TIMEOUT);
151 return 0;
152 }
153 if (!ossl_assert(ctx->end_time - time(NULL) < INT_MAX)) {
154 /* cannot really happen due to the assignment in do_certreq_seq() */
155 CMPerr(0, CMP_R_INVALID_ARGS);
156 return 0;
157 }
158 time_left = (int)(ctx->end_time - now);
159 if (ctx->msg_timeout == 0 || time_left < ctx->msg_timeout)
160 ctx->msg_timeout = time_left;
161 }
162
163 /* should print error queue since transfer_cb may call ERR_clear_error() */
164 OSSL_CMP_CTX_print_errors(ctx);
165
166 ossl_cmp_log1(INFO, ctx, "sending %s", req_type_str);
167
168 *rep = (*transfer_cb)(ctx, req);
169 ctx->msg_timeout = msg_timeout; /* restore original value */
170
171 if (*rep == NULL) {
172 CMPerr(0, CMP_R_TRANSFER_ERROR); /* or receiving response */
173 ERR_add_error_data(1, req_type_str);
174 ERR_add_error_data(2, ", expected response: ", expected_type_str);
175 return 0;
176 }
177
178 bt = ossl_cmp_msg_get_bodytype(*rep);
179 /*
180 * The body type in the 'bt' variable is not yet verified.
181 * Still we use this preliminary value already for a progress report because
182 * the following msg verification may also produce log entries and may fail.
183 */
184 ossl_cmp_log1(INFO, ctx, "received %s", ossl_cmp_bodytype_to_string(bt));
185
186 if ((bt = ossl_cmp_msg_check_received(ctx, *rep, unprotected_exception,
187 expected_type)) < 0)
188 return 0;
189
190 if (bt == expected_type
191 /* as an answer to polling, there could be IP/CP/KUP: */
192 || (IS_CREP(bt) && expected_type == OSSL_CMP_PKIBODY_POLLREP))
193 return 1;
194
195 /* received message type is not one of the expected ones (e.g., error) */
196 CMPerr(0, bt == OSSL_CMP_PKIBODY_ERROR ? CMP_R_RECEIVED_ERROR :
197 CMP_R_UNEXPECTED_PKIBODY); /* in next line for mkerr.pl */
198
199 if (bt != OSSL_CMP_PKIBODY_ERROR) {
200 ERR_add_error_data(3, "message type is '",
201 ossl_cmp_bodytype_to_string(bt), "'");
202 } else {
203 OSSL_CMP_ERRORMSGCONTENT *emc = (*rep)->body->value.error;
204 OSSL_CMP_PKISI *si = emc->pKIStatusInfo;
205 char buf[OSSL_CMP_PKISI_BUFLEN];
206
207 if (save_statusInfo(ctx, si)
208 && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
209 ERR_add_error_data(1, buf);
210 if (emc->errorCode != NULL
211 && BIO_snprintf(buf, sizeof(buf), "; errorCode: %ld",
212 ASN1_INTEGER_get(emc->errorCode)) > 0)
213 ERR_add_error_data(1, buf);
214 if (emc->errorDetails != NULL) {
215 char *text = sk_ASN1_UTF8STRING2text(emc->errorDetails, ", ",
216 OSSL_CMP_PKISI_BUFLEN - 1);
217
218 if (text != NULL)
219 ERR_add_error_data(2, "; errorDetails: ", text);
220 OPENSSL_free(text);
221 }
222 if (ctx->status != OSSL_CMP_PKISTATUS_rejection) {
223 CMPerr(0, CMP_R_UNEXPECTED_PKISTATUS);
224 if (ctx->status == OSSL_CMP_PKISTATUS_waiting)
225 ctx->status = OSSL_CMP_PKISTATUS_rejection;
226 }
227 }
228 return 0;
229 }
230
231 /*-
232 * When a 'waiting' PKIStatus has been received, this function is used to
233 * poll, which should yield a pollRep or finally a CertRepMessage in ip/cp/kup.
234 * On receiving a pollRep, which includes a checkAfter value, it return this
235 * value if sleep == 0, else it sleeps as long as indicated and retries.
236 *
237 * A transaction timeout is enabled if ctx->total_timeout is > 0.
238 * In this case polling will continue until the timeout is reached and then
239 * polling is done a last time even if this is before the "checkAfter" time.
240 *
241 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
242 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
243 * In this case the caller is responsible for freeing *rep.
244 * Returns 0 on error (which includes the case that timeout has been reached).
245 */
246 static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
247 OSSL_CMP_MSG **rep, int *checkAfter)
248 {
249 OSSL_CMP_MSG *preq = NULL;
250 OSSL_CMP_MSG *prep = NULL;
251
252 ossl_cmp_info(ctx,
253 "received 'waiting' PKIStatus, starting to poll for response");
254 *rep = NULL;
255 for (;;) {
256 /* TODO: handle potentially multiple poll requests per message */
257 if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL)
258 goto err;
259
260 if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP))
261 goto err;
262
263 /* handle potential pollRep */
264 if (ossl_cmp_msg_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) {
265 OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep;
266 OSSL_CMP_POLLREP *pollRep = NULL;
267 int64_t check_after;
268 char str[OSSL_CMP_PKISI_BUFLEN];
269 int len;
270
271 /* TODO: handle potentially multiple elements in pollRep */
272 if (sk_OSSL_CMP_POLLREP_num(prc) > 1) {
273 CMPerr(0, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
274 goto err;
275 }
276 pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid);
277 if (pollRep == NULL)
278 goto err;
279
280 if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) {
281 CMPerr(0, CMP_R_BAD_CHECKAFTER_IN_POLLREP);
282 goto err;
283 }
284 if (check_after < 0 || (uint64_t)check_after
285 > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
286 CMPerr(0, CMP_R_CHECKAFTER_OUT_OF_RANGE);
287 if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd",
288 check_after) >= 0)
289 ERR_add_error_data(1, str);
290 goto err;
291 }
292 if (ctx->total_timeout > 0) { /* timeout is not infinite */
293 const int exp = 5; /* expected max time per msg round trip */
294 int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL));
295
296 if (time_left <= 0) {
297 CMPerr(0, CMP_R_TOTAL_TIMEOUT);
298 goto err;
299 }
300 if (time_left < check_after)
301 check_after = time_left;
302 /* poll one last time just when timeout was reached */
303 }
304
305 if (pollRep->reason == NULL
306 || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN,
307 " with reason = '")) < 0) {
308 *str = '\0';
309 } else {
310 char *text = sk_ASN1_UTF8STRING2text(pollRep->reason, ", ",
311 sizeof(str) - len - 2);
312
313 if (text == NULL
314 || BIO_snprintf(str + len, sizeof(str) - len,
315 "%s'", text) < 0)
316 *str = '\0';
317 OPENSSL_free(text);
318 }
319 ossl_cmp_log2(INFO, ctx,
320 "received polling response%s; checkAfter = %ld seconds",
321 str, check_after);
322
323 OSSL_CMP_MSG_free(preq);
324 preq = NULL;
325 OSSL_CMP_MSG_free(prep);
326 prep = NULL;
327 if (sleep) {
328 ossl_sleep((unsigned long)(1000 * check_after));
329 } else {
330 if (checkAfter != NULL)
331 *checkAfter = (int)check_after;
332 return -1; /* exits the loop */
333 }
334 } else {
335 ossl_cmp_info(ctx, "received ip/cp/kup after polling");
336 /* any other body type has been rejected by send_receive_check() */
337 break;
338 }
339 }
340 if (prep == NULL)
341 goto err;
342
343 OSSL_CMP_MSG_free(preq);
344 *rep = prep;
345
346 return 1;
347 err:
348 OSSL_CMP_MSG_free(preq);
349 OSSL_CMP_MSG_free(prep);
350 return 0;
351 }
352
353 /* Send certConf for IR, CR or KUR sequences and check response */
354 int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int fail_info,
355 const char *txt)
356 {
357 OSSL_CMP_MSG *certConf;
358 OSSL_CMP_MSG *PKIconf = NULL;
359 int res = 0;
360
361 /* OSSL_CMP_certConf_new() also checks if all necessary options are set */
362 if ((certConf = ossl_cmp_certConf_new(ctx, fail_info, txt)) == NULL)
363 goto err;
364
365 res = send_receive_check(ctx, certConf, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
366
367 err:
368 OSSL_CMP_MSG_free(certConf);
369 OSSL_CMP_MSG_free(PKIconf);
370 return res;
371 }
372
373 /* Send given error and check response */
374 int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info,
375 const char *txt, int errorCode, const char *details)
376 {
377 OSSL_CMP_MSG *error = NULL;
378 OSSL_CMP_PKISI *si = NULL;
379 OSSL_CMP_MSG *PKIconf = NULL;
380 int res = 0;
381
382 if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt)) == NULL)
383 goto err;
384 /* ossl_cmp_error_new() also checks if all necessary options are set */
385 if ((error = ossl_cmp_error_new(ctx, si, errorCode, details, 0)) == NULL)
386 goto err;
387
388 res = send_receive_check(ctx, error, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
389
390 err:
391 OSSL_CMP_MSG_free(error);
392 OSSL_CMP_PKISI_free(si);
393 OSSL_CMP_MSG_free(PKIconf);
394 return res;
395 }
396
397 /*-
398 * Retrieve a copy of the certificate, if any, from the given CertResponse.
399 * Take into account PKIStatusInfo of CertResponse in ctx, report it on error.
400 * Returns NULL if not found or on error.
401 */
402 static X509 *get1_cert_status(OSSL_CMP_CTX *ctx, int bodytype,
403 OSSL_CMP_CERTRESPONSE *crep)
404 {
405 char buf[OSSL_CMP_PKISI_BUFLEN];
406 X509 *crt = NULL;
407 EVP_PKEY *privkey;
408
409 if (!ossl_assert(ctx != NULL && crep != NULL))
410 return NULL;
411
412 privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
413 switch (ossl_cmp_pkisi_get_status(crep->status)) {
414 case OSSL_CMP_PKISTATUS_waiting:
415 ossl_cmp_err(ctx,
416 "received \"waiting\" status for cert when actually aiming to extract cert");
417 CMPerr(0, CMP_R_ENCOUNTERED_WAITING);
418 goto err;
419 case OSSL_CMP_PKISTATUS_grantedWithMods:
420 ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate");
421 crt = ossl_cmp_certresponse_get1_certificate(privkey, crep);
422 break;
423 case OSSL_CMP_PKISTATUS_accepted:
424 crt = ossl_cmp_certresponse_get1_certificate(privkey, crep);
425 break;
426 /* get all information in case of a rejection before going to error */
427 case OSSL_CMP_PKISTATUS_rejection:
428 ossl_cmp_err(ctx, "received \"rejection\" status rather than cert");
429 CMPerr(0, CMP_R_REQUEST_REJECTED_BY_SERVER);
430 goto err;
431 case OSSL_CMP_PKISTATUS_revocationWarning:
432 ossl_cmp_warn(ctx,
433 "received \"revocationWarning\" - a revocation of the cert is imminent");
434 crt = ossl_cmp_certresponse_get1_certificate(privkey, crep);
435 break;
436 case OSSL_CMP_PKISTATUS_revocationNotification:
437 ossl_cmp_warn(ctx,
438 "received \"revocationNotification\" - a revocation of the cert has occurred");
439 crt = ossl_cmp_certresponse_get1_certificate(privkey, crep);
440 break;
441 case OSSL_CMP_PKISTATUS_keyUpdateWarning:
442 if (bodytype != OSSL_CMP_PKIBODY_KUR) {
443 CMPerr(0, CMP_R_ENCOUNTERED_KEYUPDATEWARNING);
444 goto err;
445 }
446 crt = ossl_cmp_certresponse_get1_certificate(privkey, crep);
447 break;
448 default:
449 ossl_cmp_log1(ERROR, ctx,
450 "received unsupported PKIStatus %d for certificate",
451 ctx->status);
452 CMPerr(0, CMP_R_UNKNOWN_PKISTATUS);
453 goto err;
454 }
455 if (crt == NULL) /* according to PKIStatus, we can expect a cert */
456 CMPerr(0, CMP_R_CERTIFICATE_NOT_FOUND);
457
458 return crt;
459
460 err:
461 if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
462 ERR_add_error_data(1, buf);
463 return NULL;
464 }
465
466 /*-
467 * Callback fn validating that the new certificate can be verified, using
468 * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and
469 * ctx->untrusted_certs, which at this point already contains ctx->extraCertsIn.
470 * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo.
471 * Quoting from RFC 4210 section 5.1. Overall PKI Message:
472 * The extraCerts field can contain certificates that may be useful to
473 * the recipient. For example, this can be used by a CA or RA to
474 * present an end entity with certificates that it needs to verify its
475 * own new certificate (if, for example, the CA that issued the end
476 * entity's certificate is not a root CA for the end entity). Note that
477 * this field does not necessarily contain a certification path; the
478 * recipient may have to sort, select from, or otherwise process the
479 * extra certificates in order to use them.
480 * Note: While often handy, there is no hard requirement by CMP that
481 * an EE must be able to validate the certificates it gets enrolled.
482 */
483 int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
484 const char **text)
485 {
486 X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
487 (void)text; /* make (artificial) use of var to prevent compiler warning */
488
489 if (fail_info != 0) /* accept any error flagged by CMP core library */
490 return fail_info;
491
492 if (out_trusted != NULL
493 && !OSSL_CMP_validate_cert_path(ctx, out_trusted, cert))
494 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
495
496 return fail_info;
497 }
498
499 /*-
500 * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR.
501 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
502 * Returns 1 on success and provides the received PKIMESSAGE in *resp.
503 * Returns 0 on error (which includes the case that timeout has been reached).
504 * Regardless of success, caller is responsible for freeing *resp (unless NULL).
505 */
506 static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
507 OSSL_CMP_MSG **resp, int *checkAfter,
508 int req_type, int expected_type)
509 {
510 EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx /* may be NULL */, 0);
511 int fail_info = 0; /* no failure */
512 const char *txt = NULL;
513 OSSL_CMP_CERTREPMESSAGE *crepmsg;
514 OSSL_CMP_CERTRESPONSE *crep;
515 X509 *cert;
516 char *subj = NULL;
517 int ret = 1;
518
519 retry:
520 crepmsg = (*resp)->body->value.ip; /* same for cp and kup */
521 if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) {
522 CMPerr(0, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
523 return 0;
524 }
525 /* TODO: handle potentially multiple CertResponses in CertRepMsg */
526 crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
527 if (crep == NULL)
528 return 0;
529 if (!save_statusInfo(ctx, crep->status))
530 return 0;
531 if (rid == -1) {
532 /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */
533 rid = ossl_cmp_asn1_get_int(crep->certReqId);
534 if (rid == -1) {
535 CMPerr(0, CMP_R_BAD_REQUEST_ID);
536 return 0;
537 }
538 }
539
540 if (ossl_cmp_pkisi_get_status(crep->status) == OSSL_CMP_PKISTATUS_waiting) {
541 OSSL_CMP_MSG_free(*resp);
542 *resp = NULL;
543 if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) {
544 if (ret == -1) /* at this point implies sleep == 0 */
545 return ret; /* waiting */
546 goto retry; /* got ip/cp/kup, which may still indicate 'waiting' */
547 } else {
548 CMPerr(0, CMP_R_POLLING_FAILED);
549 return 0;
550 }
551 }
552
553 cert = get1_cert_status(ctx, (*resp)->body->type, crep);
554 if (cert == NULL) {
555 ERR_add_error_data(1, "; cannot extract certificate from response");
556 return 0;
557 }
558 if (!ossl_cmp_ctx_set0_newCert(ctx, cert))
559 return 0;
560
561 /*
562 * if the CMP server returned certificates in the caPubs field, copy them
563 * to the context so that they can be retrieved if necessary
564 */
565 if (crepmsg->caPubs != NULL
566 && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs))
567 return 0;
568
569 /* copy received extraCerts to ctx->extraCertsIn so they can be retrieved */
570 if (!ossl_cmp_ctx_set1_extraCertsIn(ctx, (*resp)->extraCerts))
571 return 0;
572
573 subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
574 if (rkey != NULL
575 /* X509_check_private_key() also works if rkey is just public key */
576 && !(X509_check_private_key(ctx->newCert, rkey))) {
577 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
578 txt = "public key in new certificate does not match our enrollment key";
579 /*-
580 * not callling (void)ossl_cmp_exchange_error(ctx,
581 * OSSL_CMP_PKISTATUS_rejection, fail_info, txt)
582 * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt
583 * not returning 0
584 * since we better leave this for any ctx->certConf_cb to decide
585 */
586 }
587
588 /*
589 * Execute the certification checking callback function possibly set in ctx,
590 * which can determine whether to accept a newly enrolled certificate.
591 * It may overrule the pre-decision reflected in 'fail_info' and '*txt'.
592 */
593 if (ctx->certConf_cb
594 && (fail_info = ctx->certConf_cb(ctx, ctx->newCert,
595 fail_info, &txt)) != 0) {
596 if (txt == NULL)
597 txt = "CMP client application did not accept it";
598 }
599 if (fail_info != 0) /* immediately log error before any certConf exchange */
600 ossl_cmp_log1(ERROR, ctx,
601 "rejecting newly enrolled cert with subject: %s", subj);
602
603 /*
604 * TODO: better move certConf exchange to do_certreq_seq() such that
605 * also more low-level errors with CertReqMessages get reported to server
606 */
607 if (!ctx->disableConfirm
608 && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) {
609 if (!ossl_cmp_exchange_certConf(ctx, fail_info, txt))
610 ret = 0;
611 }
612
613 /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */
614 if (fail_info != 0) {
615 CMPerr(0, CMP_R_CERTIFICATE_NOT_ACCEPTED);
616 ERR_add_error_data(2, "rejecting newly enrolled cert with subject: ",
617 subj);
618 if (txt != NULL)
619 ERR_add_error_txt("; ", txt);
620 ret = 0;
621 }
622 OPENSSL_free(subj);
623 return ret;
624 }
625
626 int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type, int *checkAfter)
627 {
628 OSSL_CMP_MSG *req = NULL;
629 OSSL_CMP_MSG *rep = NULL;
630 int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
631 int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
632 int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
633 int res = 0;
634
635 if (ctx == NULL) {
636 CMPerr(0, CMP_R_NULL_ARGUMENT);
637 return 0;
638 }
639
640 if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */
641 ctx->status = -1;
642 if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
643 return 0;
644
645 if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
646 ctx->end_time = time(NULL) + ctx->total_timeout;
647
648 req = ossl_cmp_certReq_new(ctx, req_type, 0 /* req_err */);
649 if (req == NULL) /* also checks if all necessary options are set */
650 return 0;
651
652 if (!send_receive_check(ctx, req, &rep, rep_type))
653 goto err;
654 } else {
655 if (req_type < 0)
656 return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
657 0 /* TODO better fail_info value? */,
658 "polling aborted", 0 /* errorCode */,
659 "by application");
660 res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter);
661 if (res <= 0) /* waiting or error */
662 return res;
663 }
664 res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter,
665 req_type, rep_type);
666
667 err:
668 OSSL_CMP_MSG_free(req);
669 OSSL_CMP_MSG_free(rep);
670 return res;
671 }
672
673 /*-
674 * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP,
675 * certConf, PKIconf, and polling if required.
676 * Will sleep as long as indicated by the server (according to checkAfter).
677 * All enrollment options need to be present in the context.
678 * TODO: another function to request two certificates at once should be created.
679 * Returns pointer to received certificate, or NULL if none was received.
680 */
681 static X509 *do_certreq_seq(OSSL_CMP_CTX *ctx, int req_type, int req_err,
682 int rep_type)
683 {
684 OSSL_CMP_MSG *req = NULL;
685 OSSL_CMP_MSG *rep = NULL;
686 int rid = (req_type == OSSL_CMP_PKIBODY_P10CR) ? -1 : OSSL_CMP_CERTREQID;
687 X509 *result = NULL;
688
689 if (ctx == NULL) {
690 CMPerr(0, CMP_R_NULL_ARGUMENT);
691 return NULL;
692 }
693 ctx->status = -1;
694 if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
695 return NULL;
696
697 if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
698 ctx->end_time = time(NULL) + ctx->total_timeout;
699
700 /* OSSL_CMP_certreq_new() also checks if all necessary options are set */
701 if ((req = ossl_cmp_certReq_new(ctx, req_type, req_err)) == NULL)
702 goto err;
703
704 if (!send_receive_check(ctx, req, &rep, rep_type))
705 goto err;
706
707 if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type)
708 <= 0)
709 goto err;
710
711 result = ctx->newCert;
712 err:
713 OSSL_CMP_MSG_free(req);
714 OSSL_CMP_MSG_free(rep);
715 return result;
716 }
717
718 X509 *OSSL_CMP_exec_IR_ses(OSSL_CMP_CTX *ctx)
719 {
720 return do_certreq_seq(ctx, OSSL_CMP_PKIBODY_IR,
721 CMP_R_ERROR_CREATING_IR, OSSL_CMP_PKIBODY_IP);
722 }
723
724 X509 *OSSL_CMP_exec_CR_ses(OSSL_CMP_CTX *ctx)
725 {
726 return do_certreq_seq(ctx, OSSL_CMP_PKIBODY_CR,
727 CMP_R_ERROR_CREATING_CR, OSSL_CMP_PKIBODY_CP);
728 }
729
730 X509 *OSSL_CMP_exec_KUR_ses(OSSL_CMP_CTX *ctx)
731 {
732 return do_certreq_seq(ctx, OSSL_CMP_PKIBODY_KUR,
733 CMP_R_ERROR_CREATING_KUR, OSSL_CMP_PKIBODY_KUP);
734 }
735
736 X509 *OSSL_CMP_exec_P10CR_ses(OSSL_CMP_CTX *ctx)
737 {
738 return do_certreq_seq(ctx, OSSL_CMP_PKIBODY_P10CR,
739 CMP_R_ERROR_CREATING_P10CR, OSSL_CMP_PKIBODY_CP);
740 }
741
742 X509 *OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
743 {
744 OSSL_CMP_MSG *rr = NULL;
745 OSSL_CMP_MSG *rp = NULL;
746 const int num_RevDetails = 1;
747 const int rsid = OSSL_CMP_REVREQSID;
748 OSSL_CMP_REVREPCONTENT *rrep = NULL;
749 OSSL_CMP_PKISI *si = NULL;
750 char buf[OSSL_CMP_PKISI_BUFLEN];
751 X509 *result = NULL;
752
753 if (ctx == NULL) {
754 CMPerr(0, CMP_R_INVALID_ARGS);
755 return 0;
756 }
757 if (ctx->oldCert == NULL) {
758 CMPerr(0, CMP_R_MISSING_REFERENCE_CERT);
759 return 0;
760 }
761 ctx->status = -1;
762
763 /* OSSL_CMP_rr_new() also checks if all necessary options are set */
764 if ((rr = ossl_cmp_rr_new(ctx)) == NULL)
765 goto end;
766
767 if (!send_receive_check(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP))
768 goto end;
769
770 rrep = rp->body->value.rp;
771 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
772 if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
773 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
774 goto end;
775 }
776 #else
777 if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
778 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
779 goto end;
780 }
781 #endif
782
783 /* evaluate PKIStatus field */
784 si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
785 if (!save_statusInfo(ctx, si))
786 goto err;
787 switch (ossl_cmp_pkisi_get_status(si)) {
788 case OSSL_CMP_PKISTATUS_accepted:
789 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)");
790 result = ctx->oldCert;
791 break;
792 case OSSL_CMP_PKISTATUS_grantedWithMods:
793 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)");
794 result = ctx->oldCert;
795 break;
796 case OSSL_CMP_PKISTATUS_rejection:
797 CMPerr(0, CMP_R_REQUEST_REJECTED_BY_SERVER);
798 goto err;
799 case OSSL_CMP_PKISTATUS_revocationWarning:
800 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)");
801 result = ctx->oldCert;
802 break;
803 case OSSL_CMP_PKISTATUS_revocationNotification:
804 /* interpretation as warning or error depends on CA */
805 ossl_cmp_warn(ctx,
806 "revocation accepted (PKIStatus=revocationNotification)");
807 result = ctx->oldCert;
808 break;
809 case OSSL_CMP_PKISTATUS_waiting:
810 case OSSL_CMP_PKISTATUS_keyUpdateWarning:
811 CMPerr(0, CMP_R_UNEXPECTED_PKISTATUS);
812 goto err;
813 default:
814 CMPerr(0, CMP_R_UNKNOWN_PKISTATUS);
815 goto err;
816 }
817
818 /* check any present CertId in optional revCerts field */
819 if (rrep->revCerts != NULL) {
820 OSSL_CRMF_CERTID *cid;
821 OSSL_CRMF_CERTTEMPLATE *tmpl =
822 sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails;
823 const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
824 ASN1_INTEGER *serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
825
826 if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) {
827 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
828 result = NULL;
829 goto err;
830 }
831 if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) {
832 result = NULL;
833 goto err;
834 }
835 if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
836 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
837 CMPerr(0, CMP_R_WRONG_CERTID_IN_RP);
838 result = NULL;
839 goto err;
840 #endif
841 }
842 if (ASN1_INTEGER_cmp(serial,
843 OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) {
844 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
845 CMPerr(0, CMP_R_WRONG_SERIAL_IN_RP);
846 result = NULL;
847 goto err;
848 #endif
849 }
850 }
851
852 /* check number of any optionally present crls */
853 if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) {
854 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
855 result = NULL;
856 goto err;
857 }
858
859 err:
860 if (result == NULL
861 && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
862 ERR_add_error_data(1, buf);
863
864 end:
865 OSSL_CMP_MSG_free(rr);
866 OSSL_CMP_MSG_free(rp);
867 return result;
868 }
869
870 STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx)
871 {
872 OSSL_CMP_MSG *genm;
873 OSSL_CMP_MSG *genp = NULL;
874 STACK_OF(OSSL_CMP_ITAV) *rcvd_itavs = NULL;
875
876 if (ctx == NULL) {
877 CMPerr(0, CMP_R_INVALID_ARGS);
878 return 0;
879 }
880
881 if ((genm = ossl_cmp_genm_new(ctx)) == NULL)
882 goto err;
883
884 if (!send_receive_check(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP))
885 goto err;
886
887 /* received stack of itavs not to be freed with the genp */
888 rcvd_itavs = genp->body->value.genp;
889 genp->body->value.genp = NULL;
890
891 err:
892 OSSL_CMP_MSG_free(genm);
893 OSSL_CMP_MSG_free(genp);
894
895 return rcvd_itavs; /* recv_itavs == NULL indicates an error */
896 }