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