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