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