xref: /rk3399_ARM-atf/tools/cert_create/include/ext.h (revision 3105f7ba9a3a9f6f0e78761e8bdd4da621254730)
1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef EXT_H_
32 #define EXT_H_
33 
34 #include "key.h"
35 #include <openssl/x509v3.h>
36 
37 /* Extension types supported */
38 enum {
39 	EXT_TYPE_NVCOUNTER,
40 	EXT_TYPE_PKEY,
41 	EXT_TYPE_HASH
42 };
43 
44 /*
45  * This structure contains the relevant information to create the extensions
46  * to be included in the certificates. This extensions will be used to
47  * establish the chain of trust.
48  */
49 typedef struct ext_s {
50 	const char *oid;	/* OID of the extension */
51 	const char *sn;		/* Short name */
52 	const char *ln;		/* Long description */
53 	const char *help_msg;	/* Help message */
54 	int asn1_type;		/* OpenSSL ASN1 type of the extension data.
55 				 * Supported types are:
56 				 *   - V_ASN1_INTEGER
57 				 *   - V_ASN1_OCTET_STRING
58 				 */
59 	int type;
60 	const char *opt;	/* Command line option to specify data */
61 	/* Extension data (depends on extension type) */
62 	union {
63 		const char *fn;	/* File with extension data */
64 		int nvcounter;	/* Non volatile counter */
65 		int key;	/* Public key */
66 	} data;
67 
68 	int alias;		/* In case OpenSSL provides an standard
69 				 * extension of the same type, add the new
70 				 * extension as an alias of this one
71 				 */
72 
73 	X509V3_EXT_METHOD method; /* This field may be used to define a custom
74 				   * function to print the contents of the
75 				   * extension */
76 
77 	int optional;	/* This field may be used optionally to exclude an image */
78 } ext_t;
79 
80 enum {
81 	EXT_NON_CRIT = 0,
82 	EXT_CRIT = !EXT_NON_CRIT,
83 };
84 
85 /* Exported API */
86 int ext_init(void);
87 ext_t *ext_get_by_opt(const char *opt);
88 X509_EXTENSION *ext_new_hash(int nid, int crit, const EVP_MD *md,
89 		unsigned char *buf, size_t len);
90 X509_EXTENSION *ext_new_nvcounter(int nid, int crit, int value);
91 X509_EXTENSION *ext_new_key(int nid, int crit, EVP_PKEY *k);
92 
93 /* Macro to register the extensions used in the CoT */
94 #define REGISTER_EXTENSIONS(_ext) \
95 	ext_t *extensions = &_ext[0]; \
96 	const unsigned int num_extensions = sizeof(_ext)/sizeof(_ext[0])
97 
98 /* Exported variables */
99 extern ext_t *extensions;
100 extern const unsigned int num_extensions;
101 
102 #endif /* EXT_H_ */
103