1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* X.509 certificate parser internal definitions
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
11 #include <linux/time.h>
12 #include <crypto/public_key.h>
13 #include <keys/asymmetric-type.h>
14 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
16 #include <mbedtls/error.h>
17 #include <mbedtls/asn1.h>
20 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
21 struct x509_cert_mbedtls_ctx {
22 void *tbs; /* Signed data */
23 void *raw_serial; /* Raw serial number in ASN.1 */
24 void *raw_issuer; /* Raw issuer name in ASN.1 */
25 void *raw_subject; /* Raw subject name in ASN.1 */
26 void *raw_skid; /* Raw subjectKeyId in ASN.1 */
31 * MbedTLS integration Notes:
33 * Fields we don't need to populate from MbedTLS context:
34 * 'raw_sig' and 'raw_sig_size' are buffer for x509_parse_context,
35 * not needed for MbedTLS.
36 * 'signer' and 'seen' are used internally by pkcs7_verify.
37 * 'verified' is not in use.
39 struct x509_certificate {
40 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
41 struct x509_cert_mbedtls_ctx *mbedtls_ctx;
43 struct x509_certificate *next;
44 struct x509_certificate *signer; /* Certificate that signed this one */
45 struct public_key *pub; /* Public key details */
46 struct public_key_signature *sig; /* Signature parameters */
47 char *issuer; /* Name of certificate issuer */
48 char *subject; /* Name of certificate subject */
49 struct asymmetric_key_id *id; /* Issuer + Serial number */
50 struct asymmetric_key_id *skid; /* Subject + subjectKeyId (optional) */
53 const void *tbs; /* Signed data */
54 unsigned tbs_size; /* Size of signed data */
55 unsigned raw_sig_size; /* Size of sigature */
56 const void *raw_sig; /* Signature data */
57 const void *raw_serial; /* Raw serial number in ASN.1 */
58 unsigned raw_serial_size;
59 unsigned raw_issuer_size;
60 const void *raw_issuer; /* Raw issuer name in ASN.1 */
61 const void *raw_subject; /* Raw subject name in ASN.1 */
62 unsigned raw_subject_size;
63 unsigned raw_skid_size;
64 const void *raw_skid; /* Raw subjectKeyId in ASN.1 */
66 bool seen; /* Infinite recursion prevention */
68 bool self_signed; /* T if self-signed (check unsupported_sig too) */
69 bool unsupported_key; /* T if key uses unsupported crypto */
70 bool unsupported_sig; /* T if signature uses unsupported crypto */
77 extern void x509_free_certificate(struct x509_certificate *cert);
78 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
80 * x509_populate_pubkey() - Populate public key from MbedTLS context
82 * @cert: Pointer to MbedTLS X509 cert
83 * @pub_key: Pointer to the populated public key handle
84 * Return: 0 on succcess, error code on failure
86 int x509_populate_pubkey(mbedtls_x509_crt *cert, struct public_key **pub_key);
88 * x509_populate_cert() - Populate X509 cert from MbedTLS context
90 * @mbedtls_cert: Pointer to MbedTLS X509 cert
91 * @pcert: Pointer to the populated X509 cert handle
92 * Return: 0 on succcess, error code on failure
94 int x509_populate_cert(mbedtls_x509_crt *mbedtls_cert,
95 struct x509_certificate **pcert);
97 * x509_get_timestamp() - Translate timestamp from MbedTLS context
99 * @x509_time: Pointer to MbedTLS time
100 * Return: Time in time64_t format
102 time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time);
104 extern struct x509_certificate *x509_cert_parse(const void *data, size_t datalen);
105 extern int x509_decode_time(time64_t *_t, size_t hdrlen,
107 const unsigned char *value, size_t vlen);
112 #if !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
113 extern int x509_get_sig_params(struct x509_certificate *cert);
115 extern int x509_check_for_self_signed(struct x509_certificate *cert);
116 #endif /* _X509_PARSER_H */