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