/src/openssl/crypto/x509/x509_set.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "internal/refcount.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 "internal/asn1_int.h" |
19 | | #include "internal/x509_int.h" |
20 | | #include "x509_lcl.h" |
21 | | |
22 | | int X509_set_version(X509 *x, long version) |
23 | 0 | { |
24 | 0 | if (x == NULL) |
25 | 0 | return 0; |
26 | 0 | if (version == 0) { |
27 | 0 | ASN1_INTEGER_free(x->cert_info.version); |
28 | 0 | x->cert_info.version = NULL; |
29 | 0 | return 1; |
30 | 0 | } |
31 | 0 | if (x->cert_info.version == NULL) { |
32 | 0 | if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL) |
33 | 0 | return 0; |
34 | 0 | } |
35 | 0 | return ASN1_INTEGER_set(x->cert_info.version, version); |
36 | 0 | } |
37 | | |
38 | | int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial) |
39 | 0 | { |
40 | 0 | ASN1_INTEGER *in; |
41 | 0 |
|
42 | 0 | if (x == NULL) |
43 | 0 | return 0; |
44 | 0 | in = &x->cert_info.serialNumber; |
45 | 0 | if (in != serial) |
46 | 0 | return ASN1_STRING_copy(in, serial); |
47 | 0 | return 1; |
48 | 0 | } |
49 | | |
50 | | int X509_set_issuer_name(X509 *x, X509_NAME *name) |
51 | 0 | { |
52 | 0 | if (x == NULL) |
53 | 0 | return 0; |
54 | 0 | return X509_NAME_set(&x->cert_info.issuer, name); |
55 | 0 | } |
56 | | |
57 | | int X509_set_subject_name(X509 *x, X509_NAME *name) |
58 | 0 | { |
59 | 0 | if (x == NULL) |
60 | 0 | return 0; |
61 | 0 | return X509_NAME_set(&x->cert_info.subject, name); |
62 | 0 | } |
63 | | |
64 | | int x509_set1_time(ASN1_TIME **ptm, const ASN1_TIME *tm) |
65 | 0 | { |
66 | 0 | ASN1_TIME *in; |
67 | 0 | in = *ptm; |
68 | 0 | if (in != tm) { |
69 | 0 | in = ASN1_STRING_dup(tm); |
70 | 0 | if (in != NULL) { |
71 | 0 | ASN1_TIME_free(*ptm); |
72 | 0 | *ptm = in; |
73 | 0 | } |
74 | 0 | } |
75 | 0 | return (in != NULL); |
76 | 0 | } |
77 | | |
78 | | int X509_set1_notBefore(X509 *x, const ASN1_TIME *tm) |
79 | 0 | { |
80 | 0 | if (x == NULL) |
81 | 0 | return 0; |
82 | 0 | return x509_set1_time(&x->cert_info.validity.notBefore, tm); |
83 | 0 | } |
84 | | |
85 | | int X509_set1_notAfter(X509 *x, const ASN1_TIME *tm) |
86 | 0 | { |
87 | 0 | if (x == NULL) |
88 | 0 | return 0; |
89 | 0 | return x509_set1_time(&x->cert_info.validity.notAfter, tm); |
90 | 0 | } |
91 | | |
92 | | int X509_set_pubkey(X509 *x, EVP_PKEY *pkey) |
93 | 0 | { |
94 | 0 | if (x == NULL) |
95 | 0 | return 0; |
96 | 0 | return X509_PUBKEY_set(&(x->cert_info.key), pkey); |
97 | 0 | } |
98 | | |
99 | | int X509_up_ref(X509 *x) |
100 | 0 | { |
101 | 0 | int i; |
102 | 0 |
|
103 | 0 | if (CRYPTO_UP_REF(&x->references, &i, x->lock) <= 0) |
104 | 0 | return 0; |
105 | 0 | |
106 | 0 | REF_PRINT_COUNT("X509", x); |
107 | 0 | REF_ASSERT_ISNT(i < 2); |
108 | 0 | return ((i > 1) ? 1 : 0); |
109 | 0 | } |
110 | | |
111 | | long X509_get_version(const X509 *x) |
112 | 0 | { |
113 | 0 | return ASN1_INTEGER_get(x->cert_info.version); |
114 | 0 | } |
115 | | |
116 | | const ASN1_TIME *X509_get0_notBefore(const X509 *x) |
117 | 0 | { |
118 | 0 | return x->cert_info.validity.notBefore; |
119 | 0 | } |
120 | | |
121 | | const ASN1_TIME *X509_get0_notAfter(const X509 *x) |
122 | 0 | { |
123 | 0 | return x->cert_info.validity.notAfter; |
124 | 0 | } |
125 | | |
126 | | ASN1_TIME *X509_getm_notBefore(const X509 *x) |
127 | 0 | { |
128 | 0 | return x->cert_info.validity.notBefore; |
129 | 0 | } |
130 | | |
131 | | ASN1_TIME *X509_getm_notAfter(const X509 *x) |
132 | 0 | { |
133 | 0 | return x->cert_info.validity.notAfter; |
134 | 0 | } |
135 | | |
136 | | int X509_get_signature_type(const X509 *x) |
137 | 0 | { |
138 | 0 | return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm)); |
139 | 0 | } |
140 | | |
141 | | X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x) |
142 | 0 | { |
143 | 0 | return x->cert_info.key; |
144 | 0 | } |
145 | | |
146 | | const STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x) |
147 | 0 | { |
148 | 0 | return x->cert_info.extensions; |
149 | 0 | } |
150 | | |
151 | | void X509_get0_uids(const X509 *x, const ASN1_BIT_STRING **piuid, |
152 | | const ASN1_BIT_STRING **psuid) |
153 | 0 | { |
154 | 0 | if (piuid != NULL) |
155 | 0 | *piuid = x->cert_info.issuerUID; |
156 | 0 | if (psuid != NULL) |
157 | 0 | *psuid = x->cert_info.subjectUID; |
158 | 0 | } |
159 | | |
160 | | const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x) |
161 | 0 | { |
162 | 0 | return &x->cert_info.signature; |
163 | 0 | } |
164 | | |
165 | | int X509_SIG_INFO_get(const X509_SIG_INFO *siginf, int *mdnid, int *pknid, |
166 | | int *secbits, uint32_t *flags) |
167 | 0 | { |
168 | 0 | if (mdnid != NULL) |
169 | 0 | *mdnid = siginf->mdnid; |
170 | 0 | if (pknid != NULL) |
171 | 0 | *pknid = siginf->pknid; |
172 | 0 | if (secbits != NULL) |
173 | 0 | *secbits = siginf->secbits; |
174 | 0 | if (flags != NULL) |
175 | 0 | *flags = siginf->flags; |
176 | 0 | return (siginf->flags & X509_SIG_INFO_VALID) != 0; |
177 | 0 | } |
178 | | |
179 | | void X509_SIG_INFO_set(X509_SIG_INFO *siginf, int mdnid, int pknid, |
180 | | int secbits, uint32_t flags) |
181 | 0 | { |
182 | 0 | siginf->mdnid = mdnid; |
183 | 0 | siginf->pknid = pknid; |
184 | 0 | siginf->secbits = secbits; |
185 | 0 | siginf->flags = flags; |
186 | 0 | } |
187 | | |
188 | | int X509_get_signature_info(X509 *x, int *mdnid, int *pknid, int *secbits, |
189 | | uint32_t *flags) |
190 | 0 | { |
191 | 0 | X509_check_purpose(x, -1, -1); |
192 | 0 | return X509_SIG_INFO_get(&x->siginf, mdnid, pknid, secbits, flags); |
193 | 0 | } |
194 | | |
195 | | static void x509_sig_info_init(X509_SIG_INFO *siginf, const X509_ALGOR *alg, |
196 | | const ASN1_STRING *sig) |
197 | 0 | { |
198 | 0 | int pknid, mdnid; |
199 | 0 | const EVP_MD *md; |
200 | 0 |
|
201 | 0 | siginf->mdnid = NID_undef; |
202 | 0 | siginf->pknid = NID_undef; |
203 | 0 | siginf->secbits = -1; |
204 | 0 | siginf->flags = 0; |
205 | 0 | if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid) |
206 | 0 | || pknid == NID_undef) |
207 | 0 | return; |
208 | 0 | siginf->pknid = pknid; |
209 | 0 | if (mdnid == NID_undef) { |
210 | 0 | /* If we have one, use a custom handler for this algorithm */ |
211 | 0 | const EVP_PKEY_ASN1_METHOD *ameth = EVP_PKEY_asn1_find(NULL, pknid); |
212 | 0 | if (ameth == NULL || ameth->siginf_set == NULL |
213 | 0 | || ameth->siginf_set(siginf, alg, sig) == 0) |
214 | 0 | return; |
215 | 0 | siginf->flags |= X509_SIG_INFO_VALID; |
216 | 0 | return; |
217 | 0 | } |
218 | 0 | siginf->flags |= X509_SIG_INFO_VALID; |
219 | 0 | siginf->mdnid = mdnid; |
220 | 0 | md = EVP_get_digestbynid(mdnid); |
221 | 0 | if (md == NULL) |
222 | 0 | return; |
223 | 0 | /* Security bits: half number of bits in digest */ |
224 | 0 | siginf->secbits = EVP_MD_size(md) * 4; |
225 | 0 | switch (mdnid) { |
226 | 0 | case NID_sha1: |
227 | 0 | case NID_sha256: |
228 | 0 | case NID_sha384: |
229 | 0 | case NID_sha512: |
230 | 0 | siginf->flags |= X509_SIG_INFO_TLS; |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | void x509_init_sig_info(X509 *x) |
235 | 0 | { |
236 | 0 | x509_sig_info_init(&x->siginf, &x->sig_alg, &x->signature); |
237 | 0 | } |