1 /*
2 * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Siemens AG 2022
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include "cmp_local.h"
12 #include <openssl/cmp_util.h>
13
get0_trustedStore_vpm(const OSSL_CMP_CTX * ctx)14 static const X509_VERIFY_PARAM *get0_trustedStore_vpm(const OSSL_CMP_CTX *ctx)
15 {
16 const X509_STORE *ts = OSSL_CMP_CTX_get0_trustedStore(ctx);
17
18 return ts == NULL ? NULL : X509_STORE_get0_param(ts);
19 }
20
cert_msg(const char * func,const char * file,int lineno,OSSL_CMP_severity level,OSSL_CMP_CTX * ctx,const char * source,X509 * cert,const char * msg)21 static void cert_msg(const char *func, const char *file, int lineno,
22 OSSL_CMP_severity level, OSSL_CMP_CTX *ctx,
23 const char *source, X509 *cert, const char *msg)
24 {
25 char *subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
26
27 ossl_cmp_print_log(level, ctx, func, file, lineno,
28 level == OSSL_CMP_LOG_WARNING ? "WARN" : "ERR",
29 "certificate from '%s' with subject '%s' %s",
30 source, subj, msg);
31 OPENSSL_free(subj);
32 }
33
34 /* use |type_CA| -1 (no CA type check) or 0 (must be EE) or 1 (must be CA) */
ossl_X509_check(OSSL_CMP_CTX * ctx,const char * source,X509 * cert,int type_CA,const X509_VERIFY_PARAM * vpm)35 static int ossl_X509_check(OSSL_CMP_CTX *ctx, const char *source, X509 *cert,
36 int type_CA, const X509_VERIFY_PARAM *vpm)
37 {
38 uint32_t ex_flags = X509_get_extension_flags(cert);
39 int res = X509_cmp_timeframe(vpm, X509_get0_notBefore(cert),
40 X509_get0_notAfter(cert));
41 int ret = res == 0;
42 OSSL_CMP_severity level = vpm == NULL ? OSSL_CMP_LOG_WARNING : OSSL_CMP_LOG_ERR;
43
44 if (!ret)
45 cert_msg(OPENSSL_FUNC, OPENSSL_FILE, OPENSSL_LINE, level, ctx,
46 source, cert, res > 0 ? "has expired" : "not yet valid");
47 if (type_CA >= 0 && (ex_flags & EXFLAG_V1) == 0) {
48 int is_CA = (ex_flags & EXFLAG_CA) != 0;
49
50 if ((type_CA != 0) != is_CA) {
51 cert_msg(OPENSSL_FUNC, OPENSSL_FILE, OPENSSL_LINE, level, ctx,
52 source, cert,
53 is_CA ? "is not an EE cert" : "is not a CA cert");
54 ret = 0;
55 }
56 }
57 return ret;
58 }
59
ossl_X509_check_all(OSSL_CMP_CTX * ctx,const char * source,STACK_OF (X509)* certs,int type_CA,const X509_VERIFY_PARAM * vpm)60 static int ossl_X509_check_all(OSSL_CMP_CTX *ctx, const char *source,
61 STACK_OF(X509) *certs,
62 int type_CA, const X509_VERIFY_PARAM *vpm)
63 {
64 int i;
65 int ret = 1;
66
67 for (i = 0; i < sk_X509_num(certs /* may be NULL */); i++)
68 ret = ossl_X509_check(ctx, source,
69 sk_X509_value(certs, i), type_CA, vpm)
70 && ret; /* Having 'ret' after the '&&', all certs are checked. */
71 return ret;
72 }
73
get_genm_itav(OSSL_CMP_CTX * ctx,OSSL_CMP_ITAV * req,int expected,const char * desc)74 static OSSL_CMP_ITAV *get_genm_itav(OSSL_CMP_CTX *ctx,
75 OSSL_CMP_ITAV *req, /* gets consumed */
76 int expected, const char *desc)
77 {
78 STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
79 int i, n;
80
81 if (ctx == NULL) {
82 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
83 goto err;
84 }
85 if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_unspecified) {
86 ERR_raise_data(ERR_LIB_CMP, CMP_R_UNCLEAN_CTX,
87 "client context in unsuitable state; should call CMPclient_reinit() before");
88 goto err;
89 }
90
91 if (!OSSL_CMP_CTX_push0_genm_ITAV(ctx, req))
92 goto err;
93 req = NULL;
94 itavs = OSSL_CMP_exec_GENM_ses(ctx);
95 if (itavs == NULL) {
96 if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_request)
97 ERR_raise_data(ERR_LIB_CMP, CMP_R_GETTING_GENP,
98 "with infoType %s", desc);
99 return NULL;
100 }
101
102 if ((n = sk_OSSL_CMP_ITAV_num(itavs)) <= 0) {
103 ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
104 "response on genm requesting infoType %s does not include suitable value", desc);
105 sk_OSSL_CMP_ITAV_free(itavs);
106 return NULL;
107 }
108
109 if (n > 1)
110 ossl_cmp_log2(WARN, ctx,
111 "response on genm contains %d ITAVs; will use the first ITAV with infoType id-it-%s",
112 n, desc);
113 for (i = 0; i < n; i++) {
114 OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_shift(itavs);
115 ASN1_OBJECT *obj = OSSL_CMP_ITAV_get0_type(itav);
116 char name[128] = "genp contains InfoType '";
117 size_t offset = strlen(name);
118
119 if (OBJ_obj2nid(obj) == expected) {
120 for (i++; i < n; i++)
121 OSSL_CMP_ITAV_free(sk_OSSL_CMP_ITAV_shift(itavs));
122 sk_OSSL_CMP_ITAV_free(itavs);
123 return itav;
124 }
125
126 if (OBJ_obj2txt(name + offset, sizeof(name) - offset, obj, 0) < 0)
127 strcat(name, "<unknown>");
128 ossl_cmp_log2(WARN, ctx, "%s' while expecting 'id-it-%s'", name, desc);
129 OSSL_CMP_ITAV_free(itav);
130 }
131 ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
132 "could not find any ITAV for %s", desc);
133
134 err:
135 sk_OSSL_CMP_ITAV_free(itavs);
136 OSSL_CMP_ITAV_free(req);
137 return NULL;
138 }
139
OSSL_CMP_get1_caCerts(OSSL_CMP_CTX * ctx,STACK_OF (X509)** out)140 int OSSL_CMP_get1_caCerts(OSSL_CMP_CTX *ctx, STACK_OF(X509) **out)
141 {
142 OSSL_CMP_ITAV *req, *itav;
143 STACK_OF(X509) *certs = NULL;
144 int ret = 0;
145
146 if (out == NULL) {
147 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
148 return 0;
149 }
150 *out = NULL;
151
152 if ((req = OSSL_CMP_ITAV_new_caCerts(NULL)) == NULL)
153 return 0;
154 if ((itav = get_genm_itav(ctx, req, NID_id_it_caCerts, "caCerts")) == NULL)
155 return 0;
156 if (!OSSL_CMP_ITAV_get0_caCerts(itav, &certs))
157 goto end;
158 ret = 1;
159 if (certs == NULL) /* no CA certificate available */
160 goto end;
161
162 if (!ossl_X509_check_all(ctx, "genp", certs, 1 /* CA */,
163 get0_trustedStore_vpm(ctx))) {
164 ret = 0;
165 goto end;
166 }
167 *out = sk_X509_new_reserve(NULL, sk_X509_num(certs));
168 if (!X509_add_certs(*out, certs,
169 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) {
170 sk_X509_pop_free(*out, X509_free);
171 *out = NULL;
172 ret = 0;
173 }
174
175 end:
176 OSSL_CMP_ITAV_free(itav);
177 return ret;
178 }
179
selfsigned_verify_cb(int ok,X509_STORE_CTX * store_ctx)180 static int selfsigned_verify_cb(int ok, X509_STORE_CTX *store_ctx)
181 {
182 if (ok == 0
183 && X509_STORE_CTX_get_error_depth(store_ctx) == 0
184 && X509_STORE_CTX_get_error(store_ctx)
185 == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) {
186 /* in this case, custom chain building */
187 int i;
188 STACK_OF(X509) *trust;
189 STACK_OF(X509) *chain = X509_STORE_CTX_get0_chain(store_ctx);
190 STACK_OF(X509) *untrusted = X509_STORE_CTX_get0_untrusted(store_ctx);
191 X509_STORE_CTX_check_issued_fn check_issued = X509_STORE_CTX_get_check_issued(store_ctx);
192 X509 *cert = sk_X509_value(chain, 0); /* target cert */
193 X509 *issuer;
194
195 for (i = 0; i < sk_X509_num(untrusted); i++) {
196 cert = sk_X509_value(untrusted, i);
197 if (!X509_add_cert(chain, cert, X509_ADD_FLAG_UP_REF))
198 return 0;
199 }
200
201 trust = X509_STORE_get1_all_certs(X509_STORE_CTX_get0_store(store_ctx));
202 for (i = 0; i < sk_X509_num(trust); i++) {
203 issuer = sk_X509_value(trust, i);
204 if ((*check_issued)(store_ctx, cert, issuer)) {
205 if (X509_add_cert(chain, cert, X509_ADD_FLAG_UP_REF))
206 ok = 1;
207 break;
208 }
209 }
210 sk_X509_pop_free(trust, X509_free);
211 return ok;
212 } else {
213 X509_STORE *ts = X509_STORE_CTX_get0_store(store_ctx);
214 X509_STORE_CTX_verify_cb verify_cb;
215
216 if (ts == NULL || (verify_cb = X509_STORE_get_verify_cb(ts)) == NULL)
217 return ok;
218 return (*verify_cb)(ok, store_ctx);
219 }
220 }
221
222 /* vanilla X509_verify_cert() does not support self-signed certs as target */
verify_ss_cert(OSSL_LIB_CTX * libctx,const char * propq,X509_STORE * ts,STACK_OF (X509)* untrusted,X509 * target)223 static int verify_ss_cert(OSSL_LIB_CTX *libctx, const char *propq,
224 X509_STORE *ts, STACK_OF(X509) *untrusted,
225 X509 *target)
226 {
227 X509_STORE_CTX *csc = NULL;
228 int ok = 0;
229
230 if (ts == NULL || target == NULL) {
231 ERR_raise(ERR_LIB_CMP, ERR_R_PASSED_NULL_PARAMETER);
232 return 0;
233 }
234
235 if ((csc = X509_STORE_CTX_new_ex(libctx, propq)) == NULL
236 || !X509_STORE_CTX_init(csc, ts, target, untrusted))
237 goto err;
238 X509_STORE_CTX_set_verify_cb(csc, selfsigned_verify_cb);
239 ok = X509_verify_cert(csc) > 0;
240
241 err:
242 X509_STORE_CTX_free(csc);
243 return ok;
244 }
245
246 static int
verify_ss_cert_trans(OSSL_CMP_CTX * ctx,X509 * trusted,X509 * trans,X509 * target,const char * desc)247 verify_ss_cert_trans(OSSL_CMP_CTX *ctx, X509 *trusted /* may be NULL */,
248 X509 *trans /* the only untrusted cert, may be NULL */,
249 X509 *target, const char *desc)
250 {
251 X509_STORE *ts = OSSL_CMP_CTX_get0_trusted(ctx);
252 STACK_OF(X509) *untrusted = NULL;
253 int res = 0;
254
255 if (trusted != NULL) {
256 X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts);
257
258 if ((ts = X509_STORE_new()) == NULL)
259 return 0;
260 if (!X509_STORE_set1_param(ts, vpm)
261 || !X509_STORE_add_cert(ts, trusted))
262 goto err;
263 }
264
265 if (trans != NULL
266 && !ossl_x509_add_cert_new(&untrusted, trans, X509_ADD_FLAG_UP_REF))
267 goto err;
268
269 res = verify_ss_cert(OSSL_CMP_CTX_get0_libctx(ctx),
270 OSSL_CMP_CTX_get0_propq(ctx),
271 ts, untrusted, target);
272 if (!res)
273 ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_ROOTCAKEYUPDATE,
274 "failed to validate %s certificate received in genp %s",
275 desc, trusted == NULL ? "using trust store" : "with given certificate as trust anchor");
276
277 err:
278 sk_X509_pop_free(untrusted, X509_free);
279 if (trusted != NULL)
280 X509_STORE_free(ts);
281 return res;
282 }
283
OSSL_CMP_get1_rootCaKeyUpdate(OSSL_CMP_CTX * ctx,const X509 * oldWithOld,X509 ** newWithNew,X509 ** newWithOld,X509 ** oldWithNew)284 int OSSL_CMP_get1_rootCaKeyUpdate(OSSL_CMP_CTX *ctx,
285 const X509 *oldWithOld, X509 **newWithNew,
286 X509 **newWithOld, X509 **oldWithNew)
287 {
288 X509 *oldWithOld_copy = NULL, *my_newWithOld, *my_oldWithNew;
289 OSSL_CMP_ITAV *req, *itav;
290 int res = 0;
291
292 if (newWithNew == NULL) {
293 ERR_raise(ERR_LIB_CMP, ERR_R_PASSED_NULL_PARAMETER);
294 return 0;
295 }
296 *newWithNew = NULL;
297
298 if ((req = OSSL_CMP_ITAV_new_rootCaCert(oldWithOld)) == NULL)
299 return 0;
300 itav = get_genm_itav(ctx, req, NID_id_it_rootCaKeyUpdate, "rootCaKeyUpdate");
301 if (itav == NULL)
302 return 0;
303
304 if (!OSSL_CMP_ITAV_get0_rootCaKeyUpdate(itav, newWithNew,
305 &my_newWithOld, &my_oldWithNew))
306 goto end;
307 /* no root CA cert update available */
308 if (*newWithNew == NULL) {
309 res = 1;
310 goto end;
311 }
312 if ((oldWithOld_copy = X509_dup(oldWithOld)) == NULL && oldWithOld != NULL)
313 goto end;
314 if (!verify_ss_cert_trans(ctx, oldWithOld_copy, my_newWithOld,
315 *newWithNew, "newWithNew")) {
316 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ROOTCAKEYUPDATE);
317 goto end;
318 }
319 if (oldWithOld != NULL && my_oldWithNew != NULL
320 && !verify_ss_cert_trans(ctx, *newWithNew, my_oldWithNew,
321 oldWithOld_copy, "oldWithOld")) {
322 ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ROOTCAKEYUPDATE);
323 goto end;
324 }
325
326 if (!X509_up_ref(*newWithNew))
327 goto end;
328 if (newWithOld != NULL && (*newWithOld = my_newWithOld) != NULL && !X509_up_ref(*newWithOld))
329 goto free;
330 if (oldWithNew == NULL || (*oldWithNew = my_oldWithNew) == NULL || X509_up_ref(*oldWithNew)) {
331 res = 1;
332 goto end;
333 }
334 if (newWithOld != NULL)
335 X509_free(*newWithOld);
336 free:
337 X509_free(*newWithNew);
338
339 end:
340 OSSL_CMP_ITAV_free(itav);
341 X509_free(oldWithOld_copy);
342 return res;
343 }
344
OSSL_CMP_get1_crlUpdate(OSSL_CMP_CTX * ctx,const X509 * crlcert,const X509_CRL * last_crl,X509_CRL ** crl)345 int OSSL_CMP_get1_crlUpdate(OSSL_CMP_CTX *ctx, const X509 *crlcert,
346 const X509_CRL *last_crl,
347 X509_CRL **crl)
348 {
349 OSSL_CMP_CRLSTATUS *status = NULL;
350 STACK_OF(OSSL_CMP_CRLSTATUS) *list = NULL;
351 OSSL_CMP_ITAV *req = NULL, *itav = NULL;
352 STACK_OF(X509_CRL) *crls = NULL;
353 int res = 0;
354
355 if (crl == NULL) {
356 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
357 return 0;
358 }
359 *crl = NULL;
360
361 if ((status = OSSL_CMP_CRLSTATUS_create(last_crl, crlcert, 1)) == NULL) {
362 ERR_raise(ERR_LIB_CMP, CMP_R_GENERATE_CRLSTATUS);
363 goto end;
364 }
365 if ((list = sk_OSSL_CMP_CRLSTATUS_new_reserve(NULL, 1)) == NULL) {
366 ERR_raise(ERR_LIB_CMP, CMP_R_GENERATE_CRLSTATUS);
367 goto end;
368 }
369 (void)sk_OSSL_CMP_CRLSTATUS_push(list, status); /* cannot fail */
370
371 if ((req = OSSL_CMP_ITAV_new0_crlStatusList(list)) == NULL)
372 goto end;
373 status = NULL;
374 list = NULL;
375
376 if ((itav = get_genm_itav(ctx, req, NID_id_it_crls, "crl")) == NULL)
377 goto end;
378
379 if (!OSSL_CMP_ITAV_get0_crls(itav, &crls))
380 goto end;
381
382 if (crls == NULL) { /* no CRL update available */
383 res = 1;
384 goto end;
385 }
386 if (sk_X509_CRL_num(crls) != 1) {
387 ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
388 "Unexpected number of CRLs in genp: %d",
389 sk_X509_CRL_num(crls));
390 goto end;
391 }
392
393 if ((*crl = sk_X509_CRL_value(crls, 0)) == NULL || !X509_CRL_up_ref(*crl)) {
394 *crl = NULL;
395 goto end;
396 }
397 res = 1;
398 end:
399 OSSL_CMP_CRLSTATUS_free(status);
400 sk_OSSL_CMP_CRLSTATUS_free(list);
401 OSSL_CMP_ITAV_free(itav);
402 return res;
403 }
404
OSSL_CMP_get1_certReqTemplate(OSSL_CMP_CTX * ctx,OSSL_CRMF_CERTTEMPLATE ** certTemplate,OSSL_CMP_ATAVS ** keySpec)405 int OSSL_CMP_get1_certReqTemplate(OSSL_CMP_CTX *ctx,
406 OSSL_CRMF_CERTTEMPLATE **certTemplate,
407 OSSL_CMP_ATAVS **keySpec)
408 {
409 OSSL_CMP_ITAV *req, *itav = NULL;
410 int res = 0;
411
412 if (keySpec != NULL)
413 *keySpec = NULL;
414 if (certTemplate == NULL) {
415 ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
416 return 0;
417 }
418 *certTemplate = NULL;
419
420 if ((req = OSSL_CMP_ITAV_new0_certReqTemplate(NULL, NULL)) == NULL) {
421 ERR_raise(ERR_LIB_CMP, CMP_R_GENERATE_CERTREQTEMPLATE);
422 return 0;
423 }
424
425 if ((itav = get_genm_itav(ctx, req, NID_id_it_certReqTemplate,
426 "certReqTemplate"))
427 == NULL)
428 return 0;
429
430 if (!OSSL_CMP_ITAV_get1_certReqTemplate(itav, certTemplate, keySpec))
431 goto end;
432
433 res = 1;
434 end:
435 OSSL_CMP_ITAV_free(itav);
436 return res;
437 }
438