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