Coverage Report

Created: 2026-09-03 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/pkcs15-pin.c
Line
Count
Source
1
/*
2
 * pkcs15-pin.c: PKCS #15 PIN functions
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 <assert.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#include <stdio.h>
29
30
#include "internal.h"
31
#include "asn1.h"
32
#include "pkcs15.h"
33
#include "ui/notify.h"
34
35
int _sc_pkcs15_verify_pin(struct sc_pkcs15_card *, struct sc_pkcs15_object *,
36
    const unsigned char *, size_t);
37
38
static const struct sc_asn1_entry c_asn1_com_ao_attr[] = {
39
  { "authId",       SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
40
  { NULL, 0, 0, 0, NULL, NULL }
41
};
42
43
/* PIN attributes */
44
static const struct sc_asn1_entry c_asn1_pin_attr[] = {
45
  { "pinFlags",   SC_ASN1_BIT_FIELD, SC_ASN1_TAG_BIT_STRING, 0, NULL, NULL },
46
  { "pinType",      SC_ASN1_ENUMERATED, SC_ASN1_TAG_ENUMERATED, 0, NULL, NULL },
47
  { "minLength",    SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL },
48
  { "storedLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL },
49
  { "maxLength",    SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL },
50
  { "pinReference", SC_ASN1_INTEGER, SC_ASN1_CTX | 0, SC_ASN1_OPTIONAL, NULL, NULL },
51
  { "padChar",      SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
52
  { "lastPinChange",SC_ASN1_GENERALIZEDTIME, SC_ASN1_TAG_GENERALIZEDTIME, SC_ASN1_OPTIONAL, NULL, NULL },
53
  { "path",         SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
54
  { NULL, 0, 0, 0, NULL, NULL }
55
};
56
static const struct sc_asn1_entry c_asn1_type_pin_attr[] = {
57
  { "pinAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
58
  { NULL, 0, 0, 0, NULL, NULL }
59
};
60
61
/* Auth Key attributes */
62
static const struct sc_asn1_entry c_asn1_authkey_attr[] = {
63
  { "derivedKey", SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL },
64
  { "authKeyId",  SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
65
  { NULL, 0, 0, 0, NULL, NULL }
66
};
67
static const struct sc_asn1_entry c_asn1_type_authkey_attr[] = {
68
  { "authKeyAttributes",  SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
69
  { NULL, 0, 0, 0, NULL, NULL }
70
};
71
static const struct sc_asn1_entry c_asn1_auth_type[] = {
72
  { "authType",      SC_ASN1_CHOICE, 0, 0, NULL, NULL },
73
  { NULL, 0, 0, 0, NULL, NULL }
74
};
75
static const struct sc_asn1_entry c_asn1_auth_type_choice[] = {
76
  { "pin", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
77
  { "biometricTemplate", SC_ASN1_PKCS15_OBJECT,  SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
78
  { "authKey", SC_ASN1_PKCS15_OBJECT,  SC_ASN1_CTX | 1 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
79
  { NULL, 0, 0, 0, NULL, NULL }
80
};
81
82
83
int
84
sc_pkcs15_decode_aodf_entry(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj,
85
    const u8 ** buf, size_t *buflen)
86
29
{
87
29
  sc_context_t *ctx = p15card->card->ctx;
88
29
  struct sc_pkcs15_auth_info info;
89
29
  int r;
90
29
  size_t flags_len = sizeof(info.attrs.pin.flags);
91
29
  size_t derived_len = sizeof(info.attrs.authkey.derived);
92
29
  size_t padchar_len = 1;
93
29
  struct sc_asn1_entry asn1_com_ao_attr[2];
94
29
  struct sc_asn1_entry asn1_pin_attr[10], asn1_type_pin_attr[2];
95
29
  struct sc_asn1_entry asn1_authkey_attr[3], asn1_type_authkey_attr[2];
96
29
  struct sc_asn1_entry asn1_auth_type[2];
97
29
  struct sc_asn1_entry asn1_auth_type_choice[4];
98
29
  struct sc_asn1_pkcs15_object pin_obj = { obj, asn1_com_ao_attr, NULL, asn1_type_pin_attr };
99
29
  struct sc_asn1_pkcs15_object authkey_obj = { obj, asn1_com_ao_attr, NULL, asn1_type_authkey_attr };
100
101
29
  SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_ASN1);
102
103
29
  sc_copy_asn1_entry(c_asn1_auth_type, asn1_auth_type);
104
29
  sc_copy_asn1_entry(c_asn1_auth_type_choice, asn1_auth_type_choice);
105
106
29
  sc_copy_asn1_entry(c_asn1_com_ao_attr, asn1_com_ao_attr);
107
108
29
  sc_copy_asn1_entry(c_asn1_type_pin_attr, asn1_type_pin_attr);
109
29
  sc_copy_asn1_entry(c_asn1_pin_attr, asn1_pin_attr);
110
111
29
  sc_copy_asn1_entry(c_asn1_type_authkey_attr, asn1_type_authkey_attr);
112
29
  sc_copy_asn1_entry(c_asn1_authkey_attr, asn1_authkey_attr);
113
114
29
  sc_format_asn1_entry(asn1_auth_type + 0, asn1_auth_type_choice, NULL, 0);
115
29
  sc_format_asn1_entry(asn1_auth_type_choice + 0, &pin_obj, NULL, 0); /* 'pin' */
116
29
  sc_format_asn1_entry(asn1_auth_type_choice + 2, &authkey_obj, NULL, 0); /* 'authKey' */
117
118
  /* pinAttributes */
119
29
  sc_format_asn1_entry(asn1_type_pin_attr + 0, asn1_pin_attr, NULL, 0);
120
29
  sc_format_asn1_entry(asn1_pin_attr + 0, &info.attrs.pin.flags, &flags_len, 0);
121
29
  sc_format_asn1_entry(asn1_pin_attr + 1, &info.attrs.pin.type, NULL, 0);
122
29
  sc_format_asn1_entry(asn1_pin_attr + 2, &info.attrs.pin.min_length, NULL, 0);
123
29
  sc_format_asn1_entry(asn1_pin_attr + 3, &info.attrs.pin.stored_length, NULL, 0);
124
29
  sc_format_asn1_entry(asn1_pin_attr + 4, &info.attrs.pin.max_length, NULL, 0);
125
29
  sc_format_asn1_entry(asn1_pin_attr + 5, &info.attrs.pin.reference, NULL, 0);
126
29
  sc_format_asn1_entry(asn1_pin_attr + 6, &info.attrs.pin.pad_char, &padchar_len, 0);
127
128
  /* authKeyAttributes */
129
29
  sc_format_asn1_entry(asn1_type_authkey_attr + 0, asn1_authkey_attr, NULL, 0);
130
29
  sc_format_asn1_entry(asn1_authkey_attr + 0, &info.attrs.authkey.derived, &derived_len, 0);
131
29
  sc_format_asn1_entry(asn1_authkey_attr + 1, &info.attrs.authkey.skey_id, NULL, 0);
132
133
  /* We don't support lastPinChange yet. */
134
29
  sc_format_asn1_entry(asn1_pin_attr + 8, &info.path, NULL, 0);
135
136
29
  sc_format_asn1_entry(asn1_com_ao_attr + 0, &info.auth_id, NULL, 0);
137
138
  /* Fill in defaults */
139
29
  memset(&info, 0, sizeof(info));
140
29
  info.tries_left = -1;
141
29
  info.logged_in = SC_PIN_STATE_UNKNOWN;
142
143
29
  r = sc_asn1_decode(ctx, asn1_auth_type, *buf, *buflen, buf, buflen);
144
29
  if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
145
2
    return r;
146
27
  LOG_TEST_RET(ctx, r, "ASN.1 decoding failed");
147
148
13
  if (asn1_auth_type_choice[0].flags & SC_ASN1_PRESENT)   {
149
11
    sc_log(ctx, "AuthType: PIN");
150
11
    obj->type = SC_PKCS15_TYPE_AUTH_PIN;
151
11
    info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
152
11
    info.auth_method = SC_AC_CHV;
153
154
11
    if (info.attrs.pin.max_length == 0) {
155
2
      if (p15card->card->max_pin_len != 0)
156
0
        info.attrs.pin.max_length = p15card->card->max_pin_len;
157
2
      else if (info.attrs.pin.stored_length != 0)
158
1
        info.attrs.pin.max_length = info.attrs.pin.type != SC_PKCS15_PIN_TYPE_BCD ?
159
1
          info.attrs.pin.stored_length : 2 * info.attrs.pin.stored_length;
160
1
      else
161
1
        info.attrs.pin.max_length = 8; /* shouldn't happen */
162
2
    }
163
164
    /* OpenSC 0.11.4 and older encoded "pinReference" as a negative
165
       value. Fixed in 0.11.5 we need to add a hack, so old cards
166
       continue to work.
167
       The same invalid encoding has some models of the proprietary PKCS#15 cards.
168
    */
169
11
    if (info.attrs.pin.reference < 0)
170
1
      info.attrs.pin.reference += 256;
171
172
11
    if (info.attrs.pin.flags & SC_PKCS15_PIN_FLAG_LOCAL)   {
173
      /* In OpenSC pkcs#15 framework 'path' is mandatory for the 'Local' PINs.
174
       * If 'path' do not present in PinAttributes, derive it from the PKCS#15 context. */
175
2
      if (!info.path.len)   {
176
        /* Give priority to AID defined in the application DDO */
177
2
        if (p15card->app && p15card->app->ddo.aid.len)
178
0
          info.path.aid = p15card->app->ddo.aid;
179
2
        else if (p15card->file_app && p15card->file_app->path.len)
180
2
          info.path = p15card->file_app->path;
181
0
        else
182
0
          return SC_ERROR_INTERNAL;
183
2
      }
184
2
    }
185
11
    sc_debug(ctx, SC_LOG_DEBUG_ASN1, "decoded PIN(ref:%X,path:%s)", info.attrs.pin.reference, sc_print_path(&info.path));
186
11
  }
187
2
  else if (asn1_auth_type_choice[1].flags & SC_ASN1_PRESENT)   {
188
2
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "BIO authentication object not yet supported");
189
2
  }
190
0
  else if (asn1_auth_type_choice[2].flags & SC_ASN1_PRESENT)   {
191
0
    sc_log(ctx, "AuthType: AuthKey");
192
0
    obj->type = SC_PKCS15_TYPE_AUTH_AUTHKEY;
193
0
    info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_AUTH_KEY;
194
0
    info.auth_method = SC_AC_AUT;
195
0
    if (!(asn1_authkey_attr[0].flags & SC_ASN1_PRESENT))
196
0
      info.attrs.authkey.derived = 1;
197
0
  }
198
0
  else   {
199
0
    LOG_TEST_RET(ctx, SC_ERROR_NOT_SUPPORTED, "unknown authentication type");
200
0
  }
201
202
11
  obj->data = malloc(sizeof(info));
203
11
  if (obj->data == NULL)
204
11
    LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
205
11
  memcpy(obj->data, &info, sizeof(info));
206
207
11
  SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_ASN1, SC_SUCCESS);
208
11
}
209
210
int sc_pkcs15_encode_aodf_entry(sc_context_t *ctx,
211
         const struct sc_pkcs15_object *obj,
212
         u8 **buf, size_t *buflen)
213
201
{
214
201
  struct sc_asn1_entry asn1_com_ao_attr[2], asn1_pin_attr[10], asn1_type_pin_attr[2];
215
201
  struct sc_asn1_entry asn1_auth_type[2];
216
201
  struct sc_asn1_entry asn1_auth_type_choice[4];
217
201
  struct sc_pkcs15_auth_info *info = (struct sc_pkcs15_auth_info *) obj->data;
218
201
  struct sc_asn1_pkcs15_object pin_obj = { (struct sc_pkcs15_object *) obj,
219
201
             asn1_com_ao_attr, NULL, asn1_type_pin_attr };
220
201
  int r;
221
201
  size_t flags_len;
222
201
  size_t padchar_len = 1;
223
224
201
  if (info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
225
0
    return SC_ERROR_NOT_SUPPORTED;
226
227
201
  sc_copy_asn1_entry(c_asn1_auth_type, asn1_auth_type);
228
201
  sc_copy_asn1_entry(c_asn1_auth_type_choice, asn1_auth_type_choice);
229
201
  sc_copy_asn1_entry(c_asn1_type_pin_attr, asn1_type_pin_attr);
230
201
  sc_copy_asn1_entry(c_asn1_pin_attr, asn1_pin_attr);
231
201
  sc_copy_asn1_entry(c_asn1_com_ao_attr, asn1_com_ao_attr);
232
233
201
  sc_format_asn1_entry(asn1_auth_type + 0, asn1_auth_type_choice, NULL, 1);
234
201
  sc_format_asn1_entry(asn1_auth_type_choice + 0, &pin_obj, NULL, 1);
235
236
201
  sc_format_asn1_entry(asn1_type_pin_attr + 0, asn1_pin_attr, NULL, 1);
237
238
201
  flags_len = sizeof(info->attrs.pin.flags);
239
201
  sc_format_asn1_entry(asn1_pin_attr + 0, &info->attrs.pin.flags, &flags_len, 1);
240
201
  sc_format_asn1_entry(asn1_pin_attr + 1, &info->attrs.pin.type, NULL, 1);
241
201
  sc_format_asn1_entry(asn1_pin_attr + 2, &info->attrs.pin.min_length, NULL, 1);
242
201
  sc_format_asn1_entry(asn1_pin_attr + 3, &info->attrs.pin.stored_length, NULL, 1);
243
201
  if (info->attrs.pin.max_length > 0)
244
169
    sc_format_asn1_entry(asn1_pin_attr + 4, &info->attrs.pin.max_length, NULL, 1);
245
201
  if (info->attrs.pin.reference >= 0)
246
179
    sc_format_asn1_entry(asn1_pin_attr + 5, &info->attrs.pin.reference, NULL, 1);
247
  /* FIXME: check if pad_char present */
248
201
  sc_format_asn1_entry(asn1_pin_attr + 6, &info->attrs.pin.pad_char, &padchar_len, 1);
249
201
  sc_format_asn1_entry(asn1_pin_attr + 8, &info->path, NULL, info->path.len ? 1 : 0);
250
251
201
  sc_format_asn1_entry(asn1_com_ao_attr + 0, &info->auth_id, NULL, 1);
252
253
201
  r = sc_asn1_encode(ctx, asn1_auth_type, buf, buflen);
254
255
201
  return r;
256
201
}
257
258
259
static int
260
_validate_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_auth_info *auth_info, size_t pinlen)
261
10
{
262
10
  size_t max_length;
263
10
  if (p15card == NULL) {
264
0
    return SC_ERROR_INVALID_ARGUMENTS;
265
0
  }
266
267
  /* Ignore validation of the non-PIN authentication objects */
268
10
  if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
269
0
    return SC_SUCCESS;
270
271
  /* prevent buffer overflow from hostile card */
272
10
  if (auth_info->attrs.pin.stored_length > SC_MAX_PIN_SIZE)
273
0
    return SC_ERROR_BUFFER_TOO_SMALL;
274
275
  /* if we use pinpad, no more checks are needed */
276
10
  if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
277
10
        || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)
278
0
        && !pinlen)
279
0
    return SC_SUCCESS;
280
281
  /* If pin is given, make sure it is within limits */
282
10
  max_length = auth_info->attrs.pin.max_length != 0 ? auth_info->attrs.pin.max_length : SC_MAX_PIN_SIZE;
283
10
  if (pinlen > max_length || pinlen < auth_info->attrs.pin.min_length)
284
2
    return SC_ERROR_INVALID_PIN_LENGTH;
285
286
8
  return SC_SUCCESS;
287
10
}
288
289
/*
290
 * Verify a PIN.
291
 *
292
 * If the code given to us has zero length, this means we
293
 * should ask the card reader to obtain the PIN from the
294
 * reader's PIN pad
295
 */
296
int
297
sc_pkcs15_verify_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj,
298
    const unsigned char *pincode, size_t pinlen)
299
10
{
300
10
  struct sc_context *ctx = p15card->card->ctx;
301
10
  struct sc_pkcs15_auth_info *auth_info;
302
10
  int r;
303
304
10
  LOG_FUNC_CALLED(ctx);
305
306
10
  if (!pin_obj || !pin_obj->data)
307
10
    LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_PIN_REFERENCE);
308
10
  auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
309
310
  /* Check the provided pin matches pin requirements */
311
10
  r = _validate_pin(p15card, auth_info, pinlen);
312
313
10
  if (r)
314
10
    LOG_FUNC_RETURN(ctx, r);
315
316
8
  r = _sc_pkcs15_verify_pin(p15card, pin_obj, pincode, pinlen);
317
318
8
  if (r == SC_SUCCESS)
319
5
    sc_pkcs15_pincache_add(p15card, pin_obj, pincode, pinlen);
320
321
8
  LOG_FUNC_RETURN(ctx, r);
322
8
}
323
324
325
int
326
_sc_pkcs15_verify_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj,
327
    const unsigned char *pincode, size_t pinlen)
328
8
{
329
8
  return sc_pkcs15_verify_pin_with_session_pin(p15card, pin_obj, pincode,
330
8
      pinlen, NULL, NULL);
331
8
}
332
333
/*
334
 * Verify a PIN and generate a session PIN
335
 *
336
 * If the code given to us has zero length, this means we
337
 * should ask the card reader to obtain the PIN from the
338
 * reader's PIN pad
339
 */
340
int sc_pkcs15_verify_pin_with_session_pin(struct sc_pkcs15_card *p15card,
341
       struct sc_pkcs15_object *pin_obj,
342
       const unsigned char *pincode, size_t pinlen,
343
       const unsigned char *sessionpin, size_t *sessionpinlen)
344
8
{
345
8
  struct sc_context *ctx = p15card->card->ctx;
346
8
  struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
347
8
  int r;
348
8
  sc_card_t *card;
349
8
  struct sc_pin_cmd_data data;
350
351
8
  LOG_FUNC_CALLED(ctx);
352
8
  sc_log(ctx, "PIN(type:%X; method:%X; value(%p:%zu)",
353
8
      auth_info->auth_type, auth_info->auth_method, pincode, pinlen);
354
8
  card = p15card->card;
355
356
8
  if (pinlen > SC_MAX_PIN_SIZE) {
357
0
    sc_notify_id(card->ctx, &card->reader->atr, p15card,
358
0
        NOTIFY_PIN_BAD);
359
0
    LOG_TEST_RET(ctx, SC_ERROR_INVALID_PIN_LENGTH, "Invalid PIN size");
360
0
  }
361
362
  /* Initialize arguments */
363
8
  memset(&data, 0, sizeof(data));
364
8
  data.pin_type = auth_info->auth_method;
365
366
8
  if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN)   {
367
8
    data.pin_reference = auth_info->attrs.pin.reference;
368
8
    data.pin1.min_length = auth_info->attrs.pin.min_length;
369
8
    data.pin1.max_length = auth_info->attrs.pin.max_length;
370
8
    data.pin1.pad_length = auth_info->attrs.pin.stored_length;
371
8
    data.pin1.pad_char = auth_info->attrs.pin.pad_char;
372
8
    data.pin1.data = pincode;
373
8
    data.pin1.len = pinlen;
374
375
8
    if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)
376
7
      data.flags |= SC_PIN_CMD_NEED_PADDING;
377
378
8
    switch (auth_info->attrs.pin.type) {
379
0
    case SC_PKCS15_PIN_TYPE_BCD:
380
0
      data.pin1.encoding = SC_PIN_ENCODING_BCD;
381
0
      break;
382
7
    case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
383
7
      data.pin1.encoding = SC_PIN_ENCODING_ASCII;
384
7
      break;
385
1
    default:
386
      /* assume/hope the card driver knows how to encode the pin */
387
1
      data.pin1.encoding = 0;
388
8
    }
389
8
  }
390
0
  else if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_AUTH_KEY)   {
391
0
    struct sc_pkcs15_object *skey_obj = NULL;
392
0
    struct sc_pkcs15_id *skey_id =  &auth_info->attrs.authkey.skey_id;
393
0
    struct sc_pkcs15_skey_info *skey_info = NULL;
394
395
0
    r = sc_pkcs15_find_skey_by_id(p15card, skey_id, &skey_obj);
396
0
    if (r)   {
397
0
      sc_log(ctx, "cannot find secret key with id:%s", sc_pkcs15_print_id(skey_id));
398
0
      LOG_FUNC_RETURN(ctx, r);
399
0
    }
400
0
    skey_info = (struct sc_pkcs15_skey_info *)skey_obj->data;
401
402
0
    sc_log(ctx, "found secret key '%s'", skey_obj->label);
403
0
    data.pin_reference = skey_info->key_reference;
404
0
  }
405
406
8
  if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
407
8
        || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) {
408
0
    if (!pincode && !pinlen)
409
0
      data.flags |= SC_PIN_CMD_USE_PINPAD;
410
411
0
    if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN)
412
0
      data.pin1.prompt = "Please enter SO PIN";
413
0
    else
414
0
      data.pin1.prompt = "Please enter PIN";
415
0
  }
416
417
8
  if (card->caps & SC_CARD_CAP_SESSION_PIN && sessionpin && sessionpinlen) {
418
    /* session pin is requested and supported with standard verification*/
419
0
    data.cmd = SC_PIN_CMD_GET_SESSION_PIN;
420
0
    memcpy(&data.pin2, &data.pin1, sizeof (data.pin1));
421
0
    data.pin2.data = sessionpin;
422
0
    data.pin2.len = *sessionpinlen;
423
8
  } else {
424
    /* perform a standard verify */
425
8
    data.cmd = SC_PIN_CMD_VERIFY;
426
8
    if (sessionpinlen)
427
0
      *sessionpinlen = 0;
428
8
  }
429
430
8
  r = sc_lock(card);
431
8
  LOG_TEST_RET(ctx, r, "sc_lock() failed");
432
433
  /* the path in the pin object is optional */
434
8
  if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) {
435
5
    r = sc_select_file(card, &auth_info->path, NULL);
436
5
    if (r)
437
1
      goto out;
438
5
  }
439
440
7
  r = sc_pin_cmd(card, &data);
441
7
  auth_info->tries_left = data.pin1.tries_left;
442
7
  sc_log(ctx, "PIN cmd result %i", r);
443
7
  if (r == SC_SUCCESS) {
444
5
    sc_pkcs15_pincache_add(p15card, pin_obj, pincode, pinlen);
445
5
    if (data.cmd == SC_PIN_CMD_GET_SESSION_PIN && sessionpinlen) {
446
0
      *sessionpinlen = data.pin2.len;
447
0
    }
448
5
  } else {
449
2
    sc_notify_id(card->ctx, &card->reader->atr, p15card,
450
2
        NOTIFY_PIN_BAD);
451
2
    if (data.cmd == SC_PIN_CMD_GET_SESSION_PIN && sessionpinlen) {
452
0
      *sessionpinlen = 0;
453
0
    }
454
2
  }
455
456
7
  if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN
457
7
      && auth_info->auth_method != SC_AC_SESSION) {
458
7
    sc_notify_id(card->ctx, &card->reader->atr, p15card,
459
7
        r == SC_SUCCESS ? NOTIFY_PIN_GOOD : NOTIFY_PIN_BAD);
460
7
  }
461
462
8
out:
463
8
  sc_unlock(card);
464
8
  LOG_FUNC_RETURN(ctx, r);
465
8
}
466
467
468
469
/*
470
 * Change a PIN.
471
 */
472
int sc_pkcs15_change_pin(struct sc_pkcs15_card *p15card,
473
       struct sc_pkcs15_object *pin_obj,
474
       const u8 *oldpin, size_t oldpinlen,
475
       const u8 *newpin, size_t newpinlen)
476
0
{
477
0
  struct sc_context *ctx = p15card->card->ctx;
478
0
  struct sc_pin_cmd_data data;
479
0
  struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
480
0
  struct sc_card *card;
481
0
  int r;
482
483
0
  LOG_FUNC_CALLED(ctx);
484
0
  if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
485
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
486
487
  /* make sure the pins are in valid range */
488
0
  r = _validate_pin(p15card, auth_info, oldpinlen);
489
0
  LOG_TEST_RET(ctx, r, "Old PIN value do not conform PIN policy");
490
491
0
  r = _validate_pin(p15card, auth_info, newpinlen);
492
0
  LOG_TEST_RET(ctx, r, "New PIN value do not conform PIN policy");
493
494
0
  card = p15card->card;
495
0
  r = sc_lock(card);
496
0
  LOG_TEST_RET(ctx, r, "sc_lock() failed");
497
  /* the path in the pin object is optional */
498
0
  if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) {
499
0
    r = sc_select_file(card, &auth_info->path, NULL);
500
0
    if (r)
501
0
      goto out;
502
0
  }
503
504
  /* set pin_cmd data */
505
0
  memset(&data, 0, sizeof(data));
506
0
  data.cmd             = SC_PIN_CMD_CHANGE;
507
0
  data.pin_type        = SC_AC_CHV;
508
0
  data.pin_reference   = auth_info->attrs.pin.reference;
509
0
  data.pin1.data       = oldpin;
510
0
  data.pin1.len        = oldpinlen;
511
0
  data.pin1.pad_char   = auth_info->attrs.pin.pad_char;
512
0
  data.pin1.min_length = auth_info->attrs.pin.min_length;
513
0
  data.pin1.max_length = auth_info->attrs.pin.max_length;
514
0
  data.pin1.pad_length = auth_info->attrs.pin.stored_length;
515
0
  data.pin2.data       = newpin;
516
0
  data.pin2.len        = newpinlen;
517
0
  data.pin2.pad_char   = auth_info->attrs.pin.pad_char;
518
0
  data.pin2.min_length = auth_info->attrs.pin.min_length;
519
0
  data.pin2.max_length = auth_info->attrs.pin.max_length;
520
0
  data.pin2.pad_length = auth_info->attrs.pin.stored_length;
521
522
0
  if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)
523
0
    data.flags |= SC_PIN_CMD_NEED_PADDING;
524
525
0
  switch (auth_info->attrs.pin.type) {
526
0
  case SC_PKCS15_PIN_TYPE_BCD:
527
0
    data.pin1.encoding = SC_PIN_ENCODING_BCD;
528
0
    data.pin2.encoding = SC_PIN_ENCODING_BCD;
529
0
    break;
530
0
  case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
531
0
    data.pin1.encoding = SC_PIN_ENCODING_ASCII;
532
0
    data.pin2.encoding = SC_PIN_ENCODING_ASCII;
533
0
    break;
534
0
  }
535
536
0
  if((!oldpin || !newpin)
537
0
      && (p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
538
0
        || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) {
539
0
    data.flags |= SC_PIN_CMD_USE_PINPAD;
540
0
    if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) {
541
0
      data.pin1.prompt = "Please enter SO PIN";
542
0
      data.pin2.prompt = "Please enter new SO PIN";
543
0
    }
544
0
    else {
545
0
      data.pin1.prompt = "Please enter PIN";
546
0
      data.pin2.prompt = "Please enter new PIN";
547
0
    }
548
0
  }
549
550
0
  r = sc_pin_cmd(card, &data);
551
0
  auth_info->tries_left = data.pin1.tries_left;
552
0
  if (r == SC_SUCCESS)
553
0
    sc_pkcs15_pincache_add(p15card, pin_obj, newpin, newpinlen);
554
555
0
out:
556
0
  sc_unlock(card);
557
0
  return r;
558
0
}
559
560
/*
561
 * Unblock a PIN.
562
 */
563
int sc_pkcs15_unblock_pin(struct sc_pkcs15_card *p15card,
564
       struct sc_pkcs15_object *pin_obj,
565
       const u8 *puk, size_t puklen,
566
       const u8 *newpin, size_t newpinlen)
567
0
{
568
0
  struct sc_context *ctx = p15card->card->ctx;
569
0
  struct sc_pin_cmd_data data;
570
0
  struct sc_pkcs15_object *puk_obj;
571
0
  struct sc_pkcs15_auth_info *puk_info = NULL;
572
0
  int pukref = 0;
573
0
  struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
574
0
  struct sc_card *card = p15card->card;
575
0
  int r;
576
577
0
  LOG_FUNC_CALLED(ctx);
578
0
  if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
579
0
    LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
580
581
  /* make sure the pins are in valid range */
582
0
  r = _validate_pin(p15card, auth_info, newpinlen);
583
0
  LOG_TEST_RET(ctx, r, "New PIN value do not conform PIN policy");
584
585
  /* get pin_info object of the puk (this is a little bit complicated
586
   * as we don't have the id of the puk (at least now))
587
   * note: for compatibility reasons we give no error if no puk object
588
   * is found */
589
  /* first step: try to get the pkcs15 object of the puk */
590
0
  r = sc_pkcs15_find_pin_by_auth_id(p15card, &pin_obj->auth_id, &puk_obj);
591
0
  if (r >= 0 && puk_obj) {
592
    /* second step:  get the pkcs15 info object of the puk */
593
0
    puk_info = (struct sc_pkcs15_auth_info *)puk_obj->data;
594
0
    pukref = puk_info->attrs.pin.reference;
595
0
  }
596
597
0
  if (!puk_info) {
598
0
    sc_log(ctx, "Unable to get puk object, using pin object instead!");
599
0
    puk_info = auth_info;
600
0
  }
601
  /* make sure the puk is in valid range */
602
0
  r = _validate_pin(p15card, puk_info, puklen);
603
0
  LOG_TEST_RET(ctx, r, "PIN do not conforms PIN policy");
604
605
  /*
606
   * With the current card driver interface we have no way of specifying different padding
607
   * flags for the PIN and the PUK. Therefore reject this case.
608
   */
609
0
  if ((auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING) !=
610
0
      (puk_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)) {
611
0
    LOG_TEST_RET(ctx, r, "Padding mismatch for PIN/PUK");
612
0
  }
613
614
0
  r = sc_lock(card);
615
0
  LOG_TEST_RET(ctx, r, "sc_lock() failed");
616
617
  /* the path in the pin object is optional */
618
0
  if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) {
619
0
    r = sc_select_file(card, &auth_info->path, NULL);
620
0
    if (r)
621
0
      goto out;
622
0
  }
623
624
  /* set pin_cmd data */
625
0
  memset(&data, 0, sizeof(data));
626
0
  data.cmd             = SC_PIN_CMD_UNBLOCK;
627
0
  data.pin_type        = SC_AC_CHV;
628
0
  data.pin_reference   = auth_info->attrs.pin.reference;
629
0
  data.puk_reference   = pukref;
630
0
  data.pin1.data       = puk;
631
0
  data.pin1.len        = puklen;
632
0
  data.pin1.pad_char   = puk_info->attrs.pin.pad_char;
633
0
  data.pin1.min_length = puk_info->attrs.pin.min_length;
634
0
  data.pin1.max_length = puk_info->attrs.pin.max_length;
635
0
  data.pin1.pad_length = puk_info->attrs.pin.stored_length;
636
0
  data.pin2.data       = newpin;
637
0
  data.pin2.len        = newpinlen;
638
0
  data.pin2.pad_char   = auth_info->attrs.pin.pad_char;
639
0
  data.pin2.min_length = auth_info->attrs.pin.min_length;
640
0
  data.pin2.max_length = auth_info->attrs.pin.max_length;
641
0
  data.pin2.pad_length = auth_info->attrs.pin.stored_length;
642
643
0
  if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)
644
0
    data.flags |= SC_PIN_CMD_NEED_PADDING;
645
646
0
  switch (auth_info->attrs.pin.type) {
647
0
  case SC_PKCS15_PIN_TYPE_BCD:
648
0
    data.pin1.encoding = SC_PIN_ENCODING_BCD;
649
0
    break;
650
0
  case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
651
0
    data.pin1.encoding = SC_PIN_ENCODING_ASCII;
652
0
    break;
653
0
  }
654
655
0
  switch (puk_info->attrs.pin.type) {
656
0
  case SC_PKCS15_PIN_TYPE_BCD:
657
0
    data.pin2.encoding = SC_PIN_ENCODING_BCD;
658
0
    break;
659
0
  case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
660
0
    data.pin2.encoding = SC_PIN_ENCODING_ASCII;
661
0
    break;
662
0
  }
663
664
0
  if((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
665
0
        || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) {
666
0
    data.flags |= SC_PIN_CMD_USE_PINPAD;
667
0
    if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) {
668
0
      data.pin1.prompt = "Please enter PUK";
669
0
      data.pin2.prompt = "Please enter new SO PIN";
670
0
    }
671
0
    else {
672
0
      data.pin1.prompt = "Please enter PUK";
673
0
      data.pin2.prompt = "Please enter new PIN";
674
0
    }
675
0
  }
676
677
0
  r = sc_pin_cmd(card, &data);
678
0
  auth_info->tries_left = data.pin1.tries_left;
679
0
  if (r == SC_SUCCESS)
680
0
    sc_pkcs15_pincache_add(p15card, pin_obj, newpin, newpinlen);
681
682
0
out:
683
0
  sc_unlock(card);
684
0
  LOG_FUNC_RETURN(ctx, r);
685
0
}
686
687
int sc_pkcs15_get_pin_info(struct sc_pkcs15_card *p15card,
688
       struct sc_pkcs15_object *pin_obj)
689
240
{
690
240
  int r;
691
240
  struct sc_pin_cmd_data data;
692
240
  struct sc_card *card = p15card->card;
693
240
  struct sc_context *ctx = card->ctx;
694
240
  struct sc_pkcs15_auth_info *pin_info = (struct sc_pkcs15_auth_info *) pin_obj->data;
695
696
240
  LOG_FUNC_CALLED(ctx);
697
698
240
  r = sc_lock(card);
699
240
  if (r != SC_SUCCESS)
700
0
    return r;
701
702
240
  if (pin_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)   {
703
0
    r = SC_ERROR_INVALID_DATA;
704
0
    goto out;
705
0
  }
706
707
  /* the path in the pin object is optional */
708
240
  if ((pin_info->path.len > 0) || ((pin_info->path.aid.len > 0))) {
709
94
    r = sc_select_file(card, &pin_info->path, NULL);
710
94
    if (r)
711
29
      goto out;
712
94
  }
713
714
  /* Try to update PIN info from card */
715
211
  memset(&data, 0, sizeof(data));
716
211
  data.cmd = SC_PIN_CMD_GET_INFO;
717
211
  data.pin_type = pin_info->auth_method;
718
211
  data.pin_reference = pin_info->attrs.pin.reference;
719
720
211
  r = sc_pin_cmd(card, &data);
721
211
  if (r == SC_SUCCESS) {
722
144
    if (data.pin1.max_tries > 0)
723
0
      pin_info->max_tries = data.pin1.max_tries;
724
144
    pin_info->tries_left = data.pin1.tries_left;
725
144
    pin_info->logged_in = data.pin1.logged_in;
726
144
  }
727
728
240
out:
729
240
  sc_unlock(card);
730
240
  LOG_FUNC_RETURN(ctx, r);
731
240
}
732
733
734
void sc_pkcs15_free_auth_info(sc_pkcs15_auth_info_t *auth_info)
735
6.53k
{
736
6.53k
  free(auth_info);
737
6.53k
}
738
739
740
/* Add a PIN to the PIN cache related to the card. Some operations can trigger re-authentication later. */
741
void sc_pkcs15_pincache_add(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj,
742
  const u8 *pin, size_t pinlen)
743
97
{
744
97
  struct sc_context *ctx = p15card->card->ctx;
745
97
  struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
746
97
  struct sc_pkcs15_object *obj = NULL;
747
97
  int r;
748
749
97
  LOG_FUNC_CALLED(ctx);
750
751
97
  if (!pin || !pinlen)   {
752
0
    sc_log(ctx, "No cache for zero length PIN");
753
0
    return;
754
0
  }
755
97
  else if (!p15card->opts.use_pin_cache)   {
756
1
    sc_log(ctx, "PIN caching not enabled");
757
1
    return;
758
1
  }
759
96
  else if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)   {
760
0
    sc_log(ctx, "only 'PIN' auth. object can be cached");
761
0
    return;
762
0
  }
763
764
  /* If the PIN protects an object with user consent, don't cache it */
765
766
96
  obj = p15card->obj_list;
767
161
  while (obj != NULL) {
768
    /* Compare 'sc_pkcs15_object.auth_id' with 'sc_pkcs15_pin_info.auth_id'.
769
     * In accordance with PKCS#15 "6.1.8 CommonObjectAttributes" and
770
     * "6.1.16 CommonAuthenticationObjectAttributes" with the exception that
771
     * "CommonObjectAttributes.accessControlRules" are not taken into account. */
772
773
65
    if (sc_pkcs15_compare_id(&obj->auth_id, &auth_info->auth_id)) {
774
      /* Caching is refused, if the protected object requires user consent */
775
28
      if (!p15card->opts.pin_cache_ignore_user_consent) {
776
28
        if (obj->user_consent > 0) {
777
0
          sc_log(ctx, "caching refused (user consent)");
778
0
          return;
779
0
        }
780
28
      }
781
28
    }
782
783
65
    obj = obj->next;
784
65
  }
785
786
96
  r = sc_pkcs15_allocate_object_content(ctx, pin_obj, pin, pinlen);
787
96
  if (r != SC_SUCCESS)   {
788
0
    sc_log(ctx, "Failed to allocate object content");
789
0
    return;
790
0
  }
791
792
96
  pin_obj->usage_counter = 0;
793
96
  sc_log(ctx, "PIN(%s) cached", pin_obj->label);
794
96
}
795
796
/* Validate the PIN code associated with an object */
797
int
798
sc_pkcs15_pincache_revalidate(struct sc_pkcs15_card *p15card, const sc_pkcs15_object_t *obj)
799
0
{
800
0
  struct sc_context *ctx = p15card->card->ctx;
801
0
  sc_pkcs15_object_t *pin_obj;
802
0
  int r;
803
804
0
  LOG_FUNC_CALLED(ctx);
805
0
  if (!p15card->opts.use_pin_cache)
806
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
807
808
  /*  Apps that do not support CK_ALWAYS_AUTHENTICATE
809
   *  may need pin_cache_ignore_user_consent = 1 */
810
0
  if (!p15card->opts.pin_cache_ignore_user_consent) {
811
0
      if (obj->user_consent)
812
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
813
0
  }
814
815
0
  if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
816
0
        || p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH))
817
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
818
819
0
  r = sc_pkcs15_find_pin_by_auth_id(p15card, &obj->auth_id, &pin_obj);
820
0
  if (r != SC_SUCCESS) {
821
0
    sc_log(ctx, "Could not find pin object for auth_id %s", sc_pkcs15_print_id(&obj->auth_id));
822
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
823
0
  }
824
825
0
  if (pin_obj->usage_counter >= p15card->opts.pin_cache_counter) {
826
0
    sc_pkcs15_free_object_content(pin_obj);
827
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
828
0
  }
829
830
0
  if (!pin_obj->content.value || !pin_obj->content.len)
831
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
832
833
0
  pin_obj->usage_counter++;
834
0
  r = _sc_pkcs15_verify_pin(p15card, pin_obj, pin_obj->content.value, pin_obj->content.len);
835
0
  if (r != SC_SUCCESS) {
836
    /* Ensure that wrong PIN isn't used again */
837
0
    sc_pkcs15_free_object_content(pin_obj);
838
839
0
    sc_log(ctx, "Verify PIN error %i", r);
840
0
    return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
841
0
  }
842
843
0
  LOG_FUNC_RETURN(ctx, SC_SUCCESS);
844
0
}
845
846
void sc_pkcs15_pincache_clear(struct sc_pkcs15_card *p15card)
847
4.41k
{
848
4.41k
  struct sc_pkcs15_object *objs[32];
849
4.41k
  int i, r;
850
851
4.41k
  LOG_FUNC_CALLED(p15card->card->ctx);
852
4.41k
  r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, objs, 32);
853
5.88k
  for (i = 0; i < r; i++)
854
1.46k
    sc_pkcs15_free_object_content(objs[i]);
855
4.41k
}
856