CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / x509 / x509_v3.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/safestack.h>
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include "x509_local.h"
19
20 DEFINE_STACK_OF(X509_EXTENSION)
21
22 int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
23 {
24     if (x == NULL)
25         return 0;
26     return sk_X509_EXTENSION_num(x);
27 }
28
29 int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
30                           int lastpos)
31 {
32     ASN1_OBJECT *obj;
33
34     obj = OBJ_nid2obj(nid);
35     if (obj == NULL)
36         return -2;
37     return X509v3_get_ext_by_OBJ(x, obj, lastpos);
38 }
39
40 int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,
41                           const ASN1_OBJECT *obj, int lastpos)
42 {
43     int n;
44     X509_EXTENSION *ex;
45
46     if (sk == NULL)
47         return -1;
48     lastpos++;
49     if (lastpos < 0)
50         lastpos = 0;
51     n = sk_X509_EXTENSION_num(sk);
52     for (; lastpos < n; lastpos++) {
53         ex = sk_X509_EXTENSION_value(sk, lastpos);
54         if (OBJ_cmp(ex->object, obj) == 0)
55             return lastpos;
56     }
57     return -1;
58 }
59
60 int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
61                                int lastpos)
62 {
63     int n;
64     X509_EXTENSION *ex;
65
66     if (sk == NULL)
67         return -1;
68     lastpos++;
69     if (lastpos < 0)
70         lastpos = 0;
71     n = sk_X509_EXTENSION_num(sk);
72     for (; lastpos < n; lastpos++) {
73         ex = sk_X509_EXTENSION_value(sk, lastpos);
74         if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
75             return lastpos;
76     }
77     return -1;
78 }
79
80 X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
81 {
82     if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
83         return NULL;
84     else
85         return sk_X509_EXTENSION_value(x, loc);
86 }
87
88 X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
89 {
90     X509_EXTENSION *ret;
91
92     if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
93         return NULL;
94     ret = sk_X509_EXTENSION_delete(x, loc);
95     return ret;
96 }
97
98 STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
99                                          X509_EXTENSION *ex, int loc)
100 {
101     X509_EXTENSION *new_ex = NULL;
102     int n;
103     STACK_OF(X509_EXTENSION) *sk = NULL;
104
105     if (x == NULL) {
106         X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER);
107         goto err2;
108     }
109
110     if (*x == NULL) {
111         if ((sk = sk_X509_EXTENSION_new_null()) == NULL)
112             goto err;
113     } else
114         sk = *x;
115
116     n = sk_X509_EXTENSION_num(sk);
117     if (loc > n)
118         loc = n;
119     else if (loc < 0)
120         loc = n;
121
122     if ((new_ex = X509_EXTENSION_dup(ex)) == NULL)
123         goto err2;
124     if (!sk_X509_EXTENSION_insert(sk, new_ex, loc))
125         goto err;
126     if (*x == NULL)
127         *x = sk;
128     return sk;
129  err:
130     X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE);
131  err2:
132     X509_EXTENSION_free(new_ex);
133     if (x != NULL && *x == NULL)
134         sk_X509_EXTENSION_free(sk);
135     return NULL;
136 }
137
138 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
139                                              int crit,
140                                              ASN1_OCTET_STRING *data)
141 {
142     ASN1_OBJECT *obj;
143     X509_EXTENSION *ret;
144
145     obj = OBJ_nid2obj(nid);
146     if (obj == NULL) {
147         X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID);
148         return NULL;
149     }
150     ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
151     if (ret == NULL)
152         ASN1_OBJECT_free(obj);
153     return ret;
154 }
155
156 X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
157                                              const ASN1_OBJECT *obj, int crit,
158                                              ASN1_OCTET_STRING *data)
159 {
160     X509_EXTENSION *ret;
161
162     if ((ex == NULL) || (*ex == NULL)) {
163         if ((ret = X509_EXTENSION_new()) == NULL) {
164             X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,
165                     ERR_R_MALLOC_FAILURE);
166             return NULL;
167         }
168     } else
169         ret = *ex;
170
171     if (!X509_EXTENSION_set_object(ret, obj))
172         goto err;
173     if (!X509_EXTENSION_set_critical(ret, crit))
174         goto err;
175     if (!X509_EXTENSION_set_data(ret, data))
176         goto err;
177
178     if ((ex != NULL) && (*ex == NULL))
179         *ex = ret;
180     return ret;
181  err:
182     if ((ex == NULL) || (ret != *ex))
183         X509_EXTENSION_free(ret);
184     return NULL;
185 }
186
187 int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
188 {
189     if ((ex == NULL) || (obj == NULL))
190         return 0;
191     ASN1_OBJECT_free(ex->object);
192     ex->object = OBJ_dup(obj);
193     return ex->object != NULL;
194 }
195
196 int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
197 {
198     if (ex == NULL)
199         return 0;
200     ex->critical = (crit) ? 0xFF : -1;
201     return 1;
202 }
203
204 int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
205 {
206     int i;
207
208     if (ex == NULL)
209         return 0;
210     i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
211     if (!i)
212         return 0;
213     return 1;
214 }
215
216 ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
217 {
218     if (ex == NULL)
219         return NULL;
220     return ex->object;
221 }
222
223 ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
224 {
225     if (ex == NULL)
226         return NULL;
227     return &ex->value;
228 }
229
230 int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
231 {
232     if (ex == NULL)
233         return 0;
234     if (ex->critical > 0)
235         return 1;
236     return 0;
237 }