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