]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509/x_x509.c
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/evp.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include "crypto/x509.h"
18 ASN1_SEQUENCE_enc(X509_CINF
, enc
, 0) = {
19 ASN1_EXP_OPT(X509_CINF
, version
, ASN1_INTEGER
, 0),
20 ASN1_EMBED(X509_CINF
, serialNumber
, ASN1_INTEGER
),
21 ASN1_EMBED(X509_CINF
, signature
, X509_ALGOR
),
22 ASN1_SIMPLE(X509_CINF
, issuer
, X509_NAME
),
23 ASN1_EMBED(X509_CINF
, validity
, X509_VAL
),
24 ASN1_SIMPLE(X509_CINF
, subject
, X509_NAME
),
25 ASN1_SIMPLE(X509_CINF
, key
, X509_PUBKEY
),
26 ASN1_IMP_OPT(X509_CINF
, issuerUID
, ASN1_BIT_STRING
, 1),
27 ASN1_IMP_OPT(X509_CINF
, subjectUID
, ASN1_BIT_STRING
, 2),
28 ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF
, extensions
, X509_EXTENSION
, 3)
29 } ASN1_SEQUENCE_END_enc(X509_CINF
, X509_CINF
)
31 IMPLEMENT_ASN1_FUNCTIONS(X509_CINF
)
32 /* X509 top level structure needs a bit of customisation */
34 extern void policy_cache_free(X509_POLICY_CACHE
*cache
);
36 static int x509_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
39 X509
*ret
= (X509
*)*pval
;
44 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509
, ret
, &ret
->ex_data
);
45 X509_CERT_AUX_free(ret
->aux
);
46 ASN1_OCTET_STRING_free(ret
->skid
);
47 AUTHORITY_KEYID_free(ret
->akid
);
48 CRL_DIST_POINTS_free(ret
->crldp
);
49 policy_cache_free(ret
->policy_cache
);
50 GENERAL_NAMES_free(ret
->altname
);
51 NAME_CONSTRAINTS_free(ret
->nc
);
52 #ifndef OPENSSL_NO_RFC3779
53 sk_IPAddressFamily_pop_free(ret
->rfc3779_addr
, IPAddressFamily_free
);
54 ASIdentifiers_free(ret
->rfc3779_asid
);
56 ASN1_OCTET_STRING_free(ret
->distinguishing_id
);
60 case ASN1_OP_NEW_POST
:
67 ret
->ex_pcpathlen
= -1;
70 ret
->policy_cache
= NULL
;
73 #ifndef OPENSSL_NO_RFC3779
74 ret
->rfc3779_addr
= NULL
;
75 ret
->rfc3779_asid
= NULL
;
77 ret
->distinguishing_id
= NULL
;
80 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509
, ret
, &ret
->ex_data
))
84 case ASN1_OP_FREE_POST
:
85 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509
, ret
, &ret
->ex_data
);
86 X509_CERT_AUX_free(ret
->aux
);
87 ASN1_OCTET_STRING_free(ret
->skid
);
88 AUTHORITY_KEYID_free(ret
->akid
);
89 CRL_DIST_POINTS_free(ret
->crldp
);
90 policy_cache_free(ret
->policy_cache
);
91 GENERAL_NAMES_free(ret
->altname
);
92 NAME_CONSTRAINTS_free(ret
->nc
);
93 #ifndef OPENSSL_NO_RFC3779
94 sk_IPAddressFamily_pop_free(ret
->rfc3779_addr
, IPAddressFamily_free
);
95 ASIdentifiers_free(ret
->rfc3779_asid
);
97 ASN1_OCTET_STRING_free(ret
->distinguishing_id
);
106 ASN1_SEQUENCE_ref(X509
, x509_cb
) = {
107 ASN1_EMBED(X509
, cert_info
, X509_CINF
),
108 ASN1_EMBED(X509
, sig_alg
, X509_ALGOR
),
109 ASN1_EMBED(X509
, signature
, ASN1_BIT_STRING
)
110 } ASN1_SEQUENCE_END_ref(X509
, X509
)
112 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(X509
, X509
, X509
)
113 IMPLEMENT_ASN1_DUP_FUNCTION(X509
)
115 X509
*d2i_X509(X509
**a
, const unsigned char **in
, long len
)
119 cert
= (X509
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
, (X509_it()));
120 /* Only cache the extensions if the cert object was passed in */
121 if (cert
!= NULL
&& a
!= NULL
) {
122 if (!x509v3_cache_extensions(cert
))
127 int i2d_X509(const X509
*a
, unsigned char **out
)
129 return ASN1_item_i2d((const ASN1_VALUE
*)a
, out
, (X509_it()));
133 * This should only be used if the X509 object was embedded inside another
134 * asn1 object and it needs a libctx to operate.
135 * Use X509_new_ex() instead if possible.
137 int x509_set0_libctx(X509
*x
, OSSL_LIB_CTX
*libctx
, const char *propq
)
146 X509
*X509_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
)
150 cert
= (X509
*)ASN1_item_new((X509_it()));
151 (void)x509_set0_libctx(cert
, libctx
, propq
);
155 int X509_set_ex_data(X509
*r
, int idx
, void *arg
)
157 return CRYPTO_set_ex_data(&r
->ex_data
, idx
, arg
);
160 void *X509_get_ex_data(const X509
*r
, int idx
)
162 return CRYPTO_get_ex_data(&r
->ex_data
, idx
);
166 * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
167 * extra info tagged on the end. Since these functions set how a certificate
168 * is trusted they should only be used when the certificate comes from a
169 * reliable source such as local storage.
172 X509
*d2i_X509_AUX(X509
**a
, const unsigned char **pp
, long length
)
174 const unsigned char *q
;
178 /* Save start position */
181 if (a
== NULL
|| *a
== NULL
)
183 ret
= d2i_X509(a
, &q
, length
);
184 /* If certificate unreadable then forget it */
189 if (length
> 0 && !d2i_X509_CERT_AUX(&ret
->aux
, &q
, length
))
203 * Serialize trusted certificate to *pp or just return the required buffer
204 * length if pp == NULL. We ultimately want to avoid modifying *pp in the
205 * error path, but that depends on similar hygiene in lower-level functions.
206 * Here we avoid compounding the problem.
208 static int i2d_x509_aux_internal(const X509
*a
, unsigned char **pp
)
211 unsigned char *start
= pp
!= NULL
? *pp
: NULL
;
214 * This might perturb *pp on error, but fixing that belongs in i2d_X509()
215 * not here. It should be that if a == NULL length is zero, but we check
218 length
= i2d_X509(a
, pp
);
219 if (length
<= 0 || a
== NULL
)
222 tmplen
= i2d_X509_CERT_AUX(a
->aux
, pp
);
234 * Serialize trusted certificate to *pp, or just return the required buffer
235 * length if pp == NULL.
237 * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
238 * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
239 * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
242 int i2d_X509_AUX(const X509
*a
, unsigned char **pp
)
247 /* Buffer provided by caller */
248 if (pp
== NULL
|| *pp
!= NULL
)
249 return i2d_x509_aux_internal(a
, pp
);
251 /* Obtain the combined length */
252 if ((length
= i2d_x509_aux_internal(a
, NULL
)) <= 0)
255 /* Allocate requisite combined storage */
256 *pp
= tmp
= OPENSSL_malloc(length
);
258 X509err(X509_F_I2D_X509_AUX
, ERR_R_MALLOC_FAILURE
);
262 /* Encode, but keep *pp at the originally malloced pointer */
263 length
= i2d_x509_aux_internal(a
, &tmp
);
271 int i2d_re_X509_tbs(X509
*x
, unsigned char **pp
)
273 x
->cert_info
.enc
.modified
= 1;
274 return i2d_X509_CINF(&x
->cert_info
, pp
);
277 void X509_get0_signature(const ASN1_BIT_STRING
**psig
,
278 const X509_ALGOR
**palg
, const X509
*x
)
281 *psig
= &x
->signature
;
286 int X509_get_signature_nid(const X509
*x
)
288 return OBJ_obj2nid(x
->sig_alg
.algorithm
);
291 void X509_set0_distinguishing_id(X509
*x
, ASN1_OCTET_STRING
*d_id
)
293 ASN1_OCTET_STRING_free(x
->distinguishing_id
);
294 x
->distinguishing_id
= d_id
;
297 ASN1_OCTET_STRING
*X509_get0_distinguishing_id(X509
*x
)
299 return x
->distinguishing_id
;