1 /*
2  * errors.c: The textual representation of errors
3  *
4  * Copyright (C) 2001, 2002  Juha Yrjölä <juha.yrjola@iki.fi>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #if HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <stdio.h>
26 
27 #include "errors.h"
28 
29 #define DIM(v)		(sizeof(v)/(sizeof((v)[0])))
30 
sc_strerror(int error)31 const char *sc_strerror(int error)
32 {
33 	const char *rdr_errors[] = {
34 		"Generic reader error",
35 		"No readers found",
36 		"UNUSED",
37 		"UNUSED",
38 		"Card not present",
39 		"Card removed",
40 		"Card reset",
41 		"Transmit failed",
42 		"Timed out while waiting for user input",
43 		"Input operation cancelled by user",
44 		"The two PINs did not match",
45 		"Message too long (keypad)",
46 		"Timeout while waiting for event from card reader",
47 		"Unresponsive card (correctly inserted?)",
48 		"Reader detached",
49 		"Reader reattached",
50 		"Reader in use by another application"
51 	};
52 	const int rdr_base = -SC_ERROR_READER;
53 
54 	const char *card_errors[] = {
55 		"Card command failed",
56 		"File not found",
57 		"Record not found",
58 		"Unsupported CLA byte in APDU",
59 		"Unsupported INS byte in APDU",
60 		"Incorrect parameters in APDU",
61 		"Wrong length",
62 		"Card memory failure",
63 		"Card does not support the requested operation",
64 		"Not allowed",
65 		"Card is invalid or cannot be handled",
66 		"Security status not satisfied",
67 		"Authentication method blocked",
68 		"Unknown data received from card",
69 		"PIN code or key incorrect",
70 		"File already exists",
71 		"Data object not found",
72 		"Not enough memory on card",
73 		"Part of returned data may be corrupted",
74 		"End of file/record reached before reading Le bytes",
75 		"Reference data not usable"
76 	};
77 	const int card_base = -SC_ERROR_CARD_CMD_FAILED;
78 
79 	const char *arg_errors[] = {
80 		"Invalid arguments",
81 		"UNUSED",
82 		"UNUSED",
83 		"Buffer too small",
84 		"Invalid PIN length",
85 		"Invalid data",
86 	};
87 	const int arg_base = -SC_ERROR_INVALID_ARGUMENTS;
88 
89 	const char *int_errors[] = {
90 		"Internal error",
91 		"Invalid ASN.1 object",
92 		"Required ASN.1 object not found",
93 		"Premature end of ASN.1 stream",
94 		"Out of memory",
95 		"Too many objects",
96 		"Object not valid",
97 		"Requested object not found",
98 		"Not supported",
99 		"Passphrase required",
100 		"Inconsistent configuration",
101 		"Decryption failed",
102 		"Wrong padding",
103 		"Unsupported card",
104 		"Unable to load external module",
105 		"EF offset too large",
106 		"Not implemented",
107 		"Invalid Simple TLV object",
108 		"Premature end of Simple TLV stream",
109 	};
110 	const int int_base = -SC_ERROR_INTERNAL;
111 
112 	const char *p15i_errors[] = {
113 		"Generic PKCS#15 initialization error",
114 		"Syntax error",
115 		"Inconsistent or incomplete PKCS#15 profile",
116 		"Key length/algorithm not supported by card",
117 		"No default (transport) key available",
118 		"Non unique object ID",
119 		"Unable to load key and certificate(s) from file",
120 		"UNUSED",
121 		"File template not found",
122 		"Invalid PIN reference",
123 		"File too small",
124 	};
125 	const int p15i_base = -SC_ERROR_PKCS15INIT;
126 
127 	const char *sm_errors[] = {
128 		"Generic Secure Messaging error",
129 		"Data enciphering error",
130 		"Invalid secure messaging level",
131 		"No session keys",
132 		"Invalid session keys",
133 		"Secure Messaging not initialized",
134 		"Cannot authenticate card",
135 		"Random generation error",
136 		"Secure messaging keyset not found",
137 		"IFD data missing",
138 		"SM not applied",
139 		"SM session already active",
140 		"Invalid checksum"
141 	};
142 	const int sm_base = -SC_ERROR_SM;
143 
144 	const char *misc_errors[] = {
145 		"Unknown error",
146 		"PKCS#15 compatible smart card not found",
147 	};
148 	const int misc_base = -SC_ERROR_UNKNOWN;
149 
150 	const char *no_errors = "Success";
151 	const char **errors = NULL;
152 	int count = 0, err_base = 0;
153 
154 	if (!error)
155 		return no_errors;
156 	if (error < 0)
157 		error = -error;
158 
159 	if (error >= misc_base) {
160 		errors = misc_errors;
161 		count = DIM(misc_errors);
162 		err_base = misc_base;
163 	} else if (error >= sm_base) {
164 		errors = sm_errors;
165 		count = DIM(sm_errors);
166 		err_base = sm_base;
167 	} else if (error >= p15i_base) {
168 		errors = p15i_errors;
169 		count = DIM(p15i_errors);
170 		err_base = p15i_base;
171 	} else if (error >= int_base) {
172 		errors = int_errors;
173 		count = DIM(int_errors);
174 		err_base = int_base;
175 	} else if (error >= arg_base) {
176 		errors = arg_errors;
177 		count = DIM(arg_errors);
178 		err_base = arg_base;
179 	} else if (error >= card_base) {
180 		errors = card_errors;
181 		count = DIM(card_errors);
182 		err_base = card_base;
183 	} else if (error >= rdr_base) {
184 		errors = rdr_errors;
185 		count = DIM(rdr_errors);
186 		err_base = rdr_base;
187 	}
188 	error -= err_base;
189 	if (error >= count || count == 0)
190 		return misc_errors[0];
191 	return errors[error];
192 }
193