1*66bae5e7Schristos /*
2*66bae5e7Schristos * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*66bae5e7Schristos *
4*66bae5e7Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*66bae5e7Schristos * this file except in compliance with the License. You can obtain a copy
6*66bae5e7Schristos * in the file LICENSE in the source distribution or at
7*66bae5e7Schristos * https://www.openssl.org/source/license.html
8*66bae5e7Schristos */
9*66bae5e7Schristos
10*66bae5e7Schristos #include <string.h>
11*66bae5e7Schristos #include <openssl/x509v3.h>
12*66bae5e7Schristos #include <openssl/err.h>
13*66bae5e7Schristos #include <openssl/ess.h>
14*66bae5e7Schristos #include "internal/sizes.h"
15*66bae5e7Schristos #include "crypto/ess.h"
16*66bae5e7Schristos #include "crypto/x509.h"
17*66bae5e7Schristos
18*66bae5e7Schristos static ESS_CERT_ID *ESS_CERT_ID_new_init(const X509 *cert,
19*66bae5e7Schristos int set_issuer_serial);
20*66bae5e7Schristos static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
21*66bae5e7Schristos const X509 *cert,
22*66bae5e7Schristos int set_issuer_serial);
23*66bae5e7Schristos
OSSL_ESS_signing_cert_new_init(const X509 * signcert,const STACK_OF (X509)* certs,int set_issuer_serial)24*66bae5e7Schristos ESS_SIGNING_CERT *OSSL_ESS_signing_cert_new_init(const X509 *signcert,
25*66bae5e7Schristos const STACK_OF(X509) *certs,
26*66bae5e7Schristos int set_issuer_serial)
27*66bae5e7Schristos {
28*66bae5e7Schristos ESS_CERT_ID *cid = NULL;
29*66bae5e7Schristos ESS_SIGNING_CERT *sc;
30*66bae5e7Schristos int i;
31*66bae5e7Schristos
32*66bae5e7Schristos if ((sc = ESS_SIGNING_CERT_new()) == NULL)
33*66bae5e7Schristos goto err;
34*66bae5e7Schristos if (sc->cert_ids == NULL
35*66bae5e7Schristos && (sc->cert_ids = sk_ESS_CERT_ID_new_null()) == NULL)
36*66bae5e7Schristos goto err;
37*66bae5e7Schristos
38*66bae5e7Schristos if ((cid = ESS_CERT_ID_new_init(signcert, set_issuer_serial)) == NULL
39*66bae5e7Schristos || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
40*66bae5e7Schristos goto err;
41*66bae5e7Schristos for (i = 0; i < sk_X509_num(certs); ++i) {
42*66bae5e7Schristos X509 *cert = sk_X509_value(certs, i);
43*66bae5e7Schristos
44*66bae5e7Schristos if ((cid = ESS_CERT_ID_new_init(cert, 1)) == NULL
45*66bae5e7Schristos || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
46*66bae5e7Schristos goto err;
47*66bae5e7Schristos }
48*66bae5e7Schristos
49*66bae5e7Schristos return sc;
50*66bae5e7Schristos err:
51*66bae5e7Schristos ESS_SIGNING_CERT_free(sc);
52*66bae5e7Schristos ESS_CERT_ID_free(cid);
53*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
54*66bae5e7Schristos return NULL;
55*66bae5e7Schristos }
56*66bae5e7Schristos
ESS_CERT_ID_new_init(const X509 * cert,int set_issuer_serial)57*66bae5e7Schristos static ESS_CERT_ID *ESS_CERT_ID_new_init(const X509 *cert,
58*66bae5e7Schristos int set_issuer_serial)
59*66bae5e7Schristos {
60*66bae5e7Schristos ESS_CERT_ID *cid = NULL;
61*66bae5e7Schristos GENERAL_NAME *name = NULL;
62*66bae5e7Schristos unsigned char cert_sha1[SHA_DIGEST_LENGTH];
63*66bae5e7Schristos
64*66bae5e7Schristos if ((cid = ESS_CERT_ID_new()) == NULL)
65*66bae5e7Schristos goto err;
66*66bae5e7Schristos if (!X509_digest(cert, EVP_sha1(), cert_sha1, NULL))
67*66bae5e7Schristos goto err;
68*66bae5e7Schristos if (!ASN1_OCTET_STRING_set(cid->hash, cert_sha1, SHA_DIGEST_LENGTH))
69*66bae5e7Schristos goto err;
70*66bae5e7Schristos
71*66bae5e7Schristos /* Setting the issuer/serial if requested. */
72*66bae5e7Schristos if (!set_issuer_serial)
73*66bae5e7Schristos return cid;
74*66bae5e7Schristos
75*66bae5e7Schristos if (cid->issuer_serial == NULL
76*66bae5e7Schristos && (cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
77*66bae5e7Schristos goto err;
78*66bae5e7Schristos if ((name = GENERAL_NAME_new()) == NULL)
79*66bae5e7Schristos goto err;
80*66bae5e7Schristos name->type = GEN_DIRNAME;
81*66bae5e7Schristos if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
82*66bae5e7Schristos goto err;
83*66bae5e7Schristos if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
84*66bae5e7Schristos goto err;
85*66bae5e7Schristos name = NULL; /* Ownership is lost. */
86*66bae5e7Schristos ASN1_INTEGER_free(cid->issuer_serial->serial);
87*66bae5e7Schristos if ((cid->issuer_serial->serial =
88*66bae5e7Schristos ASN1_INTEGER_dup(X509_get0_serialNumber(cert))) == NULL)
89*66bae5e7Schristos goto err;
90*66bae5e7Schristos
91*66bae5e7Schristos return cid;
92*66bae5e7Schristos err:
93*66bae5e7Schristos GENERAL_NAME_free(name);
94*66bae5e7Schristos ESS_CERT_ID_free(cid);
95*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
96*66bae5e7Schristos return NULL;
97*66bae5e7Schristos }
98*66bae5e7Schristos
OSSL_ESS_signing_cert_v2_new_init(const EVP_MD * hash_alg,const X509 * signcert,const STACK_OF (X509)* certs,int set_issuer_serial)99*66bae5e7Schristos ESS_SIGNING_CERT_V2 *OSSL_ESS_signing_cert_v2_new_init(const EVP_MD *hash_alg,
100*66bae5e7Schristos const X509 *signcert,
101*66bae5e7Schristos const
102*66bae5e7Schristos STACK_OF(X509) *certs,
103*66bae5e7Schristos int set_issuer_serial)
104*66bae5e7Schristos {
105*66bae5e7Schristos ESS_CERT_ID_V2 *cid = NULL;
106*66bae5e7Schristos ESS_SIGNING_CERT_V2 *sc;
107*66bae5e7Schristos int i;
108*66bae5e7Schristos
109*66bae5e7Schristos if ((sc = ESS_SIGNING_CERT_V2_new()) == NULL)
110*66bae5e7Schristos goto err;
111*66bae5e7Schristos cid = ESS_CERT_ID_V2_new_init(hash_alg, signcert, set_issuer_serial);
112*66bae5e7Schristos if (cid == NULL)
113*66bae5e7Schristos goto err;
114*66bae5e7Schristos if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
115*66bae5e7Schristos goto err;
116*66bae5e7Schristos cid = NULL;
117*66bae5e7Schristos
118*66bae5e7Schristos for (i = 0; i < sk_X509_num(certs); ++i) {
119*66bae5e7Schristos X509 *cert = sk_X509_value(certs, i);
120*66bae5e7Schristos
121*66bae5e7Schristos if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, cert, 1)) == NULL)
122*66bae5e7Schristos goto err;
123*66bae5e7Schristos if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
124*66bae5e7Schristos goto err;
125*66bae5e7Schristos cid = NULL;
126*66bae5e7Schristos }
127*66bae5e7Schristos
128*66bae5e7Schristos return sc;
129*66bae5e7Schristos err:
130*66bae5e7Schristos ESS_SIGNING_CERT_V2_free(sc);
131*66bae5e7Schristos ESS_CERT_ID_V2_free(cid);
132*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
133*66bae5e7Schristos return NULL;
134*66bae5e7Schristos }
135*66bae5e7Schristos
ESS_CERT_ID_V2_new_init(const EVP_MD * hash_alg,const X509 * cert,int set_issuer_serial)136*66bae5e7Schristos static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
137*66bae5e7Schristos const X509 *cert,
138*66bae5e7Schristos int set_issuer_serial)
139*66bae5e7Schristos {
140*66bae5e7Schristos ESS_CERT_ID_V2 *cid;
141*66bae5e7Schristos GENERAL_NAME *name = NULL;
142*66bae5e7Schristos unsigned char hash[EVP_MAX_MD_SIZE];
143*66bae5e7Schristos unsigned int hash_len = sizeof(hash);
144*66bae5e7Schristos X509_ALGOR *alg = NULL;
145*66bae5e7Schristos
146*66bae5e7Schristos memset(hash, 0, sizeof(hash));
147*66bae5e7Schristos
148*66bae5e7Schristos if ((cid = ESS_CERT_ID_V2_new()) == NULL)
149*66bae5e7Schristos goto err;
150*66bae5e7Schristos
151*66bae5e7Schristos if (!EVP_MD_is_a(hash_alg, SN_sha256)) {
152*66bae5e7Schristos alg = X509_ALGOR_new();
153*66bae5e7Schristos if (alg == NULL)
154*66bae5e7Schristos goto err;
155*66bae5e7Schristos X509_ALGOR_set_md(alg, hash_alg);
156*66bae5e7Schristos if (alg->algorithm == NULL)
157*66bae5e7Schristos goto err;
158*66bae5e7Schristos cid->hash_alg = alg;
159*66bae5e7Schristos alg = NULL;
160*66bae5e7Schristos } else {
161*66bae5e7Schristos cid->hash_alg = NULL;
162*66bae5e7Schristos }
163*66bae5e7Schristos
164*66bae5e7Schristos if (!X509_digest(cert, hash_alg, hash, &hash_len))
165*66bae5e7Schristos goto err;
166*66bae5e7Schristos
167*66bae5e7Schristos if (!ASN1_OCTET_STRING_set(cid->hash, hash, hash_len))
168*66bae5e7Schristos goto err;
169*66bae5e7Schristos
170*66bae5e7Schristos if (!set_issuer_serial)
171*66bae5e7Schristos return cid;
172*66bae5e7Schristos
173*66bae5e7Schristos if ((cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
174*66bae5e7Schristos goto err;
175*66bae5e7Schristos if ((name = GENERAL_NAME_new()) == NULL)
176*66bae5e7Schristos goto err;
177*66bae5e7Schristos name->type = GEN_DIRNAME;
178*66bae5e7Schristos if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
179*66bae5e7Schristos goto err;
180*66bae5e7Schristos if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
181*66bae5e7Schristos goto err;
182*66bae5e7Schristos name = NULL; /* Ownership is lost. */
183*66bae5e7Schristos ASN1_INTEGER_free(cid->issuer_serial->serial);
184*66bae5e7Schristos cid->issuer_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert));
185*66bae5e7Schristos if (cid->issuer_serial->serial == NULL)
186*66bae5e7Schristos goto err;
187*66bae5e7Schristos
188*66bae5e7Schristos return cid;
189*66bae5e7Schristos err:
190*66bae5e7Schristos X509_ALGOR_free(alg);
191*66bae5e7Schristos GENERAL_NAME_free(name);
192*66bae5e7Schristos ESS_CERT_ID_V2_free(cid);
193*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
194*66bae5e7Schristos return NULL;
195*66bae5e7Schristos }
196*66bae5e7Schristos
ess_issuer_serial_cmp(const ESS_ISSUER_SERIAL * is,const X509 * cert)197*66bae5e7Schristos static int ess_issuer_serial_cmp(const ESS_ISSUER_SERIAL *is, const X509 *cert)
198*66bae5e7Schristos {
199*66bae5e7Schristos GENERAL_NAME *issuer;
200*66bae5e7Schristos
201*66bae5e7Schristos if (is == NULL || cert == NULL || sk_GENERAL_NAME_num(is->issuer) != 1)
202*66bae5e7Schristos return -1;
203*66bae5e7Schristos
204*66bae5e7Schristos issuer = sk_GENERAL_NAME_value(is->issuer, 0);
205*66bae5e7Schristos if (issuer->type != GEN_DIRNAME
206*66bae5e7Schristos || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)) != 0)
207*66bae5e7Schristos return -1;
208*66bae5e7Schristos
209*66bae5e7Schristos return ASN1_INTEGER_cmp(is->serial, X509_get0_serialNumber(cert));
210*66bae5e7Schristos }
211*66bae5e7Schristos
212*66bae5e7Schristos /*
213*66bae5e7Schristos * Find the cert in |certs| referenced by |cid| if not NULL, else by |cid_v2|.
214*66bae5e7Schristos * The cert must be the first one in |certs| if and only if |index| is 0.
215*66bae5e7Schristos * Return 0 on not found, -1 on error, else 1 + the position in |certs|.
216*66bae5e7Schristos */
find(const ESS_CERT_ID * cid,const ESS_CERT_ID_V2 * cid_v2,int index,const STACK_OF (X509)* certs)217*66bae5e7Schristos static int find(const ESS_CERT_ID *cid, const ESS_CERT_ID_V2 *cid_v2,
218*66bae5e7Schristos int index, const STACK_OF(X509) *certs)
219*66bae5e7Schristos {
220*66bae5e7Schristos const X509 *cert;
221*66bae5e7Schristos EVP_MD *md = NULL;
222*66bae5e7Schristos char name[OSSL_MAX_NAME_SIZE];
223*66bae5e7Schristos unsigned char cert_digest[EVP_MAX_MD_SIZE];
224*66bae5e7Schristos unsigned int len, cid_hash_len;
225*66bae5e7Schristos const ESS_ISSUER_SERIAL *is;
226*66bae5e7Schristos int i;
227*66bae5e7Schristos int ret = -1;
228*66bae5e7Schristos
229*66bae5e7Schristos if (cid == NULL && cid_v2 == NULL) {
230*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ERR_R_PASSED_INVALID_ARGUMENT);
231*66bae5e7Schristos return -1;
232*66bae5e7Schristos }
233*66bae5e7Schristos
234*66bae5e7Schristos if (cid != NULL)
235*66bae5e7Schristos strcpy(name, "SHA1");
236*66bae5e7Schristos else if (cid_v2->hash_alg == NULL)
237*66bae5e7Schristos strcpy(name, "SHA256");
238*66bae5e7Schristos else
239*66bae5e7Schristos OBJ_obj2txt(name, sizeof(name), cid_v2->hash_alg->algorithm, 0);
240*66bae5e7Schristos
241*66bae5e7Schristos (void)ERR_set_mark();
242*66bae5e7Schristos md = EVP_MD_fetch(NULL, name, NULL);
243*66bae5e7Schristos
244*66bae5e7Schristos if (md == NULL)
245*66bae5e7Schristos md = (EVP_MD *)EVP_get_digestbyname(name);
246*66bae5e7Schristos
247*66bae5e7Schristos if (md == NULL) {
248*66bae5e7Schristos (void)ERR_clear_last_mark();
249*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ESS_R_ESS_DIGEST_ALG_UNKNOWN);
250*66bae5e7Schristos goto end;
251*66bae5e7Schristos }
252*66bae5e7Schristos (void)ERR_pop_to_mark();
253*66bae5e7Schristos
254*66bae5e7Schristos for (i = 0; i < sk_X509_num(certs); ++i) {
255*66bae5e7Schristos cert = sk_X509_value(certs, i);
256*66bae5e7Schristos
257*66bae5e7Schristos cid_hash_len = cid != NULL ? cid->hash->length : cid_v2->hash->length;
258*66bae5e7Schristos if (!X509_digest(cert, md, cert_digest, &len)
259*66bae5e7Schristos || cid_hash_len != len) {
260*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_DIGEST_ERROR);
261*66bae5e7Schristos goto end;
262*66bae5e7Schristos }
263*66bae5e7Schristos
264*66bae5e7Schristos if (memcmp(cid != NULL ? cid->hash->data : cid_v2->hash->data,
265*66bae5e7Schristos cert_digest, len) == 0) {
266*66bae5e7Schristos is = cid != NULL ? cid->issuer_serial : cid_v2->issuer_serial;
267*66bae5e7Schristos /* Well, it's not really required to match the serial numbers. */
268*66bae5e7Schristos if (is == NULL || ess_issuer_serial_cmp(is, cert) == 0) {
269*66bae5e7Schristos if ((i == 0) == (index == 0)) {
270*66bae5e7Schristos ret = i + 1;
271*66bae5e7Schristos goto end;
272*66bae5e7Schristos }
273*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_WRONG_ORDER);
274*66bae5e7Schristos goto end;
275*66bae5e7Schristos }
276*66bae5e7Schristos }
277*66bae5e7Schristos }
278*66bae5e7Schristos
279*66bae5e7Schristos ret = 0;
280*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_NOT_FOUND);
281*66bae5e7Schristos end:
282*66bae5e7Schristos EVP_MD_free(md);
283*66bae5e7Schristos return ret;
284*66bae5e7Schristos }
285*66bae5e7Schristos
OSSL_ESS_check_signing_certs(const ESS_SIGNING_CERT * ss,const ESS_SIGNING_CERT_V2 * ssv2,const STACK_OF (X509)* chain,int require_signing_cert)286*66bae5e7Schristos int OSSL_ESS_check_signing_certs(const ESS_SIGNING_CERT *ss,
287*66bae5e7Schristos const ESS_SIGNING_CERT_V2 *ssv2,
288*66bae5e7Schristos const STACK_OF(X509) *chain,
289*66bae5e7Schristos int require_signing_cert)
290*66bae5e7Schristos {
291*66bae5e7Schristos int n_v1 = ss == NULL ? -1 : sk_ESS_CERT_ID_num(ss->cert_ids);
292*66bae5e7Schristos int n_v2 = ssv2 == NULL ? -1 : sk_ESS_CERT_ID_V2_num(ssv2->cert_ids);
293*66bae5e7Schristos int i, ret;
294*66bae5e7Schristos
295*66bae5e7Schristos if (require_signing_cert && ss == NULL && ssv2 == NULL) {
296*66bae5e7Schristos ERR_raise(ERR_LIB_CMS, ESS_R_MISSING_SIGNING_CERTIFICATE_ATTRIBUTE);
297*66bae5e7Schristos return -1;
298*66bae5e7Schristos }
299*66bae5e7Schristos if (n_v1 == 0 || n_v2 == 0) {
300*66bae5e7Schristos ERR_raise(ERR_LIB_ESS, ESS_R_EMPTY_ESS_CERT_ID_LIST);
301*66bae5e7Schristos return -1;
302*66bae5e7Schristos }
303*66bae5e7Schristos /* If both ss and ssv2 exist, as required evaluate them independently. */
304*66bae5e7Schristos for (i = 0; i < n_v1; i++) {
305*66bae5e7Schristos ret = find(sk_ESS_CERT_ID_value(ss->cert_ids, i), NULL, i, chain);
306*66bae5e7Schristos if (ret <= 0)
307*66bae5e7Schristos return ret;
308*66bae5e7Schristos }
309*66bae5e7Schristos for (i = 0; i < n_v2; i++) {
310*66bae5e7Schristos ret = find(NULL, sk_ESS_CERT_ID_V2_value(ssv2->cert_ids, i), i, chain);
311*66bae5e7Schristos if (ret <= 0)
312*66bae5e7Schristos return ret;
313*66bae5e7Schristos }
314*66bae5e7Schristos return 1;
315*66bae5e7Schristos }
316