/src/openssl/crypto/x509/x_x509a.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 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/evp.h> |
13 | | #include <openssl/asn1t.h> |
14 | | #include <openssl/x509.h> |
15 | | #include "crypto/x509.h" |
16 | | |
17 | | /* |
18 | | * X509_CERT_AUX routines. These are used to encode additional user |
19 | | * modifiable data about a certificate. This data is appended to the X509 |
20 | | * encoding when the *_X509_AUX routines are used. This means that the |
21 | | * "traditional" X509 routines will simply ignore the extra data. |
22 | | */ |
23 | | |
24 | | static X509_CERT_AUX *aux_get(X509 *x); |
25 | | |
26 | | ASN1_SEQUENCE(X509_CERT_AUX) = { |
27 | | ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT), |
28 | | ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0), |
29 | | ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING), |
30 | | ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING), |
31 | | ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1) |
32 | 0 | } ASN1_SEQUENCE_END(X509_CERT_AUX) |
33 | 0 |
|
34 | 0 | IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX) |
35 | 0 |
|
36 | 0 | int X509_trusted(const X509 *x) |
37 | 0 | { |
38 | 0 | return x->aux ? 1 : 0; |
39 | 0 | } |
40 | | |
41 | | static X509_CERT_AUX *aux_get(X509 *x) |
42 | 0 | { |
43 | 0 | if (x == NULL) |
44 | 0 | return NULL; |
45 | 0 | if (x->aux == NULL && (x->aux = X509_CERT_AUX_new()) == NULL) |
46 | 0 | return NULL; |
47 | 0 | return x->aux; |
48 | 0 | } |
49 | | |
50 | | int X509_alias_set1(X509 *x, const unsigned char *name, int len) |
51 | 0 | { |
52 | 0 | X509_CERT_AUX *aux; |
53 | 0 | size_t len_s; |
54 | |
|
55 | 0 | if (!name) { |
56 | 0 | if (!x || !x->aux || !x->aux->alias) |
57 | 0 | return 1; |
58 | 0 | ASN1_UTF8STRING_free(x->aux->alias); |
59 | 0 | x->aux->alias = NULL; |
60 | 0 | return 1; |
61 | 0 | } |
62 | 0 | if ((aux = aux_get(x)) == NULL) |
63 | 0 | return 0; |
64 | | |
65 | 0 | if (len < -1) |
66 | 0 | return 0; |
67 | | |
68 | 0 | if (len == -1) |
69 | 0 | len_s = strlen((const char *)name); |
70 | 0 | else |
71 | 0 | len_s = len; |
72 | |
|
73 | 0 | if (aux->alias == NULL && (aux->alias = ASN1_UTF8STRING_new()) == NULL) |
74 | 0 | return 0; |
75 | 0 | return ASN1_STRING_set_data(aux->alias, name, len_s); |
76 | 0 | } |
77 | | |
78 | | int X509_keyid_set1(X509 *x, const unsigned char *id, int len) |
79 | 0 | { |
80 | 0 | X509_CERT_AUX *aux; |
81 | 0 | size_t len_s; |
82 | |
|
83 | 0 | if (!id) { |
84 | 0 | if (!x || !x->aux || !x->aux->keyid) |
85 | 0 | return 1; |
86 | 0 | ASN1_OCTET_STRING_free(x->aux->keyid); |
87 | 0 | x->aux->keyid = NULL; |
88 | 0 | return 1; |
89 | 0 | } |
90 | 0 | if ((aux = aux_get(x)) == NULL) |
91 | 0 | return 0; |
92 | | |
93 | 0 | if (len < -1) |
94 | 0 | return 0; |
95 | | |
96 | 0 | if (len == -1) |
97 | 0 | len_s = strlen((const char *)id); |
98 | 0 | else |
99 | 0 | len_s = len; |
100 | |
|
101 | 0 | if (aux->keyid == NULL |
102 | 0 | && (aux->keyid = ASN1_OCTET_STRING_new()) == NULL) |
103 | 0 | return 0; |
104 | 0 | return ASN1_STRING_set_data(aux->keyid, id, len_s); |
105 | 0 | } |
106 | | |
107 | | const unsigned char *X509_alias_get0(const X509 *x, int *len) |
108 | 0 | { |
109 | 0 | if (!x->aux || !x->aux->alias) |
110 | 0 | return NULL; |
111 | 0 | if (len) |
112 | 0 | *len = x->aux->alias->length; |
113 | 0 | return x->aux->alias->data; |
114 | 0 | } |
115 | | |
116 | | const unsigned char *X509_keyid_get0(const X509 *x, int *len) |
117 | 0 | { |
118 | 0 | if (!x->aux || !x->aux->keyid) |
119 | 0 | return NULL; |
120 | 0 | if (len) |
121 | 0 | *len = x->aux->keyid->length; |
122 | 0 | return x->aux->keyid->data; |
123 | 0 | } |
124 | | |
125 | | int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj) |
126 | 0 | { |
127 | 0 | X509_CERT_AUX *aux; |
128 | 0 | ASN1_OBJECT *objtmp = NULL; |
129 | 0 | if (obj) { |
130 | 0 | objtmp = OBJ_dup(obj); |
131 | 0 | if (!objtmp) |
132 | 0 | return 0; |
133 | 0 | } |
134 | 0 | if ((aux = aux_get(x)) == NULL) |
135 | 0 | goto err; |
136 | 0 | if (aux->trust == NULL |
137 | 0 | && (aux->trust = sk_ASN1_OBJECT_new_null()) == NULL) |
138 | 0 | goto err; |
139 | 0 | if (!objtmp || sk_ASN1_OBJECT_push(aux->trust, objtmp)) |
140 | 0 | return 1; |
141 | 0 | err: |
142 | 0 | ASN1_OBJECT_free(objtmp); |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | | int X509_add1_reject_object(X509 *x, const ASN1_OBJECT *obj) |
147 | 0 | { |
148 | 0 | X509_CERT_AUX *aux; |
149 | 0 | ASN1_OBJECT *objtmp; |
150 | 0 | int res = 0; |
151 | |
|
152 | 0 | if ((objtmp = OBJ_dup(obj)) == NULL) |
153 | 0 | return 0; |
154 | 0 | if ((aux = aux_get(x)) == NULL) |
155 | 0 | goto err; |
156 | 0 | if (aux->reject == NULL |
157 | 0 | && (aux->reject = sk_ASN1_OBJECT_new_null()) == NULL) |
158 | 0 | goto err; |
159 | 0 | if (sk_ASN1_OBJECT_push(aux->reject, objtmp) > 0) |
160 | 0 | res = 1; |
161 | |
|
162 | 0 | err: |
163 | 0 | if (!res) |
164 | 0 | ASN1_OBJECT_free(objtmp); |
165 | 0 | return res; |
166 | 0 | } |
167 | | |
168 | | void X509_trust_clear(X509 *x) |
169 | 0 | { |
170 | 0 | if (x->aux) { |
171 | 0 | sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free); |
172 | 0 | x->aux->trust = NULL; |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | void X509_reject_clear(X509 *x) |
177 | 0 | { |
178 | 0 | if (x->aux) { |
179 | 0 | sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free); |
180 | 0 | x->aux->reject = NULL; |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | const STACK_OF(ASN1_OBJECT) *X509_get0_trust_objects(const X509 *x) |
185 | 0 | { |
186 | 0 | if (x->aux != NULL) |
187 | 0 | return x->aux->trust; |
188 | 0 | return NULL; |
189 | 0 | } |
190 | | |
191 | | const STACK_OF(ASN1_OBJECT) *X509_get0_reject_objects(const X509 *x) |
192 | 0 | { |
193 | 0 | if (x->aux != NULL) |
194 | 0 | return x->aux->reject; |
195 | 0 | return NULL; |
196 | 0 | } |