/src/openssl/crypto/cmp/cmp_msg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2007-2025 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 | | /* CMP functions for PKIMessage construction */ |
13 | | |
14 | | #include "cmp_local.h" |
15 | | |
16 | | /* explicit #includes not strictly needed since implied by the above: */ |
17 | | #include <openssl/asn1t.h> |
18 | | #include <openssl/cmp.h> |
19 | | #include <openssl/crmf.h> |
20 | | #include <openssl/err.h> |
21 | | #include <openssl/x509.h> |
22 | | #include <openssl/pem.h> |
23 | | #include <openssl/bio.h> |
24 | | #include <internal/cms.h> |
25 | | |
26 | | OSSL_CMP_MSG *OSSL_CMP_MSG_new(OSSL_LIB_CTX *libctx, const char *propq) |
27 | 30.9k | { |
28 | 30.9k | OSSL_CMP_MSG *msg = NULL; |
29 | | |
30 | 30.9k | msg = (OSSL_CMP_MSG *)ASN1_item_new_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), |
31 | 30.9k | libctx, propq); |
32 | 30.9k | if (!ossl_cmp_msg_set0_libctx(msg, libctx, propq)) { |
33 | 0 | OSSL_CMP_MSG_free(msg); |
34 | 0 | msg = NULL; |
35 | 0 | } |
36 | 30.9k | return msg; |
37 | 30.9k | } |
38 | | |
39 | | void OSSL_CMP_MSG_free(OSSL_CMP_MSG *msg) |
40 | 91.4k | { |
41 | 91.4k | ASN1_item_free((ASN1_VALUE *)msg, ASN1_ITEM_rptr(OSSL_CMP_MSG)); |
42 | 91.4k | } |
43 | | |
44 | | /* |
45 | | * This should only be used if the X509 object was embedded inside another |
46 | | * asn1 object and it needs a libctx to operate. |
47 | | * Use OSSL_CMP_MSG_new() instead if possible. |
48 | | */ |
49 | | int ossl_cmp_msg_set0_libctx(OSSL_CMP_MSG *msg, OSSL_LIB_CTX *libctx, |
50 | | const char *propq) |
51 | 30.9k | { |
52 | 30.9k | if (msg != NULL) { |
53 | 30.9k | msg->libctx = libctx; |
54 | 30.9k | OPENSSL_free(msg->propq); |
55 | 30.9k | msg->propq = NULL; |
56 | 30.9k | if (propq != NULL) { |
57 | 0 | msg->propq = OPENSSL_strdup(propq); |
58 | 0 | if (msg->propq == NULL) |
59 | 0 | return 0; |
60 | 0 | } |
61 | 30.9k | } |
62 | 30.9k | return 1; |
63 | 30.9k | } |
64 | | |
65 | | OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg) |
66 | 68.4k | { |
67 | 68.4k | if (msg == NULL) { |
68 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
69 | 0 | return NULL; |
70 | 0 | } |
71 | 68.4k | return msg->header; |
72 | 68.4k | } |
73 | | |
74 | | const char *ossl_cmp_bodytype_to_string(int type) |
75 | 28.3k | { |
76 | 28.3k | static const char *type_names[] = { |
77 | 28.3k | "IR", "IP", "CR", "CP", "P10CR", |
78 | 28.3k | "POPDECC", "POPDECR", "KUR", "KUP", |
79 | 28.3k | "KRR", "KRP", "RR", "RP", "CCR", "CCP", |
80 | 28.3k | "CKUANN", "CANN", "RANN", "CRLANN", "PKICONF", "NESTED", |
81 | 28.3k | "GENM", "GENP", "ERROR", "CERTCONF", "POLLREQ", "POLLREP", |
82 | 28.3k | }; |
83 | | |
84 | 28.3k | if (type < 0 || type > OSSL_CMP_PKIBODY_TYPE_MAX) |
85 | 0 | return "illegal body type"; |
86 | 28.3k | return type_names[type]; |
87 | 28.3k | } |
88 | | |
89 | | int ossl_cmp_msg_set_bodytype(OSSL_CMP_MSG *msg, int type) |
90 | 30.9k | { |
91 | 30.9k | if (!ossl_assert(msg != NULL && msg->body != NULL)) |
92 | 0 | return 0; |
93 | | |
94 | 30.9k | msg->body->type = type; |
95 | 30.9k | return 1; |
96 | 30.9k | } |
97 | | |
98 | | int OSSL_CMP_MSG_get_bodytype(const OSSL_CMP_MSG *msg) |
99 | 75.0k | { |
100 | 75.0k | if (!ossl_assert(msg != NULL && msg->body != NULL)) |
101 | 0 | return -1; |
102 | | |
103 | 75.0k | return msg->body->type; |
104 | 75.0k | } |
105 | | |
106 | | X509_PUBKEY *OSSL_CMP_MSG_get0_certreq_publickey(const OSSL_CMP_MSG *msg) |
107 | 0 | { |
108 | 0 | const OSSL_CRMF_MSGS *reqs; |
109 | 0 | const OSSL_CRMF_MSG *crm; |
110 | 0 | const OSSL_CRMF_CERTTEMPLATE *tmpl; |
111 | 0 | X509_PUBKEY *pubkey; |
112 | |
|
113 | 0 | switch (OSSL_CMP_MSG_get_bodytype(msg)) { |
114 | 0 | case OSSL_CMP_PKIBODY_IR: |
115 | 0 | case OSSL_CMP_PKIBODY_CR: |
116 | 0 | case OSSL_CMP_PKIBODY_KUR: |
117 | 0 | reqs = msg->body->value.ir; /* value.ir is same for cr and kur */ |
118 | 0 | if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) { |
119 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | 0 | if ((tmpl = OSSL_CRMF_MSG_get0_tmpl(crm)) == NULL |
123 | 0 | || (pubkey = OSSL_CRMF_CERTTEMPLATE_get0_publicKey(tmpl)) == NULL) { |
124 | 0 | ERR_raise(ERR_LIB_CMP, CRMF_R_POPO_MISSING_PUBLIC_KEY); |
125 | 0 | return NULL; |
126 | 0 | } |
127 | 0 | return pubkey; |
128 | 0 | default: |
129 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
130 | 0 | return NULL; |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | /* Add an extension to the referenced extension stack, which may be NULL */ |
135 | | static int add1_extension(X509_EXTENSIONS **pexts, int nid, int crit, void *ex) |
136 | 0 | { |
137 | 0 | X509_EXTENSION *ext; |
138 | 0 | int res; |
139 | |
|
140 | 0 | if (!ossl_assert(pexts != NULL)) /* pointer to var must not be NULL */ |
141 | 0 | return 0; |
142 | | |
143 | 0 | if ((ext = X509V3_EXT_i2d(nid, crit, ex)) == NULL) |
144 | 0 | return 0; |
145 | | |
146 | 0 | res = X509v3_add_ext(pexts, ext, 0) != NULL; |
147 | 0 | X509_EXTENSION_free(ext); |
148 | 0 | return res; |
149 | 0 | } |
150 | | |
151 | | /* Add a CRL revocation reason code to extension stack, which may be NULL */ |
152 | | static int add_crl_reason_extension(X509_EXTENSIONS **pexts, int reason_code) |
153 | 0 | { |
154 | 0 | ASN1_ENUMERATED *val = ASN1_ENUMERATED_new(); |
155 | 0 | int res = 0; |
156 | |
|
157 | 0 | if (val != NULL && ASN1_ENUMERATED_set(val, reason_code)) |
158 | 0 | res = add1_extension(pexts, NID_crl_reason, 0 /* non-critical */, val); |
159 | 0 | ASN1_ENUMERATED_free(val); |
160 | 0 | return res; |
161 | 0 | } |
162 | | |
163 | | OSSL_CMP_MSG *ossl_cmp_msg_create(OSSL_CMP_CTX *ctx, int bodytype) |
164 | 30.9k | { |
165 | 30.9k | OSSL_CMP_MSG *msg = NULL; |
166 | | |
167 | 30.9k | if (!ossl_assert(ctx != NULL)) |
168 | 0 | return NULL; |
169 | | |
170 | 30.9k | if ((msg = OSSL_CMP_MSG_new(ctx->libctx, ctx->propq)) == NULL) |
171 | 0 | return NULL; |
172 | 30.9k | if (!ossl_cmp_hdr_init(ctx, msg->header) |
173 | 30.9k | || !ossl_cmp_msg_set_bodytype(msg, bodytype)) |
174 | 0 | goto err; |
175 | 30.9k | if (ctx->geninfo_ITAVs != NULL |
176 | 30.9k | && !ossl_cmp_hdr_generalInfo_push1_items(msg->header, |
177 | 0 | ctx->geninfo_ITAVs)) |
178 | 0 | goto err; |
179 | | |
180 | 30.9k | switch (bodytype) { |
181 | 546 | case OSSL_CMP_PKIBODY_IR: |
182 | 706 | case OSSL_CMP_PKIBODY_CR: |
183 | 762 | case OSSL_CMP_PKIBODY_KUR: |
184 | 762 | if ((msg->body->value.ir = OSSL_CRMF_MSGS_new()) == NULL) |
185 | 0 | goto err; |
186 | 762 | return msg; |
187 | | |
188 | 160 | case OSSL_CMP_PKIBODY_P10CR: |
189 | 160 | if (ctx->p10CSR == NULL) { |
190 | 160 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_P10CSR); |
191 | 160 | goto err; |
192 | 160 | } |
193 | 0 | if ((msg->body->value.p10cr = X509_REQ_dup(ctx->p10CSR)) == NULL) |
194 | 0 | goto err; |
195 | 0 | return msg; |
196 | | |
197 | 0 | case OSSL_CMP_PKIBODY_IP: |
198 | 0 | case OSSL_CMP_PKIBODY_CP: |
199 | 0 | case OSSL_CMP_PKIBODY_KUP: |
200 | 0 | if ((msg->body->value.ip = OSSL_CMP_CERTREPMESSAGE_new()) == NULL) |
201 | 0 | goto err; |
202 | 0 | return msg; |
203 | | |
204 | 204 | case OSSL_CMP_PKIBODY_RR: |
205 | 204 | if ((msg->body->value.rr = sk_OSSL_CMP_REVDETAILS_new_null()) == NULL) |
206 | 0 | goto err; |
207 | 204 | return msg; |
208 | 0 | case OSSL_CMP_PKIBODY_RP: |
209 | 0 | if ((msg->body->value.rp = OSSL_CMP_REVREPCONTENT_new()) == NULL) |
210 | 0 | goto err; |
211 | 0 | return msg; |
212 | | |
213 | 0 | case OSSL_CMP_PKIBODY_CERTCONF: |
214 | 0 | if ((msg->body->value.certConf = |
215 | 0 | sk_OSSL_CMP_CERTSTATUS_new_null()) == NULL) |
216 | 0 | goto err; |
217 | 0 | return msg; |
218 | 278 | case OSSL_CMP_PKIBODY_PKICONF: |
219 | 278 | if ((msg->body->value.pkiconf = ASN1_TYPE_new()) == NULL) |
220 | 0 | goto err; |
221 | 278 | ASN1_TYPE_set(msg->body->value.pkiconf, V_ASN1_NULL, NULL); |
222 | 278 | return msg; |
223 | | |
224 | 390 | case OSSL_CMP_PKIBODY_POLLREQ: |
225 | 390 | if ((msg->body->value.pollReq = sk_OSSL_CMP_POLLREQ_new_null()) == NULL) |
226 | 0 | goto err; |
227 | 390 | return msg; |
228 | 0 | case OSSL_CMP_PKIBODY_POLLREP: |
229 | 0 | if ((msg->body->value.pollRep = sk_OSSL_CMP_POLLREP_new_null()) == NULL) |
230 | 0 | goto err; |
231 | 0 | return msg; |
232 | | |
233 | 746 | case OSSL_CMP_PKIBODY_GENM: |
234 | 746 | case OSSL_CMP_PKIBODY_GENP: |
235 | 746 | if ((msg->body->value.genm = sk_OSSL_CMP_ITAV_new_null()) == NULL) |
236 | 0 | goto err; |
237 | 746 | return msg; |
238 | | |
239 | 28.3k | case OSSL_CMP_PKIBODY_ERROR: |
240 | 28.3k | if ((msg->body->value.error = OSSL_CMP_ERRORMSGCONTENT_new()) == NULL) |
241 | 0 | goto err; |
242 | 28.3k | return msg; |
243 | | |
244 | 0 | default: |
245 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY); |
246 | 0 | goto err; |
247 | 30.9k | } |
248 | | |
249 | 160 | err: |
250 | 160 | OSSL_CMP_MSG_free(msg); |
251 | 160 | return NULL; |
252 | 30.9k | } |
253 | | |
254 | | #define HAS_SAN(ctx) \ |
255 | 1.31k | (sk_GENERAL_NAME_num((ctx)->subjectAltNames) > 0 \ |
256 | 1.31k | || OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) == 1) |
257 | | |
258 | | static const X509_NAME *determine_subj(OSSL_CMP_CTX *ctx, int for_KUR, |
259 | | const X509_NAME *ref_subj) |
260 | 762 | { |
261 | 762 | if (ctx->subjectName != NULL) |
262 | 0 | return IS_NULL_DN(ctx->subjectName) ? NULL : ctx->subjectName; |
263 | 762 | if (ctx->p10CSR != NULL) /* first default is from any given CSR */ |
264 | 0 | return X509_REQ_get_subject_name(ctx->p10CSR); |
265 | 762 | if (for_KUR || !HAS_SAN(ctx)) |
266 | | /* |
267 | | * For KUR, copy subject from any reference cert as fallback. |
268 | | * For IR or CR, do the same only if there is no subjectAltName. |
269 | | */ |
270 | 762 | return ref_subj; |
271 | 0 | return NULL; |
272 | 762 | } |
273 | | |
274 | | OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid) |
275 | 303 | { |
276 | 303 | OSSL_CRMF_MSG *crm = NULL; |
277 | 303 | int central_keygen = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_POPO_METHOD) |
278 | 303 | == OSSL_CRMF_POPO_NONE; |
279 | 303 | X509 *refcert = ctx->oldCert != NULL ? ctx->oldCert : ctx->cert; |
280 | | /* refcert defaults to current client cert */ |
281 | 303 | EVP_PKEY *rkey = ossl_cmp_ctx_get0_newPubkey(ctx); |
282 | 303 | STACK_OF(GENERAL_NAME) *default_sans = NULL; |
283 | 303 | const X509_NAME *ref_subj = |
284 | 303 | refcert != NULL ? X509_get_subject_name(refcert) : NULL; |
285 | 303 | const X509_NAME *subject = determine_subj(ctx, for_KUR, ref_subj); |
286 | 303 | const X509_NAME *issuer = ctx->issuer != NULL || refcert == NULL |
287 | 303 | ? (IS_NULL_DN(ctx->issuer) ? NULL : ctx->issuer) |
288 | 303 | : X509_get_issuer_name(refcert); |
289 | 303 | int crit = ctx->setSubjectAltNameCritical || subject == NULL; |
290 | | /* RFC5280: subjectAltName MUST be critical if subject is null */ |
291 | 303 | OSSL_CRMF_CERTTEMPLATE *tmpl; |
292 | 303 | X509_EXTENSIONS *exts = NULL; |
293 | | |
294 | 303 | if (rkey == NULL && !central_keygen) { |
295 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
296 | | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PUBLIC_KEY); |
297 | | return NULL; |
298 | | #endif |
299 | 0 | } |
300 | 303 | if (for_KUR && refcert == NULL && ctx->p10CSR == NULL) { |
301 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT); |
302 | 0 | return NULL; |
303 | 0 | } |
304 | 303 | if ((crm = OSSL_CRMF_MSG_new()) == NULL) |
305 | 0 | return NULL; |
306 | 303 | tmpl = OSSL_CRMF_MSG_get0_tmpl(crm); |
307 | 303 | if (!OSSL_CRMF_MSG_set_certReqId(crm, rid) |
308 | | /* |
309 | | * fill certTemplate, corresponding to CertificationRequestInfo |
310 | | * of PKCS#10. The rkey param cannot be NULL so far - |
311 | | * it could be NULL if centralized key creation was supported |
312 | | */ |
313 | 303 | || !OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_MSG_get0_tmpl(crm), rkey, |
314 | 303 | subject, issuer, NULL /* serial */)) |
315 | 0 | goto err; |
316 | 303 | if (rkey != NULL && central_keygen) |
317 | 0 | X509_PUBKEY_set0_public_key(OSSL_CRMF_CERTTEMPLATE_get0_publicKey(tmpl), |
318 | 0 | NULL, 0); |
319 | | |
320 | 303 | if (ctx->days != 0) { |
321 | 0 | time_t now = time(NULL); |
322 | 0 | ASN1_TIME *notBefore = ASN1_TIME_adj(NULL, now, 0, 0); |
323 | 0 | ASN1_TIME *notAfter = ASN1_TIME_adj(NULL, now, ctx->days, 0); |
324 | |
|
325 | 0 | if (notBefore == NULL |
326 | 0 | || notAfter == NULL |
327 | 0 | || !OSSL_CRMF_MSG_set0_validity(crm, notBefore, notAfter)) { |
328 | 0 | ASN1_TIME_free(notBefore); |
329 | 0 | ASN1_TIME_free(notAfter); |
330 | 0 | goto err; |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | | /* extensions */ |
335 | 303 | if (ctx->p10CSR != NULL |
336 | 303 | && (exts = X509_REQ_get_extensions(ctx->p10CSR)) == NULL) |
337 | 0 | goto err; |
338 | 303 | if (!ctx->SubjectAltName_nodefault && !HAS_SAN(ctx) && refcert != NULL |
339 | 303 | && (default_sans = X509V3_get_d2i(X509_get0_extensions(refcert), |
340 | 303 | NID_subject_alt_name, NULL, NULL)) |
341 | 303 | != NULL |
342 | 303 | && !add1_extension(&exts, NID_subject_alt_name, crit, default_sans)) |
343 | 0 | goto err; |
344 | 303 | if (sk_X509_EXTENSION_num(ctx->reqExtensions) > 0 /* augment/override existing ones */ |
345 | 303 | && X509v3_add_extensions(&exts, ctx->reqExtensions) == NULL) |
346 | 0 | goto err; |
347 | 303 | if (sk_GENERAL_NAME_num(ctx->subjectAltNames) > 0 |
348 | 303 | && !add1_extension(&exts, NID_subject_alt_name, |
349 | 0 | crit, ctx->subjectAltNames)) |
350 | 0 | goto err; |
351 | 303 | if (ctx->policies != NULL |
352 | 303 | && !add1_extension(&exts, NID_certificate_policies, |
353 | 0 | ctx->setPoliciesCritical, ctx->policies)) |
354 | 0 | goto err; |
355 | 303 | if (!OSSL_CRMF_MSG_set0_extensions(crm, exts)) |
356 | 0 | goto err; |
357 | 303 | exts = NULL; |
358 | | /* end fill certTemplate, now set any controls */ |
359 | | |
360 | | /* for KUR, set OldCertId according to D.6 */ |
361 | 303 | if (for_KUR && refcert != NULL) { |
362 | 23 | OSSL_CRMF_CERTID *cid = |
363 | 23 | OSSL_CRMF_CERTID_gen(X509_get_issuer_name(refcert), |
364 | 23 | X509_get0_serialNumber(refcert)); |
365 | 23 | int ret; |
366 | | |
367 | 23 | if (cid == NULL) |
368 | 0 | goto err; |
369 | 23 | ret = OSSL_CRMF_MSG_set1_regCtrl_oldCertID(crm, cid); |
370 | 23 | OSSL_CRMF_CERTID_free(cid); |
371 | 23 | if (ret == 0) |
372 | 0 | goto err; |
373 | 23 | } |
374 | | |
375 | 303 | goto end; |
376 | | |
377 | 303 | err: |
378 | 0 | OSSL_CRMF_MSG_free(crm); |
379 | 0 | crm = NULL; |
380 | |
|
381 | 303 | end: |
382 | 303 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
383 | 303 | sk_GENERAL_NAME_pop_free(default_sans, GENERAL_NAME_free); |
384 | 303 | return crm; |
385 | 0 | } |
386 | | |
387 | | OSSL_CMP_MSG *ossl_cmp_certreq_new(OSSL_CMP_CTX *ctx, int type, |
388 | | const OSSL_CRMF_MSG *crm) |
389 | 922 | { |
390 | 922 | OSSL_CMP_MSG *msg; |
391 | 922 | OSSL_CRMF_MSG *local_crm = NULL; |
392 | | |
393 | 922 | if (!ossl_assert(ctx != NULL)) |
394 | 0 | return NULL; |
395 | | |
396 | 922 | if (type != OSSL_CMP_PKIBODY_IR && type != OSSL_CMP_PKIBODY_CR |
397 | 922 | && type != OSSL_CMP_PKIBODY_KUR && type != OSSL_CMP_PKIBODY_P10CR) { |
398 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
399 | 0 | return NULL; |
400 | 0 | } |
401 | 922 | if (type == OSSL_CMP_PKIBODY_P10CR && crm != NULL) { |
402 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
403 | 0 | return NULL; |
404 | 0 | } |
405 | | |
406 | 922 | if ((msg = ossl_cmp_msg_create(ctx, type)) == NULL) |
407 | 160 | goto err; |
408 | | |
409 | | /* header */ |
410 | 762 | if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header)) |
411 | 0 | goto err; |
412 | | |
413 | | /* body */ |
414 | | /* For P10CR the content has already been set in OSSL_CMP_MSG_create */ |
415 | 762 | if (type != OSSL_CMP_PKIBODY_P10CR) { |
416 | 762 | EVP_PKEY *privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1); |
417 | | |
418 | | /* privkey is ctx->newPkey (if private, else NULL) or ctx->pkey */ |
419 | 762 | if (ctx->popoMethod >= OSSL_CRMF_POPO_SIGNATURE && privkey == NULL) { |
420 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY_FOR_POPO); |
421 | 0 | goto err; |
422 | 0 | } |
423 | 762 | if (crm == NULL) { |
424 | 762 | local_crm = OSSL_CMP_CTX_setup_CRM(ctx, |
425 | 762 | type == OSSL_CMP_PKIBODY_KUR, |
426 | 762 | OSSL_CMP_CERTREQID); |
427 | 762 | if (local_crm == NULL |
428 | 762 | || !OSSL_CRMF_MSG_create_popo(ctx->popoMethod, local_crm, |
429 | 762 | privkey, ctx->digest, |
430 | 762 | ctx->libctx, ctx->propq)) |
431 | 0 | goto err; |
432 | 762 | } else { |
433 | 0 | if ((local_crm = OSSL_CRMF_MSG_dup(crm)) == NULL) |
434 | 0 | goto err; |
435 | 0 | } |
436 | | |
437 | | /* value.ir is same for cr and kur */ |
438 | 762 | if (!sk_OSSL_CRMF_MSG_push(msg->body->value.ir, local_crm)) |
439 | 0 | goto err; |
440 | 762 | local_crm = NULL; |
441 | 762 | } |
442 | | |
443 | 762 | if (!ossl_cmp_msg_protect(ctx, msg)) |
444 | 762 | goto err; |
445 | | |
446 | 0 | return msg; |
447 | | |
448 | 922 | err: |
449 | 922 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREQ); |
450 | 922 | OSSL_CRMF_MSG_free(local_crm); |
451 | 922 | OSSL_CMP_MSG_free(msg); |
452 | 922 | return NULL; |
453 | 762 | } |
454 | | |
455 | | #ifndef OPENSSL_NO_CMS |
456 | | static OSSL_CRMF_ENCRYPTEDKEY *enc_privkey(OSSL_CMP_CTX *ctx, const EVP_PKEY *pkey) |
457 | 0 | { |
458 | 0 | OSSL_CRMF_ENCRYPTEDKEY *ek = NULL; |
459 | 0 | CMS_EnvelopedData *envData = NULL; |
460 | 0 | BIO *privbio = NULL; |
461 | 0 | EVP_CIPHER *cipher = NULL; |
462 | 0 | X509 *recip = ctx->validatedSrvCert; /* this is the client cert */ |
463 | 0 | STACK_OF(X509) *encryption_recips = sk_X509_new_reserve(NULL, 1); |
464 | |
|
465 | 0 | if (encryption_recips == NULL |
466 | 0 | || !X509_add_cert(encryption_recips, recip, X509_ADD_FLAG_UP_REF)) |
467 | 0 | goto err; |
468 | | |
469 | 0 | privbio = BIO_new(BIO_s_mem()); |
470 | 0 | if (privbio == NULL || i2d_PrivateKey_bio(privbio, pkey) <= 0) |
471 | 0 | goto err; |
472 | 0 | ossl_cmp_set_own_chain(ctx); |
473 | 0 | cipher = EVP_CIPHER_fetch(ctx->libctx, SN_aes_256_cbc, ctx->propq); |
474 | 0 | envData = ossl_cms_sign_encrypt(privbio, ctx->cert, ctx->chain, ctx->pkey, CMS_BINARY, |
475 | 0 | encryption_recips, cipher, CMS_BINARY, |
476 | 0 | ctx->libctx, ctx->propq); |
477 | 0 | EVP_CIPHER_free(cipher); |
478 | 0 | if (envData == NULL) |
479 | 0 | goto err; |
480 | 0 | ek = OSSL_CRMF_ENCRYPTEDKEY_init_envdata(envData); |
481 | |
|
482 | 0 | err: |
483 | 0 | sk_X509_pop_free(encryption_recips, X509_free); |
484 | 0 | BIO_free(privbio); |
485 | 0 | if (ek == NULL) |
486 | 0 | M_ASN1_free_of(envData, CMS_EnvelopedData); |
487 | |
|
488 | 0 | return ek; |
489 | 0 | } |
490 | | #endif |
491 | | |
492 | | OSSL_CMP_MSG *ossl_cmp_certrep_new(OSSL_CMP_CTX *ctx, int bodytype, |
493 | | int certReqId, const OSSL_CMP_PKISI *si, |
494 | | X509 *cert, const EVP_PKEY *pkey, |
495 | | const X509 *encryption_recip, |
496 | | STACK_OF(X509) *chain, STACK_OF(X509) *caPubs, |
497 | | int unprotectedErrors) |
498 | 0 | { |
499 | 0 | OSSL_CMP_MSG *msg = NULL; |
500 | 0 | OSSL_CMP_CERTREPMESSAGE *repMsg = NULL; |
501 | 0 | OSSL_CMP_CERTRESPONSE *resp = NULL; |
502 | 0 | int status = OSSL_CMP_PKISTATUS_unspecified; |
503 | |
|
504 | 0 | if (!ossl_assert(ctx != NULL && si != NULL)) |
505 | 0 | return NULL; |
506 | | |
507 | 0 | if ((msg = ossl_cmp_msg_create(ctx, bodytype)) == NULL) |
508 | 0 | goto err; |
509 | 0 | repMsg = msg->body->value.ip; /* value.ip is same for cp and kup */ |
510 | | |
511 | | /* header */ |
512 | 0 | if (ctx->implicitConfirm && !ossl_cmp_hdr_set_implicitConfirm(msg->header)) |
513 | 0 | goto err; |
514 | | |
515 | | /* body */ |
516 | 0 | if ((resp = OSSL_CMP_CERTRESPONSE_new()) == NULL) |
517 | 0 | goto err; |
518 | 0 | OSSL_CMP_PKISI_free(resp->status); |
519 | 0 | if ((resp->status = OSSL_CMP_PKISI_dup(si)) == NULL |
520 | 0 | || !ASN1_INTEGER_set(resp->certReqId, certReqId)) |
521 | 0 | goto err; |
522 | | |
523 | 0 | status = ossl_cmp_pkisi_get_status(resp->status); |
524 | 0 | if (status != OSSL_CMP_PKISTATUS_rejection |
525 | 0 | && status != OSSL_CMP_PKISTATUS_waiting && cert != NULL) { |
526 | 0 | if (encryption_recip != NULL) { |
527 | 0 | ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED); |
528 | 0 | goto err; |
529 | 0 | } |
530 | | |
531 | 0 | if ((resp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) |
532 | 0 | == NULL) |
533 | 0 | goto err; |
534 | 0 | resp->certifiedKeyPair->certOrEncCert->type = |
535 | 0 | OSSL_CMP_CERTORENCCERT_CERTIFICATE; |
536 | 0 | if (!X509_up_ref(cert)) |
537 | 0 | goto err; |
538 | 0 | resp->certifiedKeyPair->certOrEncCert->value.certificate = cert; |
539 | |
|
540 | 0 | if (pkey != NULL) { |
541 | 0 | #ifndef OPENSSL_NO_CMS |
542 | 0 | if ((resp->certifiedKeyPair->privateKey = enc_privkey(ctx, pkey)) == NULL) |
543 | 0 | goto err; |
544 | | #else |
545 | | ERR_raise(ERR_LIB_CMP, ERR_R_UNSUPPORTED); |
546 | | goto err; |
547 | | #endif |
548 | 0 | } |
549 | 0 | } |
550 | | |
551 | 0 | if (!sk_OSSL_CMP_CERTRESPONSE_push(repMsg->response, resp)) |
552 | 0 | goto err; |
553 | 0 | resp = NULL; |
554 | |
|
555 | 0 | if (bodytype == OSSL_CMP_PKIBODY_IP && caPubs != NULL |
556 | 0 | && (repMsg->caPubs = X509_chain_up_ref(caPubs)) == NULL) |
557 | 0 | goto err; |
558 | 0 | if (sk_X509_num(chain) > 0 |
559 | 0 | && !ossl_x509_add_certs_new(&msg->extraCerts, chain, |
560 | 0 | X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) |
561 | 0 | goto err; |
562 | | |
563 | 0 | if (!unprotectedErrors |
564 | 0 | || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection) |
565 | 0 | if (!ossl_cmp_msg_protect(ctx, msg)) |
566 | 0 | goto err; |
567 | | |
568 | 0 | return msg; |
569 | | |
570 | 0 | err: |
571 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP); |
572 | 0 | OSSL_CMP_CERTRESPONSE_free(resp); |
573 | 0 | OSSL_CMP_MSG_free(msg); |
574 | 0 | return NULL; |
575 | 0 | } |
576 | | |
577 | | OSSL_CMP_MSG *ossl_cmp_rr_new(OSSL_CMP_CTX *ctx) |
578 | 139 | { |
579 | 139 | OSSL_CMP_MSG *msg = NULL; |
580 | 139 | const X509_NAME *issuer = NULL; |
581 | 139 | const X509_NAME *subject = NULL; |
582 | 139 | const ASN1_INTEGER *serialNumber = NULL; |
583 | 139 | EVP_PKEY *pubkey = NULL; |
584 | 139 | OSSL_CMP_REVDETAILS *rd; |
585 | 139 | int ret; |
586 | | |
587 | 139 | if (!ossl_assert(ctx != NULL |
588 | 139 | && (ctx->oldCert != NULL || ctx->p10CSR != NULL |
589 | 139 | || (ctx->serialNumber != NULL && ctx->issuer != NULL)))) |
590 | 0 | return NULL; |
591 | | |
592 | 139 | if ((rd = OSSL_CMP_REVDETAILS_new()) == NULL) |
593 | 0 | goto err; |
594 | | |
595 | 139 | if (ctx->serialNumber != NULL && ctx->issuer != NULL) { |
596 | 0 | issuer = ctx->issuer; |
597 | 0 | serialNumber = ctx->serialNumber; |
598 | 139 | } else if (ctx->oldCert != NULL) { |
599 | 139 | issuer = X509_get_issuer_name(ctx->oldCert); |
600 | 139 | serialNumber = X509_get0_serialNumber(ctx->oldCert); |
601 | 139 | } else if (ctx->p10CSR != NULL) { |
602 | 0 | pubkey = X509_REQ_get0_pubkey(ctx->p10CSR); |
603 | 0 | subject = X509_REQ_get_subject_name(ctx->p10CSR); |
604 | 0 | } else { |
605 | 0 | goto err; |
606 | 0 | } |
607 | | |
608 | | /* Fill the template from the contents of the certificate to be revoked */ |
609 | 139 | ret = OSSL_CRMF_CERTTEMPLATE_fill(rd->certDetails, pubkey, subject, |
610 | 139 | issuer, serialNumber); |
611 | 139 | if (!ret) |
612 | 0 | goto err; |
613 | | |
614 | | /* revocation reason code is optional */ |
615 | 139 | if (ctx->revocationReason != CRL_REASON_NONE |
616 | 139 | && !add_crl_reason_extension(&rd->crlEntryDetails, |
617 | 0 | ctx->revocationReason)) |
618 | 0 | goto err; |
619 | | |
620 | 139 | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RR)) == NULL) |
621 | 0 | goto err; |
622 | | |
623 | 139 | if (!sk_OSSL_CMP_REVDETAILS_push(msg->body->value.rr, rd)) |
624 | 0 | goto err; |
625 | 139 | rd = NULL; |
626 | | /* Revocation Passphrase according to section 5.3.19.9 could be set here */ |
627 | | |
628 | 139 | if (!ossl_cmp_msg_protect(ctx, msg)) |
629 | 139 | goto err; |
630 | | |
631 | 0 | return msg; |
632 | | |
633 | 139 | err: |
634 | 139 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR); |
635 | 139 | OSSL_CMP_MSG_free(msg); |
636 | 139 | OSSL_CMP_REVDETAILS_free(rd); |
637 | 139 | return NULL; |
638 | 139 | } |
639 | | |
640 | | OSSL_CMP_MSG *ossl_cmp_rp_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si, |
641 | | const OSSL_CRMF_CERTID *cid, int unprotectedErrors) |
642 | 0 | { |
643 | 0 | OSSL_CMP_REVREPCONTENT *rep = NULL; |
644 | 0 | OSSL_CMP_PKISI *si1 = NULL; |
645 | 0 | OSSL_CRMF_CERTID *cid_copy = NULL; |
646 | 0 | OSSL_CMP_MSG *msg = NULL; |
647 | |
|
648 | 0 | if (!ossl_assert(ctx != NULL && si != NULL)) |
649 | 0 | return NULL; |
650 | | |
651 | 0 | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_RP)) == NULL) |
652 | 0 | goto err; |
653 | 0 | rep = msg->body->value.rp; |
654 | |
|
655 | 0 | if ((si1 = OSSL_CMP_PKISI_dup(si)) == NULL |
656 | 0 | || !sk_OSSL_CMP_PKISI_push(rep->status, si1)) |
657 | 0 | goto err; |
658 | | |
659 | 0 | si1 = NULL; /* ownership transferred to rep->status */ |
660 | |
|
661 | 0 | if ((rep->revCerts = sk_OSSL_CRMF_CERTID_new_null()) == NULL) |
662 | 0 | goto err; |
663 | 0 | if (cid != NULL) { |
664 | 0 | if ((cid_copy = OSSL_CRMF_CERTID_dup(cid)) == NULL |
665 | 0 | || !sk_OSSL_CRMF_CERTID_push(rep->revCerts, cid_copy)) |
666 | 0 | goto err; |
667 | | |
668 | 0 | cid_copy = NULL; /* ownership transferred to rep->revCerts */ |
669 | 0 | } |
670 | | |
671 | 0 | if (!unprotectedErrors |
672 | 0 | || ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_rejection) |
673 | 0 | if (!ossl_cmp_msg_protect(ctx, msg)) |
674 | 0 | goto err; |
675 | | |
676 | 0 | return msg; |
677 | | |
678 | 0 | err: |
679 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RP); |
680 | 0 | OSSL_CMP_PKISI_free(si1); |
681 | 0 | OSSL_CRMF_CERTID_free(cid_copy); |
682 | 0 | OSSL_CMP_MSG_free(msg); |
683 | 0 | return NULL; |
684 | 0 | } |
685 | | |
686 | | OSSL_CMP_MSG *ossl_cmp_pkiconf_new(OSSL_CMP_CTX *ctx) |
687 | 278 | { |
688 | 278 | OSSL_CMP_MSG *msg; |
689 | | |
690 | 278 | if (!ossl_assert(ctx != NULL)) |
691 | 0 | return NULL; |
692 | | |
693 | 278 | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_PKICONF)) == NULL) |
694 | 0 | goto err; |
695 | 278 | if (ossl_cmp_msg_protect(ctx, msg)) |
696 | 0 | return msg; |
697 | | |
698 | 278 | err: |
699 | 278 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF); |
700 | 278 | OSSL_CMP_MSG_free(msg); |
701 | 278 | return NULL; |
702 | 278 | } |
703 | | |
704 | | int ossl_cmp_msg_gen_push0_ITAV(OSSL_CMP_MSG *msg, OSSL_CMP_ITAV *itav) |
705 | 0 | { |
706 | 0 | int bodytype; |
707 | |
|
708 | 0 | if (!ossl_assert(msg != NULL && itav != NULL)) |
709 | 0 | return 0; |
710 | | |
711 | 0 | bodytype = OSSL_CMP_MSG_get_bodytype(msg); |
712 | 0 | if (bodytype != OSSL_CMP_PKIBODY_GENM |
713 | 0 | && bodytype != OSSL_CMP_PKIBODY_GENP) { |
714 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
715 | 0 | return 0; |
716 | 0 | } |
717 | | |
718 | | /* value.genp has the same structure, so this works for genp as well */ |
719 | 0 | return OSSL_CMP_ITAV_push0_stack_item(&msg->body->value.genm, itav); |
720 | 0 | } |
721 | | |
722 | | int ossl_cmp_msg_gen_push1_ITAVs(OSSL_CMP_MSG *msg, |
723 | | const STACK_OF(OSSL_CMP_ITAV) *itavs) |
724 | 0 | { |
725 | 0 | int i; |
726 | 0 | OSSL_CMP_ITAV *itav = NULL; |
727 | |
|
728 | 0 | if (!ossl_assert(msg != NULL)) |
729 | 0 | return 0; |
730 | | |
731 | 0 | for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) { |
732 | 0 | itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i)); |
733 | 0 | if (itav == NULL |
734 | 0 | || !ossl_cmp_msg_gen_push0_ITAV(msg, itav)) { |
735 | 0 | OSSL_CMP_ITAV_free(itav); |
736 | 0 | return 0; |
737 | 0 | } |
738 | 0 | } |
739 | 0 | return 1; |
740 | 0 | } |
741 | | |
742 | | /* |
743 | | * Creates a new General Message/Response with a copy of the given itav stack |
744 | | * returns a pointer to the PKIMessage on success, NULL on error |
745 | | */ |
746 | | static OSSL_CMP_MSG *gen_new(OSSL_CMP_CTX *ctx, |
747 | | const STACK_OF(OSSL_CMP_ITAV) *itavs, |
748 | | int body_type, int err_code) |
749 | 746 | { |
750 | 746 | OSSL_CMP_MSG *msg = NULL; |
751 | | |
752 | 746 | if (!ossl_assert(ctx != NULL)) |
753 | 0 | return NULL; |
754 | | |
755 | 746 | if ((msg = ossl_cmp_msg_create(ctx, body_type)) == NULL) |
756 | 0 | return NULL; |
757 | | |
758 | 746 | if (itavs != NULL && !ossl_cmp_msg_gen_push1_ITAVs(msg, itavs)) |
759 | 0 | goto err; |
760 | | |
761 | 746 | if (!ossl_cmp_msg_protect(ctx, msg)) |
762 | 746 | goto err; |
763 | | |
764 | 0 | return msg; |
765 | | |
766 | 746 | err: |
767 | 746 | ERR_raise(ERR_LIB_CMP, err_code); |
768 | 746 | OSSL_CMP_MSG_free(msg); |
769 | 746 | return NULL; |
770 | 746 | } |
771 | | |
772 | | OSSL_CMP_MSG *ossl_cmp_genm_new(OSSL_CMP_CTX *ctx) |
773 | 746 | { |
774 | 746 | return gen_new(ctx, ctx->genm_ITAVs, |
775 | 746 | OSSL_CMP_PKIBODY_GENM, CMP_R_ERROR_CREATING_GENM); |
776 | 746 | } |
777 | | |
778 | | OSSL_CMP_MSG *ossl_cmp_genp_new(OSSL_CMP_CTX *ctx, |
779 | | const STACK_OF(OSSL_CMP_ITAV) *itavs) |
780 | 0 | { |
781 | 0 | return gen_new(ctx, itavs, |
782 | 0 | OSSL_CMP_PKIBODY_GENP, CMP_R_ERROR_CREATING_GENP); |
783 | 0 | } |
784 | | |
785 | | OSSL_CMP_MSG *ossl_cmp_error_new(OSSL_CMP_CTX *ctx, const OSSL_CMP_PKISI *si, |
786 | | int64_t errorCode, const char *details, |
787 | | int unprotected) |
788 | 28.3k | { |
789 | 28.3k | OSSL_CMP_MSG *msg = NULL; |
790 | 28.3k | const char *lib = NULL, *reason = NULL; |
791 | 28.3k | OSSL_CMP_PKIFREETEXT *ft; |
792 | | |
793 | 28.3k | if (!ossl_assert(ctx != NULL && si != NULL)) |
794 | 0 | return NULL; |
795 | | |
796 | 28.3k | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_ERROR)) == NULL) |
797 | 0 | goto err; |
798 | | |
799 | 28.3k | OSSL_CMP_PKISI_free(msg->body->value.error->pKIStatusInfo); |
800 | 28.3k | if ((msg->body->value.error->pKIStatusInfo = OSSL_CMP_PKISI_dup(si)) |
801 | 28.3k | == NULL) |
802 | 0 | goto err; |
803 | 28.3k | if ((msg->body->value.error->errorCode = ASN1_INTEGER_new()) == NULL) |
804 | 0 | goto err; |
805 | 28.3k | if (!ASN1_INTEGER_set_int64(msg->body->value.error->errorCode, errorCode)) |
806 | 0 | goto err; |
807 | 28.3k | if (errorCode > 0 |
808 | 28.3k | && (uint64_t)errorCode < ((uint64_t)ERR_SYSTEM_FLAG << 1)) { |
809 | 28.3k | lib = ERR_lib_error_string((unsigned long)errorCode); |
810 | 28.3k | reason = ERR_reason_error_string((unsigned long)errorCode); |
811 | 28.3k | } |
812 | 28.3k | if (lib != NULL || reason != NULL || details != NULL) { |
813 | 28.3k | if ((ft = sk_ASN1_UTF8STRING_new_null()) == NULL) |
814 | 0 | goto err; |
815 | 28.3k | msg->body->value.error->errorDetails = ft; |
816 | 28.3k | if (lib != NULL && *lib != '\0' |
817 | 28.3k | && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, lib, -1)) |
818 | 0 | goto err; |
819 | 28.3k | if (reason != NULL && *reason != '\0' |
820 | 28.3k | && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, reason, -1)) |
821 | 0 | goto err; |
822 | 28.3k | if (details != NULL |
823 | 28.3k | && !ossl_cmp_sk_ASN1_UTF8STRING_push_str(ft, details, -1)) |
824 | 0 | goto err; |
825 | 28.3k | } |
826 | | |
827 | 28.3k | if (!unprotected && !ossl_cmp_msg_protect(ctx, msg)) |
828 | 28.3k | goto err; |
829 | 0 | return msg; |
830 | | |
831 | 28.3k | err: |
832 | 28.3k | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_ERROR); |
833 | 28.3k | OSSL_CMP_MSG_free(msg); |
834 | 28.3k | return NULL; |
835 | 28.3k | } |
836 | | |
837 | | /* |
838 | | * Set the certHash field of a OSSL_CMP_CERTSTATUS structure. |
839 | | * This is used in the certConf message, for example, |
840 | | * to confirm that the certificate was received successfully. |
841 | | */ |
842 | | int ossl_cmp_certstatus_set0_certHash(OSSL_CMP_CERTSTATUS *certStatus, |
843 | | ASN1_OCTET_STRING *hash) |
844 | 0 | { |
845 | 0 | if (!ossl_assert(certStatus != NULL)) |
846 | 0 | return 0; |
847 | 0 | ASN1_OCTET_STRING_free(certStatus->certHash); |
848 | 0 | certStatus->certHash = hash; |
849 | 0 | return 1; |
850 | 0 | } |
851 | | |
852 | | OSSL_CMP_MSG *ossl_cmp_certConf_new(OSSL_CMP_CTX *ctx, int certReqId, |
853 | | int fail_info, const char *text) |
854 | 0 | { |
855 | 0 | OSSL_CMP_MSG *msg = NULL; |
856 | 0 | OSSL_CMP_CERTSTATUS *certStatus = NULL; |
857 | 0 | EVP_MD *md; |
858 | 0 | int is_fallback; |
859 | 0 | ASN1_OCTET_STRING *certHash = NULL; |
860 | 0 | OSSL_CMP_PKISI *sinfo; |
861 | |
|
862 | 0 | if (!ossl_assert(ctx != NULL && ctx->newCert != NULL |
863 | 0 | && (certReqId == OSSL_CMP_CERTREQID |
864 | 0 | || certReqId == OSSL_CMP_CERTREQID_NONE))) |
865 | 0 | return NULL; |
866 | | |
867 | 0 | if ((unsigned)fail_info > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) { |
868 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_FAIL_INFO_OUT_OF_RANGE); |
869 | 0 | return NULL; |
870 | 0 | } |
871 | | |
872 | 0 | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_CERTCONF)) == NULL) |
873 | 0 | goto err; |
874 | | |
875 | 0 | if ((certStatus = OSSL_CMP_CERTSTATUS_new()) == NULL) |
876 | 0 | goto err; |
877 | | /* consume certStatus into msg right away so it gets deallocated with msg */ |
878 | 0 | if (sk_OSSL_CMP_CERTSTATUS_push(msg->body->value.certConf, certStatus) < 1) { |
879 | 0 | OSSL_CMP_CERTSTATUS_free(certStatus); |
880 | 0 | goto err; |
881 | 0 | } |
882 | | |
883 | | /* set the ID of the certReq */ |
884 | 0 | if (!ASN1_INTEGER_set(certStatus->certReqId, certReqId)) |
885 | 0 | goto err; |
886 | | |
887 | 0 | certStatus->hashAlg = NULL; |
888 | | /* |
889 | | * The hash of the certificate, using the same hash algorithm |
890 | | * as is used to create and verify the certificate signature. |
891 | | * If not available, a fallback hash algorithm is used. |
892 | | */ |
893 | 0 | if ((certHash = X509_digest_sig(ctx->newCert, &md, &is_fallback)) == NULL) |
894 | 0 | goto err; |
895 | 0 | if (is_fallback) { |
896 | 0 | if (!ossl_cmp_hdr_set_pvno(msg->header, OSSL_CMP_PVNO_3)) |
897 | 0 | goto err; |
898 | 0 | if ((certStatus->hashAlg = X509_ALGOR_new()) == NULL) |
899 | 0 | goto err; |
900 | 0 | X509_ALGOR_set_md(certStatus->hashAlg, md); |
901 | 0 | } |
902 | 0 | EVP_MD_free(md); |
903 | |
|
904 | 0 | if (!ossl_cmp_certstatus_set0_certHash(certStatus, certHash)) |
905 | 0 | goto err; |
906 | 0 | certHash = NULL; |
907 | | /* |
908 | | * For any particular CertStatus, omission of the statusInfo field |
909 | | * indicates ACCEPTANCE of the specified certificate. Alternatively, |
910 | | * explicit status details (with respect to acceptance or rejection) MAY |
911 | | * be provided in the statusInfo field, perhaps for auditing purposes at |
912 | | * the CA/RA. |
913 | | */ |
914 | 0 | sinfo = fail_info != 0 ? |
915 | 0 | OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection, fail_info, text) : |
916 | 0 | OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_accepted, 0, text); |
917 | 0 | if (sinfo == NULL) |
918 | 0 | goto err; |
919 | 0 | certStatus->statusInfo = sinfo; |
920 | |
|
921 | 0 | if (!ossl_cmp_msg_protect(ctx, msg)) |
922 | 0 | goto err; |
923 | | |
924 | 0 | return msg; |
925 | | |
926 | 0 | err: |
927 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTCONF); |
928 | 0 | OSSL_CMP_MSG_free(msg); |
929 | 0 | ASN1_OCTET_STRING_free(certHash); |
930 | 0 | return NULL; |
931 | 0 | } |
932 | | |
933 | | OSSL_CMP_MSG *ossl_cmp_pollReq_new(OSSL_CMP_CTX *ctx, int crid) |
934 | 390 | { |
935 | 390 | OSSL_CMP_MSG *msg = NULL; |
936 | 390 | OSSL_CMP_POLLREQ *preq = NULL; |
937 | | |
938 | 390 | if (!ossl_assert(ctx != NULL)) |
939 | 0 | return NULL; |
940 | | |
941 | 390 | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREQ)) == NULL) |
942 | 0 | goto err; |
943 | | |
944 | 390 | if ((preq = OSSL_CMP_POLLREQ_new()) == NULL |
945 | 390 | || !ASN1_INTEGER_set(preq->certReqId, crid) |
946 | 390 | || !sk_OSSL_CMP_POLLREQ_push(msg->body->value.pollReq, preq)) |
947 | 0 | goto err; |
948 | | |
949 | 390 | preq = NULL; |
950 | 390 | if (!ossl_cmp_msg_protect(ctx, msg)) |
951 | 390 | goto err; |
952 | | |
953 | 0 | return msg; |
954 | | |
955 | 390 | err: |
956 | 390 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREQ); |
957 | 390 | OSSL_CMP_POLLREQ_free(preq); |
958 | 390 | OSSL_CMP_MSG_free(msg); |
959 | 390 | return NULL; |
960 | 390 | } |
961 | | |
962 | | OSSL_CMP_MSG *ossl_cmp_pollRep_new(OSSL_CMP_CTX *ctx, int crid, |
963 | | int64_t poll_after) |
964 | 0 | { |
965 | 0 | OSSL_CMP_MSG *msg; |
966 | 0 | OSSL_CMP_POLLREP *prep; |
967 | |
|
968 | 0 | if (!ossl_assert(ctx != NULL)) |
969 | 0 | return NULL; |
970 | | |
971 | 0 | if ((msg = ossl_cmp_msg_create(ctx, OSSL_CMP_PKIBODY_POLLREP)) == NULL) |
972 | 0 | goto err; |
973 | 0 | if ((prep = OSSL_CMP_POLLREP_new()) == NULL) |
974 | 0 | goto err; |
975 | 0 | if (!sk_OSSL_CMP_POLLREP_push(msg->body->value.pollRep, prep)) |
976 | 0 | goto err; |
977 | 0 | if (!ASN1_INTEGER_set(prep->certReqId, crid)) |
978 | 0 | goto err; |
979 | 0 | if (!ASN1_INTEGER_set_int64(prep->checkAfter, poll_after)) |
980 | 0 | goto err; |
981 | | |
982 | 0 | if (!ossl_cmp_msg_protect(ctx, msg)) |
983 | 0 | goto err; |
984 | 0 | return msg; |
985 | | |
986 | 0 | err: |
987 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP); |
988 | 0 | OSSL_CMP_MSG_free(msg); |
989 | 0 | return NULL; |
990 | 0 | } |
991 | | |
992 | | /*- |
993 | | * returns the status field of the RevRepContent with the given |
994 | | * request/sequence id inside a revocation response. |
995 | | * RevRepContent has the revocation statuses in same order as they were sent in |
996 | | * RevReqContent. |
997 | | * returns NULL on error |
998 | | */ |
999 | | OSSL_CMP_PKISI * |
1000 | | ossl_cmp_revrepcontent_get_pkisi(OSSL_CMP_REVREPCONTENT *rrep, int rsid) |
1001 | 0 | { |
1002 | 0 | OSSL_CMP_PKISI *status; |
1003 | |
|
1004 | 0 | if (!ossl_assert(rrep != NULL)) |
1005 | 0 | return NULL; |
1006 | | |
1007 | 0 | if ((status = sk_OSSL_CMP_PKISI_value(rrep->status, rsid)) != NULL) |
1008 | 0 | return status; |
1009 | | |
1010 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_PKISTATUSINFO_NOT_FOUND); |
1011 | 0 | return NULL; |
1012 | 0 | } |
1013 | | |
1014 | | /* |
1015 | | * returns the CertId field in the revCerts part of the RevRepContent |
1016 | | * with the given request/sequence id inside a revocation response. |
1017 | | * RevRepContent has the CertIds in same order as they were sent in |
1018 | | * RevReqContent. |
1019 | | * returns NULL on error |
1020 | | */ |
1021 | | OSSL_CRMF_CERTID * |
1022 | | ossl_cmp_revrepcontent_get_CertId(OSSL_CMP_REVREPCONTENT *rrep, int rsid) |
1023 | 0 | { |
1024 | 0 | OSSL_CRMF_CERTID *cid = NULL; |
1025 | |
|
1026 | 0 | if (!ossl_assert(rrep != NULL)) |
1027 | 0 | return NULL; |
1028 | | |
1029 | 0 | if ((cid = sk_OSSL_CRMF_CERTID_value(rrep->revCerts, rsid)) != NULL) |
1030 | 0 | return cid; |
1031 | | |
1032 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERTID_NOT_FOUND); |
1033 | 0 | return NULL; |
1034 | 0 | } |
1035 | | |
1036 | | static int suitable_rid(const ASN1_INTEGER *certReqId, int rid) |
1037 | 0 | { |
1038 | 0 | int trid; |
1039 | |
|
1040 | 0 | if (rid == OSSL_CMP_CERTREQID_NONE) |
1041 | 0 | return 1; |
1042 | | |
1043 | 0 | trid = ossl_cmp_asn1_get_int(certReqId); |
1044 | 0 | if (trid <= OSSL_CMP_CERTREQID_INVALID) { |
1045 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID); |
1046 | 0 | return 0; |
1047 | 0 | } |
1048 | 0 | return rid == trid; |
1049 | 0 | } |
1050 | | |
1051 | | /* |
1052 | | * returns a pointer to the PollResponse with the given CertReqId |
1053 | | * (or the first one in case -1) inside a PollRepContent |
1054 | | * returns NULL on error or if no suitable PollResponse available |
1055 | | */ |
1056 | | OSSL_CMP_POLLREP * |
1057 | | ossl_cmp_pollrepcontent_get0_pollrep(const OSSL_CMP_POLLREPCONTENT *prc, |
1058 | | int rid) |
1059 | 0 | { |
1060 | 0 | OSSL_CMP_POLLREP *pollRep = NULL; |
1061 | 0 | int i; |
1062 | |
|
1063 | 0 | if (!ossl_assert(prc != NULL)) |
1064 | 0 | return NULL; |
1065 | | |
1066 | 0 | for (i = 0; i < sk_OSSL_CMP_POLLREP_num(prc); i++) { |
1067 | 0 | pollRep = sk_OSSL_CMP_POLLREP_value(prc, i); |
1068 | 0 | if (suitable_rid(pollRep->certReqId, rid)) |
1069 | 0 | return pollRep; |
1070 | 0 | } |
1071 | | |
1072 | 0 | ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND, |
1073 | 0 | "expected certReqId = %d", rid); |
1074 | 0 | return NULL; |
1075 | 0 | } |
1076 | | |
1077 | | /* |
1078 | | * returns a pointer to the CertResponse with the given CertReqId |
1079 | | * (or the first one in case -1) inside a CertRepMessage |
1080 | | * returns NULL on error or if no suitable CertResponse available |
1081 | | */ |
1082 | | OSSL_CMP_CERTRESPONSE * |
1083 | | ossl_cmp_certrepmessage_get0_certresponse(const OSSL_CMP_CERTREPMESSAGE *crm, |
1084 | | int rid) |
1085 | 0 | { |
1086 | 0 | OSSL_CMP_CERTRESPONSE *crep = NULL; |
1087 | 0 | int i; |
1088 | |
|
1089 | 0 | if (!ossl_assert(crm != NULL && crm->response != NULL)) |
1090 | 0 | return NULL; |
1091 | | |
1092 | 0 | for (i = 0; i < sk_OSSL_CMP_CERTRESPONSE_num(crm->response); i++) { |
1093 | 0 | crep = sk_OSSL_CMP_CERTRESPONSE_value(crm->response, i); |
1094 | 0 | if (suitable_rid(crep->certReqId, rid)) |
1095 | 0 | return crep; |
1096 | 0 | } |
1097 | | |
1098 | 0 | ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTRESPONSE_NOT_FOUND, |
1099 | 0 | "expected certReqId = %d", rid); |
1100 | 0 | return NULL; |
1101 | 0 | } |
1102 | | |
1103 | | /*- |
1104 | | * Retrieve newly enrolled certificate and key from the given certResponse crep. |
1105 | | * Stores any centrally generated key in ctx->newPkey. |
1106 | | * In case of indirect POPO uses ctx->newPkey to decrypt the new certificate. |
1107 | | * Returns a pointer to a copy of the found certificate, or NULL if not found. |
1108 | | */ |
1109 | | X509 *ossl_cmp_certresponse_get1_cert(const OSSL_CMP_CTX *ctx, const OSSL_CMP_CERTRESPONSE *crep) |
1110 | 0 | { |
1111 | 0 | OSSL_CMP_CERTORENCCERT *coec; |
1112 | 0 | X509 *crt = NULL; |
1113 | 0 | OSSL_CRMF_ENCRYPTEDKEY *encr_key; |
1114 | 0 | EVP_PKEY *pkey = NULL; |
1115 | 0 | int central_keygen = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_POPO_METHOD) |
1116 | 0 | == OSSL_CRMF_POPO_NONE; |
1117 | |
|
1118 | 0 | if (crep->certifiedKeyPair == NULL) { |
1119 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND); |
1120 | 0 | return NULL; |
1121 | 0 | } |
1122 | 0 | encr_key = crep->certifiedKeyPair->privateKey; |
1123 | 0 | if (encr_key == NULL && central_keygen) { |
1124 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_CENTRAL_GEN_KEY); |
1125 | 0 | return NULL; |
1126 | 0 | } |
1127 | 0 | if (encr_key != NULL) { |
1128 | 0 | if (!central_keygen) { |
1129 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_CENTRAL_GEN_KEY); |
1130 | 0 | return NULL; |
1131 | 0 | } |
1132 | | /* found encrypted private key, try to extract */ |
1133 | 0 | pkey = OSSL_CRMF_ENCRYPTEDKEY_get1_pkey(encr_key, ctx->trusted, |
1134 | 0 | ctx->untrusted, |
1135 | 0 | ctx->pkey, ctx->cert, |
1136 | 0 | ctx->secretValue, |
1137 | 0 | ctx->libctx, ctx->propq); |
1138 | 0 | if (pkey == NULL) { |
1139 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_EXTRACTING_CENTRAL_GEN_KEY); |
1140 | 0 | return NULL; |
1141 | 0 | } |
1142 | 0 | OSSL_CMP_CTX_set0_newPkey((OSSL_CMP_CTX *)ctx, 1, pkey); |
1143 | 0 | } |
1144 | | |
1145 | 0 | if (!ossl_assert(crep != NULL && ctx != NULL)) |
1146 | 0 | return NULL; |
1147 | | |
1148 | 0 | if ((coec = crep->certifiedKeyPair->certOrEncCert) != NULL) { |
1149 | 0 | switch (coec->type) { |
1150 | 0 | case OSSL_CMP_CERTORENCCERT_CERTIFICATE: |
1151 | 0 | crt = X509_dup(coec->value.certificate); |
1152 | 0 | break; |
1153 | 0 | case OSSL_CMP_CERTORENCCERT_ENCRYPTEDCERT: |
1154 | | /* cert encrypted for indirect PoP; RFC 4210, 5.2.8.2 */ |
1155 | 0 | pkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1); |
1156 | | /* pkey is ctx->newPkey (if private, else NULL) or ctx->pkey */ |
1157 | 0 | if (pkey == NULL) { |
1158 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PRIVATE_KEY); |
1159 | 0 | return NULL; |
1160 | 0 | } |
1161 | 0 | crt = OSSL_CRMF_ENCRYPTEDKEY_get1_encCert(coec->value.encryptedCert, |
1162 | 0 | ctx->libctx, ctx->propq, pkey, 0); |
1163 | 0 | break; |
1164 | 0 | default: |
1165 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_CERT_TYPE); |
1166 | 0 | return NULL; |
1167 | 0 | } |
1168 | 0 | } |
1169 | 0 | if (crt == NULL) |
1170 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND); |
1171 | 0 | else |
1172 | 0 | (void)ossl_x509_set0_libctx(crt, ctx->libctx, ctx->propq); |
1173 | 0 | return crt; |
1174 | 0 | } |
1175 | | |
1176 | | int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg) |
1177 | 0 | { |
1178 | 0 | if (ctx == NULL || msg == NULL) { |
1179 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
1180 | 0 | return 0; |
1181 | 0 | } |
1182 | 0 | if (!ossl_cmp_hdr_set_transactionID(ctx, msg->header)) |
1183 | 0 | return 0; |
1184 | 0 | return msg->header->protectionAlg == NULL |
1185 | 0 | || ossl_cmp_msg_protect(ctx, msg); |
1186 | 0 | } |
1187 | | |
1188 | | int OSSL_CMP_MSG_update_recipNonce(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg) |
1189 | 0 | { |
1190 | 0 | if (ctx == NULL || msg == NULL || msg->header == NULL) { |
1191 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
1192 | 0 | return 0; |
1193 | 0 | } |
1194 | 0 | if (ctx->recipNonce == NULL) /* nothing to do for 1st msg in transaction */ |
1195 | 0 | return 1; |
1196 | 0 | if (!ossl_cmp_asn1_octet_string_set1(&msg->header->recipNonce, |
1197 | 0 | ctx->recipNonce)) |
1198 | 0 | return 0; |
1199 | 0 | return msg->header->protectionAlg == NULL || ossl_cmp_msg_protect(ctx, msg); |
1200 | 0 | } |
1201 | | |
1202 | | OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file, OSSL_LIB_CTX *libctx, |
1203 | | const char *propq) |
1204 | 0 | { |
1205 | 0 | OSSL_CMP_MSG *msg; |
1206 | 0 | BIO *bio = NULL; |
1207 | |
|
1208 | 0 | if (file == NULL) { |
1209 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
1210 | 0 | return NULL; |
1211 | 0 | } |
1212 | | |
1213 | 0 | msg = OSSL_CMP_MSG_new(libctx, propq); |
1214 | 0 | if (msg == NULL) { |
1215 | 0 | ERR_raise(ERR_LIB_CMP, ERR_R_CMP_LIB); |
1216 | 0 | return NULL; |
1217 | 0 | } |
1218 | | |
1219 | 0 | if ((bio = BIO_new_file(file, "rb")) == NULL |
1220 | 0 | || d2i_OSSL_CMP_MSG_bio(bio, &msg) == NULL) { |
1221 | 0 | OSSL_CMP_MSG_free(msg); |
1222 | 0 | msg = NULL; |
1223 | 0 | } |
1224 | 0 | BIO_free(bio); |
1225 | 0 | return msg; |
1226 | 0 | } |
1227 | | |
1228 | | int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg) |
1229 | 0 | { |
1230 | 0 | BIO *bio; |
1231 | 0 | int res; |
1232 | |
|
1233 | 0 | if (file == NULL || msg == NULL) { |
1234 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
1235 | 0 | return -1; |
1236 | 0 | } |
1237 | | |
1238 | 0 | bio = BIO_new_file(file, "wb"); |
1239 | 0 | if (bio == NULL) |
1240 | 0 | return -2; |
1241 | 0 | res = i2d_OSSL_CMP_MSG_bio(bio, msg); |
1242 | 0 | BIO_free(bio); |
1243 | 0 | return res; |
1244 | 0 | } |
1245 | | |
1246 | | OSSL_CMP_MSG *d2i_OSSL_CMP_MSG(OSSL_CMP_MSG **msg, const unsigned char **in, |
1247 | | long len) |
1248 | 0 | { |
1249 | 0 | OSSL_LIB_CTX *libctx = NULL; |
1250 | 0 | const char *propq = NULL; |
1251 | |
|
1252 | 0 | if (msg != NULL && *msg != NULL) { |
1253 | 0 | libctx = (*msg)->libctx; |
1254 | 0 | propq = (*msg)->propq; |
1255 | 0 | } |
1256 | |
|
1257 | 0 | return (OSSL_CMP_MSG *)ASN1_item_d2i_ex((ASN1_VALUE **)msg, in, len, |
1258 | 0 | ASN1_ITEM_rptr(OSSL_CMP_MSG), |
1259 | 0 | libctx, propq); |
1260 | 0 | } |
1261 | | |
1262 | | int i2d_OSSL_CMP_MSG(const OSSL_CMP_MSG *msg, unsigned char **out) |
1263 | 56.7k | { |
1264 | 56.7k | return ASN1_item_i2d((const ASN1_VALUE *)msg, out, |
1265 | 56.7k | ASN1_ITEM_rptr(OSSL_CMP_MSG)); |
1266 | 56.7k | } |
1267 | | |
1268 | | OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg) |
1269 | 42.0k | { |
1270 | 42.0k | OSSL_LIB_CTX *libctx = NULL; |
1271 | 42.0k | const char *propq = NULL; |
1272 | | |
1273 | 42.0k | if (msg != NULL && *msg != NULL) { |
1274 | 0 | libctx = (*msg)->libctx; |
1275 | 0 | propq = (*msg)->propq; |
1276 | 0 | } |
1277 | | |
1278 | 42.0k | return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(OSSL_CMP_MSG), bio, msg, libctx, |
1279 | 42.0k | propq); |
1280 | 42.0k | } |
1281 | | |
1282 | | int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg) |
1283 | 28.3k | { |
1284 | 28.3k | return ASN1_i2d_bio_of(OSSL_CMP_MSG, i2d_OSSL_CMP_MSG, bio, msg); |
1285 | 28.3k | } |
1286 | | |
1287 | | int ossl_cmp_is_error_with_waiting(const OSSL_CMP_MSG *msg) |
1288 | 0 | { |
1289 | 0 | if (!ossl_assert(msg != NULL)) |
1290 | 0 | return 0; |
1291 | | |
1292 | 0 | return (OSSL_CMP_MSG_get_bodytype(msg) == OSSL_CMP_PKIBODY_ERROR |
1293 | 0 | && ossl_cmp_pkisi_get_status(msg->body->value.error->pKIStatusInfo) |
1294 | 0 | == OSSL_CMP_PKISTATUS_waiting); |
1295 | 0 | } |