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