xref: /openssl/crypto/x509/x509_v3.c (revision 577ec498)
1 /*
2  * Copyright 1995-2024 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 
X509v3_get_ext_count(const STACK_OF (X509_EXTENSION)* x)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 
X509v3_get_ext_by_NID(const STACK_OF (X509_EXTENSION)* x,int nid,int lastpos)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 
X509v3_get_ext_by_OBJ(const STACK_OF (X509_EXTENSION)* sk,const ASN1_OBJECT * obj,int lastpos)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 
X509v3_get_ext_by_critical(const STACK_OF (X509_EXTENSION)* sk,int crit,int lastpos)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 
X509v3_get_ext(const STACK_OF (X509_EXTENSION)* x,int loc)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 
X509v3_delete_ext(STACK_OF (X509_EXTENSION)* x,int loc)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 
STACK_OF(X509_EXTENSION)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 /* This returns NULL also in non-error case *target == NULL && sk_X509_EXTENSION_num(exts) <= 0 */
STACK_OF(X509_EXTENSION)146 STACK_OF(X509_EXTENSION) *X509v3_add_extensions(STACK_OF(X509_EXTENSION) **target,
147                                                 const STACK_OF(X509_EXTENSION) *exts)
148 {
149     int i;
150 
151     if (target == NULL) {
152         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
153         return NULL;
154     }
155 
156     for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
157         X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
158         ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);
159         int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
160 
161         /* Does extension exist in target? */
162         if (idx != -1) {
163             /* Delete all extensions of same type */
164             do {
165                 X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));
166                 idx = X509v3_get_ext_by_OBJ(*target, obj, -1);
167             } while (idx != -1);
168         }
169         if (!X509v3_add_ext(target, ext, -1))
170             return NULL;
171     }
172     return *target;
173 }
174 
X509_EXTENSION_create_by_NID(X509_EXTENSION ** ex,int nid,int crit,ASN1_OCTET_STRING * data)175 X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
176                                              int crit,
177                                              ASN1_OCTET_STRING *data)
178 {
179     ASN1_OBJECT *obj;
180     X509_EXTENSION *ret;
181 
182     obj = OBJ_nid2obj(nid);
183     if (obj == NULL) {
184         ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
185         return NULL;
186     }
187     ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);
188     if (ret == NULL)
189         ASN1_OBJECT_free(obj);
190     return ret;
191 }
192 
X509_EXTENSION_create_by_OBJ(X509_EXTENSION ** ex,const ASN1_OBJECT * obj,int crit,ASN1_OCTET_STRING * data)193 X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
194                                              const ASN1_OBJECT *obj, int crit,
195                                              ASN1_OCTET_STRING *data)
196 {
197     X509_EXTENSION *ret;
198 
199     if ((ex == NULL) || (*ex == NULL)) {
200         if ((ret = X509_EXTENSION_new()) == NULL) {
201             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
202             return NULL;
203         }
204     } else
205         ret = *ex;
206 
207     if (!X509_EXTENSION_set_object(ret, obj))
208         goto err;
209     if (!X509_EXTENSION_set_critical(ret, crit))
210         goto err;
211     if (!X509_EXTENSION_set_data(ret, data))
212         goto err;
213 
214     if ((ex != NULL) && (*ex == NULL))
215         *ex = ret;
216     return ret;
217  err:
218     if ((ex == NULL) || (ret != *ex))
219         X509_EXTENSION_free(ret);
220     return NULL;
221 }
222 
X509_EXTENSION_set_object(X509_EXTENSION * ex,const ASN1_OBJECT * obj)223 int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)
224 {
225     if ((ex == NULL) || (obj == NULL))
226         return 0;
227     ASN1_OBJECT_free(ex->object);
228     ex->object = OBJ_dup(obj);
229     return ex->object != NULL;
230 }
231 
X509_EXTENSION_set_critical(X509_EXTENSION * ex,int crit)232 int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
233 {
234     if (ex == NULL)
235         return 0;
236     ex->critical = (crit) ? 0xFF : 0;
237     return 1;
238 }
239 
X509_EXTENSION_set_data(X509_EXTENSION * ex,ASN1_OCTET_STRING * data)240 int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
241 {
242     int i;
243 
244     if (ex == NULL)
245         return 0;
246     i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);
247     if (!i)
248         return 0;
249     return 1;
250 }
251 
X509_EXTENSION_get_object(X509_EXTENSION * ex)252 ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
253 {
254     if (ex == NULL)
255         return NULL;
256     return ex->object;
257 }
258 
X509_EXTENSION_get_data(X509_EXTENSION * ex)259 ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
260 {
261     if (ex == NULL)
262         return NULL;
263     return &ex->value;
264 }
265 
X509_EXTENSION_get_critical(const X509_EXTENSION * ex)266 int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)
267 {
268     if (ex == NULL)
269         return 0;
270     if (ex->critical > 0)
271         return 1;
272     return 0;
273 }
274