/src/openssl/crypto/x509/x509_v3.c
Line  | Count  | Source  | 
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  |  |  | 
20  |  | int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)  | 
21  | 0  | { | 
22  | 0  |     int ret;  | 
23  |  | 
  | 
24  | 0  |     if (x == NULL)  | 
25  | 0  |         return 0;  | 
26  | 0  |     ret = sk_X509_EXTENSION_num(x);  | 
27  | 0  |     return ret > 0 ? ret : 0;  | 
28  | 0  | }  | 
29  |  |  | 
30  |  | int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,  | 
31  |  |                           int lastpos)  | 
32  | 0  | { | 
33  | 0  |     ASN1_OBJECT *obj;  | 
34  |  | 
  | 
35  | 0  |     obj = OBJ_nid2obj(nid);  | 
36  | 0  |     if (obj == NULL)  | 
37  | 0  |         return -2;  | 
38  | 0  |     return X509v3_get_ext_by_OBJ(x, obj, lastpos);  | 
39  | 0  | }  | 
40  |  |  | 
41  |  | int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk,  | 
42  |  |                           const ASN1_OBJECT *obj, int lastpos)  | 
43  | 0  | { | 
44  | 0  |     int n;  | 
45  | 0  |     X509_EXTENSION *ex;  | 
46  |  | 
  | 
47  | 0  |     if (sk == NULL)  | 
48  | 0  |         return -1;  | 
49  | 0  |     lastpos++;  | 
50  | 0  |     if (lastpos < 0)  | 
51  | 0  |         lastpos = 0;  | 
52  | 0  |     n = sk_X509_EXTENSION_num(sk);  | 
53  | 0  |     for (; lastpos < n; lastpos++) { | 
54  | 0  |         ex = sk_X509_EXTENSION_value(sk, lastpos);  | 
55  | 0  |         if (OBJ_cmp(ex->object, obj) == 0)  | 
56  | 0  |             return lastpos;  | 
57  | 0  |     }  | 
58  | 0  |     return -1;  | 
59  | 0  | }  | 
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, c;  | 
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  |         c = X509_EXTENSION_get_critical(ex);  | 
76  | 0  |         crit = crit != 0;  | 
77  | 0  |         if (c == crit)  | 
78  | 0  |             return lastpos;  | 
79  | 0  |     }  | 
80  | 0  |     return -1;  | 
81  | 0  | }  | 
82  |  |  | 
83  |  | X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)  | 
84  | 0  | { | 
85  | 0  |     if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)  | 
86  | 0  |         return NULL;  | 
87  | 0  |     else  | 
88  | 0  |         return sk_X509_EXTENSION_value(x, loc);  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)  | 
92  | 0  | { | 
93  | 0  |     X509_EXTENSION *ret;  | 
94  |  | 
  | 
95  | 0  |     if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)  | 
96  | 0  |         return NULL;  | 
97  | 0  |     ret = sk_X509_EXTENSION_delete(x, loc);  | 
98  | 0  |     return ret;  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,  | 
102  |  |                                          X509_EXTENSION *ex, int loc)  | 
103  | 0  | { | 
104  | 0  |     X509_EXTENSION *new_ex = NULL;  | 
105  | 0  |     int n;  | 
106  | 0  |     STACK_OF(X509_EXTENSION) *sk = NULL;  | 
107  |  | 
  | 
108  | 0  |     if (x == NULL) { | 
109  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
110  | 0  |         goto err;  | 
111  | 0  |     }  | 
112  |  |  | 
113  | 0  |     if (*x == NULL) { | 
114  | 0  |         if ((sk = sk_X509_EXTENSION_new_null()) == NULL) { | 
115  | 0  |             ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);  | 
116  | 0  |             goto err;  | 
117  | 0  |         }  | 
118  | 0  |     } else  | 
119  | 0  |         sk = *x;  | 
120  |  |  | 
121  | 0  |     n = sk_X509_EXTENSION_num(sk);  | 
122  | 0  |     if (loc > n)  | 
123  | 0  |         loc = n;  | 
124  | 0  |     else if (loc < 0)  | 
125  | 0  |         loc = n;  | 
126  |  | 
  | 
127  | 0  |     if ((new_ex = X509_EXTENSION_dup(ex)) == NULL) { | 
128  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
129  | 0  |         goto err;  | 
130  | 0  |     }  | 
131  | 0  |     if (!sk_X509_EXTENSION_insert(sk, new_ex, loc)) { | 
132  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);  | 
133  | 0  |         goto err;  | 
134  | 0  |     }  | 
135  | 0  |     if (*x == NULL)  | 
136  | 0  |         *x = sk;  | 
137  | 0  |     return sk;  | 
138  | 0  |  err:  | 
139  | 0  |     X509_EXTENSION_free(new_ex);  | 
140  | 0  |     if (x != NULL && *x == NULL)  | 
141  | 0  |         sk_X509_EXTENSION_free(sk);  | 
142  | 0  |     return NULL;  | 
143  | 0  | }  | 
144  |  |  | 
145  |  | /* This returns NULL also in non-error case *target == NULL && sk_X509_EXTENSION_num(exts) <= 0 */  | 
146  |  | STACK_OF(X509_EXTENSION) *X509v3_add_extensions(STACK_OF(X509_EXTENSION) **target,  | 
147  |  |                                                 const STACK_OF(X509_EXTENSION) *exts)  | 
148  | 0  | { | 
149  | 0  |     int i;  | 
150  |  | 
  | 
151  | 0  |     if (target == NULL) { | 
152  | 0  |         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);  | 
153  | 0  |         return NULL;  | 
154  | 0  |     }  | 
155  |  |  | 
156  | 0  |     for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { | 
157  | 0  |         X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);  | 
158  | 0  |         ASN1_OBJECT *obj = X509_EXTENSION_get_object(ext);  | 
159  | 0  |         int idx = X509v3_get_ext_by_OBJ(*target, obj, -1);  | 
160  |  |  | 
161  |  |         /* Does extension exist in target? */  | 
162  | 0  |         if (idx != -1) { | 
163  |  |             /* Delete all extensions of same type */  | 
164  | 0  |             do { | 
165  | 0  |                 X509_EXTENSION_free(sk_X509_EXTENSION_delete(*target, idx));  | 
166  | 0  |                 idx = X509v3_get_ext_by_OBJ(*target, obj, -1);  | 
167  | 0  |             } while (idx != -1);  | 
168  | 0  |         }  | 
169  | 0  |         if (!X509v3_add_ext(target, ext, -1))  | 
170  | 0  |             return NULL;  | 
171  | 0  |     }  | 
172  | 0  |     return *target;  | 
173  | 0  | }  | 
174  |  |  | 
175  |  | X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,  | 
176  |  |                                              int crit,  | 
177  |  |                                              ASN1_OCTET_STRING *data)  | 
178  | 0  | { | 
179  | 0  |     ASN1_OBJECT *obj;  | 
180  | 0  |     X509_EXTENSION *ret;  | 
181  |  | 
  | 
182  | 0  |     obj = OBJ_nid2obj(nid);  | 
183  | 0  |     if (obj == NULL) { | 
184  | 0  |         ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);  | 
185  | 0  |         return NULL;  | 
186  | 0  |     }  | 
187  | 0  |     ret = X509_EXTENSION_create_by_OBJ(ex, obj, crit, data);  | 
188  | 0  |     if (ret == NULL)  | 
189  | 0  |         ASN1_OBJECT_free(obj);  | 
190  | 0  |     return ret;  | 
191  | 0  | }  | 
192  |  |  | 
193  |  | X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,  | 
194  |  |                                              const ASN1_OBJECT *obj, int crit,  | 
195  |  |                                              ASN1_OCTET_STRING *data)  | 
196  | 0  | { | 
197  | 0  |     X509_EXTENSION *ret;  | 
198  |  | 
  | 
199  | 0  |     if ((ex == NULL) || (*ex == NULL)) { | 
200  | 0  |         if ((ret = X509_EXTENSION_new()) == NULL) { | 
201  | 0  |             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);  | 
202  | 0  |             return NULL;  | 
203  | 0  |         }  | 
204  | 0  |     } else  | 
205  | 0  |         ret = *ex;  | 
206  |  |  | 
207  | 0  |     if (!X509_EXTENSION_set_object(ret, obj))  | 
208  | 0  |         goto err;  | 
209  | 0  |     if (!X509_EXTENSION_set_critical(ret, crit))  | 
210  | 0  |         goto err;  | 
211  | 0  |     if (!X509_EXTENSION_set_data(ret, data))  | 
212  | 0  |         goto err;  | 
213  |  |  | 
214  | 0  |     if ((ex != NULL) && (*ex == NULL))  | 
215  | 0  |         *ex = ret;  | 
216  | 0  |     return ret;  | 
217  | 0  |  err:  | 
218  | 0  |     if ((ex == NULL) || (ret != *ex))  | 
219  | 0  |         X509_EXTENSION_free(ret);  | 
220  | 0  |     return NULL;  | 
221  | 0  | }  | 
222  |  |  | 
223  |  | int X509_EXTENSION_set_object(X509_EXTENSION *ex, const ASN1_OBJECT *obj)  | 
224  | 0  | { | 
225  | 0  |     if ((ex == NULL) || (obj == NULL))  | 
226  | 0  |         return 0;  | 
227  | 0  |     ASN1_OBJECT_free(ex->object);  | 
228  | 0  |     ex->object = OBJ_dup(obj);  | 
229  | 0  |     return ex->object != NULL;  | 
230  | 0  | }  | 
231  |  |  | 
232  |  | int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)  | 
233  | 0  | { | 
234  | 0  |     if (ex == NULL)  | 
235  | 0  |         return 0;  | 
236  | 0  |     ex->critical = (crit) ? 0xFF : 0;  | 
237  | 0  |     return 1;  | 
238  | 0  | }  | 
239  |  |  | 
240  |  | int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)  | 
241  | 0  | { | 
242  | 0  |     int i;  | 
243  |  | 
  | 
244  | 0  |     if (ex == NULL)  | 
245  | 0  |         return 0;  | 
246  | 0  |     i = ASN1_OCTET_STRING_set(&ex->value, data->data, data->length);  | 
247  | 0  |     if (!i)  | 
248  | 0  |         return 0;  | 
249  | 0  |     return 1;  | 
250  | 0  | }  | 
251  |  |  | 
252  |  | ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)  | 
253  | 0  | { | 
254  | 0  |     if (ex == NULL)  | 
255  | 0  |         return NULL;  | 
256  | 0  |     return ex->object;  | 
257  | 0  | }  | 
258  |  |  | 
259  |  | ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)  | 
260  | 0  | { | 
261  | 0  |     if (ex == NULL)  | 
262  | 0  |         return NULL;  | 
263  | 0  |     return &ex->value;  | 
264  | 0  | }  | 
265  |  |  | 
266  |  | int X509_EXTENSION_get_critical(const X509_EXTENSION *ex)  | 
267  | 0  | { | 
268  | 0  |     if (ex == NULL)  | 
269  | 0  |         return 0;  | 
270  | 0  |     if (ex->critical > 0)  | 
271  | 0  |         return 1;  | 
272  | 0  |     return 0;  | 
273  | 0  | }  |