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