]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
0d664759 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
1f5e0f92 | 12 | #include <openssl/safestack.h> |
ec577822 BM |
13 | #include <openssl/asn1.h> |
14 | #include <openssl/objects.h> | |
15 | #include <openssl/evp.h> | |
16 | #include <openssl/x509.h> | |
13938ace | 17 | #include <openssl/x509v3.h> |
706457b7 | 18 | #include "x509_local.h" |
d02b48c6 | 19 | |
0b3f827c | 20 | int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x) |
0f113f3e MC |
21 | { |
22 | if (x == NULL) | |
26a7d938 K |
23 | return 0; |
24 | return sk_X509_EXTENSION_num(x); | |
0f113f3e | 25 | } |
d02b48c6 | 26 | |
0b3f827c | 27 | int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid, |
0f113f3e MC |
28 | int lastpos) |
29 | { | |
30 | ASN1_OBJECT *obj; | |
d02b48c6 | 31 | |
0f113f3e MC |
32 | obj = OBJ_nid2obj(nid); |
33 | if (obj == NULL) | |
26a7d938 K |
34 | return -2; |
35 | return X509v3_get_ext_by_OBJ(x, obj, lastpos); | |
0f113f3e | 36 | } |
d02b48c6 | 37 | |
0f113f3e | 38 | int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, |
c47ba4e9 | 39 | const ASN1_OBJECT *obj, int lastpos) |
0f113f3e MC |
40 | { |
41 | int n; | |
42 | X509_EXTENSION *ex; | |
d02b48c6 | 43 | |
0f113f3e | 44 | if (sk == NULL) |
26a7d938 | 45 | return -1; |
0f113f3e MC |
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) | |
26a7d938 | 53 | return lastpos; |
0f113f3e | 54 | } |
26a7d938 | 55 | return -1; |
0f113f3e | 56 | } |
d02b48c6 | 57 | |
0b3f827c | 58 | int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit, |
0f113f3e MC |
59 | int lastpos) |
60 | { | |
61 | int n; | |
62 | X509_EXTENSION *ex; | |
d02b48c6 | 63 | |
0f113f3e | 64 | if (sk == NULL) |
26a7d938 | 65 | return -1; |
0f113f3e MC |
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)) | |
26a7d938 | 73 | return lastpos; |
0f113f3e | 74 | } |
26a7d938 | 75 | return -1; |
0f113f3e | 76 | } |
d02b48c6 | 77 | |
0b3f827c | 78 | X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc) |
0f113f3e MC |
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 | } | |
d02b48c6 | 85 | |
0b3f827c | 86 | X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc) |
0f113f3e MC |
87 | { |
88 | X509_EXTENSION *ret; | |
d02b48c6 | 89 | |
0f113f3e | 90 | if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0) |
26a7d938 | 91 | return NULL; |
0f113f3e | 92 | ret = sk_X509_EXTENSION_delete(x, loc); |
26a7d938 | 93 | return ret; |
0f113f3e | 94 | } |
d02b48c6 | 95 | |
0b3f827c | 96 | STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x, |
0f113f3e MC |
97 | X509_EXTENSION *ex, int loc) |
98 | { | |
99 | X509_EXTENSION *new_ex = NULL; | |
100 | int n; | |
101 | STACK_OF(X509_EXTENSION) *sk = NULL; | |
d02b48c6 | 102 | |
0f113f3e MC |
103 | if (x == NULL) { |
104 | X509err(X509_F_X509V3_ADD_EXT, ERR_R_PASSED_NULL_PARAMETER); | |
105 | goto err2; | |
106 | } | |
c755c5fd | 107 | |
0f113f3e MC |
108 | if (*x == NULL) { |
109 | if ((sk = sk_X509_EXTENSION_new_null()) == NULL) | |
110 | goto err; | |
111 | } else | |
112 | sk = *x; | |
d02b48c6 | 113 | |
0f113f3e MC |
114 | n = sk_X509_EXTENSION_num(sk); |
115 | if (loc > n) | |
116 | loc = n; | |
117 | else if (loc < 0) | |
118 | loc = n; | |
d02b48c6 | 119 | |
0f113f3e MC |
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; | |
26a7d938 | 126 | return sk; |
0f113f3e MC |
127 | err: |
128 | X509err(X509_F_X509V3_ADD_EXT, ERR_R_MALLOC_FAILURE); | |
129 | err2: | |
222561fe | 130 | X509_EXTENSION_free(new_ex); |
abcf2411 PK |
131 | if (x != NULL && *x == NULL) |
132 | sk_X509_EXTENSION_free(sk); | |
26a7d938 | 133 | return NULL; |
0f113f3e | 134 | } |
d02b48c6 | 135 | |
6b691a5c | 136 | X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid, |
0f113f3e MC |
137 | int crit, |
138 | ASN1_OCTET_STRING *data) | |
139 | { | |
140 | ASN1_OBJECT *obj; | |
141 | X509_EXTENSION *ret; | |
d02b48c6 | 142 | |
0f113f3e MC |
143 | obj = OBJ_nid2obj(nid); |
144 | if (obj == NULL) { | |
145 | X509err(X509_F_X509_EXTENSION_CREATE_BY_NID, X509_R_UNKNOWN_NID); | |
26a7d938 | 146 | return NULL; |
0f113f3e MC |
147 | } |
148 | ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data); | |
149 | if (ret == NULL) | |
150 | ASN1_OBJECT_free(obj); | |
26a7d938 | 151 | return ret; |
0f113f3e | 152 | } |
d02b48c6 | 153 | |
6b691a5c | 154 | X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex, |
c47ba4e9 | 155 | const ASN1_OBJECT *obj, int crit, |
0f113f3e MC |
156 | ASN1_OCTET_STRING *data) |
157 | { | |
158 | X509_EXTENSION *ret; | |
159 | ||
160 | if ((ex == NULL) || (*ex == NULL)) { | |
161 | if ((ret = X509_EXTENSION_new()) == NULL) { | |
162 | X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ, | |
163 | ERR_R_MALLOC_FAILURE); | |
26a7d938 | 164 | return NULL; |
0f113f3e MC |
165 | } |
166 | } else | |
167 | ret = *ex; | |
d02b48c6 | 168 | |
0f113f3e MC |
169 | if (!X509_EXTENSION_set_object(ret, obj)) |
170 | goto err; | |
171 | if (!X509_EXTENSION_set_critical(ret, crit)) | |
172 | goto err; | |
173 | if (!X509_EXTENSION_set_data(ret, data)) | |
174 | goto err; | |
d02b48c6 | 175 | |
0f113f3e MC |
176 | if ((ex != NULL) && (*ex == NULL)) |
177 | *ex = ret; | |
26a7d938 | 178 | return ret; |
0f113f3e MC |
179 | err: |
180 | if ((ex == NULL) || (ret != *ex)) | |
181 | X509_EXTENSION_free(ret); | |
26a7d938 | 182 | return NULL; |
0f113f3e | 183 | } |
d02b48c6 | 184 | |
c47ba4e9 | 185 | int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj) |
0f113f3e MC |
186 | { |
187 | if ((ex == NULL) || (obj == NULL)) | |
26a7d938 | 188 | return 0; |
0f113f3e MC |
189 | ASN1_OBJECT_free(ex->object); |
190 | ex->object = OBJ_dup(obj); | |
4e0e4d29 | 191 | return ex->object != NULL; |
0f113f3e | 192 | } |
d02b48c6 | 193 | |
6b691a5c | 194 | int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit) |
0f113f3e MC |
195 | { |
196 | if (ex == NULL) | |
26a7d938 | 197 | return 0; |
0f113f3e | 198 | ex->critical = (crit) ? 0xFF : -1; |
208fb891 | 199 | return 1; |
0f113f3e | 200 | } |
d02b48c6 | 201 | |
6b691a5c | 202 | int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data) |
0f113f3e MC |
203 | { |
204 | int i; | |
d02b48c6 | 205 | |
0f113f3e | 206 | if (ex == NULL) |
26a7d938 | 207 | return 0; |
4392479c | 208 | i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length); |
0f113f3e | 209 | if (!i) |
26a7d938 | 210 | return 0; |
208fb891 | 211 | return 1; |
0f113f3e | 212 | } |
d02b48c6 | 213 | |
6b691a5c | 214 | ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex) |
0f113f3e MC |
215 | { |
216 | if (ex == NULL) | |
26a7d938 K |
217 | return NULL; |
218 | return ex->object; | |
0f113f3e | 219 | } |
d02b48c6 | 220 | |
6b691a5c | 221 | ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex) |
0f113f3e MC |
222 | { |
223 | if (ex == NULL) | |
26a7d938 | 224 | return NULL; |
4392479c | 225 | return &ex->value; |
0f113f3e | 226 | } |
d02b48c6 | 227 | |
7569362e | 228 | int X509_EXTENSION_get_critical(const X509_EXTENSION *ex) |
0f113f3e MC |
229 | { |
230 | if (ex == NULL) | |
26a7d938 | 231 | return 0; |
0f113f3e MC |
232 | if (ex->critical > 0) |
233 | return 1; | |
234 | return 0; | |
235 | } |