/src/openssl33/crypto/cmp/cmp_server.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2007-2023 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 | | /* general CMP server functions */ |
13 | | |
14 | | #include <openssl/asn1t.h> |
15 | | |
16 | | #include "cmp_local.h" |
17 | | |
18 | | /* explicit #includes not strictly needed since implied by the above: */ |
19 | | #include <openssl/cmp.h> |
20 | | #include <openssl/err.h> |
21 | | |
22 | | /* the context for the generic CMP server */ |
23 | | struct ossl_cmp_srv_ctx_st |
24 | | { |
25 | | OSSL_CMP_CTX *ctx; /* CMP client context reused for transactionID etc. */ |
26 | | void *custom_ctx; /* application-specific server context */ |
27 | | int certReqId; /* of ir/cr/kur, OSSL_CMP_CERTREQID_NONE for p10cr */ |
28 | | int polling; /* current transaction is in polling mode */ |
29 | | |
30 | | OSSL_CMP_SRV_cert_request_cb_t process_cert_request; |
31 | | OSSL_CMP_SRV_rr_cb_t process_rr; |
32 | | OSSL_CMP_SRV_genm_cb_t process_genm; |
33 | | OSSL_CMP_SRV_error_cb_t process_error; |
34 | | OSSL_CMP_SRV_certConf_cb_t process_certConf; |
35 | | OSSL_CMP_SRV_pollReq_cb_t process_pollReq; |
36 | | OSSL_CMP_SRV_delayed_delivery_cb_t delayed_delivery; |
37 | | OSSL_CMP_SRV_clean_transaction_cb_t clean_transaction; |
38 | | |
39 | | int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */ |
40 | | int acceptUnprotected; /* Accept requests with no/invalid prot. */ |
41 | | int acceptRAVerified; /* Accept ir/cr/kur with POPO RAVerified */ |
42 | | int grantImplicitConfirm; /* Grant implicit confirmation if requested */ |
43 | | |
44 | | }; /* OSSL_CMP_SRV_CTX */ |
45 | | |
46 | | void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx) |
47 | 37.8k | { |
48 | 37.8k | if (srv_ctx == NULL) |
49 | 0 | return; |
50 | | |
51 | 37.8k | OSSL_CMP_CTX_free(srv_ctx->ctx); |
52 | 37.8k | OPENSSL_free(srv_ctx); |
53 | 37.8k | } |
54 | | |
55 | | OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq) |
56 | 37.8k | { |
57 | 37.8k | OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX)); |
58 | | |
59 | 37.8k | if (ctx == NULL) |
60 | 0 | goto err; |
61 | | |
62 | 37.8k | if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL) |
63 | 0 | goto err; |
64 | 37.8k | ctx->certReqId = OSSL_CMP_CERTREQID_INVALID; |
65 | 37.8k | ctx->polling = 0; |
66 | | |
67 | | /* all other elements are initialized to 0 or NULL, respectively */ |
68 | 37.8k | return ctx; |
69 | 0 | err: |
70 | 0 | OSSL_CMP_SRV_CTX_free(ctx); |
71 | 0 | return NULL; |
72 | 37.8k | } |
73 | | |
74 | | int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx, |
75 | | OSSL_CMP_SRV_cert_request_cb_t process_cert_request, |
76 | | OSSL_CMP_SRV_rr_cb_t process_rr, |
77 | | OSSL_CMP_SRV_genm_cb_t process_genm, |
78 | | OSSL_CMP_SRV_error_cb_t process_error, |
79 | | OSSL_CMP_SRV_certConf_cb_t process_certConf, |
80 | | OSSL_CMP_SRV_pollReq_cb_t process_pollReq) |
81 | 37.8k | { |
82 | 37.8k | if (srv_ctx == NULL) { |
83 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
84 | 0 | return 0; |
85 | 0 | } |
86 | 37.8k | srv_ctx->custom_ctx = custom_ctx; |
87 | 37.8k | srv_ctx->process_cert_request = process_cert_request; |
88 | 37.8k | srv_ctx->process_rr = process_rr; |
89 | 37.8k | srv_ctx->process_genm = process_genm; |
90 | 37.8k | srv_ctx->process_error = process_error; |
91 | 37.8k | srv_ctx->process_certConf = process_certConf; |
92 | 37.8k | srv_ctx->process_pollReq = process_pollReq; |
93 | 37.8k | return 1; |
94 | 37.8k | } |
95 | | |
96 | | int OSSL_CMP_SRV_CTX_init_trans(OSSL_CMP_SRV_CTX *srv_ctx, |
97 | | OSSL_CMP_SRV_delayed_delivery_cb_t delay, |
98 | | OSSL_CMP_SRV_clean_transaction_cb_t clean) |
99 | 24.5k | { |
100 | 24.5k | if (srv_ctx == NULL) { |
101 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
102 | 0 | return 0; |
103 | 0 | } |
104 | 24.5k | srv_ctx->delayed_delivery = delay; |
105 | 24.5k | srv_ctx->clean_transaction = clean; |
106 | 24.5k | return 1; |
107 | 24.5k | } |
108 | | |
109 | | OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx) |
110 | 37.8k | { |
111 | 37.8k | if (srv_ctx == NULL) { |
112 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
113 | 0 | return NULL; |
114 | 0 | } |
115 | 37.8k | return srv_ctx->ctx; |
116 | 37.8k | } |
117 | | |
118 | | void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx) |
119 | 0 | { |
120 | 0 | if (srv_ctx == NULL) { |
121 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
122 | 0 | return NULL; |
123 | 0 | } |
124 | 0 | return srv_ctx->custom_ctx; |
125 | 0 | } |
126 | | |
127 | | int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx, |
128 | | int val) |
129 | 0 | { |
130 | 0 | if (srv_ctx == NULL) { |
131 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
132 | 0 | return 0; |
133 | 0 | } |
134 | 0 | srv_ctx->sendUnprotectedErrors = val != 0; |
135 | 0 | return 1; |
136 | 0 | } |
137 | | |
138 | | int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val) |
139 | 0 | { |
140 | 0 | if (srv_ctx == NULL) { |
141 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
142 | 0 | return 0; |
143 | 0 | } |
144 | 0 | srv_ctx->acceptUnprotected = val != 0; |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | | int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val) |
149 | 0 | { |
150 | 0 | if (srv_ctx == NULL) { |
151 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
152 | 0 | return 0; |
153 | 0 | } |
154 | 0 | srv_ctx->acceptRAVerified = val != 0; |
155 | 0 | return 1; |
156 | 0 | } |
157 | | |
158 | | int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx, |
159 | | int val) |
160 | 0 | { |
161 | 0 | if (srv_ctx == NULL) { |
162 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
163 | 0 | return 0; |
164 | 0 | } |
165 | 0 | srv_ctx->grantImplicitConfirm = val != 0; |
166 | 0 | return 1; |
167 | 0 | } |
168 | | |
169 | | /* return error msg with waiting status if polling is initiated, else NULL */ |
170 | | static OSSL_CMP_MSG *delayed_delivery(OSSL_CMP_SRV_CTX *srv_ctx, |
171 | | const OSSL_CMP_MSG *req) |
172 | 7.86k | { |
173 | 7.86k | int ret; |
174 | 7.86k | unsigned long err; |
175 | 7.86k | int status = OSSL_CMP_PKISTATUS_waiting, |
176 | 7.86k | fail_info = 0, errorCode = 0; |
177 | 7.86k | const char *txt = NULL, *details = NULL; |
178 | 7.86k | OSSL_CMP_PKISI *si; |
179 | 7.86k | OSSL_CMP_MSG *msg; |
180 | | |
181 | 7.86k | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL |
182 | 7.86k | && srv_ctx->delayed_delivery != NULL)) |
183 | 0 | return NULL; |
184 | | |
185 | 7.86k | ret = srv_ctx->delayed_delivery(srv_ctx, req); |
186 | 7.86k | if (ret == 0) |
187 | 7.86k | return NULL; |
188 | 0 | if (ret == 1) { |
189 | 0 | srv_ctx->polling = 1; |
190 | 0 | } else { |
191 | 0 | status = OSSL_CMP_PKISTATUS_rejection; |
192 | 0 | fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_systemFailure; |
193 | 0 | txt = "server application error"; |
194 | 0 | err = ERR_peek_error(); |
195 | 0 | errorCode = ERR_GET_REASON(err); |
196 | 0 | details = ERR_reason_error_string(err); |
197 | 0 | } |
198 | |
|
199 | 0 | si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt); |
200 | 0 | if (si == NULL) |
201 | 0 | return NULL; |
202 | | |
203 | 0 | msg = ossl_cmp_error_new(srv_ctx->ctx, si, errorCode, details, |
204 | 0 | srv_ctx->sendUnprotectedErrors); |
205 | 0 | OSSL_CMP_PKISI_free(si); |
206 | 0 | return msg; |
207 | 0 | } |
208 | | |
209 | | /* |
210 | | * Processes an ir/cr/p10cr/kur and returns a certification response. |
211 | | * Only handles the first certification request contained in req |
212 | | * returns an ip/cp/kup on success and NULL on error |
213 | | */ |
214 | | static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx, |
215 | | const OSSL_CMP_MSG *req) |
216 | 2.49k | { |
217 | 2.49k | OSSL_CMP_MSG *msg = NULL; |
218 | 2.49k | OSSL_CMP_PKISI *si = NULL; |
219 | 2.49k | X509 *certOut = NULL; |
220 | 2.49k | STACK_OF(X509) *chainOut = NULL, *caPubs = NULL; |
221 | 2.49k | const OSSL_CRMF_MSG *crm = NULL; |
222 | 2.49k | const X509_REQ *p10cr = NULL; |
223 | 2.49k | int bodytype; |
224 | 2.49k | int certReqId; |
225 | | |
226 | 2.49k | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) |
227 | 0 | return NULL; |
228 | | |
229 | 2.49k | switch (OSSL_CMP_MSG_get_bodytype(req)) { |
230 | 1.68k | case OSSL_CMP_PKIBODY_P10CR: |
231 | 1.94k | case OSSL_CMP_PKIBODY_CR: |
232 | 1.94k | bodytype = OSSL_CMP_PKIBODY_CP; |
233 | 1.94k | break; |
234 | 522 | case OSSL_CMP_PKIBODY_IR: |
235 | 522 | bodytype = OSSL_CMP_PKIBODY_IP; |
236 | 522 | break; |
237 | 20 | case OSSL_CMP_PKIBODY_KUR: |
238 | 20 | bodytype = OSSL_CMP_PKIBODY_KUP; |
239 | 20 | break; |
240 | 0 | default: |
241 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
242 | 0 | return NULL; |
243 | 2.49k | } |
244 | | |
245 | 2.49k | if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) { |
246 | 1.68k | certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */ |
247 | 1.68k | p10cr = req->body->value.p10cr; |
248 | 1.68k | } else { |
249 | 805 | OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */ |
250 | | |
251 | 805 | if (sk_OSSL_CRMF_MSG_num(reqs) != 1) { |
252 | 13 | ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED); |
253 | 13 | return NULL; |
254 | 13 | } |
255 | 792 | if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) { |
256 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND); |
257 | 0 | return NULL; |
258 | 0 | } |
259 | 792 | certReqId = OSSL_CRMF_MSG_get_certReqId(crm); |
260 | 792 | if (certReqId != OSSL_CMP_CERTREQID) { /* so far, only possible value */ |
261 | 112 | ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID); |
262 | 112 | return NULL; |
263 | 112 | } |
264 | 792 | } |
265 | 2.36k | srv_ctx->certReqId = certReqId; |
266 | | |
267 | 2.36k | if (!ossl_cmp_verify_popo(srv_ctx->ctx, req, srv_ctx->acceptRAVerified)) { |
268 | | /* Proof of possession could not be verified */ |
269 | 0 | si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, |
270 | 0 | 1 << OSSL_CMP_PKIFAILUREINFO_badPOP, |
271 | 0 | ERR_reason_error_string(ERR_peek_error())); |
272 | 0 | if (si == NULL) |
273 | 0 | return NULL; |
274 | 2.36k | } else { |
275 | 2.36k | OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req); |
276 | | |
277 | 2.36k | si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr, |
278 | 2.36k | &certOut, &chainOut, &caPubs); |
279 | 2.36k | if (si == NULL) |
280 | 2.36k | goto err; |
281 | 0 | if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting) |
282 | 0 | srv_ctx->polling = 1; |
283 | | /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */ |
284 | 0 | if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx, |
285 | 0 | OSSL_CMP_OPT_IMPLICIT_CONFIRM, |
286 | 0 | ossl_cmp_hdr_has_implicitConfirm(hdr) |
287 | 0 | && srv_ctx->grantImplicitConfirm |
288 | | /* do not set if polling starts: */ |
289 | 0 | && certOut != NULL)) |
290 | 0 | goto err; |
291 | 0 | } |
292 | | |
293 | 0 | msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si, |
294 | 0 | certOut, NULL /* enc */, chainOut, caPubs, |
295 | 0 | srv_ctx->sendUnprotectedErrors); |
296 | | /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */ |
297 | 0 | if (msg == NULL) |
298 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP); |
299 | |
|
300 | 2.36k | err: |
301 | 2.36k | OSSL_CMP_PKISI_free(si); |
302 | 2.36k | X509_free(certOut); |
303 | 2.36k | OSSL_STACK_OF_X509_free(chainOut); |
304 | 2.36k | OSSL_STACK_OF_X509_free(caPubs); |
305 | 2.36k | return msg; |
306 | 0 | } |
307 | | |
308 | | static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx, |
309 | | const OSSL_CMP_MSG *req) |
310 | 162 | { |
311 | 162 | OSSL_CMP_MSG *msg = NULL; |
312 | 162 | OSSL_CMP_REVDETAILS *details; |
313 | 162 | OSSL_CRMF_CERTID *certId = NULL; |
314 | 162 | OSSL_CRMF_CERTTEMPLATE *tmpl; |
315 | 162 | const X509_NAME *issuer; |
316 | 162 | const ASN1_INTEGER *serial; |
317 | 162 | OSSL_CMP_PKISI *si; |
318 | | |
319 | 162 | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) |
320 | 0 | return NULL; |
321 | | |
322 | 162 | if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) { |
323 | 118 | ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED); |
324 | 118 | return NULL; |
325 | 118 | } |
326 | 44 | details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr, 0); |
327 | 44 | if (details == NULL) { |
328 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE); |
329 | 0 | return NULL; |
330 | 0 | } |
331 | | |
332 | 44 | tmpl = details->certDetails; |
333 | 44 | issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl); |
334 | 44 | serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl); |
335 | 44 | if (issuer != NULL && serial != NULL |
336 | 44 | && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL) |
337 | 0 | return NULL; |
338 | 44 | if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL) |
339 | 44 | goto err; |
340 | | |
341 | 0 | if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId, |
342 | 0 | srv_ctx->sendUnprotectedErrors)) == NULL) |
343 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR); |
344 | |
|
345 | 44 | err: |
346 | 44 | OSSL_CRMF_CERTID_free(certId); |
347 | 44 | OSSL_CMP_PKISI_free(si); |
348 | 44 | return msg; |
349 | 0 | } |
350 | | |
351 | | /* |
352 | | * Processes genm and creates a genp message mirroring the contents of the |
353 | | * incoming message |
354 | | */ |
355 | | static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx, |
356 | | const OSSL_CMP_MSG *req) |
357 | 376 | { |
358 | 376 | OSSL_CMP_GENMSGCONTENT *itavs; |
359 | 376 | OSSL_CMP_MSG *msg; |
360 | | |
361 | 376 | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) |
362 | 0 | return NULL; |
363 | | |
364 | 376 | if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs)) |
365 | 376 | return NULL; |
366 | | |
367 | 0 | msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs); |
368 | 0 | sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free); |
369 | 0 | return msg; |
370 | 376 | } |
371 | | |
372 | | static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx, |
373 | | const OSSL_CMP_MSG *req) |
374 | 768 | { |
375 | 768 | OSSL_CMP_ERRORMSGCONTENT *errorContent; |
376 | 768 | OSSL_CMP_MSG *msg; |
377 | | |
378 | 768 | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) |
379 | 0 | return NULL; |
380 | 768 | errorContent = req->body->value.error; |
381 | 768 | srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo, |
382 | 768 | errorContent->errorCode, errorContent->errorDetails); |
383 | | |
384 | 768 | if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL) |
385 | 768 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF); |
386 | 768 | return msg; |
387 | 768 | } |
388 | | |
389 | | static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx, |
390 | | const OSSL_CMP_MSG *req) |
391 | 289 | { |
392 | 289 | OSSL_CMP_CTX *ctx; |
393 | 289 | OSSL_CMP_CERTCONFIRMCONTENT *ccc; |
394 | 289 | int num; |
395 | 289 | OSSL_CMP_MSG *msg = NULL; |
396 | 289 | OSSL_CMP_CERTSTATUS *status = NULL; |
397 | | |
398 | 289 | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) |
399 | 0 | return NULL; |
400 | | |
401 | 289 | ctx = srv_ctx->ctx; |
402 | 289 | ccc = req->body->value.certConf; |
403 | 289 | num = sk_OSSL_CMP_CERTSTATUS_num(ccc); |
404 | | |
405 | 289 | if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1 |
406 | 289 | || ctx->status != OSSL_CMP_PKISTATUS_trans) { |
407 | 289 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF); |
408 | 289 | return NULL; |
409 | 289 | } |
410 | | |
411 | 0 | if (num == 0) { |
412 | 0 | ossl_cmp_err(ctx, "certificate rejected by client"); |
413 | 0 | } else { |
414 | 0 | if (num > 1) |
415 | 0 | ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored"); |
416 | 0 | status = sk_OSSL_CMP_CERTSTATUS_value(ccc, 0); |
417 | 0 | } |
418 | |
|
419 | 0 | if (status != NULL) { |
420 | 0 | int certReqId = ossl_cmp_asn1_get_int(status->certReqId); |
421 | 0 | ASN1_OCTET_STRING *certHash = status->certHash; |
422 | 0 | OSSL_CMP_PKISI *si = status->statusInfo; |
423 | |
|
424 | 0 | if (certReqId != srv_ctx->certReqId) { |
425 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID); |
426 | 0 | return NULL; |
427 | 0 | } |
428 | 0 | if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si)) |
429 | 0 | return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */ |
430 | | |
431 | 0 | if (si != NULL |
432 | 0 | && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) { |
433 | 0 | int pki_status = ossl_cmp_pkisi_get_status(si); |
434 | 0 | const char *str = ossl_cmp_PKIStatus_to_string(pki_status); |
435 | |
|
436 | 0 | ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s", |
437 | 0 | str == NULL ? "without" : "with", |
438 | 0 | str == NULL ? "PKIStatus" : str); |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | 0 | if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL) |
443 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF); |
444 | 0 | return msg; |
445 | 0 | } |
446 | | |
447 | | /* pollReq is handled separately, to avoid recursive call */ |
448 | | static OSSL_CMP_MSG *process_non_polling_request(OSSL_CMP_SRV_CTX *srv_ctx, |
449 | | const OSSL_CMP_MSG *req) |
450 | 7.86k | { |
451 | 7.86k | OSSL_CMP_MSG *rsp = NULL; |
452 | | |
453 | 7.86k | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL |
454 | 7.86k | && req->body != NULL)) |
455 | 0 | return NULL; |
456 | | |
457 | 7.86k | switch (OSSL_CMP_MSG_get_bodytype(req)) { |
458 | 909 | case OSSL_CMP_PKIBODY_IR: |
459 | 1.33k | case OSSL_CMP_PKIBODY_CR: |
460 | 4.33k | case OSSL_CMP_PKIBODY_P10CR: |
461 | 4.54k | case OSSL_CMP_PKIBODY_KUR: |
462 | 4.54k | if (srv_ctx->process_cert_request == NULL) |
463 | 4.54k | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
464 | 4.54k | else |
465 | 4.54k | rsp = process_cert_request(srv_ctx, req); |
466 | 4.54k | break; |
467 | 71 | case OSSL_CMP_PKIBODY_RR: |
468 | 71 | if (srv_ctx->process_rr == NULL) |
469 | 71 | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
470 | 71 | else |
471 | 71 | rsp = process_rr(srv_ctx, req); |
472 | 71 | break; |
473 | 273 | case OSSL_CMP_PKIBODY_GENM: |
474 | 273 | if (srv_ctx->process_genm == NULL) |
475 | 273 | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
476 | 273 | else |
477 | 273 | rsp = process_genm(srv_ctx, req); |
478 | 273 | break; |
479 | 581 | case OSSL_CMP_PKIBODY_ERROR: |
480 | 581 | if (srv_ctx->process_error == NULL) |
481 | 581 | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
482 | 581 | else |
483 | 581 | rsp = process_error(srv_ctx, req); |
484 | 581 | break; |
485 | 189 | case OSSL_CMP_PKIBODY_CERTCONF: |
486 | 189 | if (srv_ctx->process_certConf == NULL) |
487 | 189 | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
488 | 189 | else |
489 | 189 | rsp = process_certConf(srv_ctx, req); |
490 | 189 | break; |
491 | | |
492 | 0 | case OSSL_CMP_PKIBODY_POLLREQ: |
493 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
494 | 0 | break; |
495 | 2.20k | default: |
496 | 2.20k | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
497 | 2.20k | break; |
498 | 7.86k | } |
499 | | |
500 | 7.86k | return rsp; |
501 | 7.86k | } |
502 | | |
503 | | static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx, |
504 | | const OSSL_CMP_MSG *req) |
505 | 116 | { |
506 | 116 | OSSL_CMP_POLLREQCONTENT *prc; |
507 | 116 | OSSL_CMP_POLLREQ *pr; |
508 | 116 | int certReqId; |
509 | 116 | OSSL_CMP_MSG *orig_req; |
510 | 116 | int64_t check_after = 0; |
511 | 116 | OSSL_CMP_MSG *msg = NULL; |
512 | | |
513 | 116 | if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL)) |
514 | 0 | return NULL; |
515 | | |
516 | 116 | if (!srv_ctx->polling) { |
517 | 116 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
518 | 116 | return NULL; |
519 | 116 | } |
520 | | |
521 | 0 | prc = req->body->value.pollReq; |
522 | 0 | if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) { |
523 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED); |
524 | 0 | return NULL; |
525 | 0 | } |
526 | | |
527 | 0 | pr = sk_OSSL_CMP_POLLREQ_value(prc, 0); |
528 | 0 | certReqId = ossl_cmp_asn1_get_int(pr->certReqId); |
529 | 0 | if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId, |
530 | 0 | &orig_req, &check_after)) |
531 | 0 | return NULL; |
532 | | |
533 | 0 | if (orig_req != NULL) { |
534 | 0 | srv_ctx->polling = 0; |
535 | 0 | msg = process_non_polling_request(srv_ctx, orig_req); |
536 | 0 | OSSL_CMP_MSG_free(orig_req); |
537 | 0 | } else { |
538 | 0 | if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId, |
539 | 0 | check_after)) == NULL) |
540 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP); |
541 | 0 | } |
542 | 0 | return msg; |
543 | 0 | } |
544 | | |
545 | | /* |
546 | | * Determine whether missing/invalid protection of request message is allowed. |
547 | | * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error. |
548 | | */ |
549 | | static int unprotected_exception(const OSSL_CMP_CTX *ctx, |
550 | | const OSSL_CMP_MSG *req, |
551 | | int invalid_protection, |
552 | | int accept_unprotected_requests) |
553 | 13.1k | { |
554 | 13.1k | if (!ossl_assert(ctx != NULL && req != NULL)) |
555 | 0 | return -1; |
556 | | |
557 | 13.1k | if (accept_unprotected_requests) { |
558 | 0 | ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message", |
559 | 0 | invalid_protection ? "invalid" : "missing"); |
560 | 0 | return 1; |
561 | 0 | } |
562 | 13.1k | if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR |
563 | 13.1k | && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) { |
564 | 0 | ossl_cmp_warn(ctx, "ignoring missing protection of error message"); |
565 | 0 | return 1; |
566 | 0 | } |
567 | 13.1k | return 0; |
568 | 13.1k | } |
569 | | |
570 | | /* |
571 | | * returns created message and NULL on internal error |
572 | | */ |
573 | | OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx, |
574 | | const OSSL_CMP_MSG *req) |
575 | 24.5k | { |
576 | 24.5k | OSSL_CMP_CTX *ctx; |
577 | 24.5k | ASN1_OCTET_STRING *backup_secret; |
578 | 24.5k | OSSL_CMP_PKIHEADER *hdr; |
579 | 24.5k | int req_type, rsp_type; |
580 | 24.5k | int req_verified = 0; |
581 | 24.5k | OSSL_CMP_MSG *rsp = NULL; |
582 | | |
583 | 24.5k | if (srv_ctx == NULL || srv_ctx->ctx == NULL |
584 | 24.5k | || req == NULL || req->body == NULL |
585 | 24.5k | || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) { |
586 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
587 | 0 | return 0; |
588 | 0 | } |
589 | 24.5k | ctx = srv_ctx->ctx; |
590 | 24.5k | backup_secret = ctx->secretValue; |
591 | 24.5k | req_type = OSSL_CMP_MSG_get_bodytype(req); |
592 | 24.5k | ossl_cmp_log1(DEBUG, ctx, |
593 | 24.5k | "received %s", ossl_cmp_bodytype_to_string(req_type)); |
594 | | |
595 | | /* |
596 | | * Some things need to be done already before validating the message in |
597 | | * order to be able to send an error message as far as needed and possible. |
598 | | */ |
599 | 24.5k | if (hdr->sender->type != GEN_DIRNAME) { |
600 | 16.5k | ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED); |
601 | 16.5k | goto err; |
602 | 16.5k | } |
603 | 7.98k | if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName)) |
604 | 4 | goto err; |
605 | | |
606 | 7.98k | if (srv_ctx->polling && req_type != OSSL_CMP_PKIBODY_POLLREQ |
607 | 7.98k | && req_type != OSSL_CMP_PKIBODY_ERROR) { |
608 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_EXPECTED_POLLREQ); |
609 | 0 | goto err; |
610 | 0 | } |
611 | | |
612 | 7.98k | switch (req_type) { |
613 | 909 | case OSSL_CMP_PKIBODY_IR: |
614 | 1.33k | case OSSL_CMP_PKIBODY_CR: |
615 | 4.33k | case OSSL_CMP_PKIBODY_P10CR: |
616 | 4.54k | case OSSL_CMP_PKIBODY_KUR: |
617 | 4.61k | case OSSL_CMP_PKIBODY_RR: |
618 | 4.89k | case OSSL_CMP_PKIBODY_GENM: |
619 | 5.47k | case OSSL_CMP_PKIBODY_ERROR: |
620 | 5.47k | if (ctx->transactionID != NULL) { |
621 | 0 | char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID); |
622 | |
|
623 | 0 | if (tid != NULL) |
624 | 0 | ossl_cmp_log1(WARN, ctx, |
625 | 0 | "Assuming that last transaction with ID=%s got aborted", |
626 | 0 | tid); |
627 | 0 | OPENSSL_free(tid); |
628 | 0 | } |
629 | | /* start of a new transaction, reset transactionID and senderNonce */ |
630 | 5.47k | if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL) |
631 | 5.47k | || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL)) |
632 | 0 | goto err; |
633 | | |
634 | 5.47k | if (srv_ctx->clean_transaction != NULL |
635 | 5.47k | && !srv_ctx->clean_transaction(srv_ctx, NULL)) { |
636 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE); |
637 | 0 | goto err; |
638 | 0 | } |
639 | | |
640 | 5.47k | break; |
641 | 5.47k | default: |
642 | | /* transactionID should be already initialized */ |
643 | 2.50k | if (ctx->transactionID == NULL) { |
644 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
645 | | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
646 | | goto err; |
647 | | #endif |
648 | 2.50k | } |
649 | 7.98k | } |
650 | | |
651 | 7.98k | req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception, |
652 | 7.98k | srv_ctx->acceptUnprotected); |
653 | 7.98k | if (ctx->secretValue != NULL && ctx->pkey != NULL |
654 | 7.98k | && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC) |
655 | 0 | ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */ |
656 | 7.98k | if (!req_verified) |
657 | 0 | goto err; |
658 | | |
659 | 7.98k | if (req_type == OSSL_CMP_PKIBODY_POLLREQ) { |
660 | 116 | if (srv_ctx->process_pollReq == NULL) |
661 | 116 | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY); |
662 | 116 | else |
663 | 116 | rsp = process_pollReq(srv_ctx, req); |
664 | 7.86k | } else { |
665 | 7.86k | if (srv_ctx->delayed_delivery != NULL |
666 | 7.86k | && (rsp = delayed_delivery(srv_ctx, req)) != NULL) { |
667 | 0 | goto err; |
668 | 0 | } |
669 | 7.86k | rsp = process_non_polling_request(srv_ctx, req); |
670 | 7.86k | } |
671 | | |
672 | 24.5k | err: |
673 | 24.5k | if (rsp == NULL) { |
674 | | /* on error, try to respond with CMP error message to client */ |
675 | 24.5k | const char *data = NULL, *reason = NULL; |
676 | 24.5k | int flags = 0; |
677 | 24.5k | unsigned long err = ERR_peek_error_data(&data, &flags); |
678 | 24.5k | int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest; |
679 | | /* fail_info is not very specific */ |
680 | 24.5k | OSSL_CMP_PKISI *si = NULL; |
681 | | |
682 | 24.5k | if (!req_verified) { |
683 | | /* |
684 | | * Above ossl_cmp_msg_check_update() was not successfully executed, |
685 | | * which normally would set ctx->transactionID and ctx->recipNonce. |
686 | | * So anyway try to provide the right transactionID and recipNonce, |
687 | | * while ignoring any (extra) error in next two function calls. |
688 | | */ |
689 | 16.5k | if (ctx->transactionID == NULL) |
690 | 16.5k | (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID); |
691 | 16.5k | (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce); |
692 | 16.5k | } |
693 | | |
694 | 24.5k | if ((flags & ERR_TXT_STRING) == 0 || *data == '\0') |
695 | 22.8k | data = NULL; |
696 | 24.5k | reason = ERR_reason_error_string(err); |
697 | 24.5k | if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, |
698 | 24.5k | fail_info, reason)) != NULL) { |
699 | 24.5k | rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err, |
700 | 24.5k | data, srv_ctx->sendUnprotectedErrors); |
701 | 24.5k | OSSL_CMP_PKISI_free(si); |
702 | 24.5k | } |
703 | 24.5k | } |
704 | 24.5k | OSSL_CMP_CTX_print_errors(ctx); |
705 | 24.5k | ctx->secretValue = backup_secret; |
706 | | |
707 | 24.5k | rsp_type = |
708 | 24.5k | rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR; |
709 | 24.5k | if (rsp != NULL) |
710 | 0 | ossl_cmp_log1(DEBUG, ctx, |
711 | 24.5k | "sending %s", ossl_cmp_bodytype_to_string(rsp_type)); |
712 | 24.5k | else |
713 | 24.5k | ossl_cmp_log(ERR, ctx, "cannot send proper CMP response"); |
714 | | |
715 | | /* determine whether to keep the transaction open or not */ |
716 | 24.5k | ctx->status = OSSL_CMP_PKISTATUS_trans; |
717 | 24.5k | switch (rsp_type) { |
718 | 0 | case OSSL_CMP_PKIBODY_IP: |
719 | 0 | case OSSL_CMP_PKIBODY_CP: |
720 | 0 | case OSSL_CMP_PKIBODY_KUP: |
721 | 0 | if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0) |
722 | 0 | break; |
723 | | /* fall through */ |
724 | | |
725 | 24.5k | case OSSL_CMP_PKIBODY_ERROR: |
726 | 24.5k | if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp)) |
727 | 0 | break; |
728 | | /* fall through */ |
729 | | |
730 | 24.5k | case OSSL_CMP_PKIBODY_RP: |
731 | 24.5k | case OSSL_CMP_PKIBODY_PKICONF: |
732 | 24.5k | case OSSL_CMP_PKIBODY_GENP: |
733 | | /* Other terminating response message types are not supported */ |
734 | 24.5k | srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID; |
735 | | /* Prepare for next transaction, ignoring any errors here: */ |
736 | 24.5k | if (srv_ctx->clean_transaction != NULL) |
737 | 24.5k | (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID); |
738 | 24.5k | (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL); |
739 | 24.5k | (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL); |
740 | 24.5k | ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */ |
741 | | |
742 | 24.5k | default: /* not closing transaction in other cases */ |
743 | 24.5k | break; |
744 | 24.5k | } |
745 | 24.5k | return rsp; |
746 | 24.5k | } |
747 | | |
748 | | /* |
749 | | * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client. |
750 | | * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg. |
751 | | * returns received message on success, else NULL and pushes an element on the |
752 | | * error stack. |
753 | | */ |
754 | | OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx, |
755 | | const OSSL_CMP_MSG *req) |
756 | 0 | { |
757 | 0 | OSSL_CMP_SRV_CTX *srv_ctx = NULL; |
758 | |
|
759 | 0 | if (client_ctx == NULL || req == NULL) { |
760 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
761 | 0 | return NULL; |
762 | 0 | } |
763 | | |
764 | 0 | if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) { |
765 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR); |
766 | 0 | return NULL; |
767 | 0 | } |
768 | | |
769 | 0 | return OSSL_CMP_SRV_process_request(srv_ctx, req); |
770 | 0 | } |