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