Coverage Report

Created: 2025-11-06 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/pkcs11/debug.c
Line
Count
Source
1
/*
2
 * Debugging stuff for pkcs11
3
 *
4
 * Copyright (C) 2003 Olaf Kirch <okir@suse.de>
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
#include "config.h"
22
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "sc-pkcs11.h"
27
28
812
#define DUMP_TEMPLATE_MAX 32
29
30
struct fmap {
31
  CK_ULONG  value;
32
  const char *  name;
33
  const char *  (*print)(int level, struct fmap *, void *, size_t);
34
  struct fmap * map;
35
};
36
37
#define _(x)    { (x), #x, NULL, NULL }
38
#define ul(x)   { (x), #x, sc_pkcs11_print_ulong, NULL }
39
#define ulm(x)    { (x), #x, sc_pkcs11_print_ulong, map_##x }
40
#define b(x)    { (x), #x, sc_pkcs11_print_bool, NULL }
41
#define s(x)    { (x), #x, sc_pkcs11_print_string, NULL }
42
43
static void   sc_pkcs11_print_attr(int level, const char *,
44
        unsigned int, const char *, const char *,
45
        CK_ATTRIBUTE_PTR);
46
static const char * sc_pkcs11_print_value(int level, struct fmap *,
47
        void *, size_t);
48
static struct fmap *  sc_pkcs11_map_ulong(int level, struct fmap *,
49
        CK_ULONG);
50
static const char * sc_pkcs11_print_ulong(int level, struct fmap *,
51
        void *, size_t);
52
static const char * sc_pkcs11_print_bool(int level, struct fmap *,
53
        void *, size_t);
54
static const char * sc_pkcs11_print_string(int level, struct fmap *,
55
        void *, size_t);
56
57
static struct fmap  map_CKA_CLASS[] = {
58
  _(CKO_DATA),
59
  _(CKO_CERTIFICATE),
60
  _(CKO_PUBLIC_KEY),
61
  _(CKO_PRIVATE_KEY),
62
  _(CKO_SECRET_KEY),
63
  _(CKO_HW_FEATURE),
64
  _(CKO_DOMAIN_PARAMETERS),
65
66
  { 0, NULL, NULL, NULL }
67
};
68
69
static struct fmap  map_CKA_CERTIFICATE_TYPE[] = {
70
  _(CKC_X_509),
71
  _(CKC_X_509_ATTR_CERT),
72
73
  { 0, NULL, NULL, NULL }
74
};
75
76
static struct fmap  map_CKA_KEY_TYPE[] = {
77
  _(CKK_RSA),
78
  _(CKK_DSA),
79
  _(CKK_DH),
80
  _(CKK_ECDSA),
81
  _(CKK_EC),
82
  _(CKK_EC_EDWARDS),
83
  _(CKK_EC_MONTGOMERY),
84
  _(CKK_RC2),
85
  _(CKK_RC4),
86
  _(CKK_RC5),
87
  _(CKK_DES),
88
  _(CKK_DES3),
89
  _(CKK_CAST),
90
  _(CKK_CAST3),
91
  _(CKK_CAST128),
92
  _(CKK_IDEA),
93
  _(CKK_AES),
94
95
  { 0, NULL, NULL, NULL }
96
};
97
98
static struct fmap  p11_attr_names[] = {
99
  ulm(CKA_CLASS),
100
  b(CKA_TOKEN),
101
  b(CKA_PRIVATE),
102
  s(CKA_LABEL),
103
  _(CKA_APPLICATION),
104
  _(CKA_VALUE),
105
  _(CKA_OBJECT_ID),
106
  ulm(CKA_CERTIFICATE_TYPE),
107
  _(CKA_ISSUER),
108
  _(CKA_SERIAL_NUMBER),
109
  _(CKA_AC_ISSUER),
110
  _(CKA_OWNER),
111
  _(CKA_ATTR_TYPES),
112
  b(CKA_TRUSTED),
113
  ulm(CKA_KEY_TYPE),
114
  _(CKA_SUBJECT),
115
  _(CKA_ID),
116
  b(CKA_SENSITIVE),
117
  b(CKA_ENCRYPT),
118
  b(CKA_DECRYPT),
119
  b(CKA_WRAP),
120
  b(CKA_UNWRAP),
121
  b(CKA_SIGN),
122
  b(CKA_SIGN_RECOVER),
123
  b(CKA_VERIFY),
124
  b(CKA_VERIFY_RECOVER),
125
  b(CKA_DERIVE),
126
  _(CKA_START_DATE),
127
  _(CKA_END_DATE),
128
  _(CKA_MODULUS),
129
  ul(CKA_MODULUS_BITS),
130
  _(CKA_PUBLIC_EXPONENT),
131
  _(CKA_PRIVATE_EXPONENT),
132
  _(CKA_PRIME_1),
133
  _(CKA_PRIME_2),
134
  _(CKA_EXPONENT_1),
135
  _(CKA_EXPONENT_2),
136
  _(CKA_COEFFICIENT),
137
  _(CKA_PRIME),
138
  _(CKA_SUBPRIME),
139
  _(CKA_BASE),
140
  _(CKA_PRIME_BITS),
141
  _(CKA_SUB_PRIME_BITS),
142
  _(CKA_VALUE_BITS),
143
  _(CKA_VALUE_LEN),
144
  b(CKA_EXTRACTABLE),
145
  b(CKA_LOCAL),
146
  b(CKA_NEVER_EXTRACTABLE),
147
  b(CKA_ALWAYS_SENSITIVE),
148
  _(CKA_KEY_GEN_MECHANISM),
149
  b(CKA_MODIFIABLE),
150
  _(CKA_EC_PARAMS),
151
  _(CKA_ECDSA_PARAMS),
152
  _(CKA_EC_POINT),
153
  _(CKA_SECONDARY_AUTH),
154
  ul(CKA_AUTH_PIN_FLAGS),
155
  _(CKA_HW_FEATURE_TYPE),
156
  _(CKA_RESET_ON_INIT),
157
  _(CKA_HAS_RESET),
158
  _(CKA_VENDOR_DEFINED),
159
  b(CKA_ALWAYS_AUTHENTICATE),
160
  _(CKA_GOSTR3410_PARAMS),
161
162
  { 0, NULL, NULL, NULL }
163
};
164
165
void sc_pkcs11_print_attrs(int level, const char *file, unsigned int line,
166
      const char *function,
167
      const char *info,
168
      CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
169
1.97k
{
170
1.97k
  if (ulCount == 0) {
171
0
    sc_do_log(context, level,
172
0
      file, line, function,
173
0
      "%s: empty template\n",
174
0
      info);
175
0
    return;
176
0
  }
177
178
7.43k
  while (ulCount--)
179
5.46k
    sc_pkcs11_print_attr(level, file, line, function,
180
5.46k
        info, pTemplate++);
181
182
1.97k
}
183
184
static void sc_pkcs11_print_attr(int level, const char *file, unsigned int line,
185
      const char *function,
186
      const char *info, CK_ATTRIBUTE_PTR attr)
187
5.46k
{
188
5.46k
  struct fmap *fm;
189
5.46k
  const char *  value;
190
191
5.46k
  fm = sc_pkcs11_map_ulong(level, p11_attr_names, attr->type);
192
193
5.46k
  if (attr->pValue == NULL) {
194
0
    value = "<size inquiry>";
195
5.46k
  } else {
196
5.46k
    value = sc_pkcs11_print_value(level, fm,
197
5.46k
      attr->pValue, attr->ulValueLen);
198
5.46k
  }
199
200
5.46k
  if (fm == NULL) {
201
12
    sc_do_log(context, level,
202
12
        file, line, function,
203
12
        "%s: Attribute 0x%lx = %s\n",
204
12
        info, attr->type, value);
205
5.45k
  } else {
206
5.45k
    sc_do_log(context, level,
207
5.45k
        file, line, function,
208
5.45k
        "%s: %s = %s\n",
209
5.45k
        info, fm->name, value);
210
5.45k
  }
211
5.46k
}
212
213
static const char *sc_pkcs11_print_value(int level, struct fmap *fm,
214
      void *ptr, size_t count)
215
5.67k
{
216
5.67k
  static char buffer[4 * DUMP_TEMPLATE_MAX + 1] = "";
217
218
5.67k
  if (count == (CK_ULONG)-1)
219
0
    return "<error>";
220
221
5.67k
  if (!fm || !fm->print) {
222
724
    unsigned char *value = (unsigned char*) ptr;
223
724
    char  *p;
224
225
724
    if (count > DUMP_TEMPLATE_MAX)
226
88
      count = DUMP_TEMPLATE_MAX;
227
228
4.79k
    for (p = buffer; count--; value++)
229
4.06k
      p += sprintf(p, "%02X", *value);
230
724
    return buffer;
231
724
  }
232
233
4.94k
  return fm->print(level, fm, ptr, count);
234
5.67k
}
235
236
static const char *sc_pkcs11_print_ulong(int level, struct fmap *fm,
237
    void *ptr, size_t count)
238
2.31k
{
239
2.31k
  static char buffer[64];
240
2.31k
  CK_ULONG  value;
241
242
2.31k
  if (count == sizeof(CK_ULONG)) {
243
2.11k
    memcpy(&value, ptr, count);
244
2.11k
    if ((fm = sc_pkcs11_map_ulong(level, fm->map, value)) != NULL)
245
2.01k
      return fm->name;
246
94
    sprintf(buffer, "0x%lx", (unsigned long) value);
247
94
    return buffer;
248
2.11k
  }
249
250
207
  return sc_pkcs11_print_value(level, NULL, ptr, count);
251
2.31k
}
252
253
static const char *sc_pkcs11_print_bool(int level, struct fmap *fm,
254
    void *ptr, size_t count)
255
2.60k
{
256
2.60k
  CK_BBOOL  value;
257
258
2.60k
  if (count == sizeof(CK_BBOOL)) {
259
2.60k
    memcpy(&value, ptr, count);
260
2.60k
    if (value)
261
1.72k
      return "TRUE";
262
877
    return "FALSE";
263
2.60k
  }
264
265
0
  return sc_pkcs11_print_value(level, NULL, ptr, count);
266
2.60k
}
267
268
static const char *sc_pkcs11_print_string(int level, struct fmap *fm,
269
    void *ptr, size_t count)
270
24
{
271
24
  static char buffer[128];
272
273
24
  if (count >= sizeof(buffer))
274
0
    count = sizeof(buffer)-1;
275
24
  memcpy(buffer, ptr, count);
276
24
  buffer[count] = 0;
277
24
  return buffer;
278
24
}
279
280
static struct fmap *sc_pkcs11_map_ulong(int level, struct fmap *fm, CK_ULONG value)
281
7.57k
{
282
66.3k
  for (; fm && fm->name; fm++) {
283
66.2k
    if (fm->value == value)
284
7.46k
      return fm;
285
66.2k
  }
286
106
  return NULL;
287
7.57k
}
288