xref: /aosp_15_r20/external/boringssl/src/crypto/x509/x_all.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/x509.h>
58 
59 #include <limits.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/digest.h>
63 #include <openssl/dsa.h>
64 #include <openssl/evp.h>
65 #include <openssl/mem.h>
66 #include <openssl/rsa.h>
67 #include <openssl/stack.h>
68 
69 #include "../asn1/internal.h"
70 #include "internal.h"
71 
72 
X509_verify(X509 * x509,EVP_PKEY * pkey)73 int X509_verify(X509 *x509, EVP_PKEY *pkey) {
74   if (X509_ALGOR_cmp(x509->sig_alg, x509->cert_info->signature)) {
75     OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH);
76     return 0;
77   }
78   return ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), x509->sig_alg,
79                           x509->signature, x509->cert_info, pkey);
80 }
81 
X509_REQ_verify(X509_REQ * req,EVP_PKEY * pkey)82 int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey) {
83   return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), req->sig_alg,
84                           req->signature, req->req_info, pkey);
85 }
86 
X509_sign(X509 * x,EVP_PKEY * pkey,const EVP_MD * md)87 int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) {
88   asn1_encoding_clear(&x->cert_info->enc);
89   return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature,
90                          x->sig_alg, x->signature, x->cert_info, pkey, md));
91 }
92 
X509_sign_ctx(X509 * x,EVP_MD_CTX * ctx)93 int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) {
94   asn1_encoding_clear(&x->cert_info->enc);
95   return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature,
96                             x->sig_alg, x->signature, x->cert_info, ctx);
97 }
98 
X509_REQ_sign(X509_REQ * x,EVP_PKEY * pkey,const EVP_MD * md)99 int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) {
100   asn1_encoding_clear(&x->req_info->enc);
101   return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL,
102                          x->signature, x->req_info, pkey, md));
103 }
104 
X509_REQ_sign_ctx(X509_REQ * x,EVP_MD_CTX * ctx)105 int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) {
106   asn1_encoding_clear(&x->req_info->enc);
107   return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL,
108                             x->signature, x->req_info, ctx);
109 }
110 
X509_CRL_sign(X509_CRL * x,EVP_PKEY * pkey,const EVP_MD * md)111 int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) {
112   asn1_encoding_clear(&x->crl->enc);
113   return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
114                          x->sig_alg, x->signature, x->crl, pkey, md));
115 }
116 
X509_CRL_sign_ctx(X509_CRL * x,EVP_MD_CTX * ctx)117 int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) {
118   asn1_encoding_clear(&x->crl->enc);
119   return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
120                             x->sig_alg, x->signature, x->crl, ctx);
121 }
122 
NETSCAPE_SPKI_sign(NETSCAPE_SPKI * x,EVP_PKEY * pkey,const EVP_MD * md)123 int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) {
124   return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL,
125                          x->signature, x->spkac, pkey, md));
126 }
127 
NETSCAPE_SPKI_verify(NETSCAPE_SPKI * spki,EVP_PKEY * pkey)128 int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey) {
129   return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor,
130                            spki->signature, spki->spkac, pkey));
131 }
132 
d2i_X509_CRL_fp(FILE * fp,X509_CRL ** crl)133 X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) {
134   return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
135 }
136 
i2d_X509_CRL_fp(FILE * fp,X509_CRL * crl)137 int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) {
138   return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
139 }
140 
d2i_X509_CRL_bio(BIO * bp,X509_CRL ** crl)141 X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) {
142   return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
143 }
144 
i2d_X509_CRL_bio(BIO * bp,X509_CRL * crl)145 int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) {
146   return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
147 }
148 
d2i_X509_REQ_fp(FILE * fp,X509_REQ ** req)149 X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) {
150   return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
151 }
152 
i2d_X509_REQ_fp(FILE * fp,X509_REQ * req)153 int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) {
154   return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
155 }
156 
d2i_X509_REQ_bio(BIO * bp,X509_REQ ** req)157 X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) {
158   return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
159 }
160 
i2d_X509_REQ_bio(BIO * bp,X509_REQ * req)161 int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) {
162   return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
163 }
164 
165 
166 #define IMPLEMENT_D2I_FP(type, name, bio_func) \
167   type *name(FILE *fp, type **obj) {           \
168     BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);    \
169     if (bio == NULL) {                         \
170       return NULL;                             \
171     }                                          \
172     type *ret = bio_func(bio, obj);            \
173     BIO_free(bio);                             \
174     return ret;                                \
175   }
176 
177 #define IMPLEMENT_I2D_FP(type, name, bio_func) \
178   int name(FILE *fp, type *obj) {              \
179     BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);    \
180     if (bio == NULL) {                         \
181       return 0;                                \
182     }                                          \
183     int ret = bio_func(bio, obj);              \
184     BIO_free(bio);                             \
185     return ret;                                \
186   }
187 
188 IMPLEMENT_D2I_FP(X509, d2i_X509_fp, d2i_X509_bio);
189 IMPLEMENT_I2D_FP(X509, i2d_X509_fp, i2d_X509_bio);
190 
IMPLEMENT_D2I_FP(RSA,d2i_RSAPrivateKey_fp,d2i_RSAPrivateKey_bio)191 IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio)
192 IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio)
193 
194 IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio)
195 IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio)
196 
197 IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio)
198 IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio)
199 
200 #define IMPLEMENT_D2I_BIO(type, name, d2i_func)         \
201   type *name(BIO *bio, type **obj) {                    \
202     uint8_t *data;                                      \
203     size_t len;                                         \
204     if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \
205       return NULL;                                      \
206     }                                                   \
207     const uint8_t *ptr = data;                          \
208     type *ret = d2i_func(obj, &ptr, (long)len);         \
209     OPENSSL_free(data);                                 \
210     return ret;                                         \
211   }
212 
213 #define IMPLEMENT_I2D_BIO(type, name, i2d_func) \
214   int name(BIO *bio, type *obj) {               \
215     uint8_t *data = NULL;                       \
216     int len = i2d_func(obj, &data);             \
217     if (len < 0) {                              \
218       return 0;                                 \
219     }                                           \
220     int ret = BIO_write_all(bio, data, len);    \
221     OPENSSL_free(data);                         \
222     return ret;                                 \
223   }
224 
225 IMPLEMENT_D2I_BIO(X509, d2i_X509_bio, d2i_X509)
226 IMPLEMENT_I2D_BIO(X509, i2d_X509_bio, i2d_X509)
227 
228 IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey)
229 IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey)
230 
231 IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey)
232 IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey)
233 
234 IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY)
235 IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY)
236 
237 IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio)
238 IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio)
239 
240 IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio)
241 IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio)
242 
243 IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey)
244 IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey)
245 
246 IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY)
247 IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY)
248 
249 IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio)
250 IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio)
251 
252 IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio)
253 IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio)
254 
255 IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey)
256 IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey)
257 
258 IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY)
259 IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY)
260 
261 int X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
262                        unsigned int *len) {
263   ASN1_BIT_STRING *key;
264   key = X509_get0_pubkey_bitstr(data);
265   if (!key) {
266     return 0;
267   }
268   return EVP_Digest(key->data, key->length, md, len, type, NULL);
269 }
270 
X509_digest(const X509 * x509,const EVP_MD * md,uint8_t * out,unsigned * out_len)271 int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out,
272                 unsigned *out_len) {
273   uint8_t *der = NULL;
274   // TODO(https://crbug.com/boringssl/407): This function is not const-correct.
275   int der_len = i2d_X509((X509 *)x509, &der);
276   if (der_len < 0) {
277     return 0;
278   }
279 
280   int ret = EVP_Digest(der, der_len, out, out_len, md, NULL);
281   OPENSSL_free(der);
282   return ret;
283 }
284 
X509_CRL_digest(const X509_CRL * data,const EVP_MD * type,unsigned char * md,unsigned int * len)285 int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md,
286                     unsigned int *len) {
287   return (
288       ASN1_item_digest(ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len));
289 }
290 
X509_REQ_digest(const X509_REQ * data,const EVP_MD * type,unsigned char * md,unsigned int * len)291 int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md,
292                     unsigned int *len) {
293   return (
294       ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len));
295 }
296 
X509_NAME_digest(const X509_NAME * data,const EVP_MD * type,unsigned char * md,unsigned int * len)297 int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
298                      unsigned char *md, unsigned int *len) {
299   return (
300       ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len));
301 }
302 
IMPLEMENT_D2I_FP(X509_SIG,d2i_PKCS8_fp,d2i_PKCS8_bio)303 IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio)
304 IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio)
305 
306 IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG)
307 IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG)
308 
309 IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp,
310                  d2i_PKCS8_PRIV_KEY_INFO_bio)
311 IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp,
312                  i2d_PKCS8_PRIV_KEY_INFO_bio)
313 
314 int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) {
315   PKCS8_PRIV_KEY_INFO *p8inf;
316   int ret;
317   p8inf = EVP_PKEY2PKCS8(key);
318   if (!p8inf) {
319     return 0;
320   }
321   ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
322   PKCS8_PRIV_KEY_INFO_free(p8inf);
323   return ret;
324 }
325 
IMPLEMENT_D2I_FP(EVP_PKEY,d2i_PrivateKey_fp,d2i_PrivateKey_bio)326 IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio)
327 IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio)
328 
329 IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio)
330 IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio)
331 
332 IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio,
333                   d2i_PKCS8_PRIV_KEY_INFO)
334 IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio,
335                   i2d_PKCS8_PRIV_KEY_INFO)
336 
337 int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) {
338   PKCS8_PRIV_KEY_INFO *p8inf;
339   int ret;
340   p8inf = EVP_PKEY2PKCS8(key);
341   if (!p8inf) {
342     return 0;
343   }
344   ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
345   PKCS8_PRIV_KEY_INFO_free(p8inf);
346   return ret;
347 }
348 
349 IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey)
350 IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey)
351 
352 IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY)
353 IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY)
354 
355 IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams)
356 IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams)
357