Coverage Report

Created: 2026-03-21 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/errors.c
Line
Count
Source
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifdef HAVE_CONFIG_H
22
#include "config.h"
23
#endif
24
25
#include <stdio.h>
26
27
#include "errors.h"
28
29
1.34M
#define DIM(v)    (sizeof(v)/(sizeof((v)[0])))
30
31
const char *sc_strerror(int error)
32
4.92M
{
33
4.92M
  unsigned int error_index = 0;
34
4.92M
  const char *rdr_errors[] = {
35
4.92M
    "Generic reader error",
36
4.92M
    "No readers found",
37
4.92M
    "UNUSED",
38
4.92M
    "UNUSED",
39
4.92M
    "Card not present",
40
4.92M
    "Card removed",
41
4.92M
    "Card reset",
42
4.92M
    "Transmit failed",
43
4.92M
    "Timed out while waiting for user input",
44
4.92M
    "Input operation cancelled by user",
45
4.92M
    "The two PINs did not match",
46
4.92M
    "Message too long (keypad)",
47
4.92M
    "Timeout while waiting for event from card reader",
48
4.92M
    "Unresponsive card (correctly inserted?)",
49
4.92M
    "Reader detached",
50
4.92M
    "Reader reattached",
51
4.92M
    "Reader in use by another application"
52
4.92M
  };
53
4.92M
  const unsigned int rdr_base = -SC_ERROR_READER;
54
55
4.92M
  const char *card_errors[] = {
56
4.92M
    "Card command failed",
57
4.92M
    "File not found",
58
4.92M
    "Record not found",
59
4.92M
    "Unsupported CLA byte in APDU",
60
4.92M
    "Unsupported INS byte in APDU",
61
4.92M
    "Incorrect parameters in APDU",
62
4.92M
    "Wrong length",
63
4.92M
    "Card memory failure",
64
4.92M
    "Card does not support the requested operation",
65
4.92M
    "Not allowed",
66
4.92M
    "Card is invalid or cannot be handled",
67
4.92M
    "Security status not satisfied",
68
4.92M
    "Authentication method blocked",
69
4.92M
    "Unknown data received from card",
70
4.92M
    "PIN code or key incorrect",
71
4.92M
    "File already exists",
72
4.92M
    "Data object not found",
73
4.92M
    "Not enough memory on card",
74
4.92M
    "Part of returned data may be corrupted",
75
4.92M
    "End of file/record reached before reading Le bytes",
76
4.92M
    "Reference data not usable"
77
4.92M
  };
78
4.92M
  const unsigned int card_base = -SC_ERROR_CARD_CMD_FAILED;
79
80
4.92M
  const char *arg_errors[] = {
81
4.92M
    "Invalid arguments",
82
4.92M
    "UNUSED",
83
4.92M
    "UNUSED",
84
4.92M
    "Buffer too small",
85
4.92M
    "Invalid PIN length",
86
4.92M
    "Invalid data",
87
4.92M
  };
88
4.92M
  const unsigned int arg_base = -SC_ERROR_INVALID_ARGUMENTS;
89
90
4.92M
  const char *int_errors[] = {
91
4.92M
    "Internal error",
92
4.92M
    "Invalid ASN.1 object",
93
4.92M
    "Required ASN.1 object not found",
94
4.92M
    "Premature end of ASN.1 stream",
95
4.92M
    "Out of memory",
96
4.92M
    "Too many objects",
97
4.92M
    "Object not valid",
98
4.92M
    "Requested object not found",
99
4.92M
    "Not supported",
100
4.92M
    "Passphrase required",
101
4.92M
    "Inconsistent configuration",
102
4.92M
    "Decryption failed",
103
4.92M
    "Wrong padding",
104
4.92M
    "Unsupported card",
105
4.92M
    "Unable to load external module",
106
4.92M
    "EF offset too large",
107
4.92M
    "Not implemented",
108
4.92M
    "Invalid Simple TLV object",
109
4.92M
    "Premature end of Simple TLV stream",
110
4.92M
  };
111
4.92M
  const unsigned int int_base = -SC_ERROR_INTERNAL;
112
113
4.92M
  const char *p15i_errors[] = {
114
4.92M
    "Generic PKCS#15 initialization error",
115
4.92M
    "Syntax error",
116
4.92M
    "Inconsistent or incomplete PKCS#15 profile",
117
4.92M
    "Key length/algorithm not supported by card",
118
4.92M
    "No default (transport) key available",
119
4.92M
    "Non unique object ID",
120
4.92M
    "Unable to load key and certificate(s) from file",
121
4.92M
    "UNUSED",
122
4.92M
    "File template not found",
123
4.92M
    "Invalid PIN reference",
124
4.92M
    "File too small",
125
4.92M
  };
126
4.92M
  const unsigned int p15i_base = -SC_ERROR_PKCS15INIT;
127
128
4.92M
  const char *sm_errors[] = {
129
4.92M
    "Generic Secure Messaging error",
130
4.92M
    "Data enciphering error",
131
4.92M
    "Invalid secure messaging level",
132
4.92M
    "No session keys",
133
4.92M
    "Invalid session keys",
134
4.92M
    "Secure Messaging not initialized",
135
4.92M
    "Cannot authenticate card",
136
4.92M
    "Random generation error",
137
4.92M
    "Secure messaging keyset not found",
138
4.92M
    "IFD data missing",
139
4.92M
    "SM not applied",
140
4.92M
    "SM session already active",
141
4.92M
    "Invalid checksum"
142
4.92M
  };
143
4.92M
  const unsigned int sm_base = -SC_ERROR_SM;
144
145
4.92M
  const char *misc_errors[] = {
146
4.92M
    "Unknown error",
147
4.92M
    "PKCS#15 compatible smart card not found",
148
4.92M
  };
149
4.92M
  const unsigned int misc_base = -SC_ERROR_UNKNOWN;
150
151
4.92M
  const char *no_errors = "Success";
152
4.92M
  const char **errors = NULL;
153
4.92M
  unsigned int count = 0, err_base = 0;
154
155
4.92M
  if (!error)
156
3.57M
    return no_errors;
157
1.34M
  error_index = error < 0 ? (unsigned)(-(long long int)error) : (unsigned)error;
158
159
1.34M
  if (error_index >= misc_base) {
160
35
    errors = misc_errors;
161
35
    count = DIM(misc_errors);
162
35
    err_base = misc_base;
163
1.34M
  } else if (error_index >= sm_base) {
164
86
    errors = sm_errors;
165
86
    count = DIM(sm_errors);
166
86
    err_base = sm_base;
167
1.34M
  } else if (error_index >= p15i_base) {
168
705
    errors = p15i_errors;
169
705
    count = DIM(p15i_errors);
170
705
    err_base = p15i_base;
171
1.34M
  } else if (error_index >= int_base) {
172
25.7k
    errors = int_errors;
173
25.7k
    count = DIM(int_errors);
174
25.7k
    err_base = int_base;
175
1.31M
  } else if (error_index >= arg_base) {
176
11.0k
    errors = arg_errors;
177
11.0k
    count = DIM(arg_errors);
178
11.0k
    err_base = arg_base;
179
1.30M
  } else if (error_index >= card_base) {
180
1.30M
    errors = card_errors;
181
1.30M
    count = DIM(card_errors);
182
1.30M
    err_base = card_base;
183
1.30M
  } else if (error_index >= rdr_base) {
184
0
    errors = rdr_errors;
185
0
    count = DIM(rdr_errors);
186
0
    err_base = rdr_base;
187
0
  }
188
1.34M
  error_index -= err_base;
189
1.34M
  if (error_index >= count)
190
0
    return misc_errors[0];
191
1.34M
  return errors[error_index];
192
1.34M
}