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