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