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