1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_CRYPTO_ELFSIGN_H
28 #define	_SYS_CRYPTO_ELFSIGN_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /*
37  * Consolidation Private Interface for elfsign/libpkcs11/kcfd
38  */
39 
40 #include <sys/types.h>
41 #include <sys/param.h>
42 
43 /*
44  * Project Private structures and types used for communication between kcfd
45  * and KCF over the door.
46  */
47 
48 typedef enum ELFsign_status_e {
49 	ELFSIGN_UNKNOWN,
50 	ELFSIGN_SUCCESS,
51 	ELFSIGN_FAILED,
52 	ELFSIGN_NOTSIGNED,
53 	ELFSIGN_INVALID_CERTPATH,
54 	ELFSIGN_INVALID_ELFOBJ,
55 	ELFSIGN_RESTRICTED
56 } ELFsign_status_t;
57 
58 #define	KCF_KCFD_VERSION1	1
59 #define	SIG_MAX_LENGTH		1024
60 
61 #define	ELF_SIGNATURE_SECTION	".SUNW_signature"
62 #define	ELFSIGN_CRYPTO		"Solaris Cryptographic Framework"
63 #define	USAGELIMITED		"OU=UsageLimited"
64 #define	ESA			".esa"
65 #define	ESA_LEN			sizeof (".esa")
66 
67 typedef struct kcf_door_arg_s {
68 	short		da_version;
69 	boolean_t	da_iskernel;
70 
71 	union {
72 		char filename[MAXPATHLEN];	/* For request */
73 
74 		struct kcf_door_result_s {	/* For response */
75 			ELFsign_status_t	status;
76 			uint32_t		siglen;
77 			uchar_t			signature[1];
78 		} result;
79 	} da_u;
80 } kcf_door_arg_t;
81 
82 typedef uint32_t	filesig_vers_t;
83 
84 /*
85  * File Signature Structure
86  *	Applicable to ELF and other file formats
87  */
88 struct filesignatures {
89 	uint32_t	filesig_cnt;	/* count of signatures */
90 	uint32_t	filesig_pad;	/* unused */
91 	union {
92 		char	filesig_data[1];
93 		struct filesig {	/* one of these for each signature */
94 			uint32_t	filesig_size;
95 			filesig_vers_t	filesig_version;
96 			union {
97 				struct filesig_version1 {
98 					uint32_t	filesig_v1_dnsize;
99 					uint32_t	filesig_v1_sigsize;
100 					uint32_t	filesig_v1_oidsize;
101 					char	filesig_v1_data[1];
102 				} filesig_v1;
103 				struct filesig_version3 {
104 					uint64_t	filesig_v3_time;
105 					uint32_t	filesig_v3_dnsize;
106 					uint32_t	filesig_v3_sigsize;
107 					uint32_t	filesig_v3_oidsize;
108 					char	filesig_v3_data[1];
109 				} filesig_v3;
110 			} _u2;
111 		} filesig_sig;
112 		uint64_t filesig_align;
113 	} _u1;
114 };
115 #define	filesig_sig		_u1.filesig_sig
116 
117 #define	filesig_v1_dnsize	_u2.filesig_v1.filesig_v1_dnsize
118 #define	filesig_v1_sigsize	_u2.filesig_v1.filesig_v1_sigsize
119 #define	filesig_v1_oidsize	_u2.filesig_v1.filesig_v1_oidsize
120 #define	filesig_v1_data		_u2.filesig_v1.filesig_v1_data
121 
122 #define	filesig_v3_time		_u2.filesig_v3.filesig_v3_time
123 #define	filesig_v3_dnsize	_u2.filesig_v3.filesig_v3_dnsize
124 #define	filesig_v3_sigsize	_u2.filesig_v3.filesig_v3_sigsize
125 #define	filesig_v3_oidsize	_u2.filesig_v3.filesig_v3_oidsize
126 #define	filesig_v3_data		_u2.filesig_v3.filesig_v3_data
127 
128 #define	filesig_ALIGN(s)	(((s) + sizeof (uint64_t) - 1) & \
129 				    (-sizeof (uint64_t)))
130 #define	filesig_next(ptr)	(struct filesig *)((void *)((char *)(ptr) + \
131 				    filesig_ALIGN((ptr)->filesig_size)))
132 
133 #define	FILESIG_UNKNOWN		0	/* unrecognized version */
134 #define	FILESIG_VERSION1	1	/* version1, all but sig section */
135 #define	FILESIG_VERSION2	2	/* version1 format, SHF_ALLOC only */
136 #define	FILESIG_VERSION3	3	/* version3, all but sig section */
137 #define	FILESIG_VERSION4	4	/* version3 format, SHF_ALLOC only */
138 
139 #ifndef	_KERNEL
140 
141 #define	_PATH_KCFD_DOOR	"/var/run/kcfd_door"
142 
143 #define	ES_FMT_RSA_MD5_SHA1	"rsa_md5_sha1"
144 #define	ES_FMT_RSA_SHA1		"rsa_sha1"
145 enum ES_ACTION {
146 	ES_GET,
147 	ES_GET_CRYPTO,
148 	ES_UPDATE_RSA_MD5_SHA1,
149 	ES_UPDATE_RSA_SHA1
150 };
151 #define	ES_ACTISUPDATE(a)	((a) >= ES_UPDATE_RSA_MD5_SHA1)
152 
153 /*
154  * ELF signature handling
155  */
156 typedef struct ELFsign_s *ELFsign_t;
157 struct ELFsign_sig_info {
158 	char	*esi_format;
159 	char	*esi_signer;
160 	time_t	esi_time;
161 };
162 
163 extern struct filesignatures *elfsign_insert_dso(ELFsign_t ess,
164     struct filesignatures *fsp, const char *dn, int dn_len,
165     const uchar_t *sig, int sig_len, const char *oid, int oid_len);
166 extern filesig_vers_t elfsign_extract_sig(ELFsign_t ess,
167     struct filesignatures *fsp, uchar_t *sig, size_t *sig_len);
168 extern ELFsign_status_t elfsign_begin(const char *filename,
169     const char *certpath, enum ES_ACTION action, ELFsign_t *essp);
170 extern void elfsign_end(ELFsign_t ess);
171 extern ELFsign_status_t elfsign_verify_signature(ELFsign_t ess,
172     struct ELFsign_sig_info **esipp);
173 extern ELFsign_status_t elfsign_hash(ELFsign_t ess, uchar_t *hash,
174     size_t *hash_len);
175 extern ELFsign_status_t elfsign_hash_mem_resident(ELFsign_t ess,
176     uchar_t *hash, size_t *hash_len);
177 
178 extern ELFsign_status_t elfsign_signatures(ELFsign_t ess,
179     struct filesignatures **fspp, size_t *fs_len, enum ES_ACTION action);
180 
181 extern char const *elfsign_strerror(ELFsign_status_t);
182 extern boolean_t elfsign_sig_info(struct filesignatures *fssp,
183     struct ELFsign_sig_info **esipp);
184 extern void elfsign_sig_info_free(struct ELFsign_sig_info *);
185 
186 
187 /*
188  * ELF "Certificate Library"
189  */
190 
191 extern const char _PATH_ELFSIGN_CERTS[];
192 
193 #define	ELFCERT_MAX_DN_LEN	255
194 
195 typedef struct ELFCert_s *ELFCert_t;
196 
197 extern boolean_t elfcertlib_init(void);
198 
199 extern boolean_t elfcertlib_loadcert(ELFCert_t *certp, const char *pathname);
200 extern void elfcertlib_releasecert(ELFCert_t cert);
201 extern char *elfcertlib_getdn(ELFCert_t cert);
202 extern char *elfcertlib_getissuer(ELFCert_t cert);
203 
204 extern boolean_t elfcertlib_loadprivatekey(ELFsign_t ess, ELFCert_t cert,
205     const char *path);
206 extern boolean_t elfcertlib_loadtokenkey(ELFsign_t ess, ELFCert_t cert,
207     const char *token_id, const char *pin);
208 
209 extern boolean_t elfcertlib_sign(ELFsign_t ess, ELFCert_t cert,
210     const uchar_t *data, size_t data_len, uchar_t *sig, size_t *sig_len);
211 
212 #endif	/* _KERNEL */
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif /* _SYS_CRYPTO_ELFSIGN_H */
219