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