xref: /freebsd/crypto/openssl/crypto/cmp/cmp_genm.c (revision 1523ccfd9c8c254f7928143d31c305384b05fd11)
1 /*
2  * Copyright 2022-2026 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];
117 
118         if (OBJ_obj2nid(obj) == expected) {
119             for (i++; i < n; i++)
120                 OSSL_CMP_ITAV_free(sk_OSSL_CMP_ITAV_shift(itavs));
121             sk_OSSL_CMP_ITAV_free(itavs);
122             return itav;
123         }
124 
125         if (OBJ_obj2txt(name, sizeof(name), obj, 0) < 0)
126             name[0] = '\0';
127         ossl_cmp_log2(WARN, ctx,
128             "genp contains InfoType '%s' while expecting 'id-it-%s'",
129             name[0] == '\0' ? "<unknown>" : name, desc);
130         OSSL_CMP_ITAV_free(itav);
131     }
132     ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
133         "could not find any ITAV for %s", desc);
134 
135 err:
136     sk_OSSL_CMP_ITAV_free(itavs);
137     OSSL_CMP_ITAV_free(req);
138     return NULL;
139 }
140 
OSSL_CMP_get1_caCerts(OSSL_CMP_CTX * ctx,STACK_OF (X509)** out)141 int OSSL_CMP_get1_caCerts(OSSL_CMP_CTX *ctx, STACK_OF(X509) **out)
142 {
143     OSSL_CMP_ITAV *req, *itav;
144     STACK_OF(X509) *certs = NULL;
145     int ret = 0;
146 
147     if (out == NULL) {
148         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
149         return 0;
150     }
151     *out = NULL;
152 
153     if ((req = OSSL_CMP_ITAV_new_caCerts(NULL)) == NULL)
154         return 0;
155     if ((itav = get_genm_itav(ctx, req, NID_id_it_caCerts, "caCerts")) == NULL)
156         return 0;
157     if (!OSSL_CMP_ITAV_get0_caCerts(itav, &certs))
158         goto end;
159     ret = 1;
160     if (certs == NULL) /* no CA certificate available */
161         goto end;
162 
163     if (!ossl_X509_check_all(ctx, "genp", certs, 1 /* CA */,
164             get0_trustedStore_vpm(ctx))) {
165         ret = 0;
166         goto end;
167     }
168     *out = sk_X509_new_reserve(NULL, sk_X509_num(certs));
169     if (!X509_add_certs(*out, certs,
170             X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) {
171         sk_X509_pop_free(*out, X509_free);
172         *out = NULL;
173         ret = 0;
174     }
175 
176 end:
177     OSSL_CMP_ITAV_free(itav);
178     return ret;
179 }
180 
selfsigned_verify_cb(int ok,X509_STORE_CTX * store_ctx)181 static int selfsigned_verify_cb(int ok, X509_STORE_CTX *store_ctx)
182 {
183     if (ok == 0
184         && X509_STORE_CTX_get_error_depth(store_ctx) == 0
185         && X509_STORE_CTX_get_error(store_ctx)
186             == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) {
187         /* in this case, custom chain building */
188         int i;
189         STACK_OF(X509) *trust;
190         STACK_OF(X509) *chain = X509_STORE_CTX_get0_chain(store_ctx);
191         STACK_OF(X509) *untrusted = X509_STORE_CTX_get0_untrusted(store_ctx);
192         X509_STORE_CTX_check_issued_fn check_issued = X509_STORE_CTX_get_check_issued(store_ctx);
193         X509 *cert = sk_X509_value(chain, 0); /* target cert */
194         X509 *issuer;
195 
196         for (i = 0; i < sk_X509_num(untrusted); i++) {
197             cert = sk_X509_value(untrusted, i);
198             if (!X509_add_cert(chain, cert, X509_ADD_FLAG_UP_REF))
199                 return 0;
200         }
201 
202         trust = X509_STORE_get1_all_certs(X509_STORE_CTX_get0_store(store_ctx));
203         for (i = 0; i < sk_X509_num(trust); i++) {
204             issuer = sk_X509_value(trust, i);
205             if ((*check_issued)(store_ctx, cert, issuer)) {
206                 if (X509_add_cert(chain, issuer, X509_ADD_FLAG_UP_REF))
207                     ok = 1;
208                 break;
209             }
210         }
211         sk_X509_pop_free(trust, X509_free);
212         return ok;
213     } else {
214         X509_STORE *ts = X509_STORE_CTX_get0_store(store_ctx);
215         X509_STORE_CTX_verify_cb verify_cb;
216 
217         if (ts == NULL || (verify_cb = X509_STORE_get_verify_cb(ts)) == NULL)
218             return ok;
219         return (*verify_cb)(ok, store_ctx);
220     }
221 }
222 
223 /* 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)224 static int verify_ss_cert(OSSL_LIB_CTX *libctx, const char *propq,
225     X509_STORE *ts, STACK_OF(X509) *untrusted,
226     X509 *target)
227 {
228     X509_STORE_CTX *csc = NULL;
229     int ok = 0;
230 
231     if (ts == NULL || target == NULL) {
232         ERR_raise(ERR_LIB_CMP, ERR_R_PASSED_NULL_PARAMETER);
233         return 0;
234     }
235 
236     if ((csc = X509_STORE_CTX_new_ex(libctx, propq)) == NULL
237         || !X509_STORE_CTX_init(csc, ts, target, untrusted))
238         goto err;
239     X509_STORE_CTX_set_flags(csc, X509_V_FLAG_CHECK_SS_SIGNATURE);
240     X509_STORE_CTX_set_verify_cb(csc, selfsigned_verify_cb);
241     ok = X509_verify_cert(csc) > 0;
242 
243 err:
244     X509_STORE_CTX_free(csc);
245     return ok;
246 }
247 
248 static int
verify_ss_cert_trans(OSSL_CMP_CTX * ctx,X509 * trusted,X509 * trans,X509 * target,const char * desc)249 verify_ss_cert_trans(OSSL_CMP_CTX *ctx, X509 *trusted /* may be NULL */,
250     X509 *trans /* the only untrusted cert, may be NULL */,
251     X509 *target, const char *desc)
252 {
253     X509_STORE *ts = OSSL_CMP_CTX_get0_trusted(ctx);
254     STACK_OF(X509) *untrusted = NULL;
255     int res = 0;
256 
257     if (trusted != NULL) {
258         X509_VERIFY_PARAM *vpm = (ts == NULL) ? NULL
259                                               : X509_STORE_get0_param(ts);
260 
261         if ((ts = X509_STORE_new()) == NULL)
262             return 0;
263         if (!X509_STORE_set1_param(ts, vpm)
264             || !X509_STORE_add_cert(ts, trusted))
265             goto err;
266     }
267 
268     if (trans != NULL
269         && !ossl_x509_add_cert_new(&untrusted, trans, X509_ADD_FLAG_UP_REF))
270         goto err;
271 
272     res = verify_ss_cert(OSSL_CMP_CTX_get0_libctx(ctx),
273         OSSL_CMP_CTX_get0_propq(ctx),
274         ts, untrusted, target);
275     if (!res)
276         ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_ROOTCAKEYUPDATE,
277             "failed to validate %s certificate received in genp %s",
278             desc, trusted == NULL ? "using trust store" : "with given certificate as trust anchor");
279 
280 err:
281     sk_X509_pop_free(untrusted, X509_free);
282     if (trusted != NULL)
283         X509_STORE_free(ts);
284     return res;
285 }
286 
OSSL_CMP_get1_rootCaKeyUpdate(OSSL_CMP_CTX * ctx,const X509 * oldWithOld,X509 ** newWithNew,X509 ** newWithOld,X509 ** oldWithNew)287 int OSSL_CMP_get1_rootCaKeyUpdate(OSSL_CMP_CTX *ctx,
288     const X509 *oldWithOld, X509 **newWithNew,
289     X509 **newWithOld, X509 **oldWithNew)
290 {
291     X509 *oldWithOld_copy = NULL, *my_newWithOld, *my_oldWithNew;
292     OSSL_CMP_ITAV *req, *itav;
293     int res = 0;
294 
295     if (newWithNew == NULL) {
296         ERR_raise(ERR_LIB_CMP, ERR_R_PASSED_NULL_PARAMETER);
297         return 0;
298     }
299     *newWithNew = NULL;
300 
301     if ((req = OSSL_CMP_ITAV_new_rootCaCert(oldWithOld)) == NULL)
302         return 0;
303     itav = get_genm_itav(ctx, req, NID_id_it_rootCaKeyUpdate, "rootCaKeyUpdate");
304     if (itav == NULL)
305         return 0;
306 
307     if (!OSSL_CMP_ITAV_get0_rootCaKeyUpdate(itav, newWithNew,
308             &my_newWithOld, &my_oldWithNew))
309         goto end;
310     /* no root CA cert update available */
311     if (*newWithNew == NULL) {
312         res = 1;
313         goto end;
314     }
315     if ((oldWithOld_copy = X509_dup(oldWithOld)) == NULL && oldWithOld != NULL)
316         goto end;
317     if (!verify_ss_cert_trans(ctx, oldWithOld_copy, my_newWithOld,
318             *newWithNew, "newWithNew")) {
319         ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ROOTCAKEYUPDATE);
320         goto end;
321     }
322     if (oldWithOld != NULL && my_oldWithNew != NULL
323         && !verify_ss_cert_trans(ctx, *newWithNew, my_oldWithNew,
324             oldWithOld_copy, "oldWithOld")) {
325         ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ROOTCAKEYUPDATE);
326         goto end;
327     }
328 
329     if (!X509_up_ref(*newWithNew))
330         goto end;
331     if (newWithOld != NULL && (*newWithOld = my_newWithOld) != NULL && !X509_up_ref(*newWithOld))
332         goto free;
333     if (oldWithNew == NULL || (*oldWithNew = my_oldWithNew) == NULL || X509_up_ref(*oldWithNew)) {
334         res = 1;
335         goto end;
336     }
337     if (newWithOld != NULL)
338         X509_free(*newWithOld);
339 free:
340     X509_free(*newWithNew);
341 
342 end:
343     OSSL_CMP_ITAV_free(itav);
344     X509_free(oldWithOld_copy);
345     return res;
346 }
347 
OSSL_CMP_get1_crlUpdate(OSSL_CMP_CTX * ctx,const X509 * crlcert,const X509_CRL * last_crl,X509_CRL ** crl)348 int OSSL_CMP_get1_crlUpdate(OSSL_CMP_CTX *ctx, const X509 *crlcert,
349     const X509_CRL *last_crl,
350     X509_CRL **crl)
351 {
352     OSSL_CMP_CRLSTATUS *status = NULL;
353     STACK_OF(OSSL_CMP_CRLSTATUS) *list = NULL;
354     OSSL_CMP_ITAV *req = NULL, *itav = NULL;
355     STACK_OF(X509_CRL) *crls = NULL;
356     int res = 0;
357 
358     if (crl == NULL) {
359         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
360         return 0;
361     }
362     *crl = NULL;
363 
364     if ((status = OSSL_CMP_CRLSTATUS_create(last_crl, crlcert, 1)) == NULL) {
365         ERR_raise(ERR_LIB_CMP, CMP_R_GENERATE_CRLSTATUS);
366         goto end;
367     }
368     if ((list = sk_OSSL_CMP_CRLSTATUS_new_reserve(NULL, 1)) == NULL) {
369         ERR_raise(ERR_LIB_CMP, CMP_R_GENERATE_CRLSTATUS);
370         goto end;
371     }
372     (void)sk_OSSL_CMP_CRLSTATUS_push(list, status); /* cannot fail */
373 
374     if ((req = OSSL_CMP_ITAV_new0_crlStatusList(list)) == NULL)
375         goto end;
376     status = NULL;
377     list = NULL;
378 
379     if ((itav = get_genm_itav(ctx, req, NID_id_it_crls, "crl")) == NULL)
380         goto end;
381 
382     if (!OSSL_CMP_ITAV_get0_crls(itav, &crls))
383         goto end;
384 
385     if (crls == NULL) { /* no CRL update available */
386         res = 1;
387         goto end;
388     }
389     if (sk_X509_CRL_num(crls) != 1) {
390         ERR_raise_data(ERR_LIB_CMP, CMP_R_INVALID_GENP,
391             "Unexpected number of CRLs in genp: %d",
392             sk_X509_CRL_num(crls));
393         goto end;
394     }
395 
396     if ((*crl = sk_X509_CRL_value(crls, 0)) == NULL || !X509_CRL_up_ref(*crl)) {
397         *crl = NULL;
398         goto end;
399     }
400     res = 1;
401 end:
402     OSSL_CMP_CRLSTATUS_free(status);
403     sk_OSSL_CMP_CRLSTATUS_free(list);
404     OSSL_CMP_ITAV_free(itav);
405     return res;
406 }
407 
OSSL_CMP_get1_certReqTemplate(OSSL_CMP_CTX * ctx,OSSL_CRMF_CERTTEMPLATE ** certTemplate,OSSL_CMP_ATAVS ** keySpec)408 int OSSL_CMP_get1_certReqTemplate(OSSL_CMP_CTX *ctx,
409     OSSL_CRMF_CERTTEMPLATE **certTemplate,
410     OSSL_CMP_ATAVS **keySpec)
411 {
412     OSSL_CMP_ITAV *req, *itav = NULL;
413     int res = 0;
414 
415     if (keySpec != NULL)
416         *keySpec = NULL;
417     if (certTemplate == NULL) {
418         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
419         return 0;
420     }
421     *certTemplate = NULL;
422 
423     if ((req = OSSL_CMP_ITAV_new0_certReqTemplate(NULL, NULL)) == NULL) {
424         ERR_raise(ERR_LIB_CMP, CMP_R_GENERATE_CERTREQTEMPLATE);
425         return 0;
426     }
427 
428     if ((itav = get_genm_itav(ctx, req, NID_id_it_certReqTemplate,
429              "certReqTemplate"))
430         == NULL)
431         return 0;
432 
433     if (!OSSL_CMP_ITAV_get1_certReqTemplate(itav, certTemplate, keySpec))
434         goto end;
435 
436     res = 1;
437 end:
438     OSSL_CMP_ITAV_free(itav);
439     return res;
440 }
441