9445430b47a27b3cae7aa0f2b8f4fa193ff93cf4
[openssl.git] / crypto / x509 / x_x509.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "internal/x509_int.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_NEW_POST:
44         ret->ex_flags = 0;
45         ret->ex_pathlen = -1;
46         ret->skid = NULL;
47         ret->akid = NULL;
48 #ifndef OPENSSL_NO_RFC3779
49         ret->rfc3779_addr = NULL;
50         ret->rfc3779_asid = NULL;
51 #endif
52         ret->aux = NULL;
53         ret->crldp = NULL;
54         if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data))
55             return 0;
56         break;
57
58     case ASN1_OP_FREE_POST:
59         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
60         X509_CERT_AUX_free(ret->aux);
61         ASN1_OCTET_STRING_free(ret->skid);
62         AUTHORITY_KEYID_free(ret->akid);
63         CRL_DIST_POINTS_free(ret->crldp);
64         policy_cache_free(ret->policy_cache);
65         GENERAL_NAMES_free(ret->altname);
66         NAME_CONSTRAINTS_free(ret->nc);
67 #ifndef OPENSSL_NO_RFC3779
68         sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
69         ASIdentifiers_free(ret->rfc3779_asid);
70 #endif
71         break;
72
73     }
74
75     return 1;
76
77 }
78
79 ASN1_SEQUENCE_ref(X509, x509_cb) = {
80         ASN1_EMBED(X509, cert_info, X509_CINF),
81         ASN1_EMBED(X509, sig_alg, X509_ALGOR),
82         ASN1_EMBED(X509, signature, ASN1_BIT_STRING)
83 } ASN1_SEQUENCE_END_ref(X509, X509)
84
85 IMPLEMENT_ASN1_FUNCTIONS(X509)
86
87 IMPLEMENT_ASN1_DUP_FUNCTION(X509)
88
89 int X509_set_ex_data(X509 *r, int idx, void *arg)
90 {
91     return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
92 }
93
94 void *X509_get_ex_data(X509 *r, int idx)
95 {
96     return (CRYPTO_get_ex_data(&r->ex_data, idx));
97 }
98
99 /*
100  * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
101  * extra info tagged on the end. Since these functions set how a certificate
102  * is trusted they should only be used when the certificate comes from a
103  * reliable source such as local storage.
104  */
105
106 X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
107 {
108     const unsigned char *q;
109     X509 *ret;
110     int freeret = 0;
111
112     /* Save start position */
113     q = *pp;
114
115     if (a == NULL || *a == NULL)
116         freeret = 1;
117     ret = d2i_X509(a, &q, length);
118     /* If certificate unreadable then forget it */
119     if (ret == NULL)
120         return NULL;
121     /* update length */
122     length -= q - *pp;
123     if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length))
124         goto err;
125     *pp = q;
126     return ret;
127  err:
128     if (freeret) {
129         X509_free(ret);
130         if (a)
131             *a = NULL;
132     }
133     return NULL;
134 }
135
136 /*
137  * Serialize trusted certificate to *pp or just return the required buffer
138  * length if pp == NULL.  We ultimately want to avoid modifying *pp in the
139  * error path, but that depends on similar hygiene in lower-level functions.
140  * Here we avoid compounding the problem.
141  */
142 static int i2d_x509_aux_internal(X509 *a, unsigned char **pp)
143 {
144     int length, tmplen;
145     unsigned char *start = pp != NULL ? *pp : NULL;
146
147     OPENSSL_assert(pp == NULL || *pp != NULL);
148
149     /*
150      * This might perturb *pp on error, but fixing that belongs in i2d_X509()
151      * not here.  It should be that if a == NULL length is zero, but we check
152      * both just in case.
153      */
154     length = i2d_X509(a, pp);
155     if (length <= 0 || a == NULL)
156         return length;
157
158     tmplen = i2d_X509_CERT_AUX(a->aux, pp);
159     if (tmplen < 0) {
160         if (start != NULL)
161             *pp = start;
162         return tmplen;
163     }
164     length += tmplen;
165
166     return length;
167 }
168
169 /*
170  * Serialize trusted certificate to *pp, or just return the required buffer
171  * length if pp == NULL.
172  *
173  * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
174  * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
175  * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
176  * allocated buffer.
177  */
178 int i2d_X509_AUX(X509 *a, unsigned char **pp)
179 {
180     int length;
181     unsigned char *tmp;
182
183     /* Buffer provided by caller */
184     if (pp == NULL || *pp != NULL)
185         return i2d_x509_aux_internal(a, pp);
186
187     /* Obtain the combined length */
188     if ((length = i2d_x509_aux_internal(a, NULL)) <= 0)
189         return length;
190
191     /* Allocate requisite combined storage */
192     *pp = tmp = OPENSSL_malloc(length);
193     if (tmp == NULL)
194         return -1; /* Push error onto error stack? */
195
196     /* Encode, but keep *pp at the originally malloced pointer */
197     length = i2d_x509_aux_internal(a, &tmp);
198     if (length <= 0) {
199         OPENSSL_free(*pp);
200         *pp = NULL;
201     }
202     return length;
203 }
204
205 int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
206 {
207     x->cert_info.enc.modified = 1;
208     return i2d_X509_CINF(&x->cert_info, pp);
209 }
210
211 void X509_get0_signature(ASN1_BIT_STRING **psig, X509_ALGOR **palg, X509 *x)
212 {
213     if (psig)
214         *psig = &x->signature;
215     if (palg)
216         *palg = &x->sig_alg;
217 }
218
219 int X509_get_signature_nid(const X509 *x)
220 {
221     return OBJ_obj2nid(x->sig_alg.algorithm);
222 }