Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x509_set.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5 #include <openssl/cipher.h>
6 #include <openssl/evp.h>
7 #include <openssl/obj.h>
8 #include <openssl/x509.h>
9
10 #include "internal.h"
11 #include "openssl/x509v3.h"
12
13
14 long X509_get_version(const X509 *x509) {
15 // The default version is v1(0).
16 if (x509->cert_info->version == NULL) {
17 return X509_VERSION_1;
18 }
19 return ASN1_INTEGER_get(x509->cert_info->version);
20 }
21
22 int X509_set_version(X509 *x, long version) {
23 if (x == NULL) {
24 return 0;
25 }
26
27 if (version < X509_VERSION_1 || version > X509_VERSION_3) {
28 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
29 return 0;
30 }
31
32 // v1(0) is default and is represented by omitting the version.
33 if (version == X509_VERSION_1) {
34 ASN1_INTEGER_free(x->cert_info->version);
35 x->cert_info->version = NULL;
36 return 1;
37 }
38
39 if (x->cert_info->version == NULL) {
40 x->cert_info->version = ASN1_INTEGER_new();
41 if (x->cert_info->version == NULL) {
42 return 0;
43 }
44 }
45 return ASN1_INTEGER_set_int64(x->cert_info->version, version);
46 }
47
48 int X509_set_serialNumber(X509 *x, const ASN1_INTEGER *serial) {
49 if (serial->type != V_ASN1_INTEGER && serial->type != V_ASN1_NEG_INTEGER) {
50 OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE);
51 return 0;
52 }
53
54 ASN1_INTEGER *in;
55 if (x == NULL) {
56 return 0;
57 }
58 in = x->cert_info->serialNumber;
59 if (in != serial) {
60 in = ASN1_INTEGER_dup(serial);
61 if (in != NULL) {
62 ASN1_INTEGER_free(x->cert_info->serialNumber);
63 x->cert_info->serialNumber = in;
64 }
65 }
66 return in != NULL;
67 }
68
69 int X509_set_issuer_name(X509 *x, X509_NAME *name) {
70 if ((x == NULL) || (x->cert_info == NULL)) {
71 return 0;
72 }
73 return (X509_NAME_set(&x->cert_info->issuer, name));
74 }
75
76 int X509_set_subject_name(X509 *x, X509_NAME *name) {
77 if ((x == NULL) || (x->cert_info == NULL)) {
78 return 0;
79 }
80 return (X509_NAME_set(&x->cert_info->subject, name));
81 }
82
83 int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm) {
84 ASN1_TIME *in;
85
86 if ((x == NULL) || (x->cert_info->validity == NULL)) {
87 return 0;
88 }
89 in = x->cert_info->validity->notBefore;
90 if (in != tm) {
91 in = ASN1_STRING_dup(tm);
92 if (in != NULL) {
93 ASN1_TIME_free(x->cert_info->validity->notBefore);
94 x->cert_info->validity->notBefore = in;
95 }
96 }
97 return in != NULL;
98 }
99
100 int X509_set_notBefore(X509 *x, const ASN1_TIME *tm) {
101 return X509_set1_notBefore(x, tm);
102 }
103
104 const ASN1_TIME *X509_get0_notBefore(const X509 *x) {
105 return x->cert_info->validity->notBefore;
106 }
107
108 ASN1_TIME *X509_getm_notBefore(X509 *x) {
109 // Note this function takes a const |X509| pointer in OpenSSL. We require
110 // non-const as this allows mutating |x|. If it comes up for compatibility,
111 // we can relax this.
112 return x->cert_info->validity->notBefore;
113 }
114
115 ASN1_TIME *X509_get_notBefore(const X509 *x509) {
116 // In OpenSSL, this function is an alias for |X509_getm_notBefore|, but our
117 // |X509_getm_notBefore| is const-correct. |X509_get_notBefore| was
118 // originally a macro, so it needs to capture both get0 and getm use cases.
119 return x509->cert_info->validity->notBefore;
120 }
121
122 int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm) {
123 ASN1_TIME *in;
124
125 if ((x == NULL) || (x->cert_info->validity == NULL)) {
126 return 0;
127 }
128 in = x->cert_info->validity->notAfter;
129 if (in != tm) {
130 in = ASN1_STRING_dup(tm);
131 if (in != NULL) {
132 ASN1_TIME_free(x->cert_info->validity->notAfter);
133 x->cert_info->validity->notAfter = in;
134 }
135 }
136 return in != NULL;
137 }
138
139 int X509_set_notAfter(X509 *x, const ASN1_TIME *tm) {
140 return X509_set1_notAfter(x, tm);
141 }
142
143 const ASN1_TIME *X509_get0_notAfter(const X509 *x) {
144 return x->cert_info->validity->notAfter;
145 }
146
147 ASN1_TIME *X509_getm_notAfter(X509 *x) {
148 // Note this function takes a const |X509| pointer in OpenSSL. We require
149 // non-const as this allows mutating |x|. If it comes up for compatibility,
150 // we can relax this.
151 return x->cert_info->validity->notAfter;
152 }
153
154 ASN1_TIME *X509_get_notAfter(const X509 *x509) {
155 // In OpenSSL, this function is an alias for |X509_getm_notAfter|, but our
156 // |X509_getm_notAfter| is const-correct. |X509_get_notAfter| was
157 // originally a macro, so it needs to capture both get0 and getm use cases.
158 return x509->cert_info->validity->notAfter;
159 }
160
161 void X509_get0_uids(const X509 *x509, const ASN1_BIT_STRING **out_issuer_uid,
162 const ASN1_BIT_STRING **out_subject_uid) {
163 if (out_issuer_uid != NULL) {
164 *out_issuer_uid = x509->cert_info->issuerUID;
165 }
166 if (out_subject_uid != NULL) {
167 *out_subject_uid = x509->cert_info->subjectUID;
168 }
169 }
170
171 int X509_set_pubkey(X509 *x, EVP_PKEY *pkey) {
172 if ((x == NULL) || (x->cert_info == NULL)) {
173 return 0;
174 }
175 return (X509_PUBKEY_set(&(x->cert_info->key), pkey));
176 }
177
178 const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x) {
179 return x->cert_info->extensions;
180 }
181
182 const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x) {
183 return x->cert_info->signature;
184 }
185
186 X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x509) {
187 return x509->cert_info->key;
188 }
189
190 static int X509_SIG_INFO_get(const X509_SIG_INFO *sig_info, int *digest_nid,
191 int *pubkey_nid, int *sec_bits, uint32_t *flags) {
192 if (sig_info == NULL) {
193 OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
194 return 0;
195 }
196
197 if (digest_nid != NULL) {
198 *digest_nid = sig_info->digest_nid;
199 }
200 if (pubkey_nid != NULL) {
201 *pubkey_nid = sig_info->pubkey_nid;
202 }
203 if (sec_bits != NULL) {
204 *sec_bits = sig_info->sec_bits;
205 }
206 if (flags != NULL) {
207 *flags = sig_info->flags;
208 }
209 return (sig_info->flags & X509_SIG_INFO_VALID) != 0;
210 }
211
212 int X509_get_signature_info(X509 *x509, int *digest_nid, int *pubkey_nid,
213 int *sec_bits, uint32_t *flags) {
214 if (x509 == NULL) {
215 OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
216 return 0;
217 }
218
219 // The return value of |x509v3_cache_extensions| is not checked because
220 // |X509_get_signature_info|'s function contract does not encapsulate failures
221 // if any invalid extensions do exist.
222 x509v3_cache_extensions(x509);
223 return X509_SIG_INFO_get(&x509->sig_info, digest_nid, pubkey_nid, sec_bits,
224 flags);
225 }
226
227 int x509_init_signature_info(X509 *x509) {
228 int pubkey_nid, digest_nid;
229 const EVP_MD *md;
230
231 x509->sig_info.digest_nid = NID_undef;
232 x509->sig_info.pubkey_nid = NID_undef;
233 x509->sig_info.sec_bits = -1;
234 x509->sig_info.flags = 0;
235 if (!OBJ_find_sigid_algs(OBJ_obj2nid(x509->sig_alg->algorithm), &digest_nid,
236 &pubkey_nid) ||
237 pubkey_nid == NID_undef) {
238 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_SIGID_ALGS);
239 return 0;
240 }
241 x509->sig_info.pubkey_nid = pubkey_nid;
242 x509->sig_info.digest_nid = digest_nid;
243 x509->sig_info.flags |= X509_SIG_INFO_VALID;
244
245 md = EVP_get_digestbynid(digest_nid);
246 if (md == NULL) {
247 // Some valid signature algorithms have an undefined digest. See
248 // crypto/obj/obj_xref.c.
249 return 1;
250 }
251 // Security bits: half number of bits in digest.
252 x509->sig_info.sec_bits = (int)EVP_MD_size(md) * 4;
253
254 switch (digest_nid) {
255 case NID_sha1:
256 case NID_sha256:
257 case NID_sha384:
258 case NID_sha512:
259 x509->sig_info.flags |= X509_SIG_INFO_TLS;
260 }
261 return 1;
262 }