7b41ce0777f66b885412622dd93ae6beeb639c1b
[openssl.git] / crypto / x509 / x_x509.c
1 /*
2  * Copyright 1995-2018 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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include "crypto/x509.h"
17
18 ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = {
19         ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0),
20         ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER),
21         ASN1_EMBED(X509_CINF, signature, X509_ALGOR),
22         ASN1_SIMPLE(X509_CINF, issuer, X509_NAME),
23         ASN1_EMBED(X509_CINF, validity, X509_VAL),
24         ASN1_SIMPLE(X509_CINF, subject, X509_NAME),
25         ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY),
26         ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1),
27         ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2),
28         ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3)
29 } ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF)
30
31 IMPLEMENT_ASN1_FUNCTIONS(X509_CINF)
32 /* X509 top level structure needs a bit of customisation */
33
34 extern void policy_cache_free(X509_POLICY_CACHE *cache);
35
36 static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
37                    void *exarg)
38 {
39     X509 *ret = (X509 *)*pval;
40
41     switch (operation) {
42
43     case ASN1_OP_D2I_PRE:
44         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
45         X509_CERT_AUX_free(ret->aux);
46         ASN1_OCTET_STRING_free(ret->skid);
47         AUTHORITY_KEYID_free(ret->akid);
48         CRL_DIST_POINTS_free(ret->crldp);
49         policy_cache_free(ret->policy_cache);
50         GENERAL_NAMES_free(ret->altname);
51         NAME_CONSTRAINTS_free(ret->nc);
52 #ifndef OPENSSL_NO_RFC3779
53         sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
54         ASIdentifiers_free(ret->rfc3779_asid);
55 #endif
56 #ifndef OPENSSL_NO_SM2
57         ASN1_OCTET_STRING_free(ret->sm2_id);
58 #endif
59
60         /* fall thru */
61
62     case ASN1_OP_NEW_POST:
63         ret->ex_cached = 0;
64         ret->ex_kusage = 0;
65         ret->ex_xkusage = 0;
66         ret->ex_nscert = 0;
67         ret->ex_flags = 0;
68         ret->ex_pathlen = -1;
69         ret->ex_pcpathlen = -1;
70         ret->skid = NULL;
71         ret->akid = NULL;
72         ret->policy_cache = NULL;
73         ret->altname = NULL;
74         ret->nc = NULL;
75 #ifndef OPENSSL_NO_RFC3779
76         ret->rfc3779_addr = NULL;
77         ret->rfc3779_asid = NULL;
78 #endif
79 #ifndef OPENSSL_NO_SM2
80         ret->sm2_id = NULL;
81 #endif
82         ret->aux = NULL;
83         ret->crldp = NULL;
84         if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data))
85             return 0;
86         break;
87
88     case ASN1_OP_FREE_POST:
89         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
90         X509_CERT_AUX_free(ret->aux);
91         ASN1_OCTET_STRING_free(ret->skid);
92         AUTHORITY_KEYID_free(ret->akid);
93         CRL_DIST_POINTS_free(ret->crldp);
94         policy_cache_free(ret->policy_cache);
95         GENERAL_NAMES_free(ret->altname);
96         NAME_CONSTRAINTS_free(ret->nc);
97 #ifndef OPENSSL_NO_RFC3779
98         sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
99         ASIdentifiers_free(ret->rfc3779_asid);
100 #endif
101 #ifndef OPENSSL_NO_SM2
102         ASN1_OCTET_STRING_free(ret->sm2_id);
103 #endif
104         break;
105
106     }
107
108     return 1;
109
110 }
111
112 ASN1_SEQUENCE_ref(X509, x509_cb) = {
113         ASN1_EMBED(X509, cert_info, X509_CINF),
114         ASN1_EMBED(X509, sig_alg, X509_ALGOR),
115         ASN1_EMBED(X509, signature, ASN1_BIT_STRING)
116 } ASN1_SEQUENCE_END_ref(X509, X509)
117
118 IMPLEMENT_ASN1_FUNCTIONS(X509)
119 IMPLEMENT_ASN1_DUP_FUNCTION(X509)
120
121 int X509_set_ex_data(X509 *r, int idx, void *arg)
122 {
123     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
124 }
125
126 void *X509_get_ex_data(X509 *r, int idx)
127 {
128     return CRYPTO_get_ex_data(&r->ex_data, idx);
129 }
130
131 /*
132  * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
133  * extra info tagged on the end. Since these functions set how a certificate
134  * is trusted they should only be used when the certificate comes from a
135  * reliable source such as local storage.
136  */
137
138 X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
139 {
140     const unsigned char *q;
141     X509 *ret;
142     int freeret = 0;
143
144     /* Save start position */
145     q = *pp;
146
147     if (a == NULL || *a == NULL)
148         freeret = 1;
149     ret = d2i_X509(a, &q, length);
150     /* If certificate unreadable then forget it */
151     if (ret == NULL)
152         return NULL;
153     /* update length */
154     length -= q - *pp;
155     if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length))
156         goto err;
157     *pp = q;
158     return ret;
159  err:
160     if (freeret) {
161         X509_free(ret);
162         if (a)
163             *a = NULL;
164     }
165     return NULL;
166 }
167
168 /*
169  * Serialize trusted certificate to *pp or just return the required buffer
170  * length if pp == NULL.  We ultimately want to avoid modifying *pp in the
171  * error path, but that depends on similar hygiene in lower-level functions.
172  * Here we avoid compounding the problem.
173  */
174 static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp)
175 {
176     int length, tmplen;
177     unsigned char *start = pp != NULL ? *pp : NULL;
178
179     /*
180      * This might perturb *pp on error, but fixing that belongs in i2d_X509()
181      * not here.  It should be that if a == NULL length is zero, but we check
182      * both just in case.
183      */
184     length = i2d_X509(a, pp);
185     if (length <= 0 || a == NULL)
186         return length;
187
188     tmplen = i2d_X509_CERT_AUX(a->aux, pp);
189     if (tmplen < 0) {
190         if (start != NULL)
191             *pp = start;
192         return tmplen;
193     }
194     length += tmplen;
195
196     return length;
197 }
198
199 /*
200  * Serialize trusted certificate to *pp, or just return the required buffer
201  * length if pp == NULL.
202  *
203  * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
204  * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
205  * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
206  * allocated buffer.
207  */
208 int i2d_X509_AUX(const X509 *a, unsigned char **pp)
209 {
210     int length;
211     unsigned char *tmp;
212
213     /* Buffer provided by caller */
214     if (pp == NULL || *pp != NULL)
215         return i2d_x509_aux_internal(a, pp);
216
217     /* Obtain the combined length */
218     if ((length = i2d_x509_aux_internal(a, NULL)) <= 0)
219         return length;
220
221     /* Allocate requisite combined storage */
222     *pp = tmp = OPENSSL_malloc(length);
223     if (tmp == NULL) {
224         X509err(X509_F_I2D_X509_AUX, ERR_R_MALLOC_FAILURE);
225         return -1;
226     }
227
228     /* Encode, but keep *pp at the originally malloced pointer */
229     length = i2d_x509_aux_internal(a, &tmp);
230     if (length <= 0) {
231         OPENSSL_free(*pp);
232         *pp = NULL;
233     }
234     return length;
235 }
236
237 int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
238 {
239     x->cert_info.enc.modified = 1;
240     return i2d_X509_CINF(&x->cert_info, pp);
241 }
242
243 void X509_get0_signature(const ASN1_BIT_STRING **psig,
244                          const X509_ALGOR **palg, const X509 *x)
245 {
246     if (psig)
247         *psig = &x->signature;
248     if (palg)
249         *palg = &x->sig_alg;
250 }
251
252 int X509_get_signature_nid(const X509 *x)
253 {
254     return OBJ_obj2nid(x->sig_alg.algorithm);
255 }
256
257 #ifndef OPENSSL_NO_SM2
258 void X509_set0_sm2_id(X509 *x, ASN1_OCTET_STRING *sm2_id)
259 {
260     ASN1_OCTET_STRING_free(x->sm2_id);
261     x->sm2_id = sm2_id;
262 }
263
264 ASN1_OCTET_STRING *X509_get0_sm2_id(X509 *x)
265 {
266     return x->sm2_id;
267 }
268 #endif