]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509/x509_v3.c
Raise an error on syscall failure in tls_retry_write_records
[thirdparty/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;
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 if (((ex->critical > 0) && crit) || ((ex->critical <= 0) && !crit))
76 return lastpos;
77 }
78 return -1;
79 }
80
81 X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
82 {
83 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
84 return NULL;
85 else
86 return sk_X509_EXTENSION_value(x, loc);
87 }
88
89 X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
90 {
91 X509_EXTENSION *ret;
92
93 if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
94 return NULL;
95 ret = sk_X509_EXTENSION_delete(x, loc);
96 return ret;
97 }
98
99 STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
100 X509_EXTENSION *ex, int loc)
101 {
102 X509_EXTENSION *new_ex = NULL;
103 int n;
104 STACK_OF(X509_EXTENSION) *sk = NULL;
105
106 if (x == NULL) {
107 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
108 goto err;
109 }
110
111 if (*x == NULL) {
112 if ((sk = sk_X509_EXTENSION_new_null()) == NULL) {
113 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
114 goto err;
115 }
116 } else
117 sk = *x;
118
119 n = sk_X509_EXTENSION_num(sk);
120 if (loc > n)
121 loc = n;
122 else if (loc < 0)
123 loc = n;
124
125 if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) {
126 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
127 goto err;
128 }
129 if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) {
130 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
131 goto err;
132 }
133 if (*x == NULL)
134 *x = sk;
135 return sk;
136 err:
137 X509_EXTENSION_free(new_ex);
138 if (x != NULL && *x == NULL)
139 sk_X509_EXTENSION_free(sk);
140 return NULL;
141 }
142
143 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
144 int crit,
145 ASN1_OCTET_STRING *data)
146 {
147 ASN1_OBJECT *obj;
148 X509_EXTENSION *ret;
149
150 obj = OBJ_nid2obj(nid);
151 if (obj == NULL) {
152 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
153 return NULL;
154 }
155 ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
156 if (ret == NULL)
157 ASN1_OBJECT_free(obj);
158 return ret;
159 }
160
161 X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
162 const ASN1_OBJECT *obj, int crit,
163 ASN1_OCTET_STRING *data)
164 {
165 X509_EXTENSION *ret;
166
167 if ((ex == NULL) || (*ex == NULL)) {
168 if ((ret = X509_EXTENSION_new()) == NULL) {
169 ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
170 return NULL;
171 }
172 } else
173 ret = *ex;
174
175 if (!X509_EXTENSION_set_object(ret, obj))
176 goto err;
177 if (!X509_EXTENSION_set_critical(ret, crit))
178 goto err;
179 if (!X509_EXTENSION_set_data(ret, data))
180 goto err;
181
182 if ((ex != NULL) && (*ex == NULL))
183 *ex = ret;
184 return ret;
185 err:
186 if ((ex == NULL) || (ret != *ex))
187 X509_EXTENSION_free(ret);
188 return NULL;
189 }
190
191 int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
192 {
193 if ((ex == NULL) || (obj == NULL))
194 return 0;
195 ASN1_OBJECT_free(ex->object);
196 ex->object = OBJ_dup(obj);
197 return ex->object != NULL;
198 }
199
200 int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
201 {
202 if (ex == NULL)
203 return 0;
204 ex->critical = (crit) ? 0xFF : -1;
205 return 1;
206 }
207
208 int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
209 {
210 int i;
211
212 if (ex == NULL)
213 return 0;
214 i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
215 if (!i)
216 return 0;
217 return 1;
218 }
219
220 ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
221 {
222 if (ex == NULL)
223 return NULL;
224 return ex->object;
225 }
226
227 ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
228 {
229 if (ex == NULL)
230 return NULL;
231 return &ex->value;
232 }
233
234 int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
235 {
236 if (ex == NULL)
237 return 0;
238 if (ex->critical > 0)
239 return 1;
240 return 0;
241 }