1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* X.509 certificate parser internal definitions
3  *
4  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #ifndef _X509_PARSER_H
9 #define _X509_PARSER_H
10 
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)
15 #include <image.h>
16 #include <mbedtls/error.h>
17 #include <mbedtls/asn1.h>
18 #endif
19 
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 */
27 };
28 #endif
29 
30 /*
31  * MbedTLS integration Notes:
32  *
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.
38  */
39 struct x509_certificate {
40 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
41 	struct x509_cert_mbedtls_ctx *mbedtls_ctx;
42 #endif
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) */
51 	time64_t	valid_from;
52 	time64_t	valid_to;
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 */
65 	unsigned	index;
66 	bool		seen;			/* Infinite recursion prevention */
67 	bool		verified;
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 */
71 	bool		blacklisted;
72 };
73 
74 /*
75  * x509_cert_parser.c
76  */
77 extern void x509_free_certificate(struct x509_certificate *cert);
78 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
79 /**
80  * x509_populate_pubkey() - Populate public key from MbedTLS context
81  *
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
85  */
86 int x509_populate_pubkey(mbedtls_x509_crt *cert, struct public_key **pub_key);
87 /**
88  * x509_populate_cert() - Populate X509 cert from MbedTLS context
89  *
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
93  */
94 int x509_populate_cert(mbedtls_x509_crt *mbedtls_cert,
95 		       struct x509_certificate **pcert);
96 /**
97  * x509_get_timestamp() - Translate timestamp from MbedTLS context
98  *
99  * @x509_time:	Pointer to MbedTLS time
100  * Return: Time in time64_t format
101  */
102 time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time);
103 #endif
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,
106 			    unsigned char tag,
107 			    const unsigned char *value, size_t vlen);
108 
109 /*
110  * x509_public_key.c
111  */
112 #if !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
113 extern int x509_get_sig_params(struct x509_certificate *cert);
114 #endif
115 extern int x509_check_for_self_signed(struct x509_certificate *cert);
116 #endif /* _X509_PARSER_H */
117