xref: /titanic_41/usr/src/common/net/wanboot/p12err.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <assert.h>
31*7c478bd9Sstevel@tonic-gate #include <openssl/err.h>
32*7c478bd9Sstevel@tonic-gate #include <p12err.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * OpenSSL provides a framework for pushing error codes onto a stack.
36*7c478bd9Sstevel@tonic-gate  * When an error occurs, the consumer may use the framework to
37*7c478bd9Sstevel@tonic-gate  * pop the errors off the stack and provide a trace of where the
38*7c478bd9Sstevel@tonic-gate  * errors occurred.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * Our PKCS12 code plugs into this framework by calling
41*7c478bd9Sstevel@tonic-gate  * ERR_load_SUNW_strings(). To push an error (which by the way, consists
42*7c478bd9Sstevel@tonic-gate  * of a function code and an error code) onto the stack our PKCS12 code
43*7c478bd9Sstevel@tonic-gate  * calls SUNWerr().
44*7c478bd9Sstevel@tonic-gate  *
45*7c478bd9Sstevel@tonic-gate  * Consumers of our PKCS12 code can then call the OpenSSL error routines
46*7c478bd9Sstevel@tonic-gate  * when an error occurs and retrieve the stack of errors.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #ifndef OPENSSL_NO_ERR
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /* Function codes and their matching strings */
52*7c478bd9Sstevel@tonic-gate static ERR_STRING_DATA SUNW_str_functs[] = {
53*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_USE_X509CERT, 0),	   "sunw_use_x509cert" },
54*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_USE_PKEY, 0),	   "sunw_use_pkey" },
55*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_USE_TASTORE, 0),	   "sunw_use_tastore" },
56*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_USE_CERTFILE, 0),	   "sunw_p12_use_certfile" },
57*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_USE_KEYFILE, 0),	   "sunw_p12_use_keyfile" },
58*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_USE_TRUSTFILE, 0),	   "sunw_p12_use_trustfile" },
59*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_READ_FILE, 0),	   "p12_read_file" },
60*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_DOPARSE, 0),	   "p12_doparse" },
61*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_PKCS12_PARSE, 0),	   "sunw_PKCS12_parse" },
62*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_PKCS12_CONTENTS, 0),  "sunw_PKCS12_contents" },
63*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_PARSE_ONE_BAG, 0),	   "parse_one_bag" },
64*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_PKCS12_CREATE, 0),	   "sunw_PKCS12_create" },
65*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_SPLIT_CERTS, 0),	   "sunw_split_certs" },
66*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_FIND_LOCALKEYID, 0),  "sunw_find_localkeyid" },
67*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_SET_LOCALKEYID, 0),   "sunw_set_localkeyid" },
68*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_GET_LOCALKEYID, 0),   "sunw_get_localkeyid" },
69*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_GET_PKEY_FNAME, 0),   "sunw_get_pkey_fname" },
70*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_APPEND_KEYS, 0),	   "sunw_append_keys" },
71*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_PEM_INFO, 0),	   "pem_info" },
72*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_ASC2BMPSTRING, 0),	   "asc2bmpstring" },
73*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_UTF82ASCSTR, 0),	   "utf82ascstr" },
74*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_FINDATTR, 0),	   "findattr" },
75*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_TYPE2ATTRIB, 0),	   "type2attrib" },
76*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_MOVE_CERTS, 0),	   "move_certs" },
77*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_FIND_FNAME, 0),	   "sunw_find_fname" },
78*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_PARSE_OUTER, 0),	   "parse_outer" },
79*7c478bd9Sstevel@tonic-gate 	{ ERR_PACK(0, SUNW_F_CHECKFILE, 0),	   "checkfile" },
80*7c478bd9Sstevel@tonic-gate 	{ 0, NULL }
81*7c478bd9Sstevel@tonic-gate };
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /* Error codes and their matching strings */
84*7c478bd9Sstevel@tonic-gate static ERR_STRING_DATA SUNW_str_reasons[] = {
85*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_INVALID_ARG,		"invalid argument" },
86*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_MEMORY_FAILURE,	"memory failure" },
87*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_MAC_VERIFY_FAILURE,	"mac verify failure" },
88*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_MAC_CREATE_FAILURE,	"mac create failure" },
89*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_FILETYPE,		"bad file type" },
90*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_PKEY,		"bad or missing private key" },
91*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_PKEYTYPE,		"unsupported key type" },
92*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PKEY_READ_ERR,		"unable to read private key" },
93*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_NO_TRUST_ANCHOR,	"no trust anchors found" },
94*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_READ_TRUST_ERR,	"unable to read trust anchor" },
95*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_ADD_TRUST_ERR,		"unable to add trust anchor" },
96*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PKCS12_PARSE_ERR,	"PKCS12 parse error" },
97*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PKCS12_CREATE_ERR,	"PKCS12 create error" },
98*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_CERTTYPE,		"unsupported certificate type" },
99*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PARSE_CERT_ERR,	"error parsing PKCS12 certificate" },
100*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PARSE_BAG_ERR,		"error parsing PKCS12 bag" },
101*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_MAKE_BAG_ERR,		"error making PKCS12 bag" },
102*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_LKID,		"bad localKeyID format" },
103*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_SET_LKID_ERR,		"error setting localKeyID" },
104*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_FNAME,		"bad friendlyName format" },
105*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_SET_FNAME_ERR,		"error setting friendlyName" },
106*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_TRUST,		"bad or missing trust anchor" },
107*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_BAD_BAGTYPE,		"unsupported bag type" },
108*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_CERT_ERR,		"certificate error" },
109*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PKEY_ERR,		"private key error" },
110*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_READ_ERR,		"error reading file" },
111*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_ADD_ATTR_ERR,		"error adding attribute" },
112*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_STR_CONVERT_ERR,	"error converting string" },
113*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PKCS12_EMPTY_ERR,	"empty PKCS12 structure" },
114*7c478bd9Sstevel@tonic-gate 	{ SUNW_R_PASSWORD_ERR,		"bad password" },
115*7c478bd9Sstevel@tonic-gate 	{ 0, NULL }
116*7c478bd9Sstevel@tonic-gate };
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /*
119*7c478bd9Sstevel@tonic-gate  * The library name that our module will be known as. This name
120*7c478bd9Sstevel@tonic-gate  * may be retrieved via OpenSSLs error APIs.
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate static ERR_STRING_DATA SUNW_lib_name[] = {
123*7c478bd9Sstevel@tonic-gate 	{ 0,	SUNW_LIB_NAME },
124*7c478bd9Sstevel@tonic-gate 	{ 0, NULL }
125*7c478bd9Sstevel@tonic-gate };
126*7c478bd9Sstevel@tonic-gate #endif
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate  * The value of this variable (initialized by a call to
130*7c478bd9Sstevel@tonic-gate  * ERR_load_SUNW_strings()) is what identifies our errors
131*7c478bd9Sstevel@tonic-gate  * to OpenSSL as being ours.
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate static int SUNW_lib_error_code = 0;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * Called by our PKCS12 code to read our function and error codes
137*7c478bd9Sstevel@tonic-gate  * into memory so that the OpenSSL framework can retrieve them.
138*7c478bd9Sstevel@tonic-gate  */
139*7c478bd9Sstevel@tonic-gate void
ERR_load_SUNW_strings(void)140*7c478bd9Sstevel@tonic-gate ERR_load_SUNW_strings(void)
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	assert(SUNW_lib_error_code == 0);
143*7c478bd9Sstevel@tonic-gate #ifndef OPENSSL_NO_ERR
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * Have OpenSSL provide us with a unique ID.
146*7c478bd9Sstevel@tonic-gate 	 */
147*7c478bd9Sstevel@tonic-gate 	SUNW_lib_error_code = ERR_get_next_error_library();
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	ERR_load_strings(SUNW_lib_error_code, SUNW_str_functs);
150*7c478bd9Sstevel@tonic-gate 	ERR_load_strings(SUNW_lib_error_code, SUNW_str_reasons);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	SUNW_lib_name->error = ERR_PACK(SUNW_lib_error_code, 0, 0);
153*7c478bd9Sstevel@tonic-gate 	ERR_load_strings(0, SUNW_lib_name);
154*7c478bd9Sstevel@tonic-gate #endif
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate /*
158*7c478bd9Sstevel@tonic-gate  * The SUNWerr macro resolves to this routine. So when we need
159*7c478bd9Sstevel@tonic-gate  * to push an error, this routine does it for us. Notice that
160*7c478bd9Sstevel@tonic-gate  * the SUNWerr macro provides a filename and line #.
161*7c478bd9Sstevel@tonic-gate  */
162*7c478bd9Sstevel@tonic-gate void
ERR_SUNW_error(int function,int reason,char * file,int line)163*7c478bd9Sstevel@tonic-gate ERR_SUNW_error(int function, int reason, char *file, int line)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	assert(SUNW_lib_error_code != 0);
166*7c478bd9Sstevel@tonic-gate #ifndef OPENSSL_NO_ERR
167*7c478bd9Sstevel@tonic-gate 	ERR_PUT_error(SUNW_lib_error_code, function, reason, file, line);
168*7c478bd9Sstevel@tonic-gate #endif
169*7c478bd9Sstevel@tonic-gate }
170