9d9defa9d7746ba1b001e2ebc8cd078f9e639207
[openssl.git] / crypto / ess / ess_lib.c
1 /*
2  * Copyright 2019-2020 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 "crypto/ess.h"
15
16 DEFINE_STACK_OF(ESS_CERT_ID)
17 DEFINE_STACK_OF(ESS_CERT_ID_V2)
18 DEFINE_STACK_OF(X509)
19 DEFINE_STACK_OF(GENERAL_NAME)
20
21 static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed);
22 static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
23                                                X509 *cert, int issuer_needed);
24
25 ESS_SIGNING_CERT *ESS_SIGNING_CERT_new_init(X509 *signcert,
26                                             STACK_OF(X509) *certs,
27                                             int issuer_needed)
28 {
29     ESS_CERT_ID *cid = NULL;
30     ESS_SIGNING_CERT *sc;
31     int i;
32
33     if ((sc = ESS_SIGNING_CERT_new()) == NULL)
34         goto err;
35     if (sc->cert_ids == NULL
36         && (sc->cert_ids = sk_ESS_CERT_ID_new_null()) == NULL)
37         goto err;
38
39     if ((cid = ESS_CERT_ID_new_init(signcert, issuer_needed)) == NULL
40         || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
41         goto err;
42     for (i = 0; i < sk_X509_num(certs); ++i) {
43         X509 *cert = sk_X509_value(certs, i);
44         if ((cid = ESS_CERT_ID_new_init(cert, 1)) == NULL
45             || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
46             goto err;
47     }
48
49     return sc;
50  err:
51     ESS_SIGNING_CERT_free(sc);
52     ESS_CERT_ID_free(cid);
53     ESSerr(ESS_F_ESS_SIGNING_CERT_NEW_INIT, ERR_R_MALLOC_FAILURE);
54     return NULL;
55 }
56
57 static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed)
58 {
59     ESS_CERT_ID *cid = NULL;
60     GENERAL_NAME *name = NULL;
61     unsigned char cert_sha1[SHA_DIGEST_LENGTH];
62
63     /* Call for side-effect of computing hash and caching extensions */
64     X509_check_purpose(cert, -1, 0);
65     if ((cid = ESS_CERT_ID_new()) == NULL)
66         goto err;
67     if (!X509_digest(cert, EVP_sha1(), cert_sha1, NULL))
68         goto err;
69     if (!ASN1_OCTET_STRING_set(cid->hash, cert_sha1, SHA_DIGEST_LENGTH))
70         goto err;
71
72     /* Setting the issuer/serial if requested. */
73     if (!issuer_needed)
74         return cid;
75
76     if (cid->issuer_serial == NULL
77         && (cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
78         goto err;
79     if ((name = GENERAL_NAME_new()) == NULL)
80         goto err;
81     name->type = GEN_DIRNAME;
82     if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
83         goto err;
84     if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
85         goto err;
86     name = NULL;            /* Ownership is lost. */
87     ASN1_INTEGER_free(cid->issuer_serial->serial);
88     if (!(cid->issuer_serial->serial =
89           ASN1_INTEGER_dup(X509_get_serialNumber(cert))))
90         goto err;
91
92     return cid;
93  err:
94     GENERAL_NAME_free(name);
95     ESS_CERT_ID_free(cid);
96     ESSerr(ESS_F_ESS_CERT_ID_NEW_INIT, ERR_R_MALLOC_FAILURE);
97     return NULL;
98 }
99
100 ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_new_init(const EVP_MD *hash_alg,
101                                                   X509 *signcert,
102                                                   STACK_OF(X509) *certs,
103                                                   int issuer_needed)
104 {
105     ESS_CERT_ID_V2 *cid = NULL;
106     ESS_SIGNING_CERT_V2 *sc;
107     int i;
108
109     if ((sc = ESS_SIGNING_CERT_V2_new()) == NULL)
110         goto err;
111     if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, signcert, issuer_needed)) == NULL)
112         goto err;
113     if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
114         goto err;
115     cid = NULL;
116
117     for (i = 0; i < sk_X509_num(certs); ++i) {
118         X509 *cert = sk_X509_value(certs, i);
119
120         if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, cert, 1)) == NULL)
121             goto err;
122         if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
123             goto err;
124         cid = NULL;
125     }
126
127     return sc;
128  err:
129     ESS_SIGNING_CERT_V2_free(sc);
130     ESS_CERT_ID_V2_free(cid);
131     ESSerr(ESS_F_ESS_SIGNING_CERT_V2_NEW_INIT, ERR_R_MALLOC_FAILURE);
132     return NULL;
133 }
134
135 static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
136                                                X509 *cert, int issuer_needed)
137 {
138     ESS_CERT_ID_V2 *cid;
139     GENERAL_NAME *name = NULL;
140     unsigned char hash[EVP_MAX_MD_SIZE];
141     unsigned int hash_len = sizeof(hash);
142     X509_ALGOR *alg = NULL;
143
144     memset(hash, 0, sizeof(hash));
145
146     if ((cid = ESS_CERT_ID_V2_new()) == NULL)
147         goto err;
148
149     if (hash_alg != EVP_sha256()) {
150         alg = X509_ALGOR_new();
151         if (alg == NULL)
152             goto err;
153         X509_ALGOR_set_md(alg, hash_alg);
154         if (alg->algorithm == NULL)
155             goto err;
156         cid->hash_alg = alg;
157         alg = NULL;
158     } else {
159         cid->hash_alg = NULL;
160     }
161
162     if (!X509_digest(cert, hash_alg, hash, &hash_len))
163         goto err;
164
165     if (!ASN1_OCTET_STRING_set(cid->hash, hash, hash_len))
166         goto err;
167
168     if (!issuer_needed)
169         return cid;
170
171     if ((cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
172         goto err;
173     if ((name = GENERAL_NAME_new()) == NULL)
174         goto err;
175     name->type = GEN_DIRNAME;
176     if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
177         goto err;
178     if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
179         goto err;
180     name = NULL;            /* Ownership is lost. */
181     ASN1_INTEGER_free(cid->issuer_serial->serial);
182     cid->issuer_serial->serial = ASN1_INTEGER_dup(X509_get_serialNumber(cert));
183     if (cid->issuer_serial->serial == NULL)
184         goto err;
185
186     return cid;
187  err:
188     X509_ALGOR_free(alg);
189     GENERAL_NAME_free(name);
190     ESS_CERT_ID_V2_free(cid);
191     ESSerr(ESS_F_ESS_CERT_ID_V2_NEW_INIT, ERR_R_MALLOC_FAILURE);
192     return NULL;
193 }
194
195 ESS_SIGNING_CERT *ESS_SIGNING_CERT_get(PKCS7_SIGNER_INFO *si)
196 {
197     ASN1_TYPE *attr;
198     const unsigned char *p;
199     attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificate);
200     if (!attr)
201         return NULL;
202     p = attr->value.sequence->data;
203     return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
204 }
205
206 ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_get(PKCS7_SIGNER_INFO *si)
207 {
208     ASN1_TYPE *attr;
209     const unsigned char *p;
210
211     attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificateV2);
212     if (attr == NULL)
213         return NULL;
214     p = attr->value.sequence->data;
215     return d2i_ESS_SIGNING_CERT_V2(NULL, &p, attr->value.sequence->length);
216 }
217
218 int ESS_SIGNING_CERT_add(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc)
219 {
220     ASN1_STRING *seq = NULL;
221     unsigned char *p, *pp = NULL;
222     int len;
223
224     len = i2d_ESS_SIGNING_CERT(sc, NULL);
225     if ((pp = OPENSSL_malloc(len)) == NULL) {
226         ESSerr(ESS_F_ESS_SIGNING_CERT_ADD, ERR_R_MALLOC_FAILURE);
227         goto err;
228     }
229     p = pp;
230     i2d_ESS_SIGNING_CERT(sc, &p);
231     if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
232         ESSerr(ESS_F_ESS_SIGNING_CERT_ADD, ERR_R_MALLOC_FAILURE);
233         goto err;
234     }
235     OPENSSL_free(pp);
236     pp = NULL;
237     return PKCS7_add_signed_attribute(si,
238                                       NID_id_smime_aa_signingCertificate,
239                                       V_ASN1_SEQUENCE, seq);
240  err:
241     ASN1_STRING_free(seq);
242     OPENSSL_free(pp);
243
244     return 0;
245 }
246
247 int ESS_SIGNING_CERT_V2_add(PKCS7_SIGNER_INFO *si,
248                             ESS_SIGNING_CERT_V2 *sc)
249 {
250     ASN1_STRING *seq = NULL;
251     unsigned char *p, *pp = NULL;
252     int len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
253
254     if ((pp = OPENSSL_malloc(len)) == NULL) {
255         ESSerr(ESS_F_ESS_SIGNING_CERT_V2_ADD, ERR_R_MALLOC_FAILURE);
256         goto err;
257     }
258
259     p = pp;
260     i2d_ESS_SIGNING_CERT_V2(sc, &p);
261     if ((seq = ASN1_STRING_new()) == NULL || !ASN1_STRING_set(seq, pp, len)) {
262         ESSerr(ESS_F_ESS_SIGNING_CERT_V2_ADD, ERR_R_MALLOC_FAILURE);
263         goto err;
264     }
265
266     OPENSSL_free(pp);
267     pp = NULL;
268     return PKCS7_add_signed_attribute(si,
269                                       NID_id_smime_aa_signingCertificateV2,
270                                       V_ASN1_SEQUENCE, seq);
271  err:
272     ASN1_STRING_free(seq);
273     OPENSSL_free(pp);
274     return 0;
275 }