Coverage Report

Created: 2026-06-10 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/libopensc/card-piv.c
Line
Count
Source
1
/*
2
 * card-piv.c: Support for PIV-II from NIST SP800-73
3
 * card-default.c: Support for cards with no driver
4
 *
5
 * Copyright (C) 2001, 2002  Juha Yrjölä <juha.yrjola@iki.fi>
6
 * Copyright (C) 2005-2026  Douglas E. Engert <deengert@gmail.com>
7
 * Copyright (C) 2006, Identity Alliance, Thomas Harning <thomas.harning@identityalliance.com>
8
 * Copyright (C) 2007, EMC, Russell Larner <rlarner@rsa.com>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
#ifdef HAVE_CONFIG_H
26
#include "config.h"
27
#endif
28
29
#include <ctype.h>
30
#include <fcntl.h>
31
#include <limits.h>
32
#include <stdlib.h>
33
#include <string.h>
34
35
#ifdef _WIN32
36
#include <io.h>
37
#else
38
#include <unistd.h>
39
#endif
40
41
#ifdef ENABLE_OPENSSL
42
/* openssl needed for card administration and SM */
43
#include <openssl/evp.h>
44
#include <openssl/pem.h>
45
#include <openssl/rand.h>
46
#include <openssl/sha.h>
47
#if !defined(OPENSSL_NO_EC)
48
#include <openssl/ec.h>
49
#endif
50
#endif
51
52
#include "common/compat_strlcat.h"
53
#include "common/compat_strlcpy.h"
54
#include "internal.h"
55
56
/* 800-73-4 SM and VCI need: ECC, SM and OpenSSL or LibreSSL */
57
#if defined(ENABLE_PIV_SM)
58
#include "compression.h"
59
#include <openssl/cmac.h>
60
#endif
61
62
#include "asn1.h"
63
#include "cardctl.h"
64
#include "simpletlv.h"
65
66
// clang-format off
67
68
enum {
69
  PIV_OBJ_CCC = 0,
70
  PIV_OBJ_CHUI,
71
  /*  PIV_OBJ_UCHUI is not in new with 800-73-2 */
72
  PIV_OBJ_X509_PIV_AUTH,
73
  PIV_OBJ_CHF,
74
  PIV_OBJ_PI,
75
  PIV_OBJ_CHFI,
76
  PIV_OBJ_X509_DS,
77
  PIV_OBJ_X509_KM,
78
  PIV_OBJ_X509_CARD_AUTH,
79
  PIV_OBJ_SEC_OBJ,
80
  PIV_OBJ_DISCOVERY,
81
  PIV_OBJ_HISTORY,
82
  PIV_OBJ_RETIRED_X509_1,
83
  PIV_OBJ_RETIRED_X509_2,
84
  PIV_OBJ_RETIRED_X509_3,
85
  PIV_OBJ_RETIRED_X509_4,
86
  PIV_OBJ_RETIRED_X509_5,
87
  PIV_OBJ_RETIRED_X509_6,
88
  PIV_OBJ_RETIRED_X509_7,
89
  PIV_OBJ_RETIRED_X509_8,
90
  PIV_OBJ_RETIRED_X509_9,
91
  PIV_OBJ_RETIRED_X509_10,
92
  PIV_OBJ_RETIRED_X509_11,
93
  PIV_OBJ_RETIRED_X509_12,
94
  PIV_OBJ_RETIRED_X509_13,
95
  PIV_OBJ_RETIRED_X509_14,
96
  PIV_OBJ_RETIRED_X509_15,
97
  PIV_OBJ_RETIRED_X509_16,
98
  PIV_OBJ_RETIRED_X509_17,
99
  PIV_OBJ_RETIRED_X509_18,
100
  PIV_OBJ_RETIRED_X509_19,
101
  PIV_OBJ_RETIRED_X509_20,
102
  PIV_OBJ_IRIS_IMAGE,
103
  PIV_OBJ_BITGT,
104
  PIV_OBJ_SM_CERT_SIGNER,
105
  PIV_OBJ_PCRDCS,
106
  PIV_OBJ_9B03,
107
  PIV_OBJ_9A06,
108
  PIV_OBJ_9C06,
109
  PIV_OBJ_9D06,
110
  PIV_OBJ_9E06,
111
  PIV_OBJ_8206,
112
  PIV_OBJ_8306,
113
  PIV_OBJ_8406,
114
  PIV_OBJ_8506,
115
  PIV_OBJ_8606,
116
  PIV_OBJ_8706,
117
  PIV_OBJ_8806,
118
  PIV_OBJ_8906,
119
  PIV_OBJ_8A06,
120
  PIV_OBJ_8B06,
121
  PIV_OBJ_8C06,
122
  PIV_OBJ_8D06,
123
  PIV_OBJ_8E06,
124
  PIV_OBJ_8F06,
125
  PIV_OBJ_9006,
126
  PIV_OBJ_9106,
127
  PIV_OBJ_9206,
128
  PIV_OBJ_9306,
129
  PIV_OBJ_9406,
130
  PIV_OBJ_9506,
131
  PIV_OBJ_LAST_ENUM
132
};
133
134
/*
135
 * Flags in the piv_obj_cache:
136
 * PIV_OBJ_CACHE_VALID means the data in the cache can be used.
137
 * It might have zero length indicating that the object was not found.
138
 * PIV_OBJ_CACHE_NOT_PRESENT means do not even try to read the object.
139
 * Either because the object did not parse or
140
 * these objects will only be present if the history object says
141
 * they are on the card, or the discovery or history object in not present.
142
 * If the file listed in the history object offCardCertURL was found,
143
 * its certs will be read into the cache and PIV_OBJ_CACHE_VALID set
144
 * and PIV_OBJ_CACHE_NOT_PRESENT unset.
145
 *
146
 */
147
148
7.62k
#define PIV_OBJ_CACHE_VALID   1
149
1.31k
#define PIV_OBJ_CACHE_COMPRESSED  2
150
131k
#define PIV_OBJ_CACHE_NOT_PRESENT 8
151
152
typedef struct piv_obj_cache {
153
  u8* obj_data;
154
  size_t obj_len;
155
  u8* internal_obj_data; /* like a cert in the object */
156
  size_t internal_obj_len;
157
  int flags;
158
} piv_obj_cache_t;
159
160
enum {
161
  PIV_STATE_NORMAL = 0,
162
  PIV_STATE_MATCH,
163
  PIV_STATE_INIT
164
};
165
166
/* ccc_flags */
167
46
#define PIV_CCC_FOUND   0x00000001
168
12
#define PIV_CCC_F0_PIV    0x00000002
169
11
#define PIV_CCC_F0_CAC    0x00000004
170
8
#define PIV_CCC_F0_JAVA   0x00000008
171
397
#define PIV_CCC_F3_CAC_PKI  0x00000010
172
173
46
#define PIV_CCC_TAG_F0    0xF0
174
40
#define PIV_CCC_TAG_F3    0xF3
175
176
/* 800-73-4 Cipher Suite Table 14 */
177
#define PIV_CS_CS2    0x27
178
#define PIV_CS_CS7    0x2E
179
180
#ifdef ENABLE_PIV_SM
181
#ifdef USE_OPENSSL3_LIBCTX
182
#define PIV_LIBCTX card->ctx->ossl3ctx->libctx
183
#else
184
#define PIV_LIBCTX NULL
185
#endif
186
187
  /* Table 14 and other constants */
188
  typedef struct cipher_suite {
189
    u8 id; /* taken from AID "AC" tag */
190
    int field_length;
191
    int nid;     /* for OpenSSL curves */
192
    struct sc_object_id oid; /* for opensc */
193
    int p1;      /* for APDU */
194
    size_t Qlen; /* size of pubkey 04||x||y for all keys */
195
    size_t AuthCryptogramlen; /* both H and ICC must match */
196
    size_t Zlen; /* size of shared secret from ECDH */
197
    size_t otherinfolen; /* used in 4.1.6  Key Derivation */
198
199
    int o0len; /* first in otherinfo */
200
    u8 o0_char;
201
    size_t CBhlen;
202
    size_t T16Qehlen;
203
    size_t IDsicclen;
204
    size_t Nicclen;
205
    size_t CBicclen; /* last in otherinfo */
206
207
    int naeskeys; /* number of aes key generated */
208
    int aeskeylen; /* size of aes key bytes*/
209
    int kdf_hash_size; /* size of hash in bytes */
210
    EVP_MD *(*kdf_md)(void);
211
    const EVP_CIPHER *(*cipher_cbc)(void);
212
    const EVP_CIPHER *(*cipher_ecb)(void);
213
    char *cipher_cbc_name;
214
    char *cipher_ecb_name;
215
    char *curve_group;
216
  } cipher_suite_t;
217
218
#define PIV_CSS_SIZE 2
219
static cipher_suite_t css[PIV_CSS_SIZE] = {
220
    {PIV_CS_CS2, 256, NID_X9_62_prime256v1, {{1, 2, 840, 10045, 3, 1, 7, -1}},
221
    PIV_CS_CS2, 65, 16, 32, 61,
222
    4, 0x09, 1, 16, 8, 16, 1,
223
    4, 128/8, SHA256_DIGEST_LENGTH,
224
    (EVP_MD *(*)(void)) EVP_sha256,
225
    (const EVP_CIPHER *(*)(void)) EVP_aes_128_cbc,
226
    (const EVP_CIPHER *(*)(void)) EVP_aes_128_ecb,
227
    "aes-128-cbc", "aes-128-ecb",
228
    "prime256v1"},
229
230
    {PIV_CS_CS7, 384, NID_secp384r1, {{1, 3, 132, 0, 34, -1}},
231
    PIV_CS_CS7, 97, 16, 48, 69,
232
    4, 0x0D, 1, 16, 8, 24, 1,
233
    4, 256/8, SHA384_DIGEST_LENGTH,
234
    (EVP_MD *(*)(void)) EVP_sha384,
235
    (const EVP_CIPHER *(*)(void)) EVP_aes_256_cbc,
236
    (const EVP_CIPHER *(*)(void)) EVP_aes_256_ecb,
237
    "aes-256-cbc", "aes-256-ecb",
238
    "secp384r1"}
239
  };
240
241
/* 800-73-4  4.1.5 Card Verifiable Certificates */
242
typedef struct piv_cvc {
243
  sc_pkcs15_der_t der;          // Previous read der
244
  int cpi;            // Certificate profile indicator (0x80)
245
  char issuerID[8];         // Issuer Identification Number
246
  size_t issuerIDlen;         //  8 bytes of sha-1 or 16 byte for GUID
247
  u8  subjectID[16];          //  Subject Identifier (8) or GUID (16)  == CHUI
248
  size_t subjectIDlen;          //  8 bytes of sha-1 or 16 byte for GUID
249
  struct sc_object_id pubKeyOID;        // Public key algorithm object identifier
250
  u8 *publicPoint;          // Public point for ECC
251
  size_t publicPointlen;
252
  int roleID;           // Role Identifier 0x00 or 0x12
253
  u8 *body;           // signed part of CVC in DER
254
  size_t bodylen;
255
  struct sc_object_id  signatureAlgOID;     // Signature Algroithm Identifier
256
  u8 *signature;            // Certificate signature DER
257
  size_t signaturelen;
258
} piv_cvc_t;
259
260
#define PIV_SM_MAX_FIELD_LENGTH  384
261
#define PIV_SM_MAX_MD_LENGTH  SHA384_DIGEST_LENGTH
262
263
#define PIV_SM_FLAGS_SM_CERT_SIGNER_VERIFIED  0x000000001lu
264
#define PIV_SM_FLAGS_SM_CVC_VERIFIED    0X000000002lu
265
#define PIV_SM_FLAGS_SM_IN_CVC_VERIFIED   0x000000004lu
266
#define PIV_SM_FLAGS_SM_CERT_SIGNER_PRESENT 0x000000010lu
267
#define PIV_SM_FLAGS_SM_CVC_PRESENT   0X000000020lu
268
#define PIV_SM_FLAGS_SM_IN_CVC_PRESENT    0x000000040lu
269
#define PIV_SM_FLAGS_SM_IS_ACTIVE   0x000000080lu /* SM has been started */
270
  /* if card supports SP800-73-4 SM: */
271
#define PIV_SM_FLAGS_NEVER      0x000000100lu /* Don't use SM even if card support it */
272
                /* Default is use if card supports it */
273
                /* will use VCI if card supports it for contactless */
274
#define PIV_SM_FLAGS_ALWAYS     0x000000200lu /* Use SM or quit, VCI requires SM */
275
#define PIV_SM_FLAGS_DEFER_OPEN     0x000001000lu /* call sm_open from reader_lock_obtained */
276
#define PIV_SM_VCI_ACTIVE     0x000002000lu   /* VCI is active */
277
#define PIV_SM_GET_DATA_IN_CLEAR    0x000004000lu /* OK to do this GET DATA in the clear */
278
279
typedef struct piv_sm_session {
280
  /* set by piv_sm_open */
281
  int aes_size; /* 128 or 256 */
282
283
  u8 SKcfrm[32];
284
  u8 SKmac[32];
285
  u8 SKenc[32];  /* keys are either AES 128 or AES 256 */
286
  u8 SKrmac[32];
287
  u8 enc_counter[16];
288
  u8 enc_counter_last[16];
289
290
  u8 resp_enc_counter[16];
291
  u8 C_MCV[16];
292
  u8 C_MCV_last[16];
293
  u8 R_MCV[16];
294
  u8 R_MCV_last[16];
295
} piv_sm_session_t;
296
297
#define C_ASN1_PIV_CVC_PUBKEY_SIZE 3
298
  /* ECC key only */
299
static const struct sc_asn1_entry c_asn1_piv_cvc_pubkey[C_ASN1_PIV_CVC_PUBKEY_SIZE] = {
300
  { "publicKeyOID", SC_ASN1_OBJECT, SC_ASN1_UNI | SC_ASN1_OBJECT, 0, NULL, NULL },
301
  { "publicPoint", SC_ASN1_OCTET_STRING, SC_ASN1_CTX | 6, SC_ASN1_OPTIONAL | SC_ASN1_ALLOC, NULL, NULL },
302
  { NULL, 0, 0, 0, NULL, NULL }
303
};
304
305
#define C_ASN1_PIV_CVC_DSOBJ_SIZE 2
306
static const struct sc_asn1_entry c_asn1_piv_cvc_dsobj[C_ASN1_PIV_CVC_DSOBJ_SIZE] = {
307
  { "DigitalSignature", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_TAG_SEQUENCE, 0, NULL, NULL },
308
  { NULL, 0, 0, 0, NULL, NULL }
309
};
310
311
#define C_ASN1_PIV_CVC_DSSIG_SIZE 3
312
static const struct sc_asn1_entry c_asn1_piv_cvc_dssig[C_ASN1_PIV_CVC_DSSIG_SIZE] = {
313
  { "signatureAlgorithmID", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_TAG_SEQUENCE, 0, NULL, NULL },
314
  { "signatureValue", SC_ASN1_BIT_STRING_NI, SC_ASN1_TAG_BIT_STRING, SC_ASN1_ALLOC, NULL, NULL },
315
  { NULL, 0, 0, 0, NULL, NULL }
316
};
317
318
#define C_ASN1_PIV_CVC_ALG_ID_SIZE 3
319
static const struct sc_asn1_entry c_asn1_piv_cvc_alg_id[C_ASN1_PIV_CVC_ALG_ID_SIZE] = {
320
  { "signatureAlgorithmOID", SC_ASN1_OBJECT, SC_ASN1_UNI | SC_ASN1_OBJECT, 0, NULL, NULL },
321
  { "nullParam",  SC_ASN1_NULL, SC_ASN1_UNI | SC_ASN1_TAG_NULL, SC_ASN1_OPTIONAL, NULL, NULL },
322
  { NULL, 0, 0, 0, NULL, NULL }
323
};
324
325
#define C_ASN1_PIV_CVC_BODY_SIZE 7
326
static const struct sc_asn1_entry c_asn1_piv_cvc_body[C_ASN1_PIV_CVC_BODY_SIZE] = {
327
  { "certificateProfileIdentifier", SC_ASN1_INTEGER, SC_ASN1_APP | 0x1F29, 0, NULL, NULL },
328
  { "Issuer ID Number", SC_ASN1_OCTET_STRING, SC_ASN1_APP | 2, 0, NULL, NULL },
329
  { "Subject Identifier", SC_ASN1_OCTET_STRING, SC_ASN1_APP | 0x1F20, 0, NULL, NULL },
330
  { "publicKey", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F49, 0, NULL, NULL },
331
  { "roleIdentifier", SC_ASN1_CALLBACK, SC_ASN1_APP | 0x1F4C, 0, NULL, NULL },
332
  /* signature is over the above 5 entries  treat roleIdentifier special to get end */
333
  { "DSignatureObject", SC_ASN1_STRUCT, SC_ASN1_APP | 0x1F37, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, NULL, NULL },
334
  { NULL, 0, 0, 0, NULL, NULL }
335
};
336
337
338
#define C_ASN1_PIV_CVC_SIZE 2
339
static const struct sc_asn1_entry c_asn1_piv_cvc[C_ASN1_PIV_CVC_SIZE] = {
340
  { "CVC certificate", SC_ASN1_STRUCT, SC_ASN1_CONS | SC_ASN1_APP | 0x1F21, 0, NULL, NULL },
341
  { NULL, 0, 0, 0, NULL, NULL }
342
};
343
344
#define C_ASN1_PIV_SM_RESPONSE_SIZE 4
345
static const struct sc_asn1_entry c_asn1_sm_response[C_ASN1_PIV_SM_RESPONSE_SIZE] = {
346
  { "encryptedData",      SC_ASN1_CALLBACK,   SC_ASN1_CTX | 7,        SC_ASN1_OPTIONAL,       NULL, NULL },
347
  { "statusWord",         SC_ASN1_CALLBACK,   SC_ASN1_CTX | 0x19,     0,                      NULL, NULL },
348
  { "mac",                SC_ASN1_CALLBACK,   SC_ASN1_CTX | 0x0E,     0,                      NULL, NULL },
349
  { NULL, 0, 0, 0, NULL, NULL }
350
};
351
352
/*
353
 * SW internal apdu response table.
354
 *
355
 * Override APDU response error codes from iso7816.c to allow
356
 * handling of SM specific error
357
 */
358
static const struct sc_card_error piv_sm_errors[] = {
359
  {0x6882, SC_ERROR_SM, "SM not supported"},
360
  {0x6982, SC_ERROR_SM_NO_SESSION_KEYS, "SM Security status not satisfied"}, /* no session established */
361
  {0x6987, SC_ERROR_SM, "Expected SM Data Object missing"},
362
  {0x6988, SC_ERROR_SM_INVALID_SESSION_KEY, "SM Data Object incorrect"}, /* other process interference */
363
  {0, 0, NULL}
364
};
365
#endif /* ENABLE_PIV_SM */
366
367
/* 800-73-4 3.3.2 Discovery Object - PIN Usage Policy */
368
22
#define PIV_PP_PIN    0x00004000u
369
22
#define PIV_PP_GLOBAL   0x00002000u
370
#define PIV_PP_OCC    0x00001000u
371
#define PIV_PP_VCI_IMPL   0x00000800u
372
#define PIV_PP_VCI_WITHOUT_PC 0x00000400u
373
#define PIV_PP_PIV_PRIMARY  0x00000010u
374
0
#define PIV_PP_GLOBAL_PRIMARY 0x00000020u
375
376
/* init_flags */
377
1.66k
#define PIV_INIT_AID_PARSED     0x00000001u
378
382
#define PIV_INIT_AID_AC_SM      0x00000002u
379
589
#define PIV_INIT_DISCOVERY_PARSED   0x00000004u
380
11
#define PIV_INIT_DISCOVERY_PP     0x00000008u
381
1.75k
#define PIV_INIT_IN_READER_LOCK_OBTAINED  0x00000010u
382
346
#define PIV_INIT_CONTACTLESS      0x00000020u
383
384
#define PIV_PAIRING_CODE_LEN   8
385
386
typedef struct piv_private_data {
387
  struct sc_lv_data aid_der; /* previous aid response to compare */
388
  int enumtag;
389
  int max_object_size; /* use setable option. In case objects get bigger */
390
  int selected_obj; /* The index into the piv_objects last selected */
391
  int return_only_cert; /* return the cert from the object */
392
  int rwb_state; /* first time -1, 0, in middle, 1 at eof */
393
  int operation; /* saved from set_security_env */
394
  unsigned long algorithm; /* saved from set_security_env */
395
  int key_ref; /* saved from set_security_env and */
396
  int alg_id;  /* used in decrypt, signature, derive */
397
  unsigned int alg_ids; /* from AID response, NIST required, card version */
398
  unsigned char * al_label; /* from Select AID Application Label */
399
  int al_labellen;
400
  int key_size; /*  RSA: modulus_bits EC: field_length in bits */
401
  u8* w_buf;   /* write_binary buffer */
402
  size_t w_buf_len; /* length of w_buff */
403
  piv_obj_cache_t obj_cache[PIV_OBJ_LAST_ENUM];
404
  int keysWithOnCardCerts;
405
  int keysWithOffCardCerts;
406
  char * offCardCertURL;
407
  int pin_preference; /* set from Discovery object */
408
  int logged_in;
409
  int pstate;
410
  int pin_cmd_verify;
411
  int context_specific;
412
  unsigned int pin_cmd_verify_sw1;
413
  unsigned int pin_cmd_verify_sw2;
414
  int tries_left; /* SC_PIN_CMD_GET_INFO tries_left from last */
415
  unsigned int card_issues; /* card_issues flags for this card */
416
  int object_test_verify; /* Can test this object to set verification state of card */
417
  int yubico_version; /* 3 byte version number of NEO or Yubikey4  as integer */
418
  unsigned int ccc_flags;     /* From  CCC indicate if CAC card */
419
  unsigned int pin_policy; /* from discovery */
420
  unsigned int init_flags;
421
  u8  csID; /* 800-73-4 Cipher Suite ID 0x27 or 0x2E */
422
#ifdef ENABLE_PIV_SM
423
  cipher_suite_t *cs; /* active cipher_suite */
424
  piv_cvc_t sm_cvc;  /* 800-73-4:  SM CVC Table 15 */
425
  piv_cvc_t sm_in_cvc; /* Intermediate CVC Table 16 */
426
  unsigned long  sm_flags;
427
  unsigned char pairing_code[PIV_PAIRING_CODE_LEN]; /* 8 ASCII digits */
428
  piv_sm_session_t sm_session;
429
#endif /* ENABLE_PIV_SM */
430
} piv_private_data_t;
431
432
377k
#define PIV_DATA(card) ((piv_private_data_t*)card->drv_data)
433
434
struct piv_aid {
435
  int enumtag;
436
  size_t len_short; /* min length without version */
437
  size_t len_long;  /* With version and other stuff */
438
  u8 *value;
439
};
440
441
/*
442
 * The Generic AID entry should be the "A0 00 00 03 08 00 00 10 00 "
443
 * NIST published 800-73 on 10/6/2005
444
 * 800-73-1 March 2006 included Errata
445
 * 800-73-2 Part 1 implies  version is  "02 00"
446
 * i.e. "A0 00 00 03 08 00 00 01 00 02 00".
447
 * but we don't need the version number. But could get it from the PIX.
448
 * Discovery object was added.
449
 *
450
 * 800-73-3 Part 1 now refers to "01 00" i.e. going back to 800-73-1.
451
 * The main differences between 73-2, and 73-3 are the addition of the
452
 * key History object, certs and keys and Iris objects.
453
 * These can be discovered using GET DATA
454
455
 * 800-73-4 Has many changes, including optional Secure Messaging,
456
 * optional Virtual Contact Interface and pairing code.
457
 */
458
459
/* ATRs of cards known to have PIV applet. But must still be tested for a PIV applet */
460
/* Yubico, NitroKey, Token2 are identified by ATR historic bytes */
461
/* PivApplet does not provide ATR historic bytes */
462
463
static const struct sc_atr_table piv_atrs[] = {
464
  /* CAC cards with PIV from: CAC-utilziation-and-variation-matrix-v2.03-20May2016.doc */
465
  /*
466
   * https://www.cac.mil/Common-Access-Card/Developer-Resources/
467
   * https://www.cac.mil/Portals/53/Documents/DoD%20Token%20utilziation%20and%20variation%20matrix%20v2_06_17October2019.docx?ver=2019-10-18-102519-120
468
   */
469
  /* Oberthur Card Systems (PIV Endpoint) with PIV endpoint applet and PIV auth cert OBSOLETE */
470
  { "3B:DB:96:00:80:1F:03:00:31:C0:64:77:E3:03:00:82:90:00:C1", NULL, NULL, SC_CARD_TYPE_PIV_II_OBERTHUR, 0, NULL },
471
472
  /* Gemalto (PIV Endpoint) with PIV endpoint applet and PIV auth cert OBSOLETE */
473
  { "3B 7D 96 00 00 80 31 80 65 B0 83 11 13 AC 83 00 90 00", NULL, NULL, SC_CARD_TYPE_PIV_II_GEMALTO, 0, NULL },
474
475
  /* Gemalto (PIV Endpoint) 2 entries  2016, 2019 */
476
  { "3B:7D:96:00:00:80:31:80:65:B0:83:11:17:D6:83:00:90:00", NULL, NULL, SC_CARD_TYPE_PIV_II_GEMALTO, 0, NULL },
477
478
  /* Oberthur Card System (PIV Endpoint)  2 entries 2016, 2019 */
479
  { "3B:DB:96:00:80:1F:03:00:31:C0:64:B0:F3:10:00:07:90:00:80", NULL, NULL, SC_CARD_TYPE_PIV_II_OBERTHUR, 0, NULL },
480
  /* Oberthur Card System  with LCS 0F - Some VA cards have Terminated state */
481
  { "3B:DB:96:00:80:1F:03:00:31:C0:64:B0:F3:10:00:0F:90:00:88", NULL, NULL, SC_CARD_TYPE_PIV_II_OBERTHUR, 0, NULL },
482
483
  /* Giesecke & Devrient (PIV Endpoint)  2 entries 2016, 2019 */
484
  { "3B:7A:18:00:00:73:66:74:65:20:63:64:31:34:34", NULL, NULL, SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC, 0, NULL },
485
  /* Giesecke & Devrient (CAC PIV Endpoint) 2019 */
486
  { "3B:F9:18:00:00:00:53:43:45:37:20:03:00:20:46", NULL, NULL, SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC, 0, NULL },
487
488
  /* IDEMIA (new name for Oberthur) (DoD Alternate Token IDEMIA Cosmo V8.0 2019*/
489
  { "3B:D8:18:00:80:B1:FE:45:1F:07:80:31:C1:64:08:06:92:0F:D5", NULL, NULL, SC_CARD_TYPE_PIV_II_OBERTHUR, 0, NULL },
490
  { "3b:86:80:01:80:31:c1:52:41:1a:7e", NULL, NULL, SC_CARD_TYPE_PIV_II_OBERTHUR, 0, NULL }, /* contactless */
491
492
  /* Following PIVKEY entries are from Windows registry provided by gw@taglio.com 2022-09-05 */
493
  /* PIVKEY PIVKey Feitian (02) */
494
  { "3b:9f:95:81:31:fe:9f:00:66:46:53:05:10:00:11:71:df:00:00:00:00:00:02", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
495
  /* PIVKey Feitian (7C)  aka C910 contactless */
496
  { "3b:8c:80:01:90:67:46:4a:00:64:16:06:f2:72:7e:00:7c", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
497
  /*PIVKey Feitian (E0)  aka C910 */
498
  { "3b:fc:18:00:00:81:31:80:45:90:67:46:4a:00:64:16:06:f2:72:7e:00:e0", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
499
  /* PIVKey Feitian (FE)  aka PIVKEY T600 token and T800  on Feitian eJAVA */
500
  { "3b:fc:18:00:00:81:31:80:45:90:67:46:4a:00:64:2d:70:c1:72:fe:e0:fe", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
501
  /* PIVKey JCOP241 (AD) */
502
  { "3b:f9:13:00:00:81:31:fe:45:53:50:49:56:4b:45:59:37:30:ad", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
503
  /* PIVKey JCOP242R2 (16) */
504
  { "3b:88:80:01:50:49:56:4b:45:59:37:30:16", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
505
  /* PIVKey JCOP242R2 (5E) */
506
  { "3b:88:80:01:4a:43:4f:50:76:32:34:31:5e", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
507
  /* PIVKey JCOP242R2 (B7) */
508
  { "3b:f8:13:00:00:81:31:fe:45:4a:43:4f:50:76:32:34:31:b7", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
509
  /* PIVKey JCOP3 (67) */
510
  { "3b:88:80:01:46:49:44:45:53:4d:4f:31:67", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
511
  /* PIVKey JCOP3 (8E) */
512
  { "3b:f8:13:00:00:81:31:fe:45:46:49:44:45:53:4d:4f:31:8e", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
513
  /* PIVKey JCOP31 (57) */
514
  { "3b:f9:18:00:ff:81:31:fe:45:4a:43:4f:50:33:31:56:32:32:57", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
515
  /* PIVKey NXP JCOP (03) */
516
  { "3b:8a:80:01:01:50:49:56:4b:45:59:37:30:16:03", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
517
  /* PIVKey NXP JCOP (FF)  aka CP70 */
518
  { "3b:f8:13:00:00:81:31:fe:45:50:49:56:4b:45:59:37:30:ff", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
519
  /* PIVKey SLE78 (3B) */
520
  { "3b:8d:80:01:53:4c:4a:35:32:47:44:4c:31:32:38:43:52:3b", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
521
  /* PIVKey SLE78 (6D) */
522
  { "3b:88:80:01:00:00:00:11:77:81:83:00:6d", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
523
  /* PIVKey SLE78 (28) aka C980 */
524
  { "3b:f9:96:00:00:81:31:fe:45:53:50:49:56:4b:45:59:37:30:28", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
525
  /* PIVKey SLE78 (44) aka C980 contactless */
526
  { "3b:89:80:01:53:50:49:56:4b:45:59:37:30:44", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
527
  /* PIVKey SLE78 (57B) */
528
  { "3b:fd:96:00:00:81:31:fe:45:53:4c:4a:35:32:47:44:4c:31:32:38:43:52:57", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
529
  /* This ATR is too generic. It will be accepted if Application Label in AID response matches */
530
  /* Which looks like there is no Application Label in AID response of these cards */
531
  /* PivApplet (at least when run in virtual machine) uses the same ATR */
532
  /* PIVKey uTrust (01) ISO 14443 Type B without historical bytes */
533
  /* { "3b:80:80:01:01", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL }, */
534
535
  /* PIVKey uTrust (73) */
536
  { "3b:96:11:81:21:75:75:54:72:75:73:74:73", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
537
  /* PIVKey uTrust FIDO2 (73) */
538
  { "3b:96:11:81:21:75:75:54:72:75:73:74:73", NULL, NULL, SC_CARD_TYPE_PIV_II_PIVKEY, 0, NULL },
539
540
  /* Swissbit iShield Key Pro with PIV endpoint applet */
541
  { "3b:97:11:81:21:75:69:53:68:69:65:6c:64:05", NULL, NULL, SC_CARD_TYPE_PIV_II_SWISSBIT, 0, NULL },
542
543
  /* ID-One PIV 2.4.1 on Cosmo V8.1 NIST sp800-73-4 with Secure Messaging and VCI  2020 */
544
  { "3b:d6:96:00:81:b1:fe:45:1f:87:80:31:c1:52:41:1a:2a", NULL, NULL, SC_CARD_TYPE_PIV_II_800_73_4, 0, NULL },
545
  { "3b:d6:97:00:81:b1:fe:45:1f:87:80:31:c1:52:41:12:23",
546
    "ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:00:00", NULL, SC_CARD_TYPE_PIV_II_800_73_4, 0, NULL },
547
  { "3b:86:80:01:80:31:c1:52:41:12:76", NULL, NULL, SC_CARD_TYPE_PIV_II_800_73_4, 0, NULL }, /* contactless */
548
549
  { NULL, NULL, NULL, 0, 0, NULL }
550
};
551
552
static struct piv_supported_ec_curves {
553
  struct sc_object_id oid;
554
  size_t size;
555
  unsigned int key_type;
556
} ec_curves[] = {
557
    {{{1, 2, 840, 10045, 3, 1, 7, -1}}, 256, SC_ALGORITHM_EC  }, /* secp256r1, nistp256, prime256v1, ansiX9p256r1 */
558
    {{{1, 3, 132, 0, 34, -1}},     384, SC_ALGORITHM_EC    }, /* secp384r1, nistp384, prime384v1, ansiX9p384r1 */
559
    {{{1, 3, 101, 112, -1}},     255, SC_ALGORITHM_EDDSA }, /* RFC8410 OID equivalent to ed25519 */
560
    {{{1, 3, 101, 110, -1}},     255, SC_ALGORITHM_XEDDSA}, /* RFC8410 OID equivalent to curve25519 */
561
    {{{-1}},          0,   0         }  /* This entry must not be touched. */
562
};
563
564
/* all have same AID */
565
static struct piv_aid piv_aids[] = {
566
  {SC_CARD_TYPE_PIV_II_GENERIC, /* Not really card type but what PIV AID is supported */
567
     9, 9, (u8 *) "\xA0\x00\x00\x03\x08\x00\x00\x10\x00" },
568
  {0,  9, 0, NULL }
569
};
570
571
/* card_issues - bugs in PIV implementations requires special handling */
572
10.4k
#define CI_VERIFY_630X          0x00000001U /* VERIFY tries left returns 630X rather then 63CX */
573
84
#define CI_VERIFY_LC0_FAIL        0x00000002U /* VERIFY Lc=0 never returns 90 00 if PIN not needed */
574
              /* will also test after first PIN verify if protected object can be used instead */
575
556
#define CI_NO_RANDOM          0x00000004U /* can not use Challenge to get random data or no 9B key */
576
0
#define CI_CANT_USE_GETDATA_FOR_STATE     0x00000008U /* No object to test verification inplace of VERIFY Lc=0 */
577
147
#define CI_LEAKS_FILE_NOT_FOUND       0x00000010U /* GET DATA of empty object returns 6A 82 even if PIN not verified */
578
1.65k
#define CI_DISCOVERY_USELESS        0x00000020U /* Discovery can not be used to query active AID invalid or no data returned */
579
599
#define CI_PIV_AID_LOSE_STATE       0x00000040U /* PIV AID can lose the login state run with out it*/
580
581
254
#define CI_OTHER_AID_LOSE_STATE       0x00000100U /* Other drivers match routines may reset our security state and lose AID!!! */
582
59
#define CI_NFC_EXPOSE_TOO_MUCH        0x00000200U /* PIN, crypto and objects exposed over NFS in violation of 800-73-3 */
583
584
2.25k
#define CI_NO_EC384         0x00000400U /* does not have EC 384 */
585
550
#define CI_NO_EC          0x00000800U /* No EC at all */
586
587
/* PIV and PIV like cards are know to support these asymmetric algorithm identifiers used in alg_ids */
588
1.64k
#define AI_RSA_1024         0x00000001U
589
1.64k
#define AI_RSA_2048         0x00000002U
590
1.64k
#define AI_RSA_3072         0x00000004U
591
597
#define AI_RSA_4096         0x00000008U
592
1.09k
#define AI_EC_256         0x00000100U
593
1.09k
#define AI_EC_384         0x00000200U
594
2.23k
#define AI_25519          0x00100000U
595
2.12k
#define AI_X25519         0x00200000U
596
/* NIST  cards have or do support these (non SM) algorithm identifiers */
597
1.09k
#define AI_NIST (AI_RSA_1024 | AI_RSA_2048 | AI_RSA_3072 | AI_EC_256 | AI_EC_384)
598
599
static struct {
600
  unsigned long ai_flag;
601
  unsigned char ai_id;
602
  unsigned long ai_sc_type;
603
  int ai_bits;
604
} ai_map[] = {
605
    { AI_RSA_3072, 0x05, SC_ALGORITHM_RSA, 3072 },
606
    { AI_RSA_1024, 0x06, SC_ALGORITHM_RSA, 1024 },
607
    { AI_RSA_2048, 0x07, SC_ALGORITHM_RSA, 2048 },
608
    { AI_RSA_4096, 0x16, SC_ALGORITHM_RSA,4096 },
609
    { AI_EC_256, 0x11, SC_ALGORITHM_EC, 256 },
610
    { AI_EC_384, 0x14, SC_ALGORITHM_EC, 384 },
611
    { AI_25519, 0xE0, SC_ALGORITHM_EDDSA, 255 },
612
    { AI_X25519, 0xE1, SC_ALGORITHM_XEDDSA, 255 },
613
    { 0, 0, 0, 0 }
614
  };
615
616
/* Application Label from select AID response where ATR can not be matched */
617
static struct {
618
  int al_type;
619
  unsigned char * al_label;
620
  int al_labellen;
621
} al_map[] = {
622
    { SC_CARD_TYPE_PIV_II_PIVAPPLET, (u8 *)"PivApplet v", 11 },
623
    { 0, NULL, 0 }
624
  };
625
626
/*
627
 * Flags in the piv_object:
628
 * PIV_OBJECT_NOT_PRESENT: the presents of the object is
629
 * indicated by the History object.
630
 */
631
632
431
#define PIV_OBJECT_TYPE_CERT    0x01
633
0
#define PIV_OBJECT_TYPE_PUBKEY    0x02
634
259k
#define PIV_OBJECT_NOT_PRESENT    0x04
635
#define PIV_OBJECT_TYPE_CVC   0x08 /* is in cert object */
636
#define PIV_OBJECT_NEEDS_PIN    0x10 /* On both contact and contactless */
637
#define PIV_OBJECT_NEEDS_VCI    0x20 /* NIST sp800-73-4 Requires VCI on contactless and card enforces this. */
638
               /* But also See CI_NFC_EXPOSE_TOO_MUCH for non approved PIV-like cards */
639
640
struct piv_object {
641
  int enumtag;
642
  const char * name;
643
  unsigned int resp_tag;
644
  const char * oidstring;
645
  size_t tag_len;
646
  u8  tag_value[3];
647
  u8  containerid[2]; /* will use as relative paths for simulation */
648
  int flags;              /* object has some internal object like a cert */
649
};
650
651
/* Must be in order, and one per enumerated PIV_OBJ */
652
static const struct piv_object piv_objects[] = {
653
  { PIV_OBJ_CCC, "Card Capability Container",
654
      SC_ASN1_APP | 0x13,
655
      "2.16.840.1.101.3.7.1.219.0", 3, {0x5F, 0xC1, 0x07}, {0xDB, 0x00}, PIV_OBJECT_NEEDS_VCI},
656
  { PIV_OBJ_CHUI, "Card Holder Unique Identifier",
657
      SC_ASN1_APP | 0x13,
658
      "2.16.840.1.101.3.7.2.48.0", 3, {0x5F, 0xC1, 0x02}, {0x30, 0x00}, 0},
659
  { PIV_OBJ_X509_PIV_AUTH, "X.509 Certificate for PIV Authentication",
660
      SC_ASN1_APP | 0x13,
661
      "2.16.840.1.101.3.7.2.1.1", 3, {0x5F, 0xC1, 0x05}, {0x01, 0x01}, PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI} ,
662
  { PIV_OBJ_CHF, "Card Holder Fingerprints",
663
      SC_ASN1_APP | 0x13,
664
      "2.16.840.1.101.3.7.2.96.16", 3, {0x5F, 0xC1, 0x03}, {0x60, 0x10}, PIV_OBJECT_NEEDS_PIN | PIV_OBJECT_NEEDS_VCI},
665
  { PIV_OBJ_PI, "Printed Information",
666
      SC_ASN1_APP | 0x13,
667
      "2.16.840.1.101.3.7.2.48.1", 3, {0x5F, 0xC1, 0x09}, {0x30, 0x01}, PIV_OBJECT_NEEDS_PIN | PIV_OBJECT_NEEDS_VCI},
668
  { PIV_OBJ_CHFI, "Cardholder Facial Images",
669
      SC_ASN1_APP | 0x13,
670
      "2.16.840.1.101.3.7.2.96.48", 3, {0x5F, 0xC1, 0x08}, {0x60, 0x30}, PIV_OBJECT_NEEDS_PIN | PIV_OBJECT_NEEDS_VCI},
671
  { PIV_OBJ_X509_DS, "X.509 Certificate for Digital Signature",
672
      SC_ASN1_APP | 0x13,
673
      "2.16.840.1.101.3.7.2.1.0", 3, {0x5F, 0xC1, 0x0A}, {0x01, 0x00}, PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
674
  { PIV_OBJ_X509_KM, "X.509 Certificate for Key Management",
675
      SC_ASN1_APP | 0x13,
676
      "2.16.840.1.101.3.7.2.1.2", 3, {0x5F, 0xC1, 0x0B}, {0x01, 0x02}, PIV_OBJECT_TYPE_CERT},
677
  { PIV_OBJ_X509_CARD_AUTH, "X.509 Certificate for Card Authentication",
678
      SC_ASN1_APP | 0x13,
679
      "2.16.840.1.101.3.7.2.5.0", 3, {0x5F, 0xC1, 0x01}, {0x05, 0x00}, PIV_OBJECT_TYPE_CERT},
680
  { PIV_OBJ_SEC_OBJ, "Security Object",
681
      SC_ASN1_APP | 0x13,
682
      "2.16.840.1.101.3.7.2.144.0", 3, {0x5F, 0xC1, 0x06}, {0x90, 0x00}, PIV_OBJECT_NEEDS_VCI},
683
  { PIV_OBJ_DISCOVERY, "Discovery Object",
684
      SC_ASN1_APP | SC_ASN1_CONS | 0x1E,
685
      "2.16.840.1.101.3.7.2.96.80", 1, {0x7E}, {0x60, 0x50}, 0},
686
  { PIV_OBJ_HISTORY, "Key History Object",
687
      SC_ASN1_APP | 0x13,
688
      "2.16.840.1.101.3.7.2.96.96", 3, {0x5F, 0xC1, 0x0C}, {0x60, 0x60}, PIV_OBJECT_NEEDS_VCI},
689
690
/* 800-73-3, 21 new objects, 20 history certificates */
691
  { PIV_OBJ_RETIRED_X509_1, "Retired X.509 Certificate for Key Management 1",
692
      SC_ASN1_APP | 0x13,
693
      "2.16.840.1.101.3.7.2.16.1", 3, {0x5F, 0xC1, 0x0D}, {0x10, 0x01},
694
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
695
  { PIV_OBJ_RETIRED_X509_2, "Retired X.509 Certificate for Key Management 2",
696
      SC_ASN1_APP | 0x13,
697
      "2.16.840.1.101.3.7.2.16.2", 3, {0x5F, 0xC1, 0x0E}, {0x10, 0x02},
698
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
699
  { PIV_OBJ_RETIRED_X509_3, "Retired X.509 Certificate for Key Management 3",
700
      SC_ASN1_APP | 0x13,
701
      "2.16.840.1.101.3.7.2.16.3", 3, {0x5F, 0xC1, 0x0F}, {0x10, 0x03},
702
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
703
  { PIV_OBJ_RETIRED_X509_4, "Retired X.509 Certificate for Key Management 4",
704
      SC_ASN1_APP | 0x13,
705
      "2.16.840.1.101.3.7.2.16.4", 3, {0x5F, 0xC1, 0x10}, {0x10, 0x04},
706
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
707
  { PIV_OBJ_RETIRED_X509_5, "Retired X.509 Certificate for Key Management 5",
708
      SC_ASN1_APP | 0x13,
709
      "2.16.840.1.101.3.7.2.16.5", 3, {0x5F, 0xC1, 0x11}, {0x10, 0x05},
710
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
711
  { PIV_OBJ_RETIRED_X509_6, "Retired X.509 Certificate for Key Management 6",
712
      SC_ASN1_APP | 0x13,
713
      "2.16.840.1.101.3.7.2.16.6", 3, {0x5F, 0xC1, 0x12}, {0x10, 0x06},
714
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
715
  { PIV_OBJ_RETIRED_X509_7, "Retired X.509 Certificate for Key Management 7",
716
      SC_ASN1_APP | 0x13,
717
      "2.16.840.1.101.3.7.2.16.7", 3, {0x5F, 0xC1, 0x13}, {0x10, 0x07},
718
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
719
  { PIV_OBJ_RETIRED_X509_8, "Retired X.509 Certificate for Key Management 8",
720
      SC_ASN1_APP | 0x13,
721
      "2.16.840.1.101.3.7.2.16.8", 3, {0x5F, 0xC1, 0x14}, {0x10, 0x08},
722
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
723
  { PIV_OBJ_RETIRED_X509_9, "Retired X.509 Certificate for Key Management 9",
724
      SC_ASN1_APP | 0x13,
725
      "2.16.840.1.101.3.7.2.16.9", 3, {0x5F, 0xC1, 0x15}, {0x10, 0x09},
726
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
727
  { PIV_OBJ_RETIRED_X509_10, "Retired X.509 Certificate for Key Management 10",
728
      SC_ASN1_APP | 0x13,
729
      "2.16.840.1.101.3.7.2.16.10", 3, {0x5F, 0xC1, 0x16}, {0x10, 0x0A},
730
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
731
  { PIV_OBJ_RETIRED_X509_11, "Retired X.509 Certificate for Key Management 11",
732
      SC_ASN1_APP | 0x13,
733
      "2.16.840.1.101.3.7.2.16.11", 3, {0x5F, 0xC1, 0x17}, {0x10, 0x0B},
734
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
735
  { PIV_OBJ_RETIRED_X509_12, "Retired X.509 Certificate for Key Management 12",
736
      SC_ASN1_APP | 0x13,
737
      "2.16.840.1.101.3.7.2.16.12", 3, {0x5F, 0xC1, 0x18}, {0x10, 0x0C},
738
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
739
  { PIV_OBJ_RETIRED_X509_13, "Retired X.509 Certificate for Key Management 13",
740
      SC_ASN1_APP | 0x13,
741
      "2.16.840.1.101.3.7.2.16.13", 3, {0x5F, 0xC1, 0x19}, {0x10, 0x0D},
742
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
743
  { PIV_OBJ_RETIRED_X509_14, "Retired X.509 Certificate for Key Management 14",
744
      SC_ASN1_APP | 0x13,
745
      "2.16.840.1.101.3.7.2.16.14", 3, {0x5F, 0xC1, 0x1A}, {0x10, 0x0E},
746
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
747
  { PIV_OBJ_RETIRED_X509_15, "Retired X.509 Certificate for Key Management 15",
748
      SC_ASN1_APP | 0x13,
749
      "2.16.840.1.101.3.7.2.16.15", 3, {0x5F, 0xC1, 0x1B}, {0x10, 0x0F},
750
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
751
  { PIV_OBJ_RETIRED_X509_16, "Retired X.509 Certificate for Key Management 16",
752
      SC_ASN1_APP | 0x13,
753
      "2.16.840.1.101.3.7.2.16.16", 3, {0x5F, 0xC1, 0x1C}, {0x10, 0x10},
754
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
755
  { PIV_OBJ_RETIRED_X509_17, "Retired X.509 Certificate for Key Management 17",
756
      SC_ASN1_APP | 0x13,
757
      "2.16.840.1.101.3.7.2.16.17", 3, {0x5F, 0xC1, 0x1D}, {0x10, 0x11},
758
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
759
  { PIV_OBJ_RETIRED_X509_18, "Retired X.509 Certificate for Key Management 18",
760
      SC_ASN1_APP | 0x13,
761
      "2.16.840.1.101.3.7.2.16.18", 3, {0x5F, 0xC1, 0x1E}, {0x10, 0x12},
762
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
763
  { PIV_OBJ_RETIRED_X509_19, "Retired X.509 Certificate for Key Management 19",
764
      SC_ASN1_APP | 0x13,
765
      "2.16.840.1.101.3.7.2.16.19", 3, {0x5F, 0xC1, 0x1F}, {0x10, 0x13},
766
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
767
  { PIV_OBJ_RETIRED_X509_20, "Retired X.509 Certificate for Key Management 20",
768
      SC_ASN1_APP | 0x13,
769
      "2.16.840.1.101.3.7.2.16.20", 3, {0x5F, 0xC1, 0x20}, {0x10, 0x14},
770
      PIV_OBJECT_NOT_PRESENT|PIV_OBJECT_TYPE_CERT | PIV_OBJECT_NEEDS_VCI},
771
772
  { PIV_OBJ_IRIS_IMAGE, "Cardholder Iris Images",
773
      SC_ASN1_APP | 0x13,
774
      "2.16.840.1.101.3.7.2.16.21", 3, {0x5F, 0xC1, 0x21}, {0x10, 0x15}, PIV_OBJECT_NEEDS_PIN | PIV_OBJECT_NEEDS_VCI},
775
776
/* 800-73-4, 3 new objects */
777
  { PIV_OBJ_BITGT, "Biometric Information Templates Group Template",
778
       SC_ASN1_APP | SC_ASN1_CONS | 0x1F61,
779
      "2.16.840.1.101.3.7.2.16.22", 2, {0x7F, 0x61}, {0x10, 0x16}, 0},
780
  { PIV_OBJ_SM_CERT_SIGNER, "Secure Messaging Certificate Signer",
781
      SC_ASN1_APP | 0x13,
782
      "2.16.840.1.101.3.7.2.16.23", 3, {0x5F, 0xC1, 0x22}, {0x10, 0x17},
783
      PIV_OBJECT_TYPE_CERT | PIV_OBJECT_TYPE_CVC},
784
  {PIV_OBJ_PCRDCS, "Pairing Code Reference Data Container",
785
      SC_ASN1_APP | 0x13,
786
      "2.16.840.1.101.3.7.2.16.24", 3, {0x5F, 0xC1, 0x23}, {0x10, 0x18}, PIV_OBJECT_NEEDS_PIN | PIV_OBJECT_NEEDS_VCI},
787
788
/* following not standard , to be used by piv-tool only for testing */
789
  { PIV_OBJ_9B03, "3DES-ECB ADM",
790
      SC_ASN1_APP | 0x13,
791
      "2.16.840.1.101.3.7.2.9999.3", 2, {0x9B, 0x03}, {0x9B, 0x03}, 0},
792
  /* Only used when signing a cert req, usually from engine
793
   * after piv-tool generated the key and saved the pub key
794
   * to a file. Note RSA key can be 1024, 2048 or 3072
795
   * but still use the "9x06" name.
796
   */
797
  { PIV_OBJ_9A06, "RSA 9A Pub key from last genkey",
798
      SC_ASN1_APP | 0x13,
799
      "2.16.840.1.101.3.7.2.9999.20", 2, {0x9A, 0x06}, {0x9A, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
800
  { PIV_OBJ_9C06, "Pub 9C key from last genkey",
801
      SC_ASN1_APP | 0x13,
802
      "2.16.840.1.101.3.7.2.9999.21", 2, {0x9C, 0x06}, {0x9C, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
803
  { PIV_OBJ_9D06, "Pub 9D key from last genkey",
804
      SC_ASN1_APP | 0x13,
805
      "2.16.840.1.101.3.7.2.9999.22", 2, {0x9D, 0x06}, {0x9D, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
806
  { PIV_OBJ_9E06, "Pub 9E key from last genkey",
807
      SC_ASN1_APP | 0x13,
808
      "2.16.840.1.101.3.7.2.9999.23", 2, {0x9E, 0x06}, {0x9E, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
809
810
  { PIV_OBJ_8206, "Pub 82 key ",
811
      SC_ASN1_APP | 0x13,
812
      "2.16.840.1.101.3.7.2.9999.101", 2, {0x82, 0x06}, {0x82, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
813
  { PIV_OBJ_8306, "Pub 83 key ",
814
      SC_ASN1_APP | 0x13,
815
      "2.16.840.1.101.3.7.2.9999.102", 2, {0x83, 0x06}, {0x83, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
816
  { PIV_OBJ_8406, "Pub 84 key ",
817
      SC_ASN1_APP | 0x13,
818
      "2.16.840.1.101.3.7.2.9999.103", 2, {0x84, 0x06}, {0x84, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
819
  { PIV_OBJ_8506, "Pub 85 key ",
820
      SC_ASN1_APP | 0x13,
821
      "2.16.840.1.101.3.7.2.9999.104", 2, {0x85, 0x06}, {0x85, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
822
  { PIV_OBJ_8606, "Pub 86 key ",
823
      SC_ASN1_APP | 0x13,
824
      "2.16.840.1.101.3.7.2.9999.105", 2, {0x86, 0x06}, {0x86, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
825
  { PIV_OBJ_8706, "Pub 87 key ",
826
      SC_ASN1_APP | 0x13,
827
      "2.16.840.1.101.3.7.2.9999.106", 2, {0x87, 0x06}, {0x87, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
828
  { PIV_OBJ_8806, "Pub 88 key ",
829
      SC_ASN1_APP | 0x13,
830
      "2.16.840.1.101.3.7.2.9999.107", 2, {0x88, 0x06}, {0x88, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
831
  { PIV_OBJ_8906, "Pub 89 key ",
832
      SC_ASN1_APP | 0x13,
833
      "2.16.840.1.101.3.7.2.9999.108", 2, {0x89, 0x06}, {0x89, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
834
  { PIV_OBJ_8A06, "Pub 8A key ",
835
      SC_ASN1_APP | 0x13,
836
      "2.16.840.1.101.3.7.2.9999.109", 2, {0x8A, 0x06}, {0x8A, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
837
  { PIV_OBJ_8B06, "Pub 8B key ",
838
      SC_ASN1_APP | 0x13,
839
      "2.16.840.1.101.3.7.2.9999.110", 2, {0x8B, 0x06}, {0x8B, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
840
  { PIV_OBJ_8C06, "Pub 8C key ",
841
      SC_ASN1_APP | 0x13,
842
      "2.16.840.1.101.3.7.2.9999.111", 2, {0x8C, 0x06}, {0x8C, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
843
  { PIV_OBJ_8D06, "Pub 8D key ",
844
      SC_ASN1_APP | 0x13,
845
      "2.16.840.1.101.3.7.2.9999.112", 2, {0x8D, 0x06}, {0x8D, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
846
  { PIV_OBJ_8E06, "Pub 8E key ",
847
      SC_ASN1_APP | 0x13,
848
      "2.16.840.1.101.3.7.2.9999.113", 2, {0x8E, 0x06}, {0x8E, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
849
  { PIV_OBJ_8F06, "Pub 8F key ",
850
      SC_ASN1_APP | 0x13,
851
      "2.16.840.1.101.3.7.2.9999.114", 2, {0x8F, 0x06}, {0x8F, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
852
  { PIV_OBJ_9006, "Pub 90 key ",
853
      SC_ASN1_APP | 0x13,
854
      "2.16.840.1.101.3.7.2.9999.115", 2, {0x90, 0x06}, {0x90, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
855
  { PIV_OBJ_9106, "Pub 91 key ",
856
      SC_ASN1_APP | 0x13,
857
      "2.16.840.1.101.3.7.2.9999.116", 2, {0x91, 0x06}, {0x91, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
858
  { PIV_OBJ_9206, "Pub 92 key ",
859
      SC_ASN1_APP | 0x13,
860
      "2.16.840.1.101.3.7.2.9999.117", 2, {0x92, 0x06}, {0x92, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
861
  { PIV_OBJ_9306, "Pub 93 key ",
862
      SC_ASN1_APP | 0x13,
863
      "2.16.840.1.101.3.7.2.9999.118", 2, {0x93, 0x06}, {0x93, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
864
  { PIV_OBJ_9406, "Pub 94 key ",
865
      SC_ASN1_APP | 0x13,
866
      "2.16.840.1.101.3.7.2.9999.119", 2, {0x94, 0x06}, {0x94, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
867
  { PIV_OBJ_9506, "Pub 95 key ",
868
      SC_ASN1_APP | 0x13,
869
      "2.16.840.1.101.3.7.2.9999.120", 2, {0x95, 0x06}, {0x95, 0x06}, PIV_OBJECT_TYPE_PUBKEY},
870
      /*
871
       * "Secure Messaging Certificate Signer" is just a certificate.
872
       * No pub or private key on the card.
873
       */
874
  { PIV_OBJ_LAST_ENUM, "", 0, "", 0, "", "", 0}
875
};
876
// clang-format on
877
878
static struct sc_card_operations piv_ops;
879
880
static struct sc_card_driver piv_drv = {
881
    "Personal Identity Verification Card",
882
    "PIV-II",
883
    &piv_ops,
884
    NULL, 0, NULL};
885
886
static int piv_get_cached_data(sc_card_t *card, int enumtag, u8 **buf, size_t *buf_len);
887
static int piv_cache_internal_data(sc_card_t *card, int enumtag);
888
static int piv_logout(sc_card_t *card);
889
static int piv_match_card_continued(sc_card_t *card);
890
static int piv_obj_cache_free_entry(sc_card_t *card, int enumtag, int flags);
891
static int piv_ai_map_find_by_id(sc_card_t *card, u8);
892
893
#ifdef ENABLE_PIV_SM
894
static void piv_inc(u8 *counter, size_t size);
895
static int piv_encode_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t *sm_apdu);
896
static int piv_get_sm_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t **sm_apdu);
897
static int piv_free_sm_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t **sm_apdu);
898
static int piv_get_asn1_obj(sc_context_t *ctx, void *arg, const u8 *obj, size_t len, int depth);
899
static int piv_sm_open(struct sc_card *card);
900
static int piv_decode_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t *sm_apdu);
901
static int piv_sm_close(sc_card_t *card);
902
static void piv_clear_cvc_content(piv_cvc_t *cvc);
903
static void piv_clear_sm_session(piv_sm_session_t *session);
904
static int piv_decode_cvc(sc_card_t *card, u8 **buf, size_t *buflen, piv_cvc_t *cvc);
905
static int piv_parse_pairing_code(sc_card_t *card, const char *option);
906
static int Q2OS(int fsize, u8 *Q, size_t Qlen, u8 *OS, size_t *OSlen);
907
static int piv_send_vci_pairing_code(struct sc_card *card, u8 *paring_code);
908
static int piv_sm_verify_sig(struct sc_card *card, const EVP_MD *type,
909
    EVP_PKEY *pkey, u8 *data, size_t data_size,
910
    unsigned char *sig, size_t siglen);
911
static int piv_sm_verify_certs(struct sc_card *card);
912
913
static void
914
piv_inc(u8 *counter, size_t size)
915
{
916
  unsigned int c = 1;
917
  unsigned int b;
918
  int i;
919
  for (i = (int)size - 1; c != 0 && i >= 0; i--) {
920
    b = c + counter[i];
921
    counter[i] = b & 0xff;
922
    c = b >> 8;
923
  }
924
}
925
926
/*
927
 *  Construct SM protected APDU
928
 */
929
930
static int
931
piv_encode_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t *sm_apdu)
932
{
933
  int r = 0;
934
  piv_private_data_t *priv = PIV_DATA(card);
935
  cipher_suite_t *cs = priv->cs;
936
  u8 pad[16] = {0x80};
937
  u8 zeros[16] = {0x00};
938
  u8 IV[16];
939
  u8 header[16];
940
  int padlen = 16; /* may be less */
941
  u8 *sbuf = NULL;
942
  size_t sbuflen = 0;
943
  int MCVlen = 16;
944
  int enc_datalen = 0;
945
  int T87len;
946
  int T97len = 2 + 1;
947
  int T8Elen = 2 + 8;
948
949
  int outli = 0;
950
  int outl = 0;
951
  int outll = 0;
952
  int outdl = 0;
953
  u8 discard[16];
954
  int macdatalen;
955
  size_t C_MCVlen = 16; /* debugging*/
956
957
  u8 *p;
958
  EVP_CIPHER_CTX *ed_ctx = NULL;
959
#if OPENSSL_VERSION_NUMBER < 0x30000000L
960
  CMAC_CTX *cmac_ctx = NULL;
961
#else
962
  EVP_MAC_CTX *cmac_ctx = NULL;
963
  EVP_MAC *mac = NULL;
964
  OSSL_PARAM cmac_params[2];
965
  size_t cmac_params_n;
966
#endif
967
968
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
969
970
#if OPENSSL_VERSION_NUMBER < 0x30000000L
971
  cmac_ctx = CMAC_CTX_new();
972
  if (cmac_ctx == NULL) {
973
    sc_log_openssl(card->ctx);
974
    r = SC_ERROR_INTERNAL;
975
    goto err;
976
  }
977
#else
978
  mac = EVP_MAC_fetch(PIV_LIBCTX, "cmac", NULL);
979
  cmac_params_n = 0;
980
  cmac_params[cmac_params_n++] = OSSL_PARAM_construct_utf8_string("cipher", cs->cipher_cbc_name, 0);
981
  cmac_params[cmac_params_n] = OSSL_PARAM_construct_end();
982
  if (mac == NULL || (cmac_ctx = EVP_MAC_CTX_new(mac)) == NULL) {
983
    sc_log_openssl(card->ctx);
984
    r = SC_ERROR_INTERNAL;
985
    goto err;
986
  }
987
#endif
988
989
  ed_ctx = EVP_CIPHER_CTX_new();
990
  if (ed_ctx == NULL) {
991
    r = SC_ERROR_INTERNAL;
992
    goto err;
993
  }
994
995
  if (EVP_EncryptInit_ex(ed_ctx, (*cs->cipher_ecb)(), NULL, priv->sm_session.SKenc, zeros) != 1 ||
996
      EVP_CIPHER_CTX_set_padding(ed_ctx, 0) != 1 ||
997
      EVP_EncryptUpdate(ed_ctx, IV, &outli, priv->sm_session.enc_counter, 16) != 1 ||
998
      EVP_EncryptFinal_ex(ed_ctx, discard, &outdl) != 1 ||
999
      outdl != 0) {
1000
    sc_log_openssl(card->ctx);
1001
    sc_log(card->ctx, "SM encode failed in OpenSSL");
1002
    r = SC_ERROR_INTERNAL;
1003
    goto err;
1004
  }
1005
1006
  sm_apdu->cla = 0x0c;
1007
  sm_apdu->ins = plain->ins;
1008
  sm_apdu->p1 = plain->p1;
1009
  sm_apdu->p2 = plain->p2;
1010
1011
  /*
1012
   * All APDUs will be converted to case as SM data is always sent and received
1013
   * if plain->cse == SC_APDU_CASE_1 it never has the the 0x20 bit set
1014
   * which "let OpenSC decides whether to use short or extended APDUs"
1015
   * PIV SM data added for plain->cse == SC_APDU_CASE_1 will not need extended APDUs.
1016
   *
1017
   * NIST 800-73-4 does not say if cards can or must support extended APDUs
1018
   * they must support command chaining and multiple get response APDUs and
1019
   * all examples use short APDUs. The following keep the option open to use extended
1020
   * APDUs in future specifications or "PIV like" cards are know to
1021
   * support extended APDUs.
1022
   *
1023
   * Turn off the CASE bits, and set CASE 4 in sm_apdu.
1024
   */
1025
1026
  sm_apdu->cse = (plain->cse & ~SC_APDU_SHORT_MASK) | SC_APDU_CASE_4_SHORT;
1027
1028
  p = header; /* to be included in CMAC */
1029
  *p++ = 0x0c;
1030
  *p++ = plain->ins;
1031
  *p++ = plain->p1;
1032
  *p++ = plain->p2;
1033
  memcpy(p, pad, 12);
1034
1035
  /* 800-73-4 say padding is 1 to 16 bytes, with 0x80 0x00... */
1036
1037
  /* may not need enc_data for cse 1 or 2 */
1038
  if (plain->datalen == 0) {
1039
    enc_datalen = 0;
1040
    T87len = 0;
1041
    padlen = 0;
1042
  } else {
1043
    enc_datalen = (int)(((plain->datalen + 15) / 16) * 16); /* may add extra 16 bytes */
1044
    padlen = enc_datalen - (int)plain->datalen;
1045
    r = T87len = sc_asn1_put_tag(0x87, NULL, 1 + enc_datalen, NULL, 0, NULL);
1046
    if (r < 0)
1047
      goto err;
1048
  }
1049
1050
  if (plain->resplen == 0 || plain->le == 0)
1051
    T97len = 0;
1052
1053
  sbuflen = T87len + T97len + T8Elen;
1054
1055
  sbuf = calloc(1, sbuflen);
1056
  if (sbuf == NULL) {
1057
    r = SC_ERROR_OUT_OF_MEMORY;
1058
    goto err;
1059
  }
1060
1061
  p = sbuf;
1062
  if (T87len != 0) {
1063
    r = sc_asn1_put_tag(0x87, NULL, 1 + enc_datalen, sbuf, sbuflen, &p);
1064
    if (r != SC_SUCCESS)
1065
      goto err;
1066
1067
    *p++ = 0x01; /* padding context indicator */
1068
1069
    /* first round encryptes Enc counter with zero IV, and does not save the output */
1070
    if (EVP_CIPHER_CTX_reset(ed_ctx) != 1 ||
1071
        EVP_EncryptInit_ex(ed_ctx, (*cs->cipher_cbc)(), NULL, priv->sm_session.SKenc, IV) != 1 ||
1072
        EVP_CIPHER_CTX_set_padding(ed_ctx, 0) != 1 ||
1073
        EVP_EncryptUpdate(ed_ctx, p, &outl, plain->data, (int)plain->datalen) != 1 ||
1074
        EVP_EncryptUpdate(ed_ctx, p + outl, &outll, pad, padlen) != 1 ||
1075
        EVP_EncryptFinal_ex(ed_ctx, discard, &outdl) != 1 ||
1076
        outdl != 0) { /* should not happen */
1077
      sc_log_openssl(card->ctx);
1078
      sc_log(card->ctx, "SM _encode failed in OpenSSL");
1079
      r = SC_ERROR_INTERNAL;
1080
      goto err;
1081
    }
1082
    p += enc_datalen;
1083
  }
1084
1085
  if (T97len) {
1086
    *p++ = 0x97;
1087
    *p++ = 0x01;
1088
    *p++ = plain->le;
1089
  }
1090
  macdatalen = (int)(p - sbuf);
1091
1092
  memcpy(priv->sm_session.C_MCV_last, priv->sm_session.C_MCV, MCVlen); /* save is case fails */
1093
1094
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1095
  if (CMAC_Init(cmac_ctx, priv->sm_session.SKmac, priv->sm_session.aes_size, (*cs->cipher_cbc)(), NULL) != 1 ||
1096
      CMAC_Update(cmac_ctx, priv->sm_session.C_MCV, MCVlen) != 1 ||
1097
      CMAC_Update(cmac_ctx, header, sizeof(header)) != 1 ||
1098
      CMAC_Update(cmac_ctx, sbuf, macdatalen) != 1 ||
1099
      CMAC_Final(cmac_ctx, priv->sm_session.C_MCV, &C_MCVlen) != 1) {
1100
    sc_log_openssl(card->ctx);
1101
    r = SC_ERROR_INTERNAL;
1102
    goto err;
1103
  }
1104
#else
1105
  if (!EVP_MAC_init(cmac_ctx, (const unsigned char *)priv->sm_session.SKmac,
1106
          priv->sm_session.aes_size, cmac_params) ||
1107
      !EVP_MAC_update(cmac_ctx, priv->sm_session.C_MCV, MCVlen) ||
1108
      !EVP_MAC_update(cmac_ctx, header, sizeof(header)) ||
1109
      !EVP_MAC_update(cmac_ctx, sbuf, macdatalen) ||
1110
      !EVP_MAC_final(cmac_ctx, priv->sm_session.C_MCV, &C_MCVlen, MCVlen)) {
1111
    sc_log_openssl(card->ctx);
1112
    r = SC_ERROR_INTERNAL;
1113
    goto err;
1114
  }
1115
#endif
1116
1117
  *p++ = 0x8E;
1118
  *p++ = 0x08;
1119
  memcpy(p, priv->sm_session.C_MCV, 8);
1120
  p += 8;
1121
  if (p != sbuf + sbuflen) { /* debugging */
1122
    r = SC_ERROR_INTERNAL;
1123
    goto err;
1124
  }
1125
  sm_apdu->data = sbuf;
1126
  sm_apdu->datalen = sbuflen;
1127
  sbuf = NULL;
1128
1129
  sm_apdu->lc = sm_apdu->datalen;
1130
  if (sm_apdu->datalen > 255)
1131
    sm_apdu->flags |= SC_APDU_FLAGS_CHAINING;
1132
1133
  sm_apdu->resplen = plain->resplen + 40; /* expect at least tagged status and rmac8 */
1134
  sm_apdu->resp = malloc(sm_apdu->resplen);
1135
  if (sm_apdu->resp == NULL) {
1136
    r = SC_ERROR_OUT_OF_MEMORY;
1137
    goto err;
1138
  }
1139
  sm_apdu->le = 256; /* always ask for 256 */
1140
1141
  memcpy(priv->sm_session.enc_counter_last, priv->sm_session.enc_counter, sizeof(priv->sm_session.enc_counter));
1142
  piv_inc(priv->sm_session.enc_counter, sizeof(priv->sm_session.enc_counter));
1143
1144
  r = SC_SUCCESS;
1145
err:
1146
1147
  free(sbuf);
1148
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1149
  CMAC_CTX_free(cmac_ctx);
1150
#else
1151
  EVP_MAC_CTX_free(cmac_ctx);
1152
  EVP_MAC_free(mac);
1153
#endif
1154
1155
  EVP_CIPHER_CTX_free(ed_ctx);
1156
1157
  LOG_FUNC_RETURN(card->ctx, r);
1158
}
1159
1160
static int
1161
piv_get_sm_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t **sm_apdu)
1162
{
1163
  int r = SC_SUCCESS;
1164
  piv_private_data_t *priv = PIV_DATA(card);
1165
  cipher_suite_t *cs = priv->cs;
1166
1167
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1168
1169
  if (!plain || !sm_apdu)
1170
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1171
1172
  /* Does card support SM? Should not be here */
1173
  if (priv->csID == 0 || cs == NULL)
1174
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_SM_NOT_APPLIED);
1175
1176
  switch (plain->ins) {
1177
  case 0xCB: /* GET_DATA */
1178
    /* If not contactless, could read in clear */
1179
    /* Discovery object never has PIV_SM_GET_DATA_IN_CLEAR set */
1180
    sc_log(card->ctx, "init_flags:0x%8.8x sm_flags:0x%8.8lx", priv->init_flags, priv->sm_flags);
1181
    if (!(priv->init_flags & PIV_INIT_CONTACTLESS) &&
1182
        !(priv->init_flags & PIV_INIT_IN_READER_LOCK_OBTAINED) &&
1183
        (priv->sm_flags & PIV_SM_GET_DATA_IN_CLEAR)) {
1184
      priv->sm_flags &= ~PIV_SM_GET_DATA_IN_CLEAR;
1185
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_SM_NOT_APPLIED);
1186
    }
1187
    break;
1188
  case 0x20: /* VERIFY */
1189
    break;
1190
  case 0x24: /* CHANGE REFERENCE DATA */
1191
    break;
1192
  case 0x87: /* GENERAL AUTHENTICATE */
1193
    break;
1194
  default: /* just issue the plain apdu */
1195
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_SM_NOT_APPLIED);
1196
  }
1197
1198
  *sm_apdu = calloc(1, sizeof(sc_apdu_t));
1199
  if (*sm_apdu == NULL) {
1200
    return SC_ERROR_OUT_OF_MEMORY;
1201
  }
1202
1203
  r = piv_encode_apdu(card, plain, *sm_apdu);
1204
  if (r < 0 && *sm_apdu) {
1205
    piv_free_sm_apdu(card, NULL, sm_apdu);
1206
  }
1207
1208
  LOG_FUNC_RETURN(card->ctx, r);
1209
}
1210
1211
/* ASN1 callback to save address and len of the object */
1212
static int
1213
piv_get_asn1_obj(sc_context_t *ctx, void *arg, const u8 *obj, size_t len, int depth)
1214
{
1215
  struct sc_lv_data *al = arg;
1216
1217
  if (!arg)
1218
    return SC_ERROR_INTERNAL;
1219
1220
  al->value = (u8 *)obj;
1221
  al->len = len;
1222
  return SC_SUCCESS;
1223
}
1224
1225
static int
1226
piv_decode_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t *sm_apdu)
1227
{
1228
  int r = SC_SUCCESS;
1229
  int i;
1230
  piv_private_data_t *priv = PIV_DATA(card);
1231
  cipher_suite_t *cs = priv->cs;
1232
  struct sc_lv_data ee = {NULL, 0};
1233
  struct sc_lv_data status = {NULL, 0};
1234
  struct sc_lv_data rmac8 = {NULL, 0};
1235
  u8 zeros[16] = {0};
1236
  u8 IV[16];
1237
  u8 *p;
1238
  int outl;
1239
  int outli;
1240
  int outll;
1241
  int outdl;
1242
  u8 lastb[16];
1243
  u8 discard[8];
1244
  u8 *q = NULL;
1245
  int inlen;
1246
  int macdatalen;
1247
1248
  size_t MCVlen = 16;
1249
  size_t R_MCVlen = 0;
1250
1251
  EVP_CIPHER_CTX *ed_ctx = NULL;
1252
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1253
  CMAC_CTX *cmac_ctx = NULL;
1254
#else
1255
  EVP_MAC *mac = NULL;
1256
  EVP_MAC_CTX *cmac_ctx = NULL;
1257
  OSSL_PARAM cmac_params[2];
1258
  size_t cmac_params_n = 0;
1259
#endif
1260
1261
  struct sc_asn1_entry asn1_sm_response[C_ASN1_PIV_SM_RESPONSE_SIZE];
1262
1263
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1264
1265
  sc_copy_asn1_entry(c_asn1_sm_response, asn1_sm_response);
1266
1267
  sc_format_asn1_entry(asn1_sm_response + 0, piv_get_asn1_obj, &ee, 0);
1268
  sc_format_asn1_entry(asn1_sm_response + 1, piv_get_asn1_obj, &status, 0);
1269
  sc_format_asn1_entry(asn1_sm_response + 2, piv_get_asn1_obj, &rmac8, 0);
1270
1271
  r = sc_asn1_decode(card->ctx, asn1_sm_response, sm_apdu->resp, sm_apdu->resplen, NULL, NULL);
1272
1273
  if (r < 0) {
1274
    sc_log(card->ctx, "SM decode failed");
1275
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1276
    goto err;
1277
  }
1278
1279
  if (asn1_sm_response[0].flags & SC_ASN1_PRESENT && /* optional */
1280
      (ee.value == NULL || ee.len <= 2)) {
1281
    sc_log(card->ctx, "SM BER-TLV not valid");
1282
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1283
    goto err;
1284
  }
1285
1286
  if ((asn1_sm_response[1].flags & SC_ASN1_PRESENT) == 0 ||
1287
      (asn1_sm_response[2].flags & SC_ASN1_PRESENT) == 0) {
1288
    sc_log(card->ctx, "SM missing status or R-MAC");
1289
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1290
    goto err;
1291
  }
1292
1293
  if (status.len != 2 ||
1294
      status.value == NULL ||
1295
      rmac8.len != 8 ||
1296
      rmac8.value == NULL) {
1297
    sc_log(card->ctx, "SM status or R-MAC length invalid");
1298
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1299
    goto err;
1300
  }
1301
1302
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1303
  cmac_ctx = CMAC_CTX_new();
1304
  if (cmac_ctx == NULL) {
1305
    sc_log_openssl(card->ctx);
1306
    r = SC_ERROR_INTERNAL;
1307
    goto err;
1308
  }
1309
#else
1310
  mac = EVP_MAC_fetch(PIV_LIBCTX, "cmac", NULL);
1311
  cmac_params[cmac_params_n++] = OSSL_PARAM_construct_utf8_string("cipher", cs->cipher_cbc_name, 0);
1312
  cmac_params[cmac_params_n] = OSSL_PARAM_construct_end();
1313
  if (mac == NULL || (cmac_ctx = EVP_MAC_CTX_new(mac)) == NULL) {
1314
    sc_log_openssl(card->ctx);
1315
    r = SC_ERROR_INTERNAL;
1316
    goto err;
1317
  }
1318
#endif
1319
1320
  /*  MCV is first, then BER TLV Encoded Encrypted PIV Data and Status */
1321
  macdatalen = (int)(status.value + status.len - sm_apdu->resp);
1322
1323
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1324
  if (CMAC_Init(cmac_ctx, priv->sm_session.SKrmac, priv->sm_session.aes_size, (*cs->cipher_cbc)(), NULL) != 1 ||
1325
      CMAC_Update(cmac_ctx, priv->sm_session.R_MCV, MCVlen) != 1 ||
1326
      CMAC_Update(cmac_ctx, sm_apdu->resp, macdatalen) != 1 ||
1327
      CMAC_Final(cmac_ctx, priv->sm_session.R_MCV, &R_MCVlen) != 1) {
1328
    sc_log_openssl(card->ctx);
1329
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1330
    goto err;
1331
  }
1332
#else
1333
  if (!EVP_MAC_init(cmac_ctx, (const unsigned char *)priv->sm_session.SKrmac,
1334
          priv->sm_session.aes_size, cmac_params) ||
1335
      !EVP_MAC_update(cmac_ctx, priv->sm_session.R_MCV, MCVlen) ||
1336
      !EVP_MAC_update(cmac_ctx, sm_apdu->resp, macdatalen) ||
1337
      !EVP_MAC_final(cmac_ctx, priv->sm_session.R_MCV, &R_MCVlen, MCVlen)) {
1338
    sc_log_openssl(card->ctx);
1339
    r = SC_ERROR_INTERNAL;
1340
    goto err;
1341
  }
1342
#endif
1343
1344
  if (memcmp(priv->sm_session.R_MCV, rmac8.value, 8) != 0) {
1345
    sc_log(card->ctx, "SM 8 bytes of R-MAC do not match received R-MAC");
1346
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1347
    goto err;
1348
  }
1349
1350
  ed_ctx = EVP_CIPHER_CTX_new();
1351
  if (ed_ctx == NULL) {
1352
    r = SC_ERROR_INTERNAL;
1353
    goto err;
1354
  }
1355
1356
  /* generate same IV used to encrypt response on card */
1357
  if (EVP_EncryptInit_ex(ed_ctx, (*cs->cipher_ecb)(), NULL, priv->sm_session.SKenc, zeros) != 1 ||
1358
      EVP_CIPHER_CTX_set_padding(ed_ctx, 0) != 1 ||
1359
      EVP_EncryptUpdate(ed_ctx, IV, &outli, priv->sm_session.resp_enc_counter, 16) != 1 ||
1360
      EVP_EncryptFinal_ex(ed_ctx, discard, &outdl) != 1 ||
1361
      outdl != 0) {
1362
    sc_log_openssl(card->ctx);
1363
    sc_log(card->ctx, "SM encode failed in OpenSSL");
1364
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1365
    goto err;
1366
  }
1367
1368
  /* some commands do not have response data */
1369
  if (ee.value == NULL) {
1370
    plain->resplen = 0;
1371
  } else {
1372
    p = ee.value;
1373
    inlen = (int)ee.len;
1374
    if (inlen < 17 || *p != 0x01) { /*padding and padding indicator are required */
1375
      sc_log(card->ctx, "SM padding indicator not 0x01");
1376
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1377
      goto err;
1378
    }
1379
1380
    p++; /* skip padding indicator */
1381
    inlen--;
1382
1383
    if ((inlen % 16) != 0) {
1384
      sc_log(card->ctx, "SM encrypted data not multiple of 16");
1385
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1386
      goto err;
1387
    }
1388
1389
    /*
1390
     * Encrypted data has 1 to 16 pad bytes, so may be 1 to 16 bytes longer
1391
     * then expected. i.e. plain->resp and resplen.So will do last block
1392
     * and recombine.
1393
     */
1394
1395
    inlen -= 16;
1396
    if (plain->resplen < (unsigned)inlen || plain->resp == NULL) {
1397
      sc_log(card->ctx, "SM response will not fit in resp,resplen");
1398
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1399
      goto err;
1400
    }
1401
1402
    q = plain->resp;
1403
1404
    /* first round encryptes counter with zero IV, and does not save the output */
1405
    if (EVP_CIPHER_CTX_reset(ed_ctx) != 1 ||
1406
        EVP_DecryptInit_ex(ed_ctx, (*cs->cipher_cbc)(), NULL, priv->sm_session.SKenc, IV) != 1 ||
1407
        EVP_CIPHER_CTX_set_padding(ed_ctx, 0) != 1 ||
1408
        EVP_DecryptUpdate(ed_ctx, q, &outl, p, inlen) != 1 ||
1409
        EVP_DecryptUpdate(ed_ctx, lastb, &outll, p + inlen, 16) != 1 ||
1410
        EVP_DecryptFinal_ex(ed_ctx, discard, &outdl) != 1 ||
1411
        outdl != 0 ||
1412
        outll != 16) { /* should not happen */
1413
      sc_log_openssl(card->ctx);
1414
      sc_log(card->ctx, "SM _decode failed in OpenSSL");
1415
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1416
      goto err;
1417
    }
1418
1419
    /* unpad last block and get bytes in last block */
1420
    for (i = 15; i > 0; i--) {
1421
      if (lastb[i] == 0x80)
1422
        break;
1423
      if (lastb[i] == 0x00)
1424
        continue;
1425
      sc_log(card->ctx, "SM Padding not correct");
1426
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1427
      goto err;
1428
    }
1429
1430
    if (lastb[i] != 0x80) {
1431
      sc_log(card->ctx, "SM Padding not correct");
1432
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1433
      goto err;
1434
    }
1435
1436
    /* will response fit in plain resp buffer */
1437
    if ((unsigned)inlen + i > plain->resplen || plain->resp == NULL) {
1438
      sc_log(card->ctx, "SM response bigger then resplen");
1439
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1440
      goto err;
1441
    }
1442
1443
    /* copy bytes in last block  if any */
1444
    memcpy(plain->resp + inlen, lastb, i);
1445
    plain->resplen = inlen + i;
1446
  }
1447
1448
  plain->sw1 = *(status.value);
1449
  plain->sw2 = *(status.value + 1);
1450
1451
  piv_inc(priv->sm_session.resp_enc_counter, sizeof(priv->sm_session.resp_enc_counter));
1452
1453
  r = SC_SUCCESS;
1454
err:
1455
  if (r != 0 && plain) {
1456
    plain->sw1 = 0x69;
1457
    plain->sw2 = 0x88;
1458
  }
1459
1460
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1461
  CMAC_CTX_free(cmac_ctx);
1462
#else
1463
  EVP_MAC_CTX_free(cmac_ctx);
1464
  EVP_MAC_free(mac);
1465
#endif
1466
1467
  EVP_CIPHER_CTX_free(ed_ctx);
1468
1469
  LOG_FUNC_RETURN(card->ctx, r);
1470
}
1471
1472
static int
1473
piv_free_sm_apdu(sc_card_t *card, sc_apdu_t *plain, sc_apdu_t **sm_apdu)
1474
{
1475
  int r = SC_SUCCESS;
1476
1477
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1478
1479
  if (!sm_apdu)
1480
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1481
  if (!(*sm_apdu))
1482
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1483
1484
  if (plain) {
1485
    plain->sw1 = (*sm_apdu)->sw1;
1486
    plain->sw2 = (*sm_apdu)->sw2;
1487
    if (((*sm_apdu)->sw1 == 0x90 && (*sm_apdu)->sw2 == 00) ||
1488
        (*sm_apdu)->sw1 == 61) {
1489
      r = piv_decode_apdu(card, plain, *sm_apdu);
1490
      goto err;
1491
    }
1492
    sc_log(card->ctx, "SM response sw1:0x%2.2x sw2:0x%2.2x", plain->sw1, plain->sw2);
1493
    if (plain->sw1 == 0x69 && plain->sw2 == 0x88) {
1494
      /* BUT plain->sw1 and sw2 are not passed back as expected */
1495
      r = SC_ERROR_SM_INVALID_CHECKSUM; /* will use this one one for now */
1496
      goto err;
1497
    } else {
1498
      r = SC_ERROR_SM;
1499
      goto err;
1500
    }
1501
  }
1502
1503
err:
1504
  free((unsigned char **)(*sm_apdu)->data);
1505
  free((*sm_apdu)->resp);
1506
  free(*sm_apdu);
1507
  *sm_apdu = NULL;
1508
1509
  LOG_FUNC_RETURN(card->ctx, r);
1510
}
1511
1512
static int
1513
piv_sm_close(sc_card_t *card)
1514
{
1515
  int r = 0;
1516
  piv_private_data_t *priv = PIV_DATA(card);
1517
1518
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1519
  sc_log(card->ctx, "priv->sm_flags: 0x%8.8lu", priv->sm_flags);
1520
1521
  /* sm.c tries to restart sm. Will defer */
1522
  if ((priv->sm_flags & PIV_SM_FLAGS_SM_IS_ACTIVE)) {
1523
    priv->sm_flags |= PIV_SM_FLAGS_DEFER_OPEN;
1524
    priv->sm_flags &= ~PIV_SM_FLAGS_SM_IS_ACTIVE;
1525
  }
1526
1527
  LOG_FUNC_RETURN(card->ctx, r);
1528
}
1529
1530
static void
1531
piv_clear_cvc_content(piv_cvc_t *cvc)
1532
{
1533
  if (!cvc)
1534
    return;
1535
  free(cvc->body);
1536
  free(cvc->signature);
1537
  free(cvc->publicPoint);
1538
  free(cvc->der.value);
1539
  memset(cvc, 0, sizeof(piv_cvc_t));
1540
  return;
1541
}
1542
1543
static void
1544
piv_clear_sm_session(piv_sm_session_t *session)
1545
{
1546
  if (!session)
1547
    return;
1548
  sc_mem_clear(session, sizeof(piv_sm_session_t));
1549
  return;
1550
}
1551
1552
/*
1553
 * Decode a card verifiable certificate as defined in NIST 800-73-4
1554
 */
1555
static int
1556
piv_decode_cvc(sc_card_t *card, u8 **buf, size_t *buflen,
1557
    piv_cvc_t *cvc)
1558
{
1559
  struct sc_asn1_entry asn1_piv_cvc[C_ASN1_PIV_CVC_SIZE];
1560
  struct sc_asn1_entry asn1_piv_cvc_body[C_ASN1_PIV_CVC_BODY_SIZE];
1561
  struct sc_asn1_entry asn1_piv_cvc_pubkey[C_ASN1_PIV_CVC_PUBKEY_SIZE];
1562
  struct sc_asn1_entry asn1_piv_cvc_dsobj[C_ASN1_PIV_CVC_DSOBJ_SIZE];
1563
  struct sc_asn1_entry asn1_piv_cvc_dssig[C_ASN1_PIV_CVC_DSSIG_SIZE];
1564
  struct sc_asn1_entry asn1_piv_cvc_alg_id[C_ASN1_PIV_CVC_ALG_ID_SIZE];
1565
  struct sc_lv_data roleIDder = {NULL, 0};
1566
  int r;
1567
  const u8 *buf_tmp;
1568
  unsigned int cla_out, tag_out;
1569
  size_t taglen;
1570
  size_t signaturebits;
1571
1572
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1573
1574
  if (buf == NULL || *buf == NULL || cvc == NULL) {
1575
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
1576
  }
1577
1578
  /* If already read and matches previous version return SC_SUCCESS */
1579
  if (cvc->der.value && (cvc->der.len == *buflen) && (memcmp(cvc->der.value, *buf, *buflen) == 0))
1580
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1581
1582
  piv_clear_cvc_content(cvc);
1583
1584
  memset(cvc, 0, sizeof(piv_cvc_t));
1585
  cvc->issuerIDlen = sizeof(cvc->issuerID);
1586
  cvc->subjectIDlen = sizeof(cvc->subjectID);
1587
1588
  sc_copy_asn1_entry(c_asn1_piv_cvc, asn1_piv_cvc);
1589
  sc_copy_asn1_entry(c_asn1_piv_cvc_body, asn1_piv_cvc_body);
1590
  sc_copy_asn1_entry(c_asn1_piv_cvc_pubkey, asn1_piv_cvc_pubkey);
1591
  sc_copy_asn1_entry(c_asn1_piv_cvc_dsobj, asn1_piv_cvc_dsobj);
1592
  sc_copy_asn1_entry(c_asn1_piv_cvc_dssig, asn1_piv_cvc_dssig);
1593
  sc_copy_asn1_entry(c_asn1_piv_cvc_alg_id, asn1_piv_cvc_alg_id);
1594
1595
  sc_format_asn1_entry(asn1_piv_cvc_alg_id, &cvc->signatureAlgOID, NULL, 1);
1596
  sc_format_asn1_entry(asn1_piv_cvc_alg_id + 1, NULL, NULL, 1); /* NULL */
1597
1598
  sc_format_asn1_entry(asn1_piv_cvc_dssig, &asn1_piv_cvc_alg_id, NULL, 1);
1599
  sc_format_asn1_entry(asn1_piv_cvc_dssig + 1, &cvc->signature, &signaturebits, 1);
1600
1601
  sc_format_asn1_entry(asn1_piv_cvc_dsobj, &asn1_piv_cvc_dssig, NULL, 1);
1602
1603
  sc_format_asn1_entry(asn1_piv_cvc_pubkey, &cvc->pubKeyOID, NULL, 1);
1604
  sc_format_asn1_entry(asn1_piv_cvc_pubkey + 1, &cvc->publicPoint, &cvc->publicPointlen, 1);
1605
1606
  sc_format_asn1_entry(asn1_piv_cvc_body, &cvc->cpi, NULL, 1);
1607
  sc_format_asn1_entry(asn1_piv_cvc_body + 1, &cvc->issuerID, &cvc->issuerIDlen, 1);
1608
  sc_format_asn1_entry(asn1_piv_cvc_body + 2, &cvc->subjectID, &cvc->subjectIDlen, 1);
1609
  sc_format_asn1_entry(asn1_piv_cvc_body + 3, &asn1_piv_cvc_pubkey, NULL, 1);
1610
  sc_format_asn1_entry(asn1_piv_cvc_body + 4, piv_get_asn1_obj, &roleIDder, 1);
1611
  sc_format_asn1_entry(asn1_piv_cvc_body + 5, &asn1_piv_cvc_dsobj, NULL, 1);
1612
1613
  sc_format_asn1_entry(asn1_piv_cvc, &asn1_piv_cvc_body, NULL, 1);
1614
1615
  r = sc_asn1_decode(card->ctx, asn1_piv_cvc, *buf, *buflen, NULL, NULL); /*(const u8 **) &buf_tmp, &len);*/
1616
  if (r < 0) {
1617
    piv_clear_cvc_content(cvc);
1618
    sc_log(card->ctx, "Could not decode card verifiable certificate");
1619
    LOG_FUNC_RETURN(card->ctx, r);
1620
  }
1621
1622
  cvc->signaturelen = signaturebits / 8;
1623
1624
  if (roleIDder.len != 1)
1625
    LOG_TEST_RET(card->ctx, SC_ERROR_SM_AUTHENTICATION_FAILED, "roleID wrong length");
1626
1627
  cvc->roleID = *roleIDder.value;
1628
1629
  /* save body der for verification */
1630
  buf_tmp = *buf;
1631
  r = sc_asn1_read_tag(&buf_tmp, *buflen, &cla_out, &tag_out, &taglen);
1632
  LOG_TEST_RET(card->ctx, r, " failed to read tag");
1633
1634
  cvc->bodylen = (roleIDder.value + roleIDder.len) - buf_tmp;
1635
1636
  cvc->body = malloc(cvc->bodylen);
1637
  if (cvc->body == NULL)
1638
    return SC_ERROR_OUT_OF_MEMORY;
1639
  memcpy(cvc->body, buf_tmp, cvc->bodylen);
1640
1641
  /* save to reuse */
1642
  cvc->der.value = malloc(*buflen);
1643
  if (cvc->der.value == NULL) {
1644
    free(cvc->body);
1645
    return SC_ERROR_OUT_OF_MEMORY;
1646
  }
1647
  cvc->der.len = *buflen;
1648
  memcpy(cvc->der.value, *buf, cvc->der.len);
1649
1650
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
1651
}
1652
1653
static int
1654
piv_parse_pairing_code(sc_card_t *card, const char *option)
1655
{
1656
  size_t i;
1657
1658
  if (strlen(option) != PIV_PAIRING_CODE_LEN) {
1659
    sc_log(card->ctx, "pairing code length invalid must be %d", PIV_PAIRING_CODE_LEN);
1660
    return SC_ERROR_INVALID_ARGUMENTS;
1661
  }
1662
  for (i = 0; i < PIV_PAIRING_CODE_LEN; i++) {
1663
    if (!isdigit(option[i])) {
1664
      sc_log(card->ctx, "pairing code must be %d decimal digits", PIV_PAIRING_CODE_LEN);
1665
      return SC_ERROR_INVALID_ARGUMENTS;
1666
    }
1667
  }
1668
  return SC_SUCCESS;
1669
}
1670
#endif
1671
1672
static int
1673
piv_load_options(sc_card_t *card)
1674
550
{
1675
550
  int r;
1676
550
  size_t i, j;
1677
550
  scconf_block **found_blocks, *block;
1678
1679
#ifdef ENABLE_PIV_SM
1680
  piv_private_data_t *priv = PIV_DATA(card);
1681
  const char *option = NULL;
1682
  int piv_pairing_code_found = 0;
1683
  int piv_use_sm_found = 0;
1684
1685
  /* pairing code is 8 decimal digits and is card specific */
1686
  if ((option = getenv("PIV_PAIRING_CODE")) != NULL) {
1687
    sc_log(card->ctx, "getenv(\"PIV_PAIRING_CODE\") found");
1688
    if (piv_parse_pairing_code(card, option) == SC_SUCCESS) {
1689
      memcpy(priv->pairing_code, option, PIV_PAIRING_CODE_LEN);
1690
      piv_pairing_code_found = 1;
1691
    }
1692
  }
1693
1694
  if ((option = getenv("PIV_USE_SM")) != NULL) {
1695
    sc_log(card->ctx, "getenv(\"PIV_USE_SM\")=\"%s\"", option);
1696
    if (!strcmp(option, "never")) {
1697
      priv->sm_flags |= PIV_SM_FLAGS_NEVER;
1698
      piv_use_sm_found = 1;
1699
    } else if (!strcmp(option, "always")) {
1700
      priv->sm_flags |= PIV_SM_FLAGS_ALWAYS;
1701
      piv_use_sm_found = 1;
1702
    } else {
1703
      sc_log(card->ctx, "Invalid piv_use_sm: \"%s\"", option);
1704
    }
1705
  }
1706
#endif
1707
1708
1.10k
  for (i = 0; card->ctx->conf_blocks[i]; i++) {
1709
550
    found_blocks = scconf_find_blocks(card->ctx->conf, card->ctx->conf_blocks[i],
1710
550
        "card_driver", "PIV-II");
1711
550
    if (!found_blocks)
1712
0
      continue;
1713
1714
550
    for (j = 0, block = found_blocks[j]; block; j++, block = found_blocks[j]) {
1715
1716
#ifdef ENABLE_PIV_SM
1717
      /*
1718
       * "piv_use_sm" if card supports NIST sp800-73-4 sm, when should it be used
1719
       * never - use card like 800-73-3, i.e. contactless is very limited on
1720
       * true PIV cards. Some  PIV-like" card may allow this.
1721
       * this security risk
1722
       * always - Use even for contact interface.
1723
       * PINS, crypto and reading of object will not show up in logs
1724
       * or over network.
1725
       */
1726
1727
      if (piv_use_sm_found == 0) {
1728
        option = scconf_get_str(block, "piv_use_sm", "default");
1729
        sc_log(card->ctx, "conf: \"piv_use_sm\"=\"%s\"", option);
1730
        if (!strcmp(option, "default")) {
1731
          /* no new flags */
1732
        } else if (!strcmp(option, "never")) {
1733
          priv->sm_flags |= PIV_SM_FLAGS_NEVER;
1734
        } else if (!strcmp(option, "always")) {
1735
          priv->sm_flags |= PIV_SM_FLAGS_ALWAYS;
1736
        } else {
1737
          sc_log(card->ctx, "Invalid piv_use_sm: \"%s\"", option);
1738
        }
1739
      }
1740
1741
      /* This is really a card specific value and should not be in the conf file */
1742
      if (piv_pairing_code_found == 0) {
1743
        option = scconf_get_str(block, "piv_pairing_code", NULL);
1744
        if (option && piv_parse_pairing_code(card, option) == SC_SUCCESS) {
1745
          memcpy(priv->pairing_code, option, PIV_PAIRING_CODE_LEN);
1746
        }
1747
      }
1748
#endif
1749
0
    }
1750
550
    free(found_blocks);
1751
550
  }
1752
550
  r = SC_SUCCESS;
1753
550
  return r;
1754
550
}
1755
1756
static int
1757
piv_find_obj_by_containerid(sc_card_t *card, const u8 *str)
1758
38.2k
{
1759
38.2k
  int i;
1760
1761
38.2k
  LOG_FUNC_CALLED(card->ctx);
1762
38.2k
  sc_log(card->ctx, "str=0x%02X%02X\n", str[0], str[1]);
1763
1764
795k
  for (i = 0; piv_objects[i].enumtag < PIV_OBJ_LAST_ENUM; i++) {
1765
794k
    if (str[0] == piv_objects[i].containerid[0] && str[1] == piv_objects[i].containerid[1])
1766
794k
      LOG_FUNC_RETURN(card->ctx, i);
1767
794k
  }
1768
1769
1.76k
  LOG_FUNC_RETURN(card->ctx, -1);
1770
1.76k
}
1771
1772
/*
1773
 * Send a command and receive data. There is always something to send.
1774
 * Used by  GET DATA, PUT DATA, GENERAL AUTHENTICATE
1775
 * and GENERATE ASYMMETRIC KEY PAIR.
1776
 */
1777
1778
static int
1779
piv_general_io(sc_card_t *card, int ins, int p1, int p2,
1780
    const u8 *sendbuf, size_t sendbuflen, u8 *recvbuf,
1781
    size_t recvbuflen)
1782
5.42k
{
1783
5.42k
  int r;
1784
5.42k
  sc_apdu_t apdu;
1785
1786
5.42k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1787
1788
5.42k
  r = sc_lock(card);
1789
5.42k
  if (r != SC_SUCCESS)
1790
5.42k
    LOG_FUNC_RETURN(card->ctx, r);
1791
1792
5.42k
  sc_format_apdu(card, &apdu,
1793
5.42k
      recvbuf ? SC_APDU_CASE_4_SHORT : SC_APDU_CASE_3_SHORT,
1794
5.42k
      ins, p1, p2);
1795
5.42k
  apdu.flags |= SC_APDU_FLAGS_CHAINING;
1796
#ifdef ENABLE_PIV_SM
1797
  if (card->sm_ctx.sm_mode != SM_MODE_NONE && sendbuflen > 255) {
1798
    /* tell apdu.c to not do the chaining, let the SM get_apdu do it */
1799
    apdu.flags |= SC_APDU_FLAGS_SM_CHAINING;
1800
  }
1801
#endif
1802
5.42k
  apdu.lc = sendbuflen;
1803
5.42k
  apdu.datalen = sendbuflen;
1804
5.42k
  apdu.data = sendbuf;
1805
1806
5.42k
  if (recvbuf && recvbuflen) {
1807
5.42k
    apdu.le = (recvbuflen > 256) ? 256 : recvbuflen;
1808
5.42k
    apdu.resplen = recvbuflen;
1809
5.42k
  } else {
1810
0
    apdu.le = 0;
1811
0
    apdu.resplen = 0;
1812
0
  }
1813
5.42k
  apdu.resp = recvbuf;
1814
1815
  /* with new adpu.c and chaining, this actually reads the whole object */
1816
5.42k
  r = sc_transmit_apdu(card, &apdu);
1817
1818
  /* adpu will not have sw1,sw2 set because sc_sm_single_transmit called sc_sm_stop, */
1819
5.42k
  if (r < 0) {
1820
46
    sc_log(card->ctx, "Transmit failed");
1821
46
    goto err;
1822
46
  }
1823
1824
5.37k
  if (apdu.sw1 == 0x69 && apdu.sw2 == 0x88)
1825
10
    r = SC_ERROR_SM_INVALID_SESSION_KEY;
1826
5.36k
  else
1827
5.36k
    r = sc_check_sw(card, apdu.sw1, apdu.sw2);
1828
1829
5.37k
  if (r < 0) {
1830
4.27k
    sc_log(card->ctx, "Card returned error ");
1831
4.27k
    goto err;
1832
4.27k
  }
1833
1834
1.10k
  r = (int)apdu.resplen;
1835
1836
5.42k
err:
1837
5.42k
  sc_unlock(card);
1838
5.42k
  LOG_FUNC_RETURN(card->ctx, r);
1839
5.42k
}
1840
1841
#ifdef ENABLE_PIV_SM
1842
/* convert q as 04||x||y used in standard point formats to expanded leading
1843
 * zeros and concatenated X||Y as specified in SP80056A Appendix C.2
1844
 * Field-Element-to-Byte-String Conversion which
1845
 * OpenSSL has already converted X and Y to big endian and skipped leading
1846
 * zero bytes.
1847
 */
1848
static int
1849
Q2OS(int fsize, u8 *Q, size_t Qlen, u8 *OS, size_t *OSlen)
1850
{
1851
  size_t i;
1852
  size_t f = fsize / 8;
1853
1854
  i = (Qlen - 1) / 2;
1855
1856
  if (!OS || *OSlen < f * 2 || !Q || i > f)
1857
    return SC_ERROR_INTERNAL;
1858
1859
  memset(OS, 0, f * 2);
1860
  /* Check this if x and y have leading zero bytes,
1861
   * In UNCOMPRESSED FORMAT, x and Y must be same length, to tell when
1862
   * one ends and the other starts */
1863
  memcpy(OS + f - i, Q + 1, i);
1864
  memcpy(OS + 2 * f - i, Q + f + 1, i);
1865
  *OSlen = f * 2;
1866
  return 0;
1867
}
1868
1869
/*
1870
 * if needed, send VCI pairing code to card just after the
1871
 * SM key establishment. Called from piv_sm_open under same lock
1872
 */
1873
static int
1874
piv_send_vci_pairing_code(struct sc_card *card, u8 *paring_code)
1875
{
1876
  int r;
1877
  piv_private_data_t *priv = PIV_DATA(card);
1878
  sc_apdu_t plain;
1879
  sc_apdu_t sm_apdu;
1880
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1881
1882
  if (priv->pin_policy & PIV_PP_VCI_WITHOUT_PC)
1883
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_SUCCESS); /* Not needed */
1884
1885
  if ((priv->pin_policy & PIV_PP_VCI_IMPL) == 0)
1886
    SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NO_CARD_SUPPORT);
1887
1888
  sc_format_apdu(card, &plain, SC_APDU_CASE_3_SHORT, 0x20, 0x00, 0x98);
1889
  plain.datalen = plain.lc = 8;
1890
  plain.data = paring_code;
1891
  plain.resp = NULL;
1892
  plain.resplen = plain.le = 0;
1893
1894
  memset(&sm_apdu, 0, sizeof(sm_apdu));
1895
  /* build sm_apdu and set alloc sm_apdu.resp */
1896
  r = piv_encode_apdu(card, &plain, &sm_apdu);
1897
  if (r < 0) {
1898
    free(sm_apdu.resp);
1899
    sc_log(card->ctx, "piv_encode_apdu failed");
1900
    LOG_FUNC_RETURN(card->ctx, r);
1901
  }
1902
1903
  sm_apdu.flags += SC_APDU_FLAGS_NO_SM; /* run as is */
1904
  r = sc_transmit_apdu(card, &sm_apdu);
1905
  if (r < 0) {
1906
    free(sm_apdu.resp);
1907
    sc_log(card->ctx, "transmit failed");
1908
    LOG_FUNC_RETURN(card->ctx, r);
1909
  }
1910
1911
  r = piv_decode_apdu(card, &plain, &sm_apdu);
1912
  free(sm_apdu.resp);
1913
  LOG_TEST_RET(card->ctx, r, "piv_decode_apdu failed");
1914
1915
  r = sc_check_sw(card, plain.sw1, plain.sw2);
1916
  if (r < 0)
1917
    r = SC_ERROR_PIN_CODE_INCORRECT;
1918
1919
  LOG_FUNC_RETURN(card->ctx, r);
1920
}
1921
1922
/* Verify one signature using pubkey */
1923
static int
1924
piv_sm_verify_sig(struct sc_card *card, const EVP_MD *type,
1925
    EVP_PKEY *pkey,
1926
    u8 *data, size_t data_size,
1927
    unsigned char *sig, size_t siglen)
1928
{
1929
  piv_private_data_t *priv = PIV_DATA(card);
1930
  cipher_suite_t *cs = priv->cs;
1931
  int r = 0;
1932
  EVP_MD_CTX *md_ctx = NULL;
1933
1934
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1935
1936
  if (cs == NULL) {
1937
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1938
    goto err;
1939
  }
1940
1941
  if ((md_ctx = EVP_MD_CTX_new()) == NULL ||
1942
      EVP_DigestVerifyInit(md_ctx, NULL, type, NULL, pkey) != 1 ||
1943
      EVP_DigestVerifyUpdate(md_ctx, data, data_size) != 1 ||
1944
      EVP_DigestVerifyFinal(md_ctx, sig, siglen) != 1) {
1945
    sc_log_openssl(card->ctx);
1946
    sc_log(card->ctx, "EVP_DigestVerifyFinal failed");
1947
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1948
    goto err;
1949
  }
1950
  r = SC_SUCCESS;
1951
err:
1952
  EVP_MD_CTX_free(md_ctx);
1953
  LOG_FUNC_RETURN(card->ctx, r);
1954
}
1955
1956
/*
1957
 * If sm_in_cvc is present, verify PIV_OBJ_SM_CERT_SIGNER signed sm_in_cvc
1958
 * and sm_in_cvc signed sm_cvc.
1959
 * If sm_in_cvc is not present verify PIV_OBJ_SM_CERT_SIGNER signed sm_cvc.
1960
 */
1961
1962
static int
1963
piv_sm_verify_certs(struct sc_card *card)
1964
{
1965
  piv_private_data_t *priv = PIV_DATA(card);
1966
  cipher_suite_t *cs = priv->cs;
1967
  int r = 0;
1968
  u8 *cert_blob_unzipped = NULL; /* free */
1969
  u8 *cert_blob = NULL;        /* do not free */
1970
  size_t cert_bloblen = 0;
1971
1972
  u8 *rbuf; /* do not free*/
1973
  size_t rbuflen;
1974
  X509 *cert = NULL;
1975
  EVP_PKEY *cert_pkey = NULL; /* do not free */
1976
  EVP_PKEY *in_cvc_pkey = NULL;
1977
#if OPENSSL_VERSION_NUMBER < 0x30000000L
1978
  EC_GROUP *in_cvc_group = NULL;
1979
  EC_POINT *in_cvc_point = NULL;
1980
  EC_KEY *in_cvc_eckey = NULL;
1981
#else
1982
  EVP_PKEY_CTX *in_cvc_pkey_ctx = NULL;
1983
  OSSL_PARAM params[3];
1984
  size_t params_n;
1985
#endif
1986
1987
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
1988
1989
  if (cs == NULL) {
1990
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
1991
    goto err;
1992
  }
1993
1994
  /*
1995
   * Get the PIV_OBJ_SM_CERT_SIGNER and optional sm_in_cvc in cache
1996
   * both are in same object. Rbuf, and rbuflen are needed but not used here
1997
   * sm_cvc and sm_in_cvc both have EC_keys sm_in_cvc may have RSA signature
1998
   */
1999
  r = piv_get_cached_data(card, PIV_OBJ_SM_CERT_SIGNER, &rbuf, &rbuflen);
2000
  if (r < 0) {
2001
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2002
    goto err;
2003
  }
2004
  r = piv_cache_internal_data(card, PIV_OBJ_SM_CERT_SIGNER);
2005
  if (r < 0) {
2006
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2007
    goto err;
2008
  }
2009
2010
  priv->sm_flags |= PIV_SM_FLAGS_SM_CERT_SIGNER_PRESENT; /* set for debugging */
2011
2012
  /* get PIV_OBJ_SM_CERT_SIGNER cert DER  from cache */
2013
  if (priv->obj_cache[PIV_OBJ_SM_CERT_SIGNER].flags & PIV_OBJ_CACHE_COMPRESSED) {
2014
#ifdef ENABLE_ZLIB
2015
    if (SC_SUCCESS != sc_decompress_alloc(&cert_blob_unzipped, &cert_bloblen,
2016
              priv->obj_cache[PIV_OBJ_SM_CERT_SIGNER].internal_obj_data,
2017
              priv->obj_cache[PIV_OBJ_SM_CERT_SIGNER].internal_obj_len,
2018
              COMPRESSION_AUTO)) {
2019
      sc_log(card->ctx, "PIV decompression of SM CERT_SIGNER failed");
2020
      r = SC_ERROR_OBJECT_NOT_VALID;
2021
      goto err;
2022
    }
2023
    cert_blob = cert_blob_unzipped;
2024
#else
2025
    sc_log(card->ctx, "PIV compression not supported, no zlib");
2026
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
2027
#endif
2028
2029
  } else {
2030
    cert_blob = priv->obj_cache[PIV_OBJ_SM_CERT_SIGNER].internal_obj_data;
2031
    cert_bloblen = priv->obj_cache[PIV_OBJ_SM_CERT_SIGNER].internal_obj_len;
2032
  }
2033
2034
  if (cert_blob == NULL || cert_bloblen == 0) {
2035
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2036
    goto err;
2037
  }
2038
2039
  if ((cert = d2i_X509(NULL, (const u8 **)&cert_blob, cert_bloblen)) == NULL ||
2040
      (cert_pkey = X509_get0_pubkey(cert)) == NULL) {
2041
    sc_log_openssl(card->ctx);
2042
    sc_log(card->ctx, "OpenSSL failed to get pubkey from SM_CERT_SIGNER");
2043
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2044
    goto err;
2045
  }
2046
2047
  /* if intermediate sm_in_cvc present, cert signed it and sm_cvc is signed by sm_in_cvc */
2048
  if (priv->sm_flags & PIV_SM_FLAGS_SM_IN_CVC_PRESENT) {
2049
    r = piv_sm_verify_sig(card, cs->kdf_md(), cert_pkey,
2050
        priv->sm_in_cvc.body, priv->sm_in_cvc.bodylen,
2051
        priv->sm_in_cvc.signature, priv->sm_in_cvc.signaturelen);
2052
    if (r < 0) {
2053
      sc_log(card->ctx, "sm_in_cvc signature invalid");
2054
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2055
      goto err;
2056
    }
2057
2058
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2059
    if ((in_cvc_group = EC_GROUP_new_by_curve_name(cs->nid)) == NULL ||
2060
        (in_cvc_pkey = EVP_PKEY_new()) == NULL ||
2061
        (in_cvc_eckey = EC_KEY_new_by_curve_name(cs->nid)) == NULL ||
2062
        (in_cvc_point = EC_POINT_new(in_cvc_group)) == NULL ||
2063
        EC_POINT_oct2point(in_cvc_group, in_cvc_point,
2064
            priv->sm_in_cvc.publicPoint, priv->sm_in_cvc.publicPointlen, NULL) <= 0 ||
2065
        EC_KEY_set_public_key(in_cvc_eckey, in_cvc_point) <= 0 ||
2066
        EVP_PKEY_set1_EC_KEY(in_cvc_pkey, in_cvc_eckey) != 1) {
2067
      sc_log_openssl(card->ctx);
2068
      sc_log(card->ctx, "OpenSSL failed to set EC pubkey, during verify");
2069
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2070
      goto err;
2071
    }
2072
#else
2073
    params_n = 0;
2074
    params[params_n++] = OSSL_PARAM_construct_utf8_string("group", cs->curve_group, 0);
2075
    params[params_n++] = OSSL_PARAM_construct_octet_string("pub",
2076
        priv->sm_in_cvc.publicPoint, priv->sm_in_cvc.publicPointlen);
2077
    params[params_n] = OSSL_PARAM_construct_end();
2078
2079
    if (!(in_cvc_pkey_ctx = EVP_PKEY_CTX_new_from_name(PIV_LIBCTX, "EC", NULL)) ||
2080
        !EVP_PKEY_fromdata_init(in_cvc_pkey_ctx) ||
2081
        !EVP_PKEY_fromdata(in_cvc_pkey_ctx, &in_cvc_pkey, EVP_PKEY_PUBLIC_KEY, params) ||
2082
        !in_cvc_pkey) {
2083
      sc_log_openssl(card->ctx);
2084
      sc_log(card->ctx, "OpenSSL failed to set EC pubkey, during verify");
2085
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2086
      goto err;
2087
    }
2088
#endif
2089
    r = piv_sm_verify_sig(card, cs->kdf_md(), in_cvc_pkey,
2090
        priv->sm_cvc.body, priv->sm_cvc.bodylen,
2091
        priv->sm_cvc.signature, priv->sm_cvc.signaturelen);
2092
2093
  } else { /* cert signed  sm_cvc */
2094
    r = piv_sm_verify_sig(card, cs->kdf_md(), cert_pkey,
2095
        priv->sm_cvc.body, priv->sm_cvc.bodylen,
2096
        priv->sm_cvc.signature, priv->sm_cvc.signaturelen);
2097
  }
2098
  if (r < 0) {
2099
    sc_log(card->ctx, "sm_cvc signature invalid");
2100
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2101
    goto err;
2102
  }
2103
2104
  /* cert chain signatures match for oncard certs */
2105
2106
err:
2107
  X509_free(cert);
2108
  free(cert_blob_unzipped);
2109
2110
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2111
  EC_GROUP_free(in_cvc_group);
2112
  EC_POINT_free(in_cvc_point);
2113
  EC_KEY_free(in_cvc_eckey);
2114
#else
2115
  EVP_PKEY_CTX_free(in_cvc_pkey_ctx);
2116
#endif
2117
  EVP_PKEY_free(in_cvc_pkey);
2118
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
2119
}
2120
2121
/*
2122
 * NIST SP800-73-4  4.1 The key Establishment Protocol
2123
 * Variable names and Steps  are based on Client Application (h)
2124
 * and PIV Card Application (icc)
2125
 * Capital leters used for variable, and lower case for subscript names
2126
 */
2127
static int
2128
piv_sm_open(struct sc_card *card)
2129
{
2130
  piv_private_data_t *priv = PIV_DATA(card);
2131
  cipher_suite_t *cs = priv->cs;
2132
  int r = 0;
2133
  int i;
2134
  int reps;
2135
  u8 CBh;
2136
  u8 CBicc;
2137
  u8 *p;
2138
2139
  /* ephemeral EC key */
2140
  EVP_PKEY_CTX *eph_ctx = NULL;
2141
  EVP_PKEY *eph_pkey = NULL;
2142
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2143
  EC_KEY *eph_eckey = NULL;   /* don't free _get0_*/
2144
  const EC_GROUP *eph_group = NULL; /* don't free _get0_ */
2145
#else
2146
  OSSL_PARAM eph_params[5];
2147
  size_t eph_params_n;
2148
  size_t Qehxlen = 0;
2149
  u8 *Qehx = NULL;
2150
#endif
2151
  size_t Qehlen = 0;
2152
  u8 Qeh[2 * PIV_SM_MAX_FIELD_LENGTH / 8 + 1]; /*  big enough for 384 04||x||y  if x and y have leading zeros, length may be less */
2153
  size_t Qeh_OSlen = 0;
2154
  u8 Qeh_OS[2 * PIV_SM_MAX_FIELD_LENGTH / 8]; /* no leading 04, with leading zeros in X and Y */
2155
  size_t Qsicc_OSlen = 0;
2156
  u8 Qsicc_OS[2 * PIV_SM_MAX_FIELD_LENGTH / 8]; /* no leading 04, with leading zeros  in X and Y */
2157
2158
  /* pub EC key from card Cicc in sm_cvc */
2159
  EVP_PKEY_CTX *Cicc_ctx = NULL;
2160
  EVP_PKEY *Cicc_pkey = NULL;
2161
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2162
  EC_KEY *Cicc_eckey = NULL;
2163
  EC_POINT *Cicc_point = NULL;
2164
  EC_GROUP *Cicc_group = NULL;
2165
#endif
2166
2167
  /* shared secret key Z */
2168
  EVP_PKEY_CTX *Z_ctx = NULL;
2169
  u8 *Z = NULL;
2170
  size_t Zlen = 0;
2171
2172
  u8 IDsh[8] = {0};
2173
  unsigned long pid;
2174
2175
  u8 *sbuf = NULL;
2176
  size_t sbuflen;
2177
  int len2a, len2b;
2178
2179
  u8 rbuf[4096];
2180
  size_t rbuflen = sizeof(rbuf);
2181
2182
  const u8 *body, *payload;
2183
  size_t bodylen, payloadlen;
2184
  u8 Nicc[24]; /* nonce */
2185
  u8 AuthCryptogram[16];
2186
2187
  u8 *cvcder = NULL;
2188
  size_t cvclen = 0;
2189
  size_t len; /* temp len */
2190
2191
  u8 *kdf_in = NULL;
2192
  size_t kdf_inlen = 0;
2193
  unsigned int hashlen = 0;
2194
  u8 aeskeys[SHA384_DIGEST_LENGTH * 3] = {0}; /*  4 keys, Hash function is run 2 or 3 times max is 3 * 384/8 see below */
2195
  EVP_MD_CTX *hash_ctx = NULL;
2196
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2197
  CMAC_CTX *cmac_ctx = NULL;
2198
#else
2199
  EVP_MAC *mac = NULL;
2200
  EVP_MAC_CTX *cmac_ctx = NULL;
2201
  OSSL_PARAM cmac_params[2];
2202
  size_t cmac_params_n = 0;
2203
  OSSL_PARAM Cicc_params[3];
2204
  size_t Cicc_params_n = 0;
2205
#endif
2206
2207
  u8 IDsicc[8]; /* will only use 8 bytes for step H6 */
2208
2209
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
2210
2211
  /*
2212
   * The SM routines try and call this on their own.
2213
   * This routine should only be called by the card driver.
2214
   * which has set PIV_SM_FLAGS_DEFER_OPEN and unset in
2215
   * in reader_lock_obtained
2216
   * after testing PIC applet is active so SM is setup in same transaction
2217
   * as the command we are trying to run with SM.
2218
   * this avoids situation where the SM is established, and then reset by
2219
   * some other application without getting anything done or in
2220
   * a loop, each trying to reestablish a SM session and run command.
2221
   */
2222
  if (!(priv->sm_flags & PIV_SM_FLAGS_DEFER_OPEN)) {
2223
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ALLOWED);
2224
  }
2225
  if (cs == NULL)
2226
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
2227
2228
  r = sc_lock(card);
2229
  if (r != SC_SUCCESS) {
2230
    sc_log(card->ctx, "sc_lock failed");
2231
    return r;
2232
  }
2233
2234
  /* use for several hash operations */
2235
  if ((hash_ctx = EVP_MD_CTX_new()) == NULL) {
2236
    r = SC_ERROR_OUT_OF_MEMORY;
2237
    goto err;
2238
  }
2239
2240
  /* Step 1 set CBh = 0 */
2241
  CBh = 0;
2242
2243
  /* Step H2 generate ephemeral EC */
2244
2245
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2246
  if ((eph_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL ||
2247
      EVP_PKEY_keygen_init(eph_ctx) <= 0 ||
2248
      EVP_PKEY_CTX_set_ec_paramgen_curve_nid(eph_ctx, cs->nid) <= 0 ||
2249
      EVP_PKEY_keygen(eph_ctx, &eph_pkey) <= 0 ||
2250
      (eph_eckey = EVP_PKEY_get0_EC_KEY(eph_pkey)) == NULL ||
2251
      (eph_group = EC_KEY_get0_group(eph_eckey)) == NULL ||
2252
      (Qehlen = EC_POINT_point2oct(eph_group, EC_KEY_get0_public_key(eph_eckey),
2253
           POINT_CONVERSION_UNCOMPRESSED, NULL, Qehlen, NULL)) <= 0 || /* get length */
2254
      Qehlen > cs->Qlen ||
2255
      (Qehlen = EC_POINT_point2oct(eph_group, EC_KEY_get0_public_key(eph_eckey),
2256
           POINT_CONVERSION_UNCOMPRESSED, Qeh, Qehlen, NULL)) <= 0 ||
2257
      Qehlen > cs->Qlen) {
2258
    sc_log_openssl(card->ctx);
2259
    sc_log(card->ctx, "OpenSSL failed to create ephemeral EC key");
2260
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2261
    goto err;
2262
  }
2263
#else
2264
  /* generate Qeh */
2265
  eph_params_n = 0;
2266
  eph_params[eph_params_n++] = OSSL_PARAM_construct_utf8_string("group", cs->curve_group, 0);
2267
  eph_params[eph_params_n++] = OSSL_PARAM_construct_utf8_string("point-format", "uncompressed", 0);
2268
  eph_params[eph_params_n] = OSSL_PARAM_construct_end();
2269
  if (!(eph_ctx = EVP_PKEY_CTX_new_from_name(PIV_LIBCTX, "EC", NULL)) ||
2270
      !EVP_PKEY_keygen_init(eph_ctx) ||
2271
      !EVP_PKEY_CTX_set_params(eph_ctx, eph_params) ||
2272
      !EVP_PKEY_generate(eph_ctx, &eph_pkey) ||
2273
      !(Qehxlen = EVP_PKEY_get1_encoded_public_key(eph_pkey, &Qehx)) ||
2274
      !Qehx ||
2275
      Qehxlen > cs->Qlen) {
2276
    sc_log_openssl(card->ctx);
2277
    sc_log(card->ctx, "OpenSSL failed to create ephemeral EC key");
2278
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2279
    goto err;
2280
  }
2281
  memcpy(Qeh, Qehx, Qehxlen);
2282
  Qehlen = Qehxlen;
2283
#endif
2284
2285
  /* For later use, get  Qeh without 04 and full size  X || Y */
2286
  Qeh_OSlen = sizeof(Qeh_OS);
2287
  if (Q2OS(cs->field_length, Qeh, Qehlen, Qeh_OS, &Qeh_OSlen)) {
2288
    sc_log(card->ctx, "Q2OS for Qeh failed");
2289
    r = SC_ERROR_INTERNAL;
2290
    goto err;
2291
  }
2292
2293
  r = len2a = sc_asn1_put_tag(0x81, NULL, 1 + sizeof(IDsh) + Qehlen, NULL, 0, NULL);
2294
  if (r < 0)
2295
    goto err;
2296
  r = len2b = sc_asn1_put_tag(0x80, NULL, 0, NULL, 0, NULL);
2297
  if (r < 0)
2298
    goto err;
2299
  sbuflen = r = sc_asn1_put_tag(0x7C, NULL, len2a + len2b, NULL, 0, NULL);
2300
  if (r < 0)
2301
    goto err;
2302
2303
  sbuf = malloc(sbuflen);
2304
  if (sbuf == NULL) {
2305
    r = SC_ERROR_OUT_OF_MEMORY;
2306
    goto err;
2307
  }
2308
  p = sbuf;
2309
2310
  r = sc_asn1_put_tag(0x7C, NULL, len2a + len2b, sbuf, sbuflen, &p);
2311
  if (r != SC_SUCCESS)
2312
    goto err;
2313
2314
  r = sc_asn1_put_tag(0x81, NULL, 1 + sizeof(IDsh) + Qehlen, p, sbuflen - (p - sbuf), &p);
2315
  if (r != SC_SUCCESS)
2316
    goto err;
2317
2318
  /* Step H1 set CBh to 0x00 */
2319
  *p++ = CBh;
2320
2321
#ifdef WIN32
2322
  pid = (unsigned long)GetCurrentProcessId();
2323
#else
2324
  pid = (unsigned long)getpid(); /* use PID as our ID so different from other processes */
2325
#endif
2326
  memcpy(IDsh, &pid, MIN(sizeof(pid), sizeof(IDsh)));
2327
  memcpy(p, IDsh, sizeof(IDsh));
2328
  p += sizeof(IDsh);
2329
  memcpy(p, Qeh, Qehlen);
2330
  p += Qehlen;
2331
2332
  r = sc_asn1_put_tag(0x82, NULL, 0, p, sbuflen - (p - sbuf), &p); /* null data */
2333
  if (r != SC_SUCCESS)
2334
    goto err;
2335
2336
  /* Step H3 send CBh||IDsh|| Qeh  Qeh in 04||x||y */
2337
  /* Or call sc_transmit directly */
2338
  r = piv_general_io(card, 0x87, cs->p1, 0x04, sbuf, (p - sbuf), rbuf, rbuflen);
2339
  if (r <= 0)
2340
    goto err;
2341
2342
  rbuflen = r;
2343
  p = rbuf;
2344
2345
  body = sc_asn1_find_tag(card->ctx, rbuf, rbuflen, 0x7C, &bodylen);
2346
  if (body == NULL || bodylen < 20 || rbuf[0] != 0x7C) {
2347
    sc_log(card->ctx, "SM response data to short");
2348
    r = SC_ERROR_SM_NO_SESSION_KEYS;
2349
    goto err;
2350
  }
2351
2352
  payload = sc_asn1_find_tag(card->ctx, body, bodylen, 0x82, &payloadlen);
2353
  if (payload == NULL || payloadlen < 1 + cs->Nicclen + cs->AuthCryptogramlen || *body != 0x82) {
2354
    sc_log(card->ctx, "SM response data to short");
2355
    r = SC_ERROR_SM_NO_SESSION_KEYS;
2356
    goto err;
2357
  }
2358
2359
  /* payload is CBicc (1) || Nicc (16 or 24) || AuthCryptogram (CMAC 16 or 16) ||Cicc (variable) */
2360
  p = (u8 *)payload;
2361
2362
  /* Step H4 check CBicc == 0x00 */
2363
  CBicc = *p++;
2364
  if (CBicc != 0x00) { /* CBicc must be  zero */
2365
    sc_log(card->ctx, "SM card did not accept request");
2366
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2367
    goto err;
2368
  }
2369
2370
  memcpy(Nicc, p, cs->Nicclen);
2371
  p += cs->Nicclen;
2372
2373
  memcpy(AuthCryptogram, p, cs->AuthCryptogramlen);
2374
  p += cs->AuthCryptogramlen;
2375
2376
  if (p > payload + payloadlen) {
2377
    sc_log(card->ctx, "SM card CVC is to short");
2378
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2379
    goto err;
2380
  }
2381
  cvclen = len = payloadlen - (p - payload);
2382
  if (len) {
2383
    cvcder = p; /* in rbuf */
2384
2385
    r = piv_decode_cvc(card, &p, &len, &priv->sm_cvc);
2386
    if (r != SC_SUCCESS) {
2387
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2388
      goto err;
2389
    }
2390
    priv->sm_flags |= PIV_SM_FLAGS_SM_CVC_PRESENT;
2391
  }
2392
2393
  /* Step H5 Verify Cicc CVC and pubkey */
2394
  /* Verify Cicc (sm_cvc) is signed by sm_in_cvc or PIV_OBJ_SM_CERT_SIGNER  */
2395
  /* sm_in_cvc is signed by PIV_OBJ_SM_CERT_SIGNER */
2396
2397
  /* Verify the cert chain is valid. */
2398
  r = piv_sm_verify_certs(card);
2399
  if (r < 0) {
2400
    sc_log(card->ctx, "SM  piv_sm_verify_certs r:%d", r);
2401
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2402
    goto err;
2403
  }
2404
2405
  /* Step H6  need left most 8 bytes of hash of sm_cvc */
2406
  {
2407
    u8 hash[SHA256_DIGEST_LENGTH] = {0};
2408
    const u8 *tag;
2409
    size_t taglen;
2410
    const u8 *tmpder;
2411
    size_t tmpderlen;
2412
2413
    if ((tag = sc_asn1_find_tag(card->ctx, cvcder, cvclen, 0x7F21, &taglen)) == NULL ||
2414
        *cvcder != 0x7F || *(cvcder + 1) != 0x21) {
2415
2416
      r = SC_ERROR_INTERNAL;
2417
      goto err;
2418
    }
2419
2420
    /* debug choice */
2421
    tmpder = cvcder;
2422
    tmpderlen = cvclen;
2423
2424
    if (EVP_DigestInit(hash_ctx, EVP_sha256()) != 1 ||
2425
        EVP_DigestUpdate(hash_ctx, tmpder, tmpderlen) != 1 ||
2426
        EVP_DigestFinal_ex(hash_ctx, hash, NULL) != 1) {
2427
      sc_log_openssl(card->ctx);
2428
      sc_log(card->ctx, "IDsicc hash failed");
2429
      r = SC_ERROR_INTERNAL;
2430
      goto err;
2431
    }
2432
    memcpy(IDsicc, hash, sizeof(IDsicc)); /* left 8 bytes */
2433
  }
2434
2435
  /* Step H7 get the cards public key Qsicc into OpenSSL Cicc_eckey */
2436
2437
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2438
  if ((Cicc_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL ||
2439
      (Cicc_group = EC_GROUP_new_by_curve_name(cs->nid)) == NULL ||
2440
      (Cicc_pkey = EVP_PKEY_new()) == NULL ||
2441
      (Cicc_eckey = EC_KEY_new_by_curve_name(cs->nid)) == NULL ||
2442
      (Cicc_point = EC_POINT_new(Cicc_group)) == NULL ||
2443
      EC_POINT_oct2point(Cicc_group, Cicc_point,
2444
          priv->sm_cvc.publicPoint, priv->sm_cvc.publicPointlen, NULL) <= 0 ||
2445
      EC_KEY_set_public_key(Cicc_eckey, Cicc_point) <= 0 ||
2446
      EVP_PKEY_set1_EC_KEY(Cicc_pkey, Cicc_eckey) <= 0) {
2447
    sc_log_openssl(card->ctx);
2448
    sc_log(card->ctx, "OpenSSL failed to get card's EC pubkey");
2449
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2450
    goto err;
2451
  }
2452
#else
2453
  Cicc_params_n = 0;
2454
  Cicc_params[Cicc_params_n++] = OSSL_PARAM_construct_utf8_string("group", cs->curve_group, 0);
2455
  Cicc_params[Cicc_params_n++] = OSSL_PARAM_construct_octet_string("pub",
2456
      priv->sm_cvc.publicPoint, priv->sm_cvc.publicPointlen);
2457
  Cicc_params[Cicc_params_n] = OSSL_PARAM_construct_end();
2458
2459
  if (!(Cicc_ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) ||
2460
      !EVP_PKEY_fromdata_init(Cicc_ctx) ||
2461
      !EVP_PKEY_fromdata(Cicc_ctx, &Cicc_pkey, EVP_PKEY_PUBLIC_KEY, Cicc_params) ||
2462
      !Cicc_pkey) {
2463
    sc_log_openssl(card->ctx);
2464
    sc_log(card->ctx, "OpenSSL failed to set EC pubkey for Cicc");
2465
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2466
    goto err;
2467
  }
2468
#endif
2469
2470
  /* Qsicc without 04 and expanded x||y */
2471
  Qsicc_OSlen = sizeof(Qsicc_OS);
2472
  if (Q2OS(cs->field_length, priv->sm_cvc.publicPoint, priv->sm_cvc.publicPointlen, Qsicc_OS, &Qsicc_OSlen)) {
2473
    sc_log(card->ctx, "Q2OS for Qsicc failed");
2474
    r = SC_ERROR_INTERNAL;
2475
    goto err;
2476
  }
2477
2478
  /* Step H8 Compute the shared secret Z */
2479
  if ((Z_ctx = EVP_PKEY_CTX_new(eph_pkey, NULL)) == NULL ||
2480
      EVP_PKEY_derive_init(Z_ctx) <= 0 ||
2481
      EVP_PKEY_derive_set_peer(Z_ctx, Cicc_pkey) <= 0 ||
2482
      EVP_PKEY_derive(Z_ctx, NULL, &Zlen) <= 0 ||
2483
      Zlen != cs->Zlen ||
2484
      (Z = malloc(Zlen)) == NULL ||
2485
      EVP_PKEY_derive(Z_ctx, Z, &Zlen) <= 0 ||
2486
      Zlen != cs->Zlen) {
2487
    sc_log_openssl(card->ctx);
2488
    sc_log(card->ctx, "OpenSSL failed to create secret Z");
2489
    r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2490
    goto err;
2491
  }
2492
2493
  sc_log(card->ctx, "debug Zlen:%" SC_FORMAT_LEN_SIZE_T "u Z[0]:0x%2.2x", Zlen, Z[0]);
2494
2495
  /* Step H9 zeroize deh from step H2 */
2496
  EVP_PKEY_free(eph_pkey); /* OpenSSL  BN_clear_free calls OPENSSL_cleanse */
2497
  eph_pkey = NULL;
2498
2499
  /* Step H10 Create AES session Keys */
2500
  /* kdf in is 4byte counter || Z || otherinfo  800-56A 5.8.1 */
2501
2502
  kdf_inlen = 4 + Zlen + cs->otherinfolen;
2503
  kdf_in = malloc(kdf_inlen);
2504
  if (kdf_in == NULL) {
2505
    r = SC_ERROR_OUT_OF_MEMORY;
2506
    goto err;
2507
  }
2508
  p = kdf_in;
2509
  *p++ = 0x00;
2510
  *p++ = 0x00;
2511
  *p++ = 0x00;
2512
  *p++ = 0x01;
2513
  memcpy(p, Z, cs->Zlen);
2514
  p += Zlen;
2515
2516
  /* otherinfo */
2517
  *p++ = cs->o0len;
2518
  for (i = 0; i < cs->o0len; i++)
2519
    *p++ = cs->o0_char; /* 0x09 or 0x0d */
2520
2521
  *p++ = sizeof(IDsh);
2522
  memcpy(p, IDsh, sizeof(IDsh));
2523
  p += sizeof(IDsh);
2524
2525
  *p++ = cs->CBhlen;
2526
  memcpy(p, &CBh, cs->CBhlen);
2527
  p += cs->CBhlen;
2528
2529
  *p++ = cs->T16Qehlen;
2530
  /* First 16 bytes of Qeh without 04 800-56A Appendix C.2 */
2531
  memcpy(p, Qeh_OS, cs->T16Qehlen);
2532
  p += cs->T16Qehlen;
2533
2534
  *p++ = cs->IDsicclen;
2535
  memcpy(p, IDsicc, cs->IDsicclen);
2536
  p += cs->IDsicclen;
2537
2538
  *p++ = cs->Nicclen;
2539
  memcpy(p, Nicc, cs->Nicclen);
2540
  p += cs->Nicclen;
2541
2542
  *p++ = cs->CBicclen;
2543
  memcpy(p, &CBicc, cs->CBicclen);
2544
  p += cs->CBicclen;
2545
2546
  if (p != kdf_in + kdf_inlen) {
2547
    r = SC_ERROR_INTERNAL;
2548
    goto err;
2549
  }
2550
2551
  /* 4 keys needs reps =  ceil (naeskeys * aeskeylen) / kdf_hash_size) */
2552
  /* 800-56A-2007, 5.8.1 Process and 800-56C rev 3 2018 4.1 Process. */
2553
  /* so it is 2 times for 128 or 3 times for 256 bit AES keys */
2554
  p = aeskeys; /* 4 keys + overflow */
2555
  reps = (cs->naeskeys * cs->aeskeylen + cs->kdf_hash_size - 1) / (cs->kdf_hash_size);
2556
2557
  EVP_MD_CTX_reset(hash_ctx);
2558
  for (i = 0; i < reps; i++) {
2559
    if (EVP_DigestInit(hash_ctx, (*cs->kdf_md)()) != 1 ||
2560
        EVP_DigestUpdate(hash_ctx, kdf_in, kdf_inlen) != 1 ||
2561
        EVP_DigestFinal_ex(hash_ctx, p, &hashlen) != 1) {
2562
      sc_log_openssl(card->ctx);
2563
      sc_log(card->ctx, "KDF hash failed");
2564
      r = SC_ERROR_INTERNAL;
2565
      goto err;
2566
    }
2567
    kdf_in[3]++; /* inc the counter */
2568
    p += cs->kdf_hash_size;
2569
  }
2570
2571
  /* copy keys used for APDU */
2572
  memset(&priv->sm_session, 0, sizeof(piv_sm_session_t)); /* clear */
2573
  priv->sm_session.aes_size = cs->aeskeylen;
2574
  memcpy(&priv->sm_session.SKcfrm, &aeskeys[cs->aeskeylen * 0], cs->aeskeylen);
2575
  memcpy(&priv->sm_session.SKmac, &aeskeys[cs->aeskeylen * 1], cs->aeskeylen);
2576
  memcpy(&priv->sm_session.SKenc, &aeskeys[cs->aeskeylen * 2], cs->aeskeylen);
2577
  memcpy(&priv->sm_session.SKrmac, &aeskeys[cs->aeskeylen * 3], cs->aeskeylen);
2578
  sc_mem_clear(&aeskeys, sizeof(aeskeys));
2579
2580
  priv->sm_session.enc_counter[15] = 0x01;
2581
  priv->sm_session.resp_enc_counter[0] = 0x80;
2582
  priv->sm_session.resp_enc_counter[15] = 0x01;
2583
  /* C_MCV is zero */
2584
  /* R_MCV is zero */
2585
2586
  /*  Step H11 Zeroize Z (and kdf_in which has Z) */
2587
  if (Z && Zlen) {
2588
    sc_mem_clear(Z, Zlen);
2589
    free(Z);
2590
    Z = NULL;
2591
    Zlen = 0;
2592
  }
2593
  if (kdf_in && kdf_inlen) {
2594
    sc_mem_clear(kdf_in, kdf_inlen);
2595
    free(kdf_in);
2596
    kdf_in = NULL;
2597
    kdf_inlen = 0;
2598
  }
2599
2600
  /* Step H12 check AuthCryptogramting our version  */
2601
  /* Generate CMAC */
2602
2603
  {
2604
    u8 Check_AuthCryptogram[32];
2605
    size_t Check_Alen = 0;
2606
2607
    u8 MacData[200];
2608
    int MacDatalen;
2609
    memset(MacData, 0, sizeof(MacData));
2610
2611
    p = MacData;
2612
    memcpy(p, "\x4B\x43\x5f\x31\x5f\x56", 6);
2613
    p += 6;
2614
    memcpy(p, IDsicc, cs->IDsicclen);
2615
    p += cs->IDsicclen;
2616
    memcpy(p, IDsh, sizeof(IDsh));
2617
    p += sizeof(IDsh);
2618
2619
    memcpy(p, Qeh_OS, Qeh_OSlen);
2620
    p += Qeh_OSlen;
2621
    MacDatalen = (int)(p - MacData);
2622
2623
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2624
    if ((cmac_ctx = CMAC_CTX_new()) == NULL ||
2625
        CMAC_Init(cmac_ctx, priv->sm_session.SKcfrm, cs->aeskeylen, (*cs->cipher_cbc)(), NULL) != 1 ||
2626
        CMAC_Update(cmac_ctx, MacData, MacDatalen) != 1 ||
2627
        CMAC_Final(cmac_ctx, Check_AuthCryptogram, &Check_Alen) != 1) {
2628
      r = SC_ERROR_INTERNAL;
2629
      sc_log_openssl(card->ctx);
2630
      sc_log(card->ctx, "AES_CMAC failed %d", r);
2631
      goto err;
2632
    }
2633
#else
2634
    mac = EVP_MAC_fetch(PIV_LIBCTX, "cmac", NULL);
2635
    cmac_params[cmac_params_n++] = OSSL_PARAM_construct_utf8_string("cipher", cs->cipher_cbc_name, 0);
2636
2637
    cmac_params[cmac_params_n] = OSSL_PARAM_construct_end();
2638
    if (mac == NULL ||
2639
        (cmac_ctx = EVP_MAC_CTX_new(mac)) == NULL ||
2640
        !EVP_MAC_init(cmac_ctx, priv->sm_session.SKcfrm,
2641
            priv->sm_session.aes_size, cmac_params) ||
2642
        !EVP_MAC_update(cmac_ctx, MacData, MacDatalen) ||
2643
        !EVP_MAC_final(cmac_ctx, Check_AuthCryptogram, &Check_Alen, cs->AuthCryptogramlen)) {
2644
      sc_log_openssl(card->ctx);
2645
      r = SC_ERROR_INTERNAL;
2646
      sc_log(card->ctx, "AES_CMAC failed %d", r);
2647
      goto err;
2648
    }
2649
#endif
2650
2651
    if (0 == memcmp(AuthCryptogram, Check_AuthCryptogram, cs->AuthCryptogramlen)) {
2652
      sc_log(card->ctx, "AuthCryptogram compare");
2653
      r = 0;
2654
    } else {
2655
      sc_log(card->ctx, "AuthCryptogram compare failed");
2656
      r = SC_ERROR_SM_AUTHENTICATION_FAILED;
2657
      goto err;
2658
    }
2659
  }
2660
2661
  /* VCI only needed for contactless */
2662
  if (priv->init_flags & PIV_INIT_CONTACTLESS) {
2663
    /* Is pairing code required? */
2664
    if (!(priv->pin_policy & PIV_PP_VCI_WITHOUT_PC)) {
2665
      r = piv_send_vci_pairing_code(card, priv->pairing_code);
2666
      if (r < 0)
2667
        goto err;
2668
    }
2669
  }
2670
2671
  r = 0;
2672
  priv->sm_flags |= PIV_SM_FLAGS_SM_IS_ACTIVE;
2673
  card->sm_ctx.sm_mode = SM_MODE_TRANSMIT;
2674
2675
err:
2676
  priv->sm_flags &= ~PIV_SM_FLAGS_DEFER_OPEN;
2677
  if (r != 0) {
2678
    memset(&priv->sm_session, 0, sizeof(piv_sm_session_t));
2679
    sc_log_openssl(card->ctx); /* catch any not logged above */
2680
  }
2681
2682
  sc_unlock(card);
2683
2684
  free(sbuf);
2685
  free(kdf_in);
2686
  free(Z);
2687
2688
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2689
  EC_GROUP_free(Cicc_group);
2690
  EC_POINT_free(Cicc_point);
2691
  EC_KEY_free(Cicc_eckey);
2692
#endif
2693
2694
  EVP_PKEY_free(eph_pkey); /* in case not cleared in step H9 */
2695
  EVP_PKEY_CTX_free(eph_ctx);
2696
  EVP_PKEY_free(Cicc_pkey);
2697
  EVP_PKEY_CTX_free(Cicc_ctx);
2698
  EVP_PKEY_CTX_free(Z_ctx);
2699
  EVP_MD_CTX_free(hash_ctx);
2700
2701
#if OPENSSL_VERSION_NUMBER < 0x30000000L
2702
  CMAC_CTX_free(cmac_ctx);
2703
#else
2704
  EVP_MAC_CTX_free(cmac_ctx);
2705
  EVP_MAC_free(mac);
2706
  OPENSSL_free(Qehx);
2707
#endif
2708
2709
  LOG_FUNC_RETURN(card->ctx, r);
2710
}
2711
#endif /* ENABLE_PIV_SM */
2712
2713
/* Add the PIV-II operations */
2714
/* Should use our own keydata, actually should be common to all cards */
2715
/* RSA and EC are added. */
2716
2717
static int
2718
piv_generate_key(sc_card_t *card,
2719
    sc_cardctl_piv_genkey_info_t *keydata)
2720
0
{
2721
0
  int r;
2722
0
  u8 rbuf[4096];
2723
0
  u8 *p;
2724
0
  const u8 *tag;
2725
0
  u8 tagbuf[16];
2726
0
  u8 outdata[3]; /* we could also add tag 81 for exponent */
2727
0
  size_t taglen;
2728
0
  size_t out_len;
2729
0
  size_t in_len;
2730
0
  unsigned int cla_out, tag_out;
2731
2732
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
2733
2734
0
  keydata->exponent = NULL;
2735
0
  keydata->exponent_len = 0;
2736
0
  keydata->pubkey = NULL;
2737
0
  keydata->pubkey_len = 0;
2738
0
  keydata->ecparam = NULL; /* will show size as we only support 2 curves */
2739
0
  keydata->ecparam_len = 0;
2740
0
  keydata->ecpoint = NULL;
2741
0
  keydata->ecpoint_len = 0;
2742
2743
0
  out_len = 3;
2744
0
  outdata[0] = 0x80;
2745
0
  outdata[1] = 0x01;
2746
0
  outdata[2] = keydata->key_algid;
2747
0
  switch (keydata->key_algid) {
2748
0
  case 0x05:
2749
0
    keydata->key_bits = 3072;
2750
0
    break;
2751
0
  case 0x06:
2752
0
    keydata->key_bits = 1024;
2753
0
    break;
2754
0
  case 0x07:
2755
0
    keydata->key_bits = 2048;
2756
0
    break;
2757
0
  case 0x16: /* Yubico 5.7 support for 4096 */
2758
0
    keydata->key_bits = 4096;
2759
0
    break;
2760
0
  case 0x11:
2761
0
    keydata->key_bits = 0;
2762
0
    keydata->ecparam = 0; /* we only support prime256v1 */
2763
0
    keydata->ecparam_len = 0;
2764
0
    break;
2765
0
  case 0x14:
2766
0
    keydata->key_bits = 0;
2767
0
    keydata->ecparam = 0; /* we only support secp384r1 */
2768
0
    keydata->ecparam_len = 0;
2769
0
    break;
2770
0
  case 0xE0: /* Yubico 5.7 support for EDDSA 25519 */
2771
0
  case 0xE1: /* Yubico 5.7 support for XEDDSA 25519 */
2772
0
    keydata->key_bits = 0;
2773
0
    keydata->ecparam = 0;
2774
0
    keydata->ecparam_len = 0;
2775
0
    break;
2776
0
  default:
2777
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
2778
0
  }
2779
2780
0
  p = tagbuf;
2781
2782
0
  r = sc_asn1_put_tag(0xAC, outdata, out_len, tagbuf, sizeof(tagbuf), &p);
2783
0
  if (r != SC_SUCCESS) {
2784
0
    sc_log(card->ctx, "Failed to encode ASN1 tag");
2785
0
    goto err;
2786
0
  }
2787
2788
0
  r = piv_general_io(card, 0x47, 0x00, keydata->key_num,
2789
0
      tagbuf, p - tagbuf, rbuf, sizeof rbuf);
2790
2791
0
  if (r >= 0) {
2792
0
    const u8 *cp;
2793
2794
0
    cp = rbuf;
2795
0
    in_len = r;
2796
2797
    /* expected tag is 0x7f49,returned as cla_out == 0x60 and tag_out = 0x1F49 */
2798
0
    r = sc_asn1_read_tag(&cp, in_len, &cla_out, &tag_out, &in_len);
2799
0
    if (r < 0 || cp == NULL || in_len == 0 || cla_out != 0x60 || tag_out != 0x1f49) {
2800
0
      r = SC_ERROR_ASN1_OBJECT_NOT_FOUND;
2801
0
    }
2802
0
    if (r != SC_SUCCESS) {
2803
0
      sc_log(card->ctx, "Tag buffer not found");
2804
0
      goto err;
2805
0
    }
2806
2807
    /* if RSA vs EC, ED25519 or X25519 */
2808
0
    if (keydata->key_bits > 0) {
2809
0
      tag = sc_asn1_find_tag(card->ctx, cp, in_len, 0x82, &taglen);
2810
0
      if (tag != NULL && taglen <= 4) {
2811
0
        keydata->exponent = malloc(taglen);
2812
0
        if (keydata->exponent == NULL)
2813
0
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2814
0
        keydata->exponent_len = taglen;
2815
0
        memcpy(keydata->exponent, tag, taglen);
2816
0
      } else {
2817
0
        sc_log(card->ctx, "Tag 0x82 not found");
2818
0
        r = SC_ERROR_UNKNOWN_DATA_RECEIVED;
2819
0
        goto err;
2820
0
      }
2821
2822
0
      tag = sc_asn1_find_tag(card->ctx, cp, in_len, 0x81, &taglen);
2823
0
      if (tag != NULL && taglen > 0) {
2824
0
        keydata->pubkey = malloc(taglen);
2825
0
        if (keydata->pubkey == NULL) {
2826
0
          free(keydata->exponent);
2827
0
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2828
0
        }
2829
0
        keydata->pubkey_len = taglen;
2830
0
        memcpy(keydata->pubkey, tag, taglen);
2831
0
      } else {
2832
0
        sc_log(card->ctx, "Tag 0x81 not found");
2833
0
        r = SC_ERROR_UNKNOWN_DATA_RECEIVED;
2834
0
        goto err;
2835
0
      }
2836
0
    } else { /* must be EC, ED25519 or X25519 */
2837
0
      tag = sc_asn1_find_tag(card->ctx, cp, in_len, 0x86, &taglen);
2838
0
      if (tag != NULL && taglen > 0) {
2839
0
        keydata->ecpoint = malloc(taglen);
2840
0
        if (keydata->ecpoint == NULL)
2841
0
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2842
0
        keydata->ecpoint_len = taglen;
2843
0
        memcpy(keydata->ecpoint, tag, taglen);
2844
0
      } else {
2845
0
        sc_log(card->ctx, "Tag 0x86 not found");
2846
0
        r = SC_ERROR_UNKNOWN_DATA_RECEIVED;
2847
0
        goto err;
2848
0
      }
2849
0
    }
2850
2851
    /* Could add key to cache so could use engine to generate key,
2852
     * and sign req in single operation or write temporary selfsigned
2853
     * certificate with new public key
2854
     */
2855
0
    r = 0;
2856
0
  }
2857
2858
0
err:
2859
0
  if (r < 0) {
2860
0
    free(keydata->exponent);
2861
0
    keydata->exponent = NULL;
2862
0
    free(keydata->pubkey);
2863
0
    keydata->pubkey = NULL;
2864
0
    free(keydata->ecpoint);
2865
0
    keydata->ecpoint = NULL;
2866
0
  }
2867
2868
0
  LOG_FUNC_RETURN(card->ctx, r);
2869
0
}
2870
2871
static int
2872
piv_ai_map_find_by_id(sc_card_t *card, u8 id)
2873
22
{
2874
  /* piv_private_data_t * priv = PIV_DATA(card); */
2875
22
  int i;
2876
2877
130
  for (i = 0; ai_map[i].ai_id != 0; i++) {
2878
119
    if (ai_map[i].ai_id == id)
2879
11
      return i;
2880
119
  }
2881
11
  return -1;
2882
22
}
2883
2884
/* find the PIV AID on the card. If card->type already filled in,
2885
 * then look for specific AID only
2886
 */
2887
2888
static int
2889
piv_find_aid(sc_card_t *card)
2890
4.30k
{
2891
4.30k
  piv_private_data_t *priv = PIV_DATA(card);
2892
4.30k
  u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
2893
4.30k
  int r, i, j;
2894
4.30k
  const u8 *tag;
2895
4.30k
  size_t taglen;
2896
4.30k
  const u8 *nextac;
2897
4.30k
  const u8 *next80;
2898
4.30k
  const u8 *pix;
2899
4.30k
  size_t pixlen;
2900
4.30k
  const u8 *al_label;
2901
4.30k
  size_t al_labellen;
2902
4.30k
  const u8 *actag; /* Cipher Suite */
2903
4.30k
  size_t actaglen;
2904
4.30k
  const u8 *csai; /* Cipher Suite Algorithm Identifier */
2905
4.30k
  size_t csailen;
2906
4.30k
  size_t resplen = sizeof(rbuf);
2907
#ifdef ENABLE_PIV_SM
2908
  int found_csai = 0;
2909
#endif
2910
2911
4.30k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
2912
2913
  /* first  see if the default application will return a template
2914
   * that we know about.
2915
   */
2916
2917
4.30k
  r = iso7816_select_aid(card, piv_aids[0].value, piv_aids[0].len_short, rbuf, &resplen);
2918
4.30k
  if (r > 0 && priv->aid_der.value && resplen == priv->aid_der.len && !memcmp(priv->aid_der.value, rbuf, resplen)) {
2919
0
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
2920
    /* no need to parse again, same as last time */
2921
0
  }
2922
4.30k
  if (r >= 0 && resplen > 2) {
2923
607
    tag = sc_asn1_find_tag(card->ctx, rbuf, resplen, 0x61, &taglen);
2924
607
    if (tag != NULL) {
2925
563
      priv->init_flags |= PIV_INIT_AID_PARSED;
2926
      /* look for 800-73-4 0xAC for Cipher Suite Algorithm Identifier Table 14 */
2927
      /* 800-73-4 only expects 1 0xAC tag len 6 with a 80 01 xx 06 01 00
2928
       * where xx is the SM csID either 27 or 2E.
2929
       * Some vendors may include entries for supported Algorithms even when
2930
       * not required. We will use these if possible
2931
       * Will look for multiple 0x80 with in 0xAC value too.
2932
       */
2933
563
      nextac = tag;
2934
624
      while ((actag = sc_asn1_find_tag(card->ctx, nextac, taglen - (nextac - tag),
2935
624
            0xAC, &actaglen)) != NULL) {
2936
61
        nextac = actag + actaglen;
2937
2938
61
        next80 = actag;
2939
93
        while ((csai = sc_asn1_find_tag(card->ctx, next80, actaglen - (next80 - actag),
2940
93
              0x80, &csailen)) != NULL) {
2941
32
          next80 = csai + csailen;
2942
32
          if (csailen == 1) {
2943
22
            sc_log(card->ctx, "0xAC 0x80 entry:0x%2.2x found", *csai);
2944
22
            j = piv_ai_map_find_by_id(card, *csai);
2945
22
            if (j >= 0) {
2946
11
              priv->alg_ids |= ai_map[j].ai_flag;
2947
11
              continue;
2948
11
            }
2949
#ifdef ENABLE_PIV_SM
2950
            /* check if id is NIST SM */
2951
            for (i = 0; i < PIV_CSS_SIZE; i++) {
2952
              if (*csai != css[i].id)
2953
                continue;
2954
              if (found_csai) {
2955
                sc_log(card->ctx, "found multiple csIDs, using first");
2956
              } else {
2957
                priv->cs = &css[i];
2958
                priv->csID = *csai;
2959
                found_csai++;
2960
                priv->init_flags |= PIV_INIT_AID_AC_SM;
2961
              }
2962
            }
2963
#endif /* ENABLE_PIV_SM */
2964
22
          }
2965
32
        }
2966
61
      }
2967
2968
      /* Last chance to distinguish card type based on Application Label '50' */
2969
563
      al_label = sc_asn1_find_tag(card->ctx, tag, taglen, 0x50, &al_labellen);
2970
563
      if (al_label != NULL) {
2971
3
        sc_log_hex(card->ctx, "Application Label", al_label, al_labellen);
2972
3
        if ((priv->al_label = malloc(al_labellen)) == NULL) {
2973
0
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2974
0
        }
2975
3
        memcpy(priv->al_label, al_label, al_labellen);
2976
3
        priv->al_labellen = (int)al_labellen;
2977
3
      }
2978
2979
563
      pix = sc_asn1_find_tag(card->ctx, tag, taglen, 0x4F, &pixlen);
2980
563
      if (pix != NULL) {
2981
541
        sc_log(card->ctx, "found PIX");
2982
2983
        /* early cards returned full AID, rather then just the pix */
2984
560
        for (i = 0; piv_aids[i].len_long != 0; i++) {
2985
541
          if ((pixlen >= 6 && memcmp(pix, piv_aids[i].value + 5, piv_aids[i].len_long - 5) == 0) ||
2986
96
              ((pixlen >= piv_aids[i].len_short && memcmp(pix, piv_aids[i].value,
2987
522
                               piv_aids[i].len_short) == 0))) {
2988
522
            free(priv->aid_der.value); /* free previous value if any */
2989
522
            if ((priv->aid_der.value = malloc(resplen)) == NULL) {
2990
0
              LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
2991
0
            }
2992
522
            memcpy(priv->aid_der.value, rbuf, resplen);
2993
522
            priv->aid_der.len = resplen;
2994
522
            LOG_FUNC_RETURN(card->ctx, i);
2995
522
          }
2996
541
        }
2997
541
      }
2998
563
    }
2999
607
  }
3000
3001
3.78k
  LOG_FUNC_RETURN(card->ctx, SC_ERROR_NO_CARD_SUPPORT);
3002
3.78k
}
3003
3004
/*
3005
 * Read a DER encoded object from a file. Allocate and return the buf.
3006
 * Used to read the file defined in offCardCertURL from a cache.
3007
 * Also used for testing of History and Discovery objects from a file
3008
 * when testing with a card that does not support these new objects.
3009
 */
3010
static int
3011
piv_read_obj_from_file(sc_card_t *card, char *filename,
3012
    u8 **buf, size_t *buf_len)
3013
0
{
3014
0
  int r;
3015
0
  int r_tag;
3016
0
  int f = -1;
3017
0
  size_t len;
3018
0
  u8 tagbuf[16];
3019
0
  size_t rbuflen;
3020
0
  const u8 *body;
3021
0
  unsigned int cla_out, tag_out;
3022
0
  size_t bodylen;
3023
3024
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3025
3026
0
  *buf = NULL;
3027
0
  *buf_len = 0;
3028
0
  f = open(filename, O_RDONLY);
3029
0
  if (f < 0) {
3030
0
    sc_log(card->ctx, "Unable to load PIV off card file: \"%s\"", filename);
3031
0
    r = SC_ERROR_FILE_NOT_FOUND;
3032
0
    goto err;
3033
0
  }
3034
0
  len = read(f, tagbuf, sizeof(tagbuf)); /* get tag and length */
3035
0
  if (len < 2 || len > sizeof(tagbuf)) {
3036
0
    sc_log(card->ctx, "Problem with \"%s\"", filename);
3037
0
    r = SC_ERROR_DATA_OBJECT_NOT_FOUND;
3038
0
    goto err;
3039
0
  }
3040
0
  body = tagbuf;
3041
  /* accept any tag for now, just get length */
3042
0
  r_tag = sc_asn1_read_tag(&body, len, &cla_out, &tag_out, &bodylen);
3043
0
  if ((r_tag != SC_SUCCESS && r_tag != SC_ERROR_ASN1_END_OF_CONTENTS) ||
3044
0
      body == NULL) {
3045
0
    sc_log(card->ctx, "DER problem");
3046
0
    r = SC_ERROR_FILE_NOT_FOUND;
3047
0
    goto err;
3048
0
  }
3049
0
  rbuflen = body - tagbuf + bodylen;
3050
0
  *buf = malloc(rbuflen);
3051
0
  if (!*buf) {
3052
0
    r = SC_ERROR_OUT_OF_MEMORY;
3053
0
    goto err;
3054
0
  }
3055
0
  memcpy(*buf, tagbuf, len); /* copy first or only part */
3056
  /* read rest of file */
3057
0
  if (rbuflen > len + sizeof(tagbuf)) {
3058
0
    len = read(f, *buf + sizeof(tagbuf), rbuflen - sizeof(tagbuf)); /* read rest */
3059
0
    if (len != rbuflen - sizeof(tagbuf)) {
3060
0
      r = SC_ERROR_INVALID_ASN1_OBJECT;
3061
0
      free(*buf);
3062
0
      *buf = NULL;
3063
0
      goto err;
3064
0
    }
3065
0
  }
3066
0
  r = (int)rbuflen;
3067
0
  *buf_len = rbuflen;
3068
0
err:
3069
0
  if (f >= 0)
3070
0
    close(f);
3071
0
  LOG_FUNC_RETURN(card->ctx, r);
3072
0
}
3073
3074
/* the tag is the PIV_OBJ_*  */
3075
static int
3076
piv_get_data(sc_card_t *card, int enumtag, u8 **buf, size_t *buf_len)
3077
5.40k
{
3078
5.40k
  piv_private_data_t *priv = PIV_DATA(card);
3079
5.40k
  u8 *p;
3080
5.40k
  u8 *tbuf;
3081
5.40k
  int r = 0;
3082
5.40k
  u8 tagbuf[8];
3083
5.40k
  size_t tag_len;
3084
5.40k
  int alloc_buf = 0;
3085
3086
5.40k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3087
3088
5.40k
  sc_log(card->ctx, "#%d, %s", enumtag, piv_objects[enumtag].name);
3089
3090
5.40k
  r = sc_lock(card); /* do check len and get data in same transaction */
3091
5.40k
  if (r != SC_SUCCESS) {
3092
0
    sc_log(card->ctx, "sc_lock failed");
3093
0
    return r;
3094
0
  }
3095
3096
5.40k
  tag_len = piv_objects[enumtag].tag_len;
3097
3098
5.40k
  p = tagbuf;
3099
5.40k
  r = sc_asn1_put_tag(0x5c, piv_objects[enumtag].tag_value, tag_len, tagbuf, sizeof(tagbuf), &p);
3100
5.40k
  if (r != SC_SUCCESS) {
3101
0
    sc_log(card->ctx, "Failed to encode ASN1 tag");
3102
0
    goto err;
3103
0
  }
3104
3105
5.40k
  if (*buf_len == 1 && *buf == NULL) {
3106
5.40k
    *buf_len = priv->max_object_size; /* will allocate below */
3107
5.40k
    alloc_buf = 1;
3108
5.40k
  }
3109
3110
5.40k
  sc_log(card->ctx,
3111
5.40k
      "buffer for #%d *buf=0x%p len=%" SC_FORMAT_LEN_SIZE_T "u",
3112
5.40k
      enumtag, *buf, *buf_len);
3113
5.40k
  if (*buf == NULL && *buf_len > 0) {
3114
5.40k
    if (*buf_len > MAX_FILE_SIZE) {
3115
0
      r = SC_ERROR_INTERNAL;
3116
0
      goto err;
3117
0
    }
3118
5.40k
    *buf = malloc(*buf_len);
3119
5.40k
    if (*buf == NULL) {
3120
0
      r = SC_ERROR_OUT_OF_MEMORY;
3121
0
      goto err;
3122
0
    }
3123
5.40k
  }
3124
3125
#ifdef ENABLE_PIV_SM
3126
  /*
3127
   * Over contact reader, OK to read non sensitive object in clear even when SM is active
3128
   * but only if using default policy and we are not in reader_lock_obtained
3129
   * Discovery object will use SM from reader_lock_obtained to catch if SM is still valid
3130
   * i.e. no interference from other applications
3131
   */
3132
  sc_log(card->ctx, "enumtag:%d sm_ctx.sm_mode:%d piv_objects[enumtag].flags:0x%8.8x sm_flags:0x%8.8lx it_flags:0x%8.8x",
3133
      enumtag, card->sm_ctx.sm_mode, piv_objects[enumtag].flags, priv->sm_flags, priv->init_flags);
3134
  if (priv->sm_flags & PIV_SM_FLAGS_SM_IS_ACTIVE &&
3135
      enumtag != PIV_OBJ_DISCOVERY &&
3136
      card->sm_ctx.sm_mode == SM_MODE_TRANSMIT &&
3137
      !(piv_objects[enumtag].flags & PIV_OBJECT_NEEDS_PIN) &&
3138
      !(priv->sm_flags & (PIV_SM_FLAGS_NEVER | PIV_SM_FLAGS_ALWAYS)) &&
3139
      !(priv->init_flags & (PIV_INIT_CONTACTLESS | PIV_INIT_IN_READER_LOCK_OBTAINED))) {
3140
    sc_log(card->ctx, "Set PIV_SM_GET_DATA_IN_CLEAR");
3141
    priv->sm_flags |= PIV_SM_GET_DATA_IN_CLEAR;
3142
  }
3143
3144
#endif /* ENABLE_PIV_SM */
3145
5.40k
  r = piv_general_io(card, 0xCB, 0x3F, 0xFF, tagbuf, p - tagbuf, *buf, *buf_len);
3146
5.40k
  if (r > 0) {
3147
1.02k
    int r_tag;
3148
1.02k
    unsigned int cla_out, tag_out;
3149
1.02k
    size_t bodylen = 0;
3150
1.02k
    const u8 *body = *buf;
3151
1.02k
    r_tag = sc_asn1_read_tag(&body, r, &cla_out, &tag_out, &bodylen);
3152
1.02k
    if (r_tag != SC_SUCCESS || body == NULL ||
3153
882
        ((cla_out << 24 | tag_out) != piv_objects[enumtag].resp_tag)) {
3154
264
      sc_log(card->ctx, "invalid tag or length r_tag:%d body:%p", r_tag, body);
3155
264
      r = SC_ERROR_FILE_NOT_FOUND;
3156
264
      goto err;
3157
264
    }
3158
756
    *buf_len = (body - *buf) + bodylen;
3159
4.38k
  } else if (r == 0) {
3160
76
    r = SC_ERROR_FILE_NOT_FOUND;
3161
76
    goto err;
3162
4.30k
  } else {
3163
4.30k
    goto err;
3164
4.30k
  }
3165
3166
756
  if (alloc_buf && *buf) {
3167
756
    tbuf = malloc(r);
3168
756
    if (tbuf == NULL) {
3169
0
      r = SC_ERROR_OUT_OF_MEMORY;
3170
0
      goto err;
3171
0
    }
3172
756
    memcpy(tbuf, *buf, r);
3173
756
    free(*buf);
3174
756
    alloc_buf = 0;
3175
756
    *buf = tbuf;
3176
756
  }
3177
3178
5.40k
err:
3179
5.40k
  if (alloc_buf) {
3180
4.64k
    free(*buf);
3181
4.64k
    *buf = NULL;
3182
4.64k
  }
3183
5.40k
  sc_unlock(card);
3184
5.40k
  LOG_FUNC_RETURN(card->ctx, r);
3185
5.40k
}
3186
3187
static int
3188
piv_get_cached_data(sc_card_t *card, int enumtag, u8 **buf, size_t *buf_len)
3189
5.70k
{
3190
3191
5.70k
  piv_private_data_t *priv = PIV_DATA(card);
3192
5.70k
  int r;
3193
5.70k
  u8 *rbuf = NULL;
3194
5.70k
  size_t rbuflen;
3195
3196
5.70k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3197
3198
5.70k
  if (enumtag < 0 || enumtag >= PIV_OBJ_LAST_ENUM)
3199
0
    return SC_ERROR_INTERNAL;
3200
3201
5.70k
  sc_log(card->ctx, "#%d, %s", enumtag, piv_objects[enumtag].name);
3202
3203
  /* see if we have it cached */
3204
5.70k
  if (priv->obj_cache[enumtag].flags & PIV_OBJ_CACHE_VALID) {
3205
3206
347
    sc_log(card->ctx,
3207
347
        "found #%d %p:%" SC_FORMAT_LEN_SIZE_T "u %p:%" SC_FORMAT_LEN_SIZE_T "u",
3208
347
        enumtag,
3209
347
        priv->obj_cache[enumtag].obj_data,
3210
347
        priv->obj_cache[enumtag].obj_len,
3211
347
        priv->obj_cache[enumtag].internal_obj_data,
3212
347
        priv->obj_cache[enumtag].internal_obj_len);
3213
3214
347
    if (priv->obj_cache[enumtag].obj_len == 0) {
3215
0
      r = SC_ERROR_FILE_NOT_FOUND;
3216
0
      sc_log(card->ctx, "#%d found but len=0", enumtag);
3217
0
      goto err;
3218
0
    }
3219
347
    *buf = priv->obj_cache[enumtag].obj_data;
3220
347
    *buf_len = priv->obj_cache[enumtag].obj_len;
3221
347
    r = (int)*buf_len;
3222
347
    goto ok;
3223
347
  }
3224
3225
  /*
3226
   * If we know it can not be on the card  i.e. History object
3227
   * has been read, and we know what other certs may or
3228
   * may not be on the card. We can avoid extra overhead
3229
   * Also used if object on card was not parsable
3230
   */
3231
3232
5.35k
  if (priv->obj_cache[enumtag].flags & PIV_OBJ_CACHE_NOT_PRESENT) {
3233
0
    sc_log(card->ctx, "no_obj #%d", enumtag);
3234
0
    r = SC_ERROR_FILE_NOT_FOUND;
3235
0
    goto err;
3236
0
  }
3237
3238
  /* Not cached, try to get it, piv_get_data will allocate a buf */
3239
5.35k
  sc_log(card->ctx, "get #%d", enumtag);
3240
5.35k
  rbuflen = 1;
3241
5.35k
  r = piv_get_data(card, enumtag, &rbuf, &rbuflen);
3242
5.35k
  if (r > 0) {
3243
724
    priv->obj_cache[enumtag].flags |= PIV_OBJ_CACHE_VALID;
3244
724
    priv->obj_cache[enumtag].obj_len = r;
3245
724
    priv->obj_cache[enumtag].obj_data = rbuf;
3246
724
    *buf = rbuf;
3247
724
    *buf_len = r;
3248
3249
724
    sc_log(card->ctx,
3250
724
        "added #%d  %p:%" SC_FORMAT_LEN_SIZE_T "u %p:%" SC_FORMAT_LEN_SIZE_T "u",
3251
724
        enumtag,
3252
724
        priv->obj_cache[enumtag].obj_data,
3253
724
        priv->obj_cache[enumtag].obj_len,
3254
724
        priv->obj_cache[enumtag].internal_obj_data,
3255
724
        priv->obj_cache[enumtag].internal_obj_len);
3256
3257
4.63k
  } else {
3258
4.63k
    free(rbuf);
3259
4.63k
    if (r == 0 || r == SC_ERROR_FILE_NOT_FOUND) {
3260
392
      r = SC_ERROR_FILE_NOT_FOUND;
3261
392
      priv->obj_cache[enumtag].flags |= PIV_OBJ_CACHE_VALID;
3262
392
      priv->obj_cache[enumtag].obj_len = 0;
3263
4.24k
    } else {
3264
4.24k
      goto err;
3265
4.24k
    }
3266
4.63k
  }
3267
1.46k
ok:
3268
3269
5.70k
err:
3270
5.70k
  LOG_FUNC_RETURN(card->ctx, r);
3271
5.70k
}
3272
3273
static int
3274
piv_cache_internal_data(sc_card_t *card, int enumtag)
3275
778
{
3276
778
  piv_private_data_t *priv = PIV_DATA(card);
3277
778
  const u8 *tag;
3278
778
  const u8 *body;
3279
778
  size_t taglen;
3280
778
  size_t bodylen;
3281
778
  int compressed = 0;
3282
778
  int r = SC_SUCCESS;
3283
#ifdef ENABLE_PIV_SM
3284
  u8 *cvc_start = NULL;
3285
  size_t cvc_len = 0;
3286
#endif
3287
3288
  /* if already cached */
3289
778
  if (priv->obj_cache[enumtag].internal_obj_data && priv->obj_cache[enumtag].internal_obj_len) {
3290
347
    sc_log(card->ctx,
3291
347
        "#%d found internal %p:%" SC_FORMAT_LEN_SIZE_T "u",
3292
347
        enumtag,
3293
347
        priv->obj_cache[enumtag].internal_obj_data,
3294
347
        priv->obj_cache[enumtag].internal_obj_len);
3295
347
    LOG_FUNC_RETURN(card->ctx, r);
3296
347
  }
3297
3298
431
  body = sc_asn1_find_tag(card->ctx,
3299
431
      priv->obj_cache[enumtag].obj_data,
3300
431
      priv->obj_cache[enumtag].obj_len,
3301
431
      0x53, &bodylen);
3302
3303
431
  if (body == NULL || priv->obj_cache[enumtag].obj_data[0] != 0x53)
3304
431
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_VALID);
3305
3306
  /* get the certificate out */
3307
431
  if (piv_objects[enumtag].flags & PIV_OBJECT_TYPE_CERT) {
3308
3309
431
    tag = sc_asn1_find_tag(card->ctx, body, bodylen, 0x71, &taglen);
3310
    /* 800-72-1 not clear if this is 80 or 01 Sent comment to NIST for 800-72-2 */
3311
    /* 800-73-3 says it is 01, keep dual test so old cards still work */
3312
431
    if (tag && taglen > 0 && (((*tag) & 0x80) || ((*tag) & 0x01)))
3313
20
      compressed = 1;
3314
3315
#ifdef ENABLE_PIV_SM
3316
    cvc_start = (u8 *)tag + taglen; /* save for later as cvs (if present) follows  0x71 */
3317
#endif
3318
3319
431
    tag = sc_asn1_find_tag(card->ctx, body, bodylen, 0x70, &taglen);
3320
431
    if (tag == NULL)
3321
431
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_VALID);
3322
3323
358
    if (taglen == 0)
3324
358
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_NOT_FOUND);
3325
3326
347
    if (compressed) {
3327
11
      priv->obj_cache[enumtag].flags |= PIV_OBJ_CACHE_COMPRESSED;
3328
11
    }
3329
    /* internal certificate remains compressed */
3330
347
    if (!(priv->obj_cache[enumtag].internal_obj_data = malloc(taglen)))
3331
347
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3332
3333
347
    memcpy(priv->obj_cache[enumtag].internal_obj_data, tag, taglen);
3334
347
    priv->obj_cache[enumtag].internal_obj_len = taglen;
3335
3336
#ifdef ENABLE_PIV_SM
3337
    /* PIV_OBJ_SM_CERT_SIGNER  CERT OBJECT may also have a intermediate CVC */
3338
    if (piv_objects[enumtag].flags & PIV_OBJECT_TYPE_CVC) {
3339
      /* cvc if present should be at cvc_start.
3340
       * find the tag(T) and get value(V) and len(L) from TLV.
3341
       * Could reconstruct ASN1 of (T)(L) stating location from length and known tag.
3342
       * as the size of (L) depends on the length of value
3343
       */
3344
      if ((tag = sc_asn1_find_tag(card->ctx, body, bodylen, 0x7F21, &taglen)) != NULL &&
3345
          cvc_start &&
3346
          cvc_start < tag &&
3347
          cvc_start[0] == 0x7f && cvc_start[1] == 0x21) {
3348
        cvc_len = tag - cvc_start + taglen;
3349
        /* decode the intermediate CVC */
3350
        r = piv_decode_cvc(card, &cvc_start, &cvc_len, &priv->sm_in_cvc);
3351
        if (r < 0) {
3352
          sc_log(card->ctx, "unable to parse intermediate CVC: %d skipping", r);
3353
        }
3354
        priv->sm_flags |= PIV_SM_FLAGS_SM_IN_CVC_PRESENT;
3355
      }
3356
    }
3357
#endif /* ENABLE_PIV_SM */
3358
3359
    /* convert pub key to internal */
3360
347
  } else if (piv_objects[enumtag].flags & PIV_OBJECT_TYPE_PUBKEY) {
3361
0
    tag = sc_asn1_find_tag(card->ctx, body, bodylen, *body, &taglen);
3362
0
    if (tag == NULL)
3363
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_VALID);
3364
3365
0
    if (taglen == 0)
3366
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_NOT_FOUND);
3367
3368
0
    if (!(priv->obj_cache[enumtag].internal_obj_data = malloc(taglen)))
3369
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3370
3371
0
    memcpy(priv->obj_cache[enumtag].internal_obj_data, tag, taglen);
3372
0
    priv->obj_cache[enumtag].internal_obj_len = taglen;
3373
0
  } else {
3374
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3375
0
  }
3376
3377
347
  sc_log(card->ctx, "added #%d internal %p:%" SC_FORMAT_LEN_SIZE_T "u",
3378
347
      enumtag,
3379
347
      priv->obj_cache[enumtag].internal_obj_data,
3380
347
      priv->obj_cache[enumtag].internal_obj_len);
3381
3382
347
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
3383
347
}
3384
3385
/*
3386
 * Callers of this may be expecting a certificate,
3387
 * select file will have saved the object type for us
3388
 * as well as set that we want the cert from the object.
3389
 */
3390
static int
3391
piv_read_binary(sc_card_t *card, unsigned int idx, unsigned char *buf, size_t count, unsigned long *flags)
3392
1.30k
{
3393
1.30k
  piv_private_data_t *priv = PIV_DATA(card);
3394
1.30k
  int enumtag;
3395
1.30k
  int r;
3396
1.30k
  u8 *rbuf = NULL;
3397
1.30k
  size_t rbuflen = 0;
3398
1.30k
  const u8 *body;
3399
1.30k
  size_t bodylen;
3400
3401
1.30k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3402
1.30k
  if (priv->selected_obj < 0)
3403
1.30k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3404
1.30k
  enumtag = piv_objects[priv->selected_obj].enumtag;
3405
3406
1.30k
  if (priv->rwb_state == -1) {
3407
347
    r = piv_get_cached_data(card, enumtag, &rbuf, &rbuflen);
3408
3409
347
    if (r >= 0) {
3410
      /* an object with no data will be considered not found */
3411
      /* Discovery tag = 0x73, all others are 0x53 */
3412
347
      if (!rbuf || rbuf[0] == 0x00 || ((rbuf[0] & 0xDF) == 0x53 && rbuf[1] == 0x00)) {
3413
0
        r = SC_ERROR_FILE_NOT_FOUND;
3414
0
        goto err;
3415
0
      }
3416
3417
347
      body = sc_asn1_find_tag(card->ctx, rbuf, rbuflen, rbuf[0], &bodylen);
3418
347
      if (body == NULL) {
3419
        /* if missing, assume its the body */
3420
        /* DEE bug in the beta card */
3421
0
        sc_log(card->ctx, " ***** tag 0x53 MISSING");
3422
0
        r = SC_ERROR_INVALID_DATA;
3423
0
        goto err;
3424
0
      }
3425
347
      if (bodylen > body - rbuf + rbuflen) {
3426
0
        sc_log(card->ctx,
3427
0
            " ***** tag length > then data: %" SC_FORMAT_LEN_SIZE_T "u>%" SC_FORMAT_LEN_PTRDIFF_T "u+%" SC_FORMAT_LEN_SIZE_T "u",
3428
0
            bodylen, body - rbuf, rbuflen);
3429
0
        r = SC_ERROR_INVALID_DATA;
3430
0
        goto err;
3431
0
      }
3432
      /* if cached obj has internal interesting data (cert or pub key) */
3433
347
      if (priv->return_only_cert || piv_objects[enumtag].flags & PIV_OBJECT_TYPE_PUBKEY) {
3434
347
        r = piv_cache_internal_data(card, enumtag);
3435
347
        if (r < 0)
3436
0
          goto err;
3437
347
      }
3438
347
    }
3439
347
    priv->rwb_state = 0;
3440
347
  }
3441
3442
1.30k
  if (priv->return_only_cert || piv_objects[enumtag].flags & PIV_OBJECT_TYPE_PUBKEY) {
3443
1.30k
    rbuf = priv->obj_cache[enumtag].internal_obj_data;
3444
1.30k
    rbuflen = priv->obj_cache[enumtag].internal_obj_len;
3445
1.30k
    if ((priv->obj_cache[enumtag].flags & PIV_OBJ_CACHE_COMPRESSED) && flags) {
3446
50
      *flags |= SC_FILE_FLAG_COMPRESSED_AUTO;
3447
50
    }
3448
1.30k
  } else {
3449
0
    rbuf = priv->obj_cache[enumtag].obj_data;
3450
0
    rbuflen = priv->obj_cache[enumtag].obj_len;
3451
0
  }
3452
  /* rbuf rbuflen has pointer and length to cached data */
3453
3454
1.30k
  if (rbuflen < idx + count)
3455
0
    count = rbuflen - idx;
3456
1.30k
  if (count <= 0) {
3457
0
    r = 0;
3458
0
    priv->rwb_state = 1;
3459
1.30k
  } else {
3460
1.30k
    memcpy(buf, rbuf + idx, count);
3461
1.30k
    r = (int)count;
3462
1.30k
  }
3463
3464
1.30k
err:
3465
1.30k
  LOG_FUNC_RETURN(card->ctx, r);
3466
1.30k
}
3467
3468
/*
3469
 * the tag is the PIV_OBJ_*
3470
 * The buf should have the 0x53 tag+len+tags and data
3471
 */
3472
3473
static int
3474
piv_put_data(sc_card_t *card, int tag, const u8 *buf, size_t buf_len)
3475
0
{
3476
0
  int r;
3477
0
  u8 *sbuf;
3478
0
  size_t sbuflen;
3479
0
  u8 *p;
3480
0
  size_t tag_len;
3481
3482
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3483
3484
0
  tag_len = piv_objects[tag].tag_len;
3485
0
  r = sc_asn1_put_tag(0x5c, piv_objects[tag].tag_value, tag_len, NULL, 0, NULL);
3486
0
  if (r <= 0) {
3487
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3488
0
  }
3489
0
  sbuflen = r + buf_len;
3490
0
  if (!(sbuf = malloc(sbuflen))) {
3491
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3492
0
  }
3493
3494
0
  p = sbuf;
3495
0
  r = sc_asn1_put_tag(0x5c, piv_objects[tag].tag_value, tag_len, sbuf, sbuflen, &p);
3496
0
  if (r != SC_SUCCESS) {
3497
0
    LOG_FUNC_RETURN(card->ctx, r);
3498
0
  }
3499
3500
  /* This is safe as we calculated the size of buffer above */
3501
0
  memcpy(p, buf, buf_len);
3502
0
  p += buf_len;
3503
3504
0
  r = piv_general_io(card, 0xDB, 0x3F, 0xFF, sbuf, p - sbuf, NULL, 0);
3505
3506
0
  if (sbuf)
3507
0
    free(sbuf);
3508
0
  LOG_FUNC_RETURN(card->ctx, r);
3509
0
}
3510
3511
static int
3512
piv_write_certificate(sc_card_t *card, const u8 *buf, size_t count, unsigned long flags)
3513
0
{
3514
0
  piv_private_data_t *priv = PIV_DATA(card);
3515
0
  int enumtag, tmplen, tmplen2, tmplen3;
3516
0
  int r = SC_SUCCESS;
3517
0
  u8 *sbuf = NULL;
3518
0
  u8 *p;
3519
0
  size_t sbuflen;
3520
0
  size_t taglen;
3521
3522
0
  if ((tmplen = sc_asn1_put_tag(0x70, buf, count, NULL, 0, NULL)) <= 0 ||
3523
0
      (tmplen2 = sc_asn1_put_tag(0x71, NULL, 1, NULL, 0, NULL)) <= 0 ||
3524
0
      (tmplen3 = sc_asn1_put_tag(0xFE, NULL, 0, NULL, 0, NULL)) <= 0) {
3525
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3526
0
  }
3527
3528
0
  taglen = tmplen + tmplen2 + tmplen3;
3529
0
  tmplen = sc_asn1_put_tag(0x53, NULL, taglen, NULL, 0, NULL);
3530
0
  if (tmplen <= 0) {
3531
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3532
0
  }
3533
3534
0
  sbuflen = tmplen;
3535
0
  sbuf = malloc(sbuflen);
3536
0
  if (sbuf == NULL)
3537
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3538
0
  p = sbuf;
3539
0
  if ((r = sc_asn1_put_tag(0x53, NULL, taglen, sbuf, sbuflen, &p)) != SC_SUCCESS ||
3540
0
      (r = sc_asn1_put_tag(0x70, buf, count, p, sbuflen - (p - sbuf), &p)) != SC_SUCCESS ||
3541
0
      (r = sc_asn1_put_tag(0x71, NULL, 1, p, sbuflen - (p - sbuf), &p)) != SC_SUCCESS) {
3542
0
    goto out;
3543
0
  }
3544
  /* Use 01 as per NIST 800-73-3 */
3545
0
  *p++ = (flags) ? 0x01 : 0x00; /* certinfo, i.e. gzipped? */
3546
0
  r = sc_asn1_put_tag(0xFE, NULL, 0, p, sbuflen - (p - sbuf), &p);
3547
0
  if (r != SC_SUCCESS) {
3548
0
    goto out;
3549
0
  }
3550
3551
0
  enumtag = piv_objects[priv->selected_obj].enumtag;
3552
0
  r = piv_put_data(card, enumtag, sbuf, sbuflen);
3553
3554
0
out:
3555
0
  free(sbuf);
3556
0
  LOG_FUNC_RETURN(card->ctx, r);
3557
0
}
3558
3559
/*
3560
 * For certs we need to add the 0x53 tag and other specific tags,
3561
 * and call the piv_put_data
3562
 * Note: the select file will have saved the object type for us
3563
 * Write is used by piv-tool, so we will use flags:
3564
 *  length << 8 | 8bits:
3565
 * object            xxxx0000
3566
 * uncompressed cert xxx00001
3567
 * compressed cert   xxx10001
3568
 * pubkey            xxxx0010
3569
 *
3570
 * to indicate we are writing a cert and if is compressed
3571
 * or if we are writing a pubkey in to the cache.
3572
 * if its not a cert or pubkey its an object.
3573
 *
3574
 * Therefore when idx=0, we will get the length of the object
3575
 * and allocate a buffer, so we can support partial writes.
3576
 * When the last chuck of the data is sent, we will write it.
3577
 */
3578
3579
static int
3580
piv_write_binary(sc_card_t *card, unsigned int idx,
3581
    const u8 *buf, size_t count, unsigned long flags)
3582
0
{
3583
0
  piv_private_data_t *priv = PIV_DATA(card);
3584
0
  int r;
3585
0
  int enumtag;
3586
3587
0
  LOG_FUNC_CALLED(card->ctx);
3588
3589
0
  if (priv->selected_obj < 0)
3590
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3591
3592
0
  enumtag = piv_objects[priv->selected_obj].enumtag;
3593
3594
0
  if (priv->rwb_state == 1) /* trying to write at end */
3595
0
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
3596
3597
0
  if (priv->rwb_state == -1) {
3598
3599
    /* if  cached, remove old entry */
3600
0
    if (priv->obj_cache[enumtag].flags & PIV_OBJ_CACHE_VALID) {
3601
0
      piv_obj_cache_free_entry(card, enumtag, 0);
3602
0
    }
3603
3604
0
    if (idx != 0)
3605
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NO_CARD_SUPPORT);
3606
3607
0
    priv->w_buf_len = flags >> 8;
3608
0
    if (priv->w_buf_len == 0)
3609
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
3610
3611
0
    priv->w_buf = malloc(priv->w_buf_len);
3612
0
    priv->rwb_state = 0;
3613
0
  }
3614
3615
  /* on each pass make sure we have w_buf */
3616
0
  if (priv->w_buf == NULL)
3617
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
3618
3619
0
  if (idx + count > priv->w_buf_len)
3620
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OBJECT_NOT_VALID);
3621
3622
0
  memcpy(priv->w_buf + idx, buf, count); /* copy one chunk */
3623
3624
  /* if this was not the last chunk, return to get rest */
3625
0
  if (idx + count < priv->w_buf_len)
3626
0
    LOG_FUNC_RETURN(card->ctx, (int)count);
3627
3628
0
  priv->rwb_state = 1; /* at end of object */
3629
3630
0
  switch (flags & 0x0f) {
3631
0
  case 1:
3632
0
    r = piv_write_certificate(card, priv->w_buf, priv->w_buf_len, flags & 0x10);
3633
0
    break;
3634
0
  case 2: /* pubkey to be added to cache, it should have 0x53 and 0x99 tags. */
3635
0
    r = (int)priv->w_buf_len;
3636
0
    break;
3637
0
  default:
3638
0
    r = piv_put_data(card, enumtag, priv->w_buf, priv->w_buf_len);
3639
0
    break;
3640
0
  }
3641
  /* if it worked, will cache it */
3642
0
  if (r >= 0 && priv->w_buf) {
3643
0
    priv->obj_cache[enumtag].flags |= PIV_OBJ_CACHE_VALID;
3644
0
    priv->obj_cache[enumtag].obj_data = priv->w_buf;
3645
0
    priv->obj_cache[enumtag].obj_len = priv->w_buf_len;
3646
0
  } else {
3647
0
    if (priv->w_buf)
3648
0
      free(priv->w_buf);
3649
0
  }
3650
0
  priv->w_buf = NULL;
3651
0
  priv->w_buf_len = 0;
3652
0
  LOG_FUNC_RETURN(card->ctx, (r < 0) ? r : (int)count);
3653
0
}
3654
3655
/*
3656
 * Card initialization is NOT standard.
3657
 * Some cards use mutual or external authentication using 3des or aes key. We
3658
 * will read in the key from a file either binary or hex encoded.
3659
 * This is only needed during initialization/personalization of the card
3660
 */
3661
3662
#ifdef ENABLE_OPENSSL
3663
static EVP_CIPHER *
3664
get_cipher_for_algo(sc_card_t *card, int alg_id)
3665
0
{
3666
0
  const char *algo;
3667
0
  switch (alg_id) {
3668
0
  case 0x0:
3669
0
  case 0x1: /* 2TDES */
3670
0
  case 0x3:
3671
0
    algo = "DES-EDE3-ECB";
3672
0
    break;
3673
0
  case 0x8:
3674
0
    algo = "AES-128-ECB";
3675
0
    break;
3676
0
  case 0xA:
3677
0
    algo = "AES-192-ECB";
3678
0
    break;
3679
0
  case 0xC:
3680
0
    algo = "AES-256-ECB";
3681
0
    break;
3682
0
  default:
3683
0
    return NULL;
3684
0
  }
3685
0
  return sc_evp_cipher(card->ctx, algo);
3686
0
}
3687
3688
static int
3689
get_keylen(unsigned int alg_id, size_t *size)
3690
0
{
3691
0
  switch (alg_id) {
3692
0
  case 0x01:
3693
0
    *size = 192 / 8; /* 2TDES still has 3 single des keys  phase out by 12/31/2010 */
3694
0
    break;
3695
0
  case 0x00:
3696
0
  case 0x03:
3697
0
    *size = 192 / 8;
3698
0
    break;
3699
0
  case 0x08:
3700
0
    *size = 128 / 8;
3701
0
    break;
3702
0
  case 0x0A:
3703
0
    *size = 192 / 8;
3704
0
    break;
3705
0
  case 0x0C:
3706
0
    *size = 256 / 8;
3707
0
    break;
3708
0
  default:
3709
0
    return SC_ERROR_INVALID_ARGUMENTS;
3710
0
  }
3711
0
  return SC_SUCCESS;
3712
0
}
3713
3714
static int
3715
piv_get_key(sc_card_t *card, unsigned int alg_id, u8 **key, size_t *len)
3716
0
{
3717
3718
0
  int r;
3719
0
  size_t fsize;
3720
0
  FILE *f = NULL;
3721
0
  char *keyfilename = NULL;
3722
0
  size_t expected_keylen;
3723
0
  size_t keylen, readlen;
3724
0
  u8 *keybuf = NULL;
3725
0
  u8 *tkey = NULL;
3726
3727
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3728
3729
0
  keyfilename = (char *)getenv("PIV_EXT_AUTH_KEY");
3730
3731
0
  if (keyfilename == NULL) {
3732
0
    sc_log(card->ctx,
3733
0
        "Unable to get PIV_EXT_AUTH_KEY=(null) for general_external_authenticate");
3734
0
    r = SC_ERROR_FILE_NOT_FOUND;
3735
0
    goto err;
3736
0
  }
3737
3738
0
  r = get_keylen(alg_id, &expected_keylen);
3739
0
  if (r) {
3740
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid cipher selector, none found for:  %02x", alg_id);
3741
0
    r = SC_ERROR_INVALID_ARGUMENTS;
3742
0
    goto err;
3743
0
  }
3744
3745
0
  f = fopen(keyfilename, "rb");
3746
0
  if (!f) {
3747
0
    sc_log(card->ctx, " Unable to load key from file\n");
3748
0
    r = SC_ERROR_FILE_NOT_FOUND;
3749
0
    goto err;
3750
0
  }
3751
3752
0
  if (0 > fseek(f, 0L, SEEK_END))
3753
0
    r = SC_ERROR_INTERNAL;
3754
0
  fsize = ftell(f);
3755
0
  if (0 > (long)fsize)
3756
0
    r = SC_ERROR_INTERNAL;
3757
0
  if (0 > fseek(f, 0L, SEEK_SET))
3758
0
    r = SC_ERROR_INTERNAL;
3759
0
  if (r) {
3760
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not read %s\n", keyfilename);
3761
0
    goto err;
3762
0
  }
3763
3764
0
  keybuf = malloc(fsize + 1); /* if not binary, need null to make it a string */
3765
0
  if (!keybuf) {
3766
0
    sc_log(card->ctx, " Unable to allocate key memory");
3767
0
    r = SC_ERROR_OUT_OF_MEMORY;
3768
0
    goto err;
3769
0
  }
3770
0
  keybuf[fsize] = 0x00; /* in case it is text need null */
3771
3772
0
  if ((readlen = fread(keybuf, 1, fsize, f)) != fsize) {
3773
0
    sc_log(card->ctx, " Unable to read key\n");
3774
0
    r = SC_ERROR_WRONG_LENGTH;
3775
0
    goto err;
3776
0
  }
3777
0
  keybuf[readlen] = '\0';
3778
3779
0
  tkey = malloc(expected_keylen);
3780
0
  if (!tkey) {
3781
0
    sc_log(card->ctx, " Unable to allocate key memory");
3782
0
    r = SC_ERROR_OUT_OF_MEMORY;
3783
0
    goto err;
3784
0
  }
3785
3786
0
  if (fsize == expected_keylen) { /* it must be binary */
3787
0
    memcpy(tkey, keybuf, expected_keylen);
3788
0
  } else {
3789
    /* if the key-length is larger then binary length, we assume hex encoded */
3790
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Treating key as hex-encoded!\n");
3791
0
    sc_right_trim(keybuf, fsize);
3792
0
    keylen = expected_keylen;
3793
0
    r = sc_hex_to_bin((char *)keybuf, tkey, &keylen);
3794
0
    if (keylen != expected_keylen || r != 0) {
3795
0
      sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Error formatting key\n");
3796
0
      if (r == 0)
3797
0
        r = SC_ERROR_INCOMPATIBLE_KEY;
3798
0
      goto err;
3799
0
    }
3800
0
  }
3801
0
  *key = tkey;
3802
0
  tkey = NULL;
3803
0
  *len = expected_keylen;
3804
0
  r = SC_SUCCESS;
3805
3806
0
err:
3807
0
  if (f)
3808
0
    fclose(f);
3809
0
  if (keybuf) {
3810
0
    free(keybuf);
3811
0
  }
3812
0
  if (tkey) {
3813
0
    free(tkey);
3814
0
  }
3815
3816
0
  LOG_FUNC_RETURN(card->ctx, r);
3817
0
  return r;
3818
0
}
3819
#endif
3820
3821
/*
3822
 * will only deal with 3des for now
3823
 * assumptions include:
3824
 *  size of encrypted data is same as unencrypted
3825
 *  challenges, nonces etc  from card are less then 114 (keeps tags simple)
3826
 */
3827
3828
static int
3829
piv_general_mutual_authenticate(sc_card_t *card,
3830
    unsigned int key_ref, unsigned int alg_id)
3831
0
{
3832
0
  int r;
3833
0
#ifdef ENABLE_OPENSSL
3834
0
  int N;
3835
0
  int locked = 0;
3836
0
  u8 rbuf[4096];
3837
0
  u8 *nonce = NULL;
3838
0
  size_t nonce_len;
3839
0
  u8 *p;
3840
0
  u8 *key = NULL;
3841
0
  size_t keylen;
3842
0
  u8 *plain_text = NULL;
3843
0
  size_t plain_text_len = 0;
3844
0
  u8 *tmp;
3845
0
  size_t tmplen, tmplen2;
3846
0
  u8 *built = NULL;
3847
0
  size_t built_len;
3848
0
  const u8 *body = NULL;
3849
0
  size_t body_len;
3850
0
  const u8 *witness_data = NULL;
3851
0
  size_t witness_len;
3852
0
  const u8 *challenge_response = NULL;
3853
0
  size_t challenge_response_len;
3854
0
  u8 *decrypted_reponse = NULL;
3855
0
  size_t decrypted_reponse_len;
3856
0
  EVP_CIPHER_CTX *ctx = NULL;
3857
3858
0
  u8 sbuf[255];
3859
0
  EVP_CIPHER *cipher = NULL;
3860
3861
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
3862
3863
0
  ctx = EVP_CIPHER_CTX_new();
3864
0
  if (ctx == NULL) {
3865
0
    sc_log_openssl(card->ctx);
3866
0
    r = SC_ERROR_OUT_OF_MEMORY;
3867
0
    goto err;
3868
0
  }
3869
3870
0
  cipher = get_cipher_for_algo(card, alg_id);
3871
0
  if (!cipher) {
3872
0
    sc_log_openssl(card->ctx);
3873
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid cipher selector, none found for:  %02x\n", alg_id);
3874
0
    r = SC_ERROR_INVALID_ARGUMENTS;
3875
0
    goto err;
3876
0
  }
3877
3878
0
  r = piv_get_key(card, alg_id, &key, &keylen);
3879
0
  if (r) {
3880
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Error getting General Auth key\n");
3881
0
    goto err;
3882
0
  }
3883
3884
0
  r = sc_lock(card);
3885
0
  if (r != SC_SUCCESS) {
3886
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "sc_lock failed\n");
3887
0
    goto err; /* cleanup */
3888
0
  }
3889
0
  locked = 1;
3890
3891
0
  p = sbuf;
3892
0
  *p++ = 0x7C;
3893
0
  *p++ = 0x02;
3894
0
  *p++ = 0x80;
3895
0
  *p++ = 0x00;
3896
3897
  /* get the encrypted nonce */
3898
0
  r = piv_general_io(card, 0x87, alg_id, key_ref, sbuf, p - sbuf, rbuf, sizeof rbuf);
3899
3900
0
  if (r < 0)
3901
0
    goto err;
3902
3903
  /* Remove the encompassing outer TLV of 0x7C and get the data */
3904
0
  body = sc_asn1_find_tag(card->ctx, rbuf,
3905
0
      r, 0x7C, &body_len);
3906
0
  if (!body || rbuf[0] != 0x7C) {
3907
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid Witness Data response of NULL\n");
3908
0
    r = SC_ERROR_INVALID_DATA;
3909
0
    goto err;
3910
0
  }
3911
3912
  /* Get the witness data indicated by the TAG 0x80 */
3913
0
  witness_data = sc_asn1_find_tag(card->ctx, body,
3914
0
      body_len, 0x80, &witness_len);
3915
0
  if (!witness_len || body_len == 0 || body[0] != 0x80) {
3916
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid Challenge Data none found in TLV\n");
3917
0
    r = SC_ERROR_INVALID_DATA;
3918
0
    goto err;
3919
0
  }
3920
3921
  /* Allocate an output buffer for openssl */
3922
0
  plain_text = malloc(witness_len);
3923
0
  if (!plain_text) {
3924
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not allocate buffer for plain text\n");
3925
0
    r = SC_ERROR_INTERNAL;
3926
0
    goto err;
3927
0
  }
3928
3929
  /* decrypt the data from the card */
3930
0
  if (!EVP_DecryptInit(ctx, cipher, key, NULL)) {
3931
    /* may fail if des parity of key is wrong. depends on OpenSSL options */
3932
0
    sc_log_openssl(card->ctx);
3933
0
    r = SC_ERROR_INTERNAL;
3934
0
    goto err;
3935
0
  }
3936
0
  EVP_CIPHER_CTX_set_padding(ctx, 0);
3937
3938
0
  p = plain_text;
3939
0
  if (!EVP_DecryptUpdate(ctx, p, &N, witness_data, (int)witness_len)) {
3940
0
    sc_log_openssl(card->ctx);
3941
0
    r = SC_ERROR_INTERNAL;
3942
0
    goto err;
3943
0
  }
3944
0
  plain_text_len = tmplen = N;
3945
0
  p += tmplen;
3946
3947
0
  if (!EVP_DecryptFinal(ctx, p, &N)) {
3948
0
    sc_log_openssl(card->ctx);
3949
0
    r = SC_ERROR_INTERNAL;
3950
0
    goto err;
3951
0
  }
3952
0
  tmplen = N;
3953
0
  plain_text_len += tmplen;
3954
3955
0
  if (plain_text_len != witness_len) {
3956
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE,
3957
0
        "Encrypted and decrypted lengths do not match: %" SC_FORMAT_LEN_SIZE_T "u:%" SC_FORMAT_LEN_SIZE_T "u\n",
3958
0
        witness_len, plain_text_len);
3959
0
    r = SC_ERROR_INTERNAL;
3960
0
    goto err;
3961
0
  }
3962
3963
  /* Build a response to the card of:
3964
   * [GEN AUTH][ 80<decrypted witness>81 <challenge> ]
3965
   * Start by computing the nonce for <challenge> the
3966
   * nonce length should match the witness length of
3967
   * the card.
3968
   */
3969
0
  nonce = malloc(witness_len);
3970
0
  if (!nonce) {
3971
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE,
3972
0
        "OOM allocating nonce (%" SC_FORMAT_LEN_SIZE_T "u : %" SC_FORMAT_LEN_SIZE_T "u)\n",
3973
0
        witness_len, plain_text_len);
3974
0
    r = SC_ERROR_INTERNAL;
3975
0
    goto err;
3976
0
  }
3977
0
  nonce_len = witness_len;
3978
3979
0
  r = RAND_bytes(nonce, (int)witness_len);
3980
0
  if (!r) {
3981
0
    sc_log_openssl(card->ctx);
3982
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE,
3983
0
        "Generating random for nonce (%" SC_FORMAT_LEN_SIZE_T "u : %" SC_FORMAT_LEN_SIZE_T "u)\n",
3984
0
        witness_len, plain_text_len);
3985
0
    r = SC_ERROR_INTERNAL;
3986
0
    goto err;
3987
0
  }
3988
3989
  /* nonce for challenge */
3990
0
  r = sc_asn1_put_tag(0x81, NULL, witness_len, NULL, 0, NULL);
3991
0
  if (r <= 0) {
3992
0
    r = SC_ERROR_INTERNAL;
3993
0
    goto err;
3994
0
  }
3995
0
  tmplen = r;
3996
3997
  /* plain text witness keep a length separate for the 0x7C tag */
3998
0
  r = sc_asn1_put_tag(0x80, NULL, witness_len, NULL, 0, NULL);
3999
0
  if (r <= 0) {
4000
0
    r = SC_ERROR_INTERNAL;
4001
0
    goto err;
4002
0
  }
4003
0
  tmplen += r;
4004
0
  tmplen2 = tmplen;
4005
4006
  /* outside 7C tag with 81:80 as innards */
4007
0
  r = sc_asn1_put_tag(0x7C, NULL, tmplen, NULL, 0, NULL);
4008
0
  if (r <= 0) {
4009
0
    r = SC_ERROR_INTERNAL;
4010
0
    goto err;
4011
0
  }
4012
4013
0
  built_len = r;
4014
4015
  /* Build the response buffer */
4016
0
  p = built = malloc(built_len);
4017
0
  if (!built) {
4018
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "OOM Building witness response and challenge\n");
4019
0
    r = SC_ERROR_INTERNAL;
4020
0
    goto err;
4021
0
  }
4022
4023
0
  p = built;
4024
4025
  /* Start with the 7C Tag */
4026
0
  r = sc_asn1_put_tag(0x7C, NULL, tmplen2, p, built_len, &p);
4027
0
  if (r != SC_SUCCESS) {
4028
0
    goto err;
4029
0
  }
4030
4031
  /* Add the DECRYPTED witness, tag 0x80 */
4032
0
  r = sc_asn1_put_tag(0x80, plain_text, witness_len, p, built_len - (p - built), &p);
4033
0
  if (r != SC_SUCCESS) {
4034
0
    goto err;
4035
0
  }
4036
4037
  /* Add the challenge, tag 0x81 */
4038
0
  r = sc_asn1_put_tag(0x81, nonce, witness_len, p, built_len - (p - built), &p);
4039
0
  if (r != SC_SUCCESS) {
4040
0
    goto err;
4041
0
  }
4042
4043
  /* Send constructed data */
4044
0
  r = piv_general_io(card, 0x87, alg_id, key_ref, built, built_len, rbuf, sizeof rbuf);
4045
0
  if (r < 0) {
4046
0
    goto err;
4047
0
  }
4048
4049
  /* Remove the encompassing outer TLV of 0x7C and get the data */
4050
0
  body = sc_asn1_find_tag(card->ctx, rbuf,
4051
0
      r, 0x7C, &body_len);
4052
0
  if (!body || rbuf[0] != 0x7C) {
4053
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not find outer tag 0x7C in response");
4054
0
    r = SC_ERROR_INVALID_DATA;
4055
0
    goto err;
4056
0
  }
4057
4058
  /* SP800-73 not clear if  80 or 82 */
4059
0
  challenge_response = sc_asn1_find_tag(card->ctx, body,
4060
0
      body_len, 0x82, &challenge_response_len);
4061
0
  if (!challenge_response) {
4062
0
    challenge_response = sc_asn1_find_tag(card->ctx, body,
4063
0
        body_len, 0x80, &challenge_response_len);
4064
0
    if (!challenge_response) {
4065
0
      sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not find tag 0x82 or 0x80 in response");
4066
0
      r = SC_ERROR_INVALID_DATA;
4067
0
      goto err;
4068
0
    }
4069
0
  }
4070
4071
  /* Decrypt challenge and check against nonce */
4072
0
  decrypted_reponse = malloc(challenge_response_len);
4073
0
  if (!decrypted_reponse) {
4074
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "OOM Allocating decryption buffer");
4075
0
    r = SC_ERROR_INVALID_DATA;
4076
0
    goto err;
4077
0
  }
4078
4079
0
  EVP_CIPHER_CTX_reset(ctx);
4080
4081
0
  if (!EVP_DecryptInit(ctx, cipher, key, NULL)) {
4082
0
    sc_log_openssl(card->ctx);
4083
0
    r = SC_ERROR_INTERNAL;
4084
0
    goto err;
4085
0
  }
4086
0
  EVP_CIPHER_CTX_set_padding(ctx, 0);
4087
4088
0
  tmp = decrypted_reponse;
4089
0
  if (!EVP_DecryptUpdate(ctx, tmp, &N, challenge_response, (int)challenge_response_len)) {
4090
0
    sc_log_openssl(card->ctx);
4091
0
    r = SC_ERROR_INTERNAL;
4092
0
    goto err;
4093
0
  }
4094
0
  decrypted_reponse_len = tmplen = N;
4095
0
  tmp += tmplen;
4096
4097
0
  if (!EVP_DecryptFinal(ctx, tmp, &N)) {
4098
0
    sc_log_openssl(card->ctx);
4099
0
    r = SC_ERROR_INTERNAL;
4100
0
    goto err;
4101
0
  }
4102
0
  tmplen = N;
4103
0
  decrypted_reponse_len += tmplen;
4104
4105
0
  if (decrypted_reponse_len != nonce_len || memcmp(nonce, decrypted_reponse, nonce_len) != 0) {
4106
0
    sc_log(card->ctx,
4107
0
        "mutual authentication failed, card returned wrong value %" SC_FORMAT_LEN_SIZE_T "u:%" SC_FORMAT_LEN_SIZE_T "u",
4108
0
        decrypted_reponse_len, nonce_len);
4109
0
    r = SC_ERROR_DECRYPT_FAILED;
4110
0
    goto err;
4111
0
  }
4112
0
  r = SC_SUCCESS;
4113
4114
0
err:
4115
0
  sc_evp_cipher_free(cipher);
4116
0
  if (ctx)
4117
0
    EVP_CIPHER_CTX_free(ctx);
4118
0
  if (locked)
4119
0
    sc_unlock(card);
4120
0
  if (decrypted_reponse)
4121
0
    free(decrypted_reponse);
4122
0
  if (built)
4123
0
    free(built);
4124
0
  if (plain_text)
4125
0
    free(plain_text);
4126
0
  if (nonce)
4127
0
    free(nonce);
4128
0
  if (key)
4129
0
    free(key);
4130
4131
#else
4132
  sc_log(card->ctx, "OpenSSL Required");
4133
  r = SC_ERROR_NOT_SUPPORTED;
4134
#endif /* ENABLE_OPENSSL */
4135
4136
0
  LOG_FUNC_RETURN(card->ctx, r);
4137
0
}
4138
4139
/* Currently only used for card administration */
4140
static int
4141
piv_general_external_authenticate(sc_card_t *card,
4142
    unsigned int key_ref, unsigned int alg_id)
4143
0
{
4144
0
  int r;
4145
0
#ifdef ENABLE_OPENSSL
4146
0
  size_t tmplen;
4147
0
  int outlen;
4148
0
  int locked = 0;
4149
0
  u8 *p;
4150
0
  u8 rbuf[4096];
4151
0
  u8 *key = NULL;
4152
0
  u8 *cipher_text = NULL;
4153
0
  u8 *output_buf = NULL;
4154
0
  const u8 *body = NULL;
4155
0
  const u8 *challenge_data = NULL;
4156
0
  size_t body_len;
4157
0
  size_t output_len;
4158
0
  size_t challenge_len;
4159
0
  size_t keylen = 0;
4160
0
  size_t cipher_text_len = 0;
4161
0
  u8 sbuf[255];
4162
0
  EVP_CIPHER_CTX *ctx = NULL;
4163
0
  EVP_CIPHER *cipher = NULL;
4164
4165
0
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4166
4167
0
  ctx = EVP_CIPHER_CTX_new();
4168
0
  if (ctx == NULL) {
4169
0
    sc_log_openssl(card->ctx);
4170
0
    r = SC_ERROR_OUT_OF_MEMORY;
4171
0
    goto err;
4172
0
  }
4173
4174
0
  sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Selected cipher for algorithm id: %02x\n", alg_id);
4175
4176
0
  cipher = get_cipher_for_algo(card, alg_id);
4177
0
  if (!cipher) {
4178
0
    sc_log_openssl(card->ctx);
4179
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid cipher selector, none found for:  %02x\n", alg_id);
4180
0
    r = SC_ERROR_INVALID_ARGUMENTS;
4181
0
    goto err;
4182
0
  }
4183
4184
0
  r = piv_get_key(card, alg_id, &key, &keylen);
4185
0
  if (r) {
4186
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Error getting General Auth key\n");
4187
0
    goto err;
4188
0
  }
4189
4190
0
  r = sc_lock(card);
4191
0
  if (r != SC_SUCCESS) {
4192
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "sc_lock failed\n");
4193
0
    goto err; /* cleanup */
4194
0
  }
4195
0
  locked = 1;
4196
4197
0
  p = sbuf;
4198
0
  *p++ = 0x7C;
4199
0
  *p++ = 0x02;
4200
0
  *p++ = 0x81;
4201
0
  *p++ = 0x00;
4202
4203
  /* get a challenge */
4204
0
  r = piv_general_io(card, 0x87, alg_id, key_ref, sbuf, p - sbuf, rbuf, sizeof rbuf);
4205
0
  if (r < 0) {
4206
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Error getting Challenge\n");
4207
0
    goto err;
4208
0
  }
4209
4210
  /*
4211
   * the value here corresponds with the response size, so we use this
4212
   * to alloc the response buffer, rather than re-computing it.
4213
   */
4214
0
  output_len = r;
4215
4216
  /* Remove the encompassing outer TLV of 0x7C and get the data */
4217
0
  body = sc_asn1_find_tag(card->ctx, rbuf,
4218
0
      r, 0x7C, &body_len);
4219
0
  if (!body || rbuf[0] != 0x7C) {
4220
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid Challenge Data response of NULL\n");
4221
0
    r = SC_ERROR_INVALID_DATA;
4222
0
    goto err;
4223
0
  }
4224
4225
  /* Get the challenge data indicated by the TAG 0x81 */
4226
0
  challenge_data = sc_asn1_find_tag(card->ctx, body,
4227
0
      body_len, 0x81, &challenge_len);
4228
0
  if (!challenge_data) {
4229
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Invalid Challenge Data none found in TLV\n");
4230
0
    r = SC_ERROR_INVALID_DATA;
4231
0
    goto err;
4232
0
  }
4233
4234
  /* Store this to sanity check that plaintext length and ciphertext lengths match */
4235
0
  tmplen = challenge_len;
4236
4237
  /* Encrypt the challenge with the secret */
4238
0
  if (!EVP_EncryptInit(ctx, cipher, key, NULL)) {
4239
0
    sc_log_openssl(card->ctx);
4240
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Encrypt fail\n");
4241
0
    r = SC_ERROR_INTERNAL;
4242
0
    goto err;
4243
0
  }
4244
4245
0
  cipher_text = malloc(challenge_len);
4246
0
  if (!cipher_text) {
4247
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not allocate buffer for cipher text\n");
4248
0
    r = SC_ERROR_INTERNAL;
4249
0
    goto err;
4250
0
  }
4251
4252
0
  EVP_CIPHER_CTX_set_padding(ctx, 0);
4253
0
  if (!EVP_EncryptUpdate(ctx, cipher_text, &outlen, challenge_data, (int)challenge_len)) {
4254
0
    sc_log_openssl(card->ctx);
4255
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Encrypt update fail\n");
4256
0
    r = SC_ERROR_INTERNAL;
4257
0
    goto err;
4258
0
  }
4259
0
  cipher_text_len += outlen;
4260
4261
0
  if (!EVP_EncryptFinal(ctx, cipher_text + cipher_text_len, &outlen)) {
4262
0
    sc_log_openssl(card->ctx);
4263
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Final fail\n");
4264
0
    r = SC_ERROR_INTERNAL;
4265
0
    goto err;
4266
0
  }
4267
0
  cipher_text_len += outlen;
4268
4269
  /*
4270
   * Actually perform the sanity check on lengths plaintext length vs
4271
   * encrypted length
4272
   */
4273
0
  if (cipher_text_len != tmplen) {
4274
0
    sc_log_openssl(card->ctx);
4275
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Length test fail\n");
4276
0
    r = SC_ERROR_INTERNAL;
4277
0
    goto err;
4278
0
  }
4279
4280
0
  output_buf = malloc(output_len);
4281
0
  if (!output_buf) {
4282
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not allocate output buffer: %s\n",
4283
0
        strerror(errno));
4284
0
    r = SC_ERROR_INTERNAL;
4285
0
    goto err;
4286
0
  }
4287
4288
0
  p = output_buf;
4289
4290
  /*
4291
   * Build: 7C<len>[82<len><challenge>]
4292
   * Start off by capturing the data of the response:
4293
   *     - 82<len><encrypted challenege response>
4294
   * Build the outside TLV (7C)
4295
   * Advance past that tag + len
4296
   * Build the body (82)
4297
   * memcopy the body past the 7C<len> portion
4298
   * Transmit
4299
   */
4300
0
  tmplen = sc_asn1_put_tag(0x82, NULL, cipher_text_len, NULL, 0, NULL);
4301
0
  if (tmplen <= 0) {
4302
0
    r = SC_ERROR_INTERNAL;
4303
0
    goto err;
4304
0
  }
4305
4306
0
  r = sc_asn1_put_tag(0x7C, NULL, tmplen, p, output_len, &p);
4307
0
  if (r != SC_SUCCESS) {
4308
0
    goto err;
4309
0
  }
4310
4311
  /* Build the 0x82 TLV and append to the 7C<len> tag */
4312
0
  r = sc_asn1_put_tag(0x82, cipher_text, cipher_text_len, p, output_len - (p - output_buf), &p);
4313
0
  if (r != SC_SUCCESS) {
4314
0
    goto err;
4315
0
  }
4316
4317
  /* Sanity check the lengths again */
4318
0
  tmplen = sc_asn1_put_tag(0x7C, NULL, tmplen, NULL, 0, NULL);
4319
0
  if (output_len != (size_t)tmplen) {
4320
0
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Allocated and computed lengths do not match! "
4321
0
                "Expected %" SC_FORMAT_LEN_SIZE_T "d, found: %zu\n",
4322
0
        output_len, tmplen);
4323
0
    r = SC_ERROR_INTERNAL;
4324
0
    goto err;
4325
0
  }
4326
4327
0
  r = piv_general_io(card, 0x87, alg_id, key_ref, output_buf, output_len, NULL, 0);
4328
0
  sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Got response  challenge\n");
4329
4330
0
err:
4331
0
  sc_evp_cipher_free(cipher);
4332
0
  if (ctx)
4333
0
    EVP_CIPHER_CTX_free(ctx);
4334
4335
0
  if (locked)
4336
0
    sc_unlock(card);
4337
4338
0
  if (key) {
4339
0
    sc_mem_clear(key, keylen);
4340
0
    free(key);
4341
0
  }
4342
4343
0
  if (cipher_text)
4344
0
    free(cipher_text);
4345
4346
0
  if (output_buf)
4347
0
    free(output_buf);
4348
#else
4349
  sc_log(card->ctx, "OpenSSL Required");
4350
  r = SC_ERROR_NOT_SUPPORTED;
4351
#endif /* ENABLE_OPENSSL */
4352
4353
0
  LOG_FUNC_RETURN(card->ctx, r);
4354
0
}
4355
4356
/*
4357
 * with sp800-73-4 and SM GUID is also in sm_cvc.subjectID
4358
 */
4359
static int
4360
piv_get_serial_nr_from_CHUI(sc_card_t *card, sc_serial_number_t *serial)
4361
547
{
4362
547
  int r;
4363
547
  int i;
4364
547
  u8 gbits = 0; /* 0 not present or wrong length or all zeros */
4365
547
  u8 fbits = 0; /* 0 not present or wrong length or all zeros */
4366
547
  u8 *rbuf = NULL;
4367
547
  const u8 *body;
4368
547
  const u8 *fascn;
4369
547
  const u8 *guid;
4370
547
  size_t rbuflen = 0, bodylen, fascnlen, guidlen;
4371
4372
547
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4373
547
  if (card->serialnr.len) {
4374
0
    *serial = card->serialnr;
4375
0
    LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
4376
0
  }
4377
4378
  /*
4379
   * 800-73-3 Part 1 and CIO Council docs say for PIV Compatible cards
4380
   * the FASC-N Agency code should be 9999 and there should be a GUID
4381
   * based on RFC 4122. If GUID present and not zero
4382
   * we will use the GUID as the serial number.
4383
   */
4384
4385
547
  r = piv_get_cached_data(card, PIV_OBJ_CHUI, &rbuf, &rbuflen);
4386
547
  LOG_TEST_RET(card->ctx, r, "Failure retrieving CHUI");
4387
4388
79
  r = SC_ERROR_INTERNAL;
4389
79
  if (rbuflen != 0) {
4390
79
    body = sc_asn1_find_tag(card->ctx, rbuf, rbuflen, 0x53, &bodylen); /* Pass the outer wrapper asn1 */
4391
79
    if (body != NULL && bodylen != 0 && rbuf[0] == 0x53) {
4392
75
      fascn = sc_asn1_find_tag(card->ctx, body, bodylen, 0x30, &fascnlen); /* Find the FASC-N data */
4393
4394
75
      if (fascn && fascnlen == 25) {
4395
468
        for (i = 0; i < 25; i++) {
4396
450
          fbits = fbits || fascn[i]; /* if all are zero, gbits will be zero */
4397
450
        }
4398
18
      }
4399
4400
75
      guid = sc_asn1_find_tag(card->ctx, body, bodylen, 0x34, &guidlen);
4401
4402
75
      if (guid && guidlen == 16) {
4403
170
        for (i = 0; i < 16; i++) {
4404
160
          gbits = gbits | guid[i]; /* if all are zero, gbits will be zero */
4405
160
        }
4406
10
      }
4407
75
      sc_log(card->ctx,
4408
75
          "fascn=%p,fascnlen=%" SC_FORMAT_LEN_SIZE_T "u,guid=%p,guidlen=%" SC_FORMAT_LEN_SIZE_T "u,gbits=%2.2x",
4409
75
          fascn, fascnlen, guid, guidlen, gbits);
4410
4411
75
      if (fascn && fascnlen == 25 && fbits) {
4412
        /* test if guid and the fascn starts with ;9999 (in ISO 4bit + parity code) */
4413
        /* ;9999 is non-gov issued FASC-N, will use FASC-N for gov issued if no guid */
4414
17
        if (!(gbits && fascn[0] == 0xD4 && fascn[1] == 0xE7 &&
4415
16
                fascn[2] == 0x39 && (fascn[3] | 0x7F) == 0xFF)) {
4416
          /* fascnlen is 25 */
4417
16
          serial->len = fascnlen;
4418
16
          memcpy(serial->value, fascn, serial->len);
4419
16
          r = SC_SUCCESS;
4420
16
          gbits = 0; /* set to skip using guid below */
4421
16
        }
4422
17
      }
4423
75
      if (guid && guidlen == 16 && gbits) {
4424
4
        serial->len = guidlen;
4425
4
        memcpy(serial->value, guid, serial->len);
4426
4
        r = SC_SUCCESS;
4427
4
      }
4428
75
    }
4429
79
  }
4430
4431
79
  if (fbits == 0 && gbits == 0) { /* were not able to set the serial number */
4432
59
    serial->len = 16;
4433
59
    memset(serial->value, 0x00, serial->len);
4434
59
    r = SC_ERROR_INTERNAL;
4435
59
  }
4436
4437
79
  card->serialnr = *serial;
4438
79
  LOG_FUNC_RETURN(card->ctx, r);
4439
79
}
4440
4441
/*
4442
 * If the object can not be present on the card, because the History
4443
 * object is not present or the History object says its not present,
4444
 * return 1. If object may be present return 0.
4445
 * Cuts down on overhead, by not showing non existent objects to pkcs11
4446
 * The path for the object is passed in and the first 2 bytes are used.
4447
 * Note: If the History or Discovery object is not found the
4448
 * PIV_OBJ_CACHE_NOT_PRESENT is set, as older cards do not have these.
4449
 * pkcs15-piv.c calls this via cardctl.
4450
 */
4451
4452
static int
4453
piv_is_object_present(sc_card_t *card, u8 *ptr)
4454
33.9k
{
4455
33.9k
  piv_private_data_t *priv = PIV_DATA(card);
4456
33.9k
  int r = 0;
4457
33.9k
  int enumtag;
4458
4459
33.9k
  enumtag = piv_find_obj_by_containerid(card, ptr);
4460
33.9k
  if (enumtag >= 0 && priv->obj_cache[enumtag].flags & PIV_OBJ_CACHE_NOT_PRESENT)
4461
22.0k
    r = 1;
4462
4463
33.9k
  LOG_FUNC_RETURN(card->ctx, r);
4464
33.9k
}
4465
4466
/*
4467
 * NIST 800-73-3 allows the default pin to be the PIV application 0x80
4468
 * or the global pin for the card 0x00. Look at Discovery object to get this.
4469
 * called by pkcs15-piv.c  via cardctl when setting up the pins.
4470
 */
4471
static int
4472
piv_get_pin_preference(sc_card_t *card, int *pin_ref)
4473
547
{
4474
547
  piv_private_data_t *priv = PIV_DATA(card);
4475
4476
547
  *pin_ref = priv->pin_preference;
4477
547
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
4478
547
}
4479
4480
static int
4481
piv_card_ctl(sc_card_t *card, unsigned long cmd, void *ptr)
4482
35.0k
{
4483
35.0k
  piv_private_data_t *priv = PIV_DATA(card);
4484
35.0k
  u8 *opts; /*  A or M, key_ref, alg_id */
4485
4486
35.0k
  LOG_FUNC_CALLED(card->ctx);
4487
4488
35.0k
  if (priv == NULL) {
4489
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
4490
0
  }
4491
35.0k
  switch (cmd) {
4492
0
  case SC_CARDCTL_PIV_AUTHENTICATE:
4493
0
    opts = (u8 *)ptr;
4494
0
    switch (*opts) {
4495
0
    case 'A':
4496
0
      return piv_general_external_authenticate(card,
4497
0
          *(opts + 1), *(opts + 2));
4498
0
      break;
4499
0
    case 'M':
4500
0
      return piv_general_mutual_authenticate(card,
4501
0
          *(opts + 1), *(opts + 2));
4502
0
      break;
4503
0
    }
4504
0
    break;
4505
0
  case SC_CARDCTL_PIV_GENERATE_KEY:
4506
0
    return piv_generate_key(card,
4507
0
        (sc_cardctl_piv_genkey_info_t *)ptr);
4508
0
    break;
4509
547
  case SC_CARDCTL_GET_SERIALNR:
4510
547
    return piv_get_serial_nr_from_CHUI(card, (sc_serial_number_t *)ptr);
4511
0
    break;
4512
547
  case SC_CARDCTL_PIV_PIN_PREFERENCE:
4513
547
    return piv_get_pin_preference(card, ptr);
4514
0
    break;
4515
33.9k
  case SC_CARDCTL_PIV_OBJECT_PRESENT:
4516
33.9k
    return piv_is_object_present(card, ptr);
4517
0
    break;
4518
35.0k
  }
4519
4520
0
  LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
4521
0
}
4522
4523
static int
4524
piv_get_challenge(sc_card_t *card, u8 *rnd, size_t len)
4525
0
{
4526
  /* Dynamic Authentication Template (Challenge) */
4527
0
  u8 sbuf[] = {0x7c, 0x02, 0x81, 0x00};
4528
0
  u8 rbuf[4096];
4529
0
  const u8 *p;
4530
0
  size_t out_len = 0;
4531
0
  int r;
4532
0
  unsigned int tag_out = 0, cla_out = 0;
4533
0
  piv_private_data_t *priv = PIV_DATA(card);
4534
4535
0
  LOG_FUNC_CALLED(card->ctx);
4536
4537
0
  if (priv->card_issues & CI_NO_RANDOM) {
4538
0
    r = SC_ERROR_NOT_SUPPORTED;
4539
0
    LOG_TEST_GOTO_ERR(card->ctx, r, "No support for random data");
4540
0
  }
4541
4542
  /* NIST 800-73-3 says use 9B, previous versions used 00 */
4543
0
  r = piv_general_io(card, 0x87, 0x00, 0x9B, sbuf, sizeof sbuf, rbuf, sizeof rbuf);
4544
  /*
4545
   * piv_get_challenge is called in a loop.
4546
   * some cards may allow 1 challenge expecting it to be part of
4547
   * NIST 800-73-3 part 2 "Authentication of PIV Card Application Administrator"
4548
   * and return "6A 80" if last command was a get_challenge.
4549
   * Now that the card returned error, we can try one more time.
4550
   */
4551
0
  if (r == SC_ERROR_INCORRECT_PARAMETERS) {
4552
0
    r = piv_general_io(card, 0x87, 0x00, 0x9B, sbuf, sizeof sbuf, rbuf, sizeof rbuf);
4553
0
    if (r == SC_ERROR_INCORRECT_PARAMETERS) {
4554
0
      r = SC_ERROR_NOT_SUPPORTED;
4555
0
    }
4556
0
  }
4557
0
  LOG_TEST_GOTO_ERR(card->ctx, r, "GENERAL AUTHENTICATE failed");
4558
4559
0
  p = rbuf;
4560
0
  r = sc_asn1_read_tag(&p, r, &cla_out, &tag_out, &out_len);
4561
0
  if (r < 0 || (cla_out | tag_out) != 0x7C) {
4562
0
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_DATA, "Can't find Dynamic Authentication Template");
4563
0
  }
4564
4565
0
  r = sc_asn1_read_tag(&p, out_len, &cla_out, &tag_out, &out_len);
4566
0
  if (r < 0 || (cla_out | tag_out) != 0x81) {
4567
0
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_DATA, "Can't find Challenge");
4568
0
  }
4569
4570
0
  if (len < out_len) {
4571
0
    out_len = len;
4572
0
  }
4573
0
  memcpy(rnd, p, out_len);
4574
4575
0
  r = (int)out_len;
4576
4577
0
err:
4578
0
  LOG_FUNC_RETURN(card->ctx, r);
4579
0
}
4580
4581
static int
4582
piv_set_security_env(sc_card_t *card, const sc_security_env_t *env, int se_num)
4583
23
{
4584
23
  piv_private_data_t *priv = PIV_DATA(card);
4585
23
  int r = 0;
4586
4587
23
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4588
4589
23
  sc_log(card->ctx,
4590
23
      "flags=%08lx op=%d alg=%lu algf=%08lx algr=%08lx kr0=%02x, krfl=%" SC_FORMAT_LEN_SIZE_T "u",
4591
23
      env->flags, env->operation, env->algorithm, env->algorithm_flags,
4592
23
      env->algorithm_ref, env->key_ref[0], env->key_ref_len);
4593
4594
23
  priv->operation = env->operation;
4595
23
  priv->algorithm = env->algorithm;
4596
4597
23
  if (env->algorithm == SC_ALGORITHM_RSA) {
4598
12
    priv->alg_id = 0x06; /* Say it is RSA, set 5, 6, 7 later */
4599
12
  } else if (env->algorithm == SC_ALGORITHM_EC) {
4600
11
    if (env->flags & SC_SEC_ENV_ALG_REF_PRESENT) {
4601
11
      switch (env->algorithm_ref) {
4602
11
      case 256:
4603
11
        priv->alg_id = 0x11; /* Say it is EC 256 */
4604
11
        priv->key_size = 256;
4605
11
        break;
4606
0
      case 384:
4607
0
        priv->alg_id = 0x14;
4608
0
        priv->key_size = 384;
4609
0
        break;
4610
0
      default:
4611
0
        r = SC_ERROR_NO_CARD_SUPPORT;
4612
11
      }
4613
11
    } else
4614
0
      r = SC_ERROR_NO_CARD_SUPPORT;
4615
11
  } else if (env->algorithm == SC_ALGORITHM_EDDSA) {
4616
0
    priv->alg_id = 0xE0;
4617
0
    priv->key_size = 255;
4618
0
  } else if (env->algorithm == SC_ALGORITHM_XEDDSA) {
4619
0
    priv->alg_id = 0xE1;
4620
0
    priv->key_size = 255;
4621
0
  } else
4622
0
    r = SC_ERROR_NO_CARD_SUPPORT;
4623
23
  priv->key_ref = env->key_ref[0];
4624
4625
23
  LOG_FUNC_RETURN(card->ctx, r);
4626
23
}
4627
4628
static int
4629
piv_restore_security_env(sc_card_t *card, int se_num)
4630
0
{
4631
0
  LOG_FUNC_CALLED(card->ctx);
4632
4633
0
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
4634
0
}
4635
4636
static int
4637
piv_validate_general_authentication(sc_card_t *card,
4638
    const u8 *data, size_t datalen,
4639
    u8 *out, size_t outlen)
4640
23
{
4641
23
  piv_private_data_t *priv = PIV_DATA(card);
4642
23
  int r, tmplen, tmplen2;
4643
23
  u8 *p;
4644
23
  const unsigned char *p2;
4645
23
  size_t taglen;
4646
23
  size_t bodylen;
4647
23
  unsigned int cla, tag;
4648
23
  unsigned int real_alg_id, op_tag;
4649
4650
23
  u8 sbuf[4096]; /* needs work. for 4096 needs 512+10 or so */
4651
23
  size_t sbuflen = sizeof(sbuf);
4652
23
  u8 rbuf[4096];
4653
4654
23
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4655
4656
  /* should assume large send data */
4657
23
  p = sbuf;
4658
23
  tmplen = sc_asn1_put_tag(0xff, NULL, datalen, NULL, 0, NULL);
4659
23
  tmplen2 = sc_asn1_put_tag(0x82, NULL, 0, NULL, 0, NULL);
4660
23
  if (tmplen <= 0 || tmplen2 <= 0) {
4661
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
4662
0
  }
4663
23
  tmplen += tmplen2;
4664
23
  if ((r = sc_asn1_put_tag(0x7c, NULL, tmplen, p, sbuflen, &p)) != SC_SUCCESS ||
4665
23
      (r = sc_asn1_put_tag(0x82, NULL, 0, p, sbuflen - (p - sbuf), &p)) != SC_SUCCESS) {
4666
0
    LOG_FUNC_RETURN(card->ctx, r);
4667
0
  }
4668
23
  if (priv->operation == SC_SEC_OPERATION_DERIVE && (priv->algorithm == SC_ALGORITHM_EC || priv->algorithm == SC_ALGORITHM_XEDDSA)) {
4669
0
    op_tag = 0x85;
4670
23
  } else {
4671
23
    op_tag = 0x81;
4672
23
  }
4673
23
  r = sc_asn1_put_tag(op_tag, data, datalen, p, sbuflen - (p - sbuf), &p);
4674
23
  if (r != SC_SUCCESS) {
4675
0
    LOG_FUNC_RETURN(card->ctx, r);
4676
0
  }
4677
4678
  /*
4679
   * alg_id=06 is a place holder for all RSA keys.
4680
   * Derive the real alg_id based on the size of the
4681
   * the data, as we are always using raw mode.
4682
   * Non RSA keys needs some work in this area.
4683
   */
4684
4685
23
  real_alg_id = priv->alg_id;
4686
23
  if (priv->alg_id == 0x06) {
4687
12
    switch (datalen) {
4688
0
    case 128:
4689
0
      real_alg_id = 0x06;
4690
0
      break;
4691
9
    case 256:
4692
9
      real_alg_id = 0x07;
4693
9
      break;
4694
0
    case 384:
4695
0
      real_alg_id = 0x05;
4696
0
      break;
4697
0
    case 512:
4698
0
      real_alg_id = 0x16;
4699
0
      break;
4700
3
    default:
4701
3
      SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, SC_ERROR_NO_CARD_SUPPORT);
4702
12
    }
4703
12
  }
4704
  /* EC and ED alg_id was already set */
4705
4706
20
  r = piv_general_io(card, 0x87, real_alg_id, priv->key_ref,
4707
20
      sbuf, p - sbuf, rbuf, sizeof rbuf);
4708
20
  if (r < 0)
4709
10
    goto err;
4710
4711
10
  p2 = rbuf;
4712
10
  r = sc_asn1_read_tag(&p2, r, &cla, &tag, &bodylen);
4713
10
  if (p2 == NULL || r < 0 || bodylen == 0 || (cla | tag) != 0x7C) {
4714
5
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_DATA, "Can't find 0x7C");
4715
5
  }
4716
4717
5
  r = sc_asn1_read_tag(&p2, bodylen, &cla, &tag, &taglen);
4718
5
  if (p2 == NULL || r < 0 || taglen == 0 || (cla | tag) != 0x82) {
4719
4
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_DATA, "Can't find 0x82");
4720
4
  }
4721
4722
1
  if (taglen > outlen) {
4723
0
    LOG_TEST_GOTO_ERR(card->ctx, SC_ERROR_INVALID_DATA, "data read longer then buffer");
4724
0
  }
4725
4726
1
  memcpy(out, p2, taglen);
4727
1
  r = (int)taglen;
4728
4729
20
err:
4730
20
  LOG_FUNC_RETURN(card->ctx, r);
4731
20
}
4732
4733
static int
4734
piv_compute_signature(sc_card_t *card, const u8 *data, size_t datalen,
4735
    u8 *out, size_t outlen)
4736
12
{
4737
12
  piv_private_data_t *priv = PIV_DATA(card);
4738
12
  int r;
4739
12
  size_t nLen;
4740
12
  u8 rbuf[128]; /* For EC conversions  384 will fit */
4741
4742
12
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4743
  /* The PIV returns a DER SEQUENCE{INTEGER, INTEGER}
4744
   * Which may have leading 00 to force a positive integer
4745
   * But PKCS11 just wants 2* field_length in bytes
4746
   * So we have to strip out the integers
4747
   * and pad on left if too short.
4748
   */
4749
4750
12
  if (priv->alg_id == 0x11 || priv->alg_id == 0x14) {
4751
11
    nLen = BYTES4BITS(priv->key_size);
4752
11
    if (outlen < 2 * nLen) {
4753
0
      sc_log(card->ctx,
4754
0
          " output too small for EC signature %" SC_FORMAT_LEN_SIZE_T "u < %" SC_FORMAT_LEN_SIZE_T "u",
4755
0
          outlen, 2 * nLen);
4756
0
      r = SC_ERROR_INVALID_DATA;
4757
0
      goto err;
4758
0
    }
4759
4760
11
    r = piv_validate_general_authentication(card, data, datalen, rbuf, sizeof rbuf);
4761
11
    if (r < 0)
4762
9
      goto err;
4763
4764
2
    r = sc_asn1_decode_ecdsa_signature(card->ctx, rbuf, r, nLen, &out, outlen);
4765
    /* Yubikey 5.7.x supports ED25519 */
4766
2
  } else if (priv->alg_id == 0xE0) {
4767
0
    nLen = BYTES4BITS(priv->key_size);
4768
0
    if (outlen < nLen) {
4769
0
      sc_log(card->ctx,
4770
0
          " output too small for ED signature %" SC_FORMAT_LEN_SIZE_T "u < %" SC_FORMAT_LEN_SIZE_T "u",
4771
0
          outlen, nLen);
4772
0
      r = SC_ERROR_INVALID_DATA;
4773
0
      goto err;
4774
0
    }
4775
0
    r = piv_validate_general_authentication(card, data, datalen, out, outlen);
4776
4777
1
  } else { /* RSA is all set */
4778
1
    r = piv_validate_general_authentication(card, data, datalen, out, outlen);
4779
1
  }
4780
4781
12
err:
4782
12
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
4783
12
}
4784
4785
static int
4786
piv_decipher(sc_card_t *card, const u8 *data, size_t datalen, u8 *out, size_t outlen)
4787
11
{
4788
11
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4789
4790
11
  SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, piv_validate_general_authentication(card, data, datalen, out, outlen));
4791
11
}
4792
4793
/*
4794
 * the PIV-II does not always support files, but we will simulate
4795
 * files and reading/writing using get/put_data
4796
 * The path is the containerID number
4797
 * We can use this to determine the type of data requested, like a cert
4798
 * or pub key.
4799
 * We only support write from the piv_tool with file_out==NULL
4800
 * All other requests should be to read.
4801
 * Only if file_out != null, will we read to get length.
4802
 */
4803
static int
4804
piv_select_file(sc_card_t *card, const sc_path_t *in_path,
4805
    sc_file_t **file_out)
4806
4.33k
{
4807
4.33k
  piv_private_data_t *priv = PIV_DATA(card);
4808
4.33k
  int r;
4809
4.33k
  int i;
4810
4.33k
  const u8 *path;
4811
4.33k
  size_t pathlen;
4812
4.33k
  sc_file_t *file = NULL;
4813
4.33k
  u8 *rbuf = NULL;
4814
4.33k
  size_t rbuflen = 0;
4815
4816
4.33k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4817
4818
4.33k
  path = in_path->value;
4819
4.33k
  pathlen = in_path->len;
4820
4821
  /* only support single EF in current application */
4822
  /*
4823
   * PIV emulates files, and only does so because sc_pkcs15_* uses
4824
   * select_file and read_binary. The emulation adds path emulated structures
4825
   * so piv_select_file will find it.
4826
   * there is no dir. Only direct access to emulated files
4827
   * thus opensc-tool and opensc-explorer can not read the emulated files
4828
   */
4829
4830
4.33k
  if (memcmp(path, "\x3F\x00", 2) == 0) {
4831
1.20k
    if (pathlen > 2) {
4832
1.20k
      path += 2;
4833
1.20k
      pathlen -= 2;
4834
1.20k
    }
4835
1.20k
  }
4836
4837
4.33k
  i = piv_find_obj_by_containerid(card, path);
4838
4839
4.33k
  if (i < 0)
4840
4.33k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_NOT_FOUND);
4841
4842
  /*
4843
   * pkcs15 will use a 2 byte path or a 4 byte path
4844
   * with cece added to path to request only the cert from the cert obj
4845
   * PIV "Container ID" is used as the path, and are two bytes long
4846
   */
4847
3.11k
  priv->return_only_cert = (pathlen == 4 && path[2] == 0xce && path[3] == 0xce);
4848
4849
3.11k
  priv->selected_obj = i;
4850
3.11k
  priv->rwb_state = -1;
4851
4852
  /* make it look like the file was found. */
4853
  /* We don't want to read it now  unless we need the length */
4854
4855
3.11k
  if (file_out) {
4856
    /* we need to read it now, to get length into cache */
4857
3.11k
    r = piv_get_cached_data(card, i, &rbuf, &rbuflen);
4858
4859
3.11k
    if (r < 0)
4860
3.11k
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_FILE_NOT_FOUND);
4861
4862
    /* get the cert or the pub key out and into the cache too */
4863
431
    if (priv->return_only_cert || piv_objects[i].flags & PIV_OBJECT_TYPE_PUBKEY) {
4864
431
      r = piv_cache_internal_data(card, i);
4865
431
      if (r < 0)
4866
431
        LOG_FUNC_RETURN(card->ctx, r);
4867
431
    }
4868
4869
347
    file = sc_file_new();
4870
347
    if (file == NULL)
4871
347
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
4872
4873
347
    file->path = *in_path;
4874
    /* this could be like the FCI */
4875
347
    file->type = SC_FILE_TYPE_DF;
4876
347
    file->shareable = 0;
4877
347
    file->ef_structure = 0;
4878
347
    if (priv->return_only_cert)
4879
347
      file->size = priv->obj_cache[i].internal_obj_len;
4880
0
    else
4881
0
      file->size = priv->obj_cache[i].obj_len;
4882
4883
347
    file->id = (piv_objects[i].containerid[0] << 8) + piv_objects[i].containerid[1];
4884
4885
347
    *file_out = file;
4886
347
  }
4887
4888
347
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
4889
347
}
4890
4891
static int
4892
piv_parse_discovery(sc_card_t *card, u8 *rbuf, size_t rbuflen, int aid_only)
4893
77
{
4894
77
  piv_private_data_t *priv = PIV_DATA(card);
4895
77
  int r = 0;
4896
77
  const u8 *body;
4897
77
  size_t bodylen;
4898
77
  const u8 *aid;
4899
77
  size_t aidlen;
4900
77
  const u8 *pinp;
4901
77
  size_t pinplen;
4902
77
  unsigned int cla_out, tag_out;
4903
4904
77
  if (rbuflen != 0) {
4905
77
    body = rbuf;
4906
77
    if ((r = sc_asn1_read_tag(&body, rbuflen, &cla_out, &tag_out, &bodylen)) != SC_SUCCESS ||
4907
77
        body == NULL ||
4908
77
        bodylen == 0 ||
4909
76
        ((cla_out | tag_out) != 0x7E)) {
4910
1
      sc_log(card->ctx, "DER problem %d", r);
4911
1
      r = SC_ERROR_INVALID_ASN1_OBJECT;
4912
1
      goto err;
4913
1
    }
4914
4915
76
    sc_log(card->ctx,
4916
76
        "Discovery 0x%2.2x 0x%2.2x %p:%" SC_FORMAT_LEN_SIZE_T "u",
4917
76
        cla_out, tag_out, body, bodylen);
4918
76
    aidlen = 0;
4919
76
    aid = sc_asn1_find_tag(card->ctx, body, bodylen, 0x4F, &aidlen);
4920
76
    if (aid == NULL || aidlen < piv_aids[0].len_short ||
4921
67
        memcmp(aid, piv_aids[0].value, piv_aids[0].len_short) != 0) {
4922
33
      sc_log(card->ctx, "Discovery object not PIV");
4923
33
      r = SC_ERROR_INVALID_CARD; /* This is an error */
4924
33
      goto err;
4925
33
    }
4926
43
    if (aid_only == 0) {
4927
43
      pinp = sc_asn1_find_tag(card->ctx, body, bodylen, 0x5F2F, &pinplen);
4928
43
      if (pinp && pinplen == 2) {
4929
11
        priv->init_flags |= PIV_INIT_DISCOVERY_PP;
4930
11
        priv->pin_policy = (*pinp << 8) + *(pinp + 1);
4931
11
        sc_log(card->ctx, "Discovery pinp flags=0x%2.2x 0x%2.2x", *pinp, *(pinp + 1));
4932
11
        if ((priv->pin_policy & (PIV_PP_PIN | PIV_PP_GLOBAL)) == (PIV_PP_PIN | PIV_PP_GLOBAL) &&
4933
0
            priv->pin_policy & PIV_PP_GLOBAL_PRIMARY) {
4934
0
          sc_log(card->ctx, "Pin Preference - Global");
4935
0
          priv->pin_preference = 0x00;
4936
0
        }
4937
11
      }
4938
43
    }
4939
43
    r = SC_SUCCESS;
4940
43
    priv->init_flags |= PIV_INIT_DISCOVERY_PARSED;
4941
43
  }
4942
4943
77
err:
4944
77
  LOG_FUNC_RETURN(card->ctx, r);
4945
77
}
4946
4947
/* normal way to get the discovery object via cache */
4948
static int
4949
piv_process_discovery(sc_card_t *card)
4950
761
{
4951
761
  int r;
4952
761
  u8 *rbuf = NULL;
4953
761
  size_t rbuflen = 0;
4954
4955
761
  r = piv_get_cached_data(card, PIV_OBJ_DISCOVERY, &rbuf, &rbuflen);
4956
  /* Note rbuf and rbuflen are now pointers into cache */
4957
761
  if (r < 0)
4958
684
    goto err;
4959
4960
  /* the object is now cached, see what we have */
4961
77
  r = piv_parse_discovery(card, rbuf, rbuflen, 0);
4962
4963
761
err:
4964
761
  LOG_FUNC_RETURN(card->ctx, r);
4965
761
}
4966
4967
/*
4968
 * parse a CCC to test  if this is a Dual CAC/PIV
4969
 * We read the CCC using the PIV API.
4970
 * Look for CAC RID=A0 00 00 00 79
4971
 */
4972
static int
4973
piv_parse_ccc(sc_card_t *card, u8 *rbuf, size_t rbuflen)
4974
48
{
4975
48
  int r = 0;
4976
48
  const u8 *body;
4977
48
  size_t bodylen;
4978
48
  unsigned int cla_out, tag_out;
4979
4980
48
  u8 tag;
4981
48
  const u8 *end;
4982
48
  size_t len;
4983
4984
48
  piv_private_data_t *priv = PIV_DATA(card);
4985
4986
48
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
4987
4988
48
  if (rbuf == NULL || rbuflen == 0) {
4989
0
    r = SC_ERROR_WRONG_LENGTH;
4990
0
    goto err;
4991
0
  }
4992
4993
  /* Outer layer is a DER tlv */
4994
48
  body = rbuf;
4995
48
  if ((r = sc_asn1_read_tag(&body, rbuflen, &cla_out, &tag_out, &bodylen)) != SC_SUCCESS ||
4996
48
      body == NULL ||
4997
48
      bodylen == 0 ||
4998
46
      ((cla_out << 24 | tag_out) != piv_objects[PIV_OBJ_CCC].resp_tag)) {
4999
2
    sc_log(card->ctx, "DER problem %d", r);
5000
2
    r = SC_ERROR_INVALID_ASN1_OBJECT;
5001
2
    goto err;
5002
2
  }
5003
5004
46
  priv->ccc_flags |= PIV_CCC_FOUND;
5005
5006
  /* CCC  entries are simple tlv */
5007
46
  end = body + bodylen;
5008
803
  for (; (body < end); body += len) {
5009
797
    r = sc_simpletlv_read_tag(&body, end - body, &tag, &len);
5010
797
    if (r < 0)
5011
40
      goto err;
5012
757
    switch (tag) {
5013
46
    case PIV_CCC_TAG_F0:
5014
46
      if (len == 0x15) {
5015
37
        if (memcmp(body, "\xA0\x00\x00\x03\08", 5) == 0)
5016
12
          priv->ccc_flags |= PIV_CCC_F0_PIV;
5017
25
        else if (memcmp(body, "\xA0\x00\x00\x00\x79", 5) == 0)
5018
11
          priv->ccc_flags |= PIV_CCC_F0_CAC;
5019
37
        if (*(body + 6) == 0x02)
5020
8
          priv->ccc_flags |= PIV_CCC_F0_JAVA;
5021
37
      }
5022
46
      break;
5023
40
    case PIV_CCC_TAG_F3:
5024
40
      if (len == 0x10) {
5025
26
        if (memcmp(body, "\xA0\x00\x00\x00\x79\x04", 6) == 0)
5026
10
          priv->ccc_flags |= PIV_CCC_F3_CAC_PKI;
5027
26
      }
5028
40
      break;
5029
757
    }
5030
757
  }
5031
5032
48
err:
5033
48
  LOG_FUNC_RETURN(card->ctx, r);
5034
48
}
5035
5036
static int
5037
piv_process_ccc(sc_card_t *card)
5038
387
{
5039
387
  int r = 0;
5040
387
  u8 *rbuf = NULL;
5041
387
  size_t rbuflen = 0;
5042
5043
387
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
5044
387
  r = piv_get_cached_data(card, PIV_OBJ_CCC, &rbuf, &rbuflen);
5045
5046
387
  if (r < 0)
5047
339
    goto err;
5048
5049
  /* the object is now cached, see what we have */
5050
48
  r = piv_parse_ccc(card, rbuf, rbuflen);
5051
387
err:
5052
387
  LOG_FUNC_RETURN(card->ctx, r);
5053
387
}
5054
5055
static int
5056
piv_find_discovery(sc_card_t *card)
5057
808
{
5058
808
  int r = 0;
5059
808
  size_t rbuflen;
5060
808
  u8 *rbuf = NULL;
5061
808
  piv_private_data_t *priv = PIV_DATA(card);
5062
5063
808
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
5064
5065
  /*
5066
   * During piv_card_reader_lock_obtained,
5067
   * we use the discovery object to test if card present, and
5068
   * if PIV AID is active.
5069
   */
5070
808
  if (priv->obj_cache[PIV_OBJ_DISCOVERY].flags & PIV_OBJ_CACHE_NOT_PRESENT) {
5071
0
    r = SC_ERROR_DATA_OBJECT_NOT_FOUND;
5072
0
    goto end;
5073
0
  }
5074
5075
  /* If not valid: read, test,  cache */
5076
808
  if (!(priv->obj_cache[PIV_OBJ_DISCOVERY].flags & PIV_OBJ_CACHE_VALID)) {
5077
761
    r = piv_process_discovery(card);
5078
761
  } else {
5079
    /* if already in cache,force read */
5080
47
    rbuflen = 1;
5081
47
    r = piv_get_data(card, PIV_OBJ_DISCOVERY, &rbuf, &rbuflen);
5082
    /* if same response as last, no need to parse */
5083
47
    if (r == 0 && priv->obj_cache[PIV_OBJ_DISCOVERY].obj_len == 0)
5084
0
      goto end;
5085
5086
47
    if (r >= 0 && priv->obj_cache[PIV_OBJ_DISCOVERY].obj_len == rbuflen &&
5087
23
        priv->obj_cache[PIV_OBJ_DISCOVERY].obj_data &&
5088
23
        !memcmp(rbuf, priv->obj_cache[PIV_OBJ_DISCOVERY].obj_data, rbuflen)) {
5089
9
      goto end;
5090
9
    }
5091
    /* This should not happen  bad card */
5092
38
    sc_log(card->ctx, "Discovery not the same as previously read object");
5093
38
    r = SC_ERROR_CORRUPTED_DATA;
5094
38
    goto end;
5095
47
  }
5096
5097
808
end:
5098
808
  free(rbuf);
5099
808
  LOG_FUNC_RETURN(card->ctx, r);
5100
808
}
5101
5102
/*
5103
 * The history object lists what retired keys and certs are on the card
5104
 * or listed in the offCardCertURL. The user will need to download the
5105
 * the offCardURL file.
5106
 * If the offCardCertURL is not cached by the user it will not be used.
5107
 */
5108
static int
5109
piv_process_history(sc_card_t *card)
5110
550
{
5111
550
  piv_private_data_t *priv = PIV_DATA(card);
5112
550
  int r;
5113
550
  int i, tmplen, tmplen2, tmplen3;
5114
550
  int enumtag;
5115
550
  u8 *rbuf = NULL;
5116
550
  size_t rbuflen = 0;
5117
550
  const u8 *body;
5118
550
  size_t bodylen;
5119
550
  const u8 *num;
5120
550
  size_t numlen;
5121
550
  const u8 *url = NULL;
5122
550
  size_t urllen;
5123
550
  u8 *ocfhfbuf = NULL;
5124
550
  unsigned int cla_out, tag_out;
5125
550
  size_t ocfhflen;
5126
550
  const u8 *seq;
5127
550
  const u8 *seqtag;
5128
550
  size_t seqlen;
5129
550
  const u8 *keyref;
5130
550
  size_t keyreflen;
5131
550
  const u8 *cert;
5132
550
  size_t certlen;
5133
550
  size_t certobjlen, i2;
5134
550
  u8 *certobj;
5135
550
  u8 *cp;
5136
5137
550
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
5138
5139
550
  r = piv_get_cached_data(card, PIV_OBJ_HISTORY, &rbuf, &rbuflen);
5140
550
  if (r == SC_ERROR_FILE_NOT_FOUND)
5141
96
    r = 0; /* OK if not found */
5142
550
  if (r <= 0) {
5143
461
    priv->obj_cache[PIV_OBJ_HISTORY].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
5144
461
    goto err; /* no file, must be pre 800-73-3 card and not on card */
5145
461
  }
5146
5147
  /* the object is now cached, see what we have */
5148
89
  if (rbuflen != 0) {
5149
89
    body = rbuf;
5150
89
    if ((r = sc_asn1_read_tag(&body, rbuflen, &cla_out, &tag_out, &bodylen)) != SC_SUCCESS ||
5151
89
        ((cla_out << 24 | tag_out) != piv_objects[PIV_OBJ_HISTORY].resp_tag)) {
5152
0
      sc_log(card->ctx, "DER problem %d", r);
5153
0
      r = SC_ERROR_INVALID_ASN1_OBJECT;
5154
0
      goto err;
5155
0
    }
5156
5157
89
    if (body != NULL && bodylen != 0) {
5158
88
      numlen = 0;
5159
88
      num = sc_asn1_find_tag(card->ctx, body, bodylen, 0xC1, &numlen);
5160
88
      if (num) {
5161
28
        if (numlen != 1 || *num > PIV_OBJ_RETIRED_X509_20 - PIV_OBJ_RETIRED_X509_1 + 1) {
5162
4
          r = SC_ERROR_INVALID_ASN1_OBJECT;
5163
4
          goto err;
5164
4
        }
5165
5166
24
        priv->keysWithOnCardCerts = *num;
5167
24
      }
5168
5169
84
      numlen = 0;
5170
84
      num = sc_asn1_find_tag(card->ctx, body, bodylen, 0xC2, &numlen);
5171
84
      if (num) {
5172
1
        if (numlen != 1 || *num > PIV_OBJ_RETIRED_X509_20 - PIV_OBJ_RETIRED_X509_1 + 1) {
5173
0
          r = SC_ERROR_INVALID_ASN1_OBJECT;
5174
0
          goto err;
5175
0
        }
5176
5177
1
        priv->keysWithOffCardCerts = *num;
5178
1
      }
5179
5180
84
      url = sc_asn1_find_tag(card->ctx, body, bodylen, 0xF3, &urllen);
5181
84
      if (url) {
5182
53
        if (urllen > 118) {
5183
0
          r = SC_ERROR_INVALID_ASN1_OBJECT;
5184
0
          goto err;
5185
0
        }
5186
53
        priv->offCardCertURL = calloc(1, urllen + 1);
5187
53
        if (priv->offCardCertURL == NULL)
5188
53
          LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
5189
53
        memcpy(priv->offCardCertURL, url, urllen);
5190
53
      }
5191
84
    } else {
5192
1
      sc_log(card->ctx, "Problem with History object\n");
5193
1
      r = SC_SUCCESS; /* OK if not found */
5194
1
      goto err;
5195
1
    }
5196
89
  }
5197
84
  sc_log(card->ctx, "History on=%d off=%d URL=%s",
5198
84
      priv->keysWithOnCardCerts, priv->keysWithOffCardCerts,
5199
84
      priv->offCardCertURL ? priv->offCardCertURL : "NONE");
5200
5201
  /* now mark what objects are on the card */
5202
460
  for (i = 0; i < priv->keysWithOnCardCerts; i++)
5203
376
    priv->obj_cache[PIV_OBJ_RETIRED_X509_1 + i].flags &= ~PIV_OBJ_CACHE_NOT_PRESENT;
5204
5205
  /*
5206
   * If user has gotten copy of the file from the offCardCertsURL,
5207
   * we will read in and add the certs to the cache as listed on
5208
   * the card. some of the certs may be on the card as well.
5209
   *
5210
   * Get file name from url. verify that the filename is valid
5211
   * The URL ends in a SHA-256 string. We will use this as the filename
5212
   * in the directory used for the  PKCS15 cache
5213
   * "http://" <DNS name> "/" <ASCII-HEX encoded SHA-256 hash of OffCardKeyHistoryFile>
5214
   */
5215
5216
84
  r = 0;
5217
84
  if (priv->offCardCertURL) {
5218
53
    char *fp;
5219
53
    char filename[PATH_MAX];
5220
5221
53
    if (strncmp("http://", priv->offCardCertURL, 7)) {
5222
51
      r = SC_ERROR_INVALID_DATA;
5223
51
      goto err;
5224
51
    }
5225
    /* find the last /  so we have the filename part */
5226
2
    fp = strrchr(priv->offCardCertURL + 7, '/');
5227
2
    if (fp == NULL) {
5228
1
      r = SC_ERROR_INVALID_DATA;
5229
1
      goto err;
5230
1
    }
5231
1
    fp++;
5232
1
    if (strlen(fp) != 64) { /* ASCII-HEX encoded SHA-256 */
5233
1
      r = SC_ERROR_INVALID_DATA;
5234
1
      goto err;
5235
1
    }
5236
0
    for (i = 0; i < 64; i++) {
5237
0
      if (isxdigit((unsigned char)fp[i]) == 0) {
5238
0
        r = SC_ERROR_INVALID_DATA;
5239
0
        goto err;
5240
0
      }
5241
0
    }
5242
5243
    /* Use the same directory as used for other OpenSC cached items */
5244
0
    r = sc_get_cache_dir(card->ctx, filename, sizeof(filename) - strlen(fp) - 2);
5245
0
    if (r != SC_SUCCESS)
5246
0
      goto err;
5247
#ifdef _WIN32
5248
    strlcat(filename, "\\", PATH_MAX);
5249
#else
5250
0
    strlcat(filename, "/", PATH_MAX);
5251
0
#endif
5252
0
    strlcat(filename, fp, PATH_MAX);
5253
5254
0
    r = piv_read_obj_from_file(card, filename,
5255
0
        &ocfhfbuf, &ocfhflen);
5256
0
    if (r == SC_ERROR_FILE_NOT_FOUND) {
5257
0
      r = 0;
5258
0
      goto err;
5259
0
    }
5260
5261
    /*
5262
     * Its a seq of seq of a key ref and cert
5263
     */
5264
5265
0
    body = ocfhfbuf;
5266
0
    if (sc_asn1_read_tag(&body, ocfhflen, &cla_out, &tag_out, &bodylen) != SC_SUCCESS ||
5267
0
        body == NULL ||
5268
0
        bodylen == 0 ||
5269
0
        (cla_out | tag_out) != 0x30) {
5270
0
      sc_log(card->ctx, "DER problem");
5271
0
      r = SC_ERROR_INVALID_ASN1_OBJECT;
5272
0
      goto err;
5273
0
    }
5274
0
    seq = body;
5275
0
    while (bodylen > 0) {
5276
0
      seqtag = seq;
5277
0
      if (sc_asn1_read_tag(&seq, bodylen, &cla_out, &tag_out, &seqlen) != SC_SUCCESS ||
5278
0
          seq == 0 ||
5279
0
          seqlen == 0 ||
5280
0
          (cla_out | tag_out) != 0x30) {
5281
0
        sc_log(card->ctx, "DER problem");
5282
0
        r = SC_ERROR_INVALID_ASN1_OBJECT;
5283
0
        goto err;
5284
0
      }
5285
0
      keyref = sc_asn1_find_tag(card->ctx, seq, seqlen, 0x04, &keyreflen);
5286
0
      if (!keyref || keyreflen != 1 ||
5287
0
          (*keyref < 0x82 || *keyref > 0x95)) {
5288
0
        sc_log(card->ctx, "DER problem");
5289
0
        r = SC_ERROR_INVALID_ASN1_OBJECT;
5290
0
        goto err;
5291
0
      }
5292
0
      cert = keyref + keyreflen;
5293
0
      certlen = seqlen - (cert - seq);
5294
5295
0
      enumtag = PIV_OBJ_RETIRED_X509_1 + *keyref - 0x82;
5296
      /* now add the cert like another object */
5297
5298
0
      if ((tmplen = sc_asn1_put_tag(0x70, NULL, certlen, NULL, 0, NULL)) <= 0 ||
5299
0
          (tmplen2 = sc_asn1_put_tag(0x71, NULL, 1, NULL, 0, NULL)) <= 0 ||
5300
0
          (tmplen3 = sc_asn1_put_tag(0xFE, NULL, 0, NULL, 0, NULL)) <= 0) {
5301
0
        r = SC_ERROR_INVALID_ASN1_OBJECT;
5302
0
        goto err;
5303
0
      }
5304
0
      i2 = tmplen + tmplen2 + tmplen3;
5305
0
      tmplen = sc_asn1_put_tag(0x53, NULL, i2, NULL, 0, NULL);
5306
0
      if (tmplen <= 0) {
5307
0
        r = SC_ERROR_INVALID_ASN1_OBJECT;
5308
0
        goto err;
5309
0
      }
5310
5311
0
      certobjlen = tmplen;
5312
0
      certobj = malloc(certobjlen);
5313
0
      if (certobj == NULL) {
5314
0
        r = SC_ERROR_OUT_OF_MEMORY;
5315
0
        goto err;
5316
0
      }
5317
0
      cp = certobj;
5318
0
      if ((r = sc_asn1_put_tag(0x53, NULL, i2, cp, certobjlen, &cp)) != SC_SUCCESS ||
5319
0
          (r = sc_asn1_put_tag(0x70, cert, certlen, cp, certobjlen - (cp - certobj), &cp)) != SC_SUCCESS ||
5320
0
          (r = sc_asn1_put_tag(0x71, NULL, 1, cp, certobjlen - (cp - certobj), &cp)) != SC_SUCCESS) {
5321
0
        goto err;
5322
0
      }
5323
0
      *cp++ = 0x00;
5324
0
      r = sc_asn1_put_tag(0xFE, NULL, 0, cp, certobjlen - (cp - certobj), &cp);
5325
0
      if (r != SC_SUCCESS) {
5326
0
        goto err;
5327
0
      }
5328
5329
0
      priv->obj_cache[enumtag].obj_data = certobj;
5330
0
      priv->obj_cache[enumtag].obj_len = certobjlen;
5331
0
      priv->obj_cache[enumtag].flags |= PIV_OBJ_CACHE_VALID;
5332
0
      priv->obj_cache[enumtag].flags &= ~PIV_OBJ_CACHE_NOT_PRESENT;
5333
5334
0
      r = piv_cache_internal_data(card, enumtag);
5335
0
      sc_log(card->ctx, "got internal r=%d", r);
5336
5337
0
      sc_log(card->ctx,
5338
0
          "Added from off card file #%d %p:%" SC_FORMAT_LEN_SIZE_T "u 0x%02X",
5339
0
          enumtag,
5340
0
          priv->obj_cache[enumtag].obj_data,
5341
0
          priv->obj_cache[enumtag].obj_len, *keyref);
5342
5343
0
      bodylen -= (seqlen + seq - seqtag);
5344
0
      seq += seqlen;
5345
0
    }
5346
0
  }
5347
550
err:
5348
550
  if (ocfhfbuf)
5349
0
    free(ocfhfbuf);
5350
550
  LOG_FUNC_RETURN(card->ctx, r);
5351
550
}
5352
5353
static int
5354
piv_obj_cache_free_entry(sc_card_t *card, int enumtag, int flags)
5355
259k
{
5356
259k
  piv_private_data_t *priv = PIV_DATA(card);
5357
5358
259k
  if (priv->obj_cache[enumtag].obj_data)
5359
724
    free(priv->obj_cache[enumtag].obj_data);
5360
259k
  priv->obj_cache[enumtag].obj_data = NULL;
5361
259k
  priv->obj_cache[enumtag].obj_len = 0;
5362
5363
259k
  if (priv->obj_cache[enumtag].internal_obj_data)
5364
347
    free(priv->obj_cache[enumtag].internal_obj_data);
5365
259k
  priv->obj_cache[enumtag].internal_obj_data = NULL;
5366
259k
  priv->obj_cache[enumtag].internal_obj_len = 0;
5367
259k
  priv->obj_cache[enumtag].flags = flags;
5368
5369
259k
  return SC_SUCCESS;
5370
259k
}
5371
5372
static int
5373
piv_finish(sc_card_t *card)
5374
8.09k
{
5375
8.09k
  piv_private_data_t *priv = PIV_DATA(card);
5376
8.09k
  int i;
5377
5378
8.09k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
5379
8.09k
  if (priv) {
5380
4.32k
    if (priv->context_specific) {
5381
1
      sc_log(card->ctx, "Clearing CONTEXT_SPECIFIC lock");
5382
1
      priv->context_specific = 0;
5383
1
      sc_unlock(card);
5384
1
    }
5385
4.32k
    free(priv->aid_der.value);
5386
4.32k
    free(priv->al_label);
5387
4.32k
    if (priv->w_buf)
5388
0
      free(priv->w_buf);
5389
4.32k
    if (priv->offCardCertURL)
5390
53
      free(priv->offCardCertURL);
5391
263k
    for (i = 0; i < PIV_OBJ_LAST_ENUM - 1; i++) {
5392
259k
      piv_obj_cache_free_entry(card, i, 0);
5393
259k
    }
5394
#ifdef ENABLE_PIV_SM
5395
    piv_clear_cvc_content(&priv->sm_cvc);
5396
    piv_clear_cvc_content(&priv->sm_in_cvc);
5397
    piv_clear_sm_session(&priv->sm_session);
5398
#endif /* ENABLE_PIV_SM */
5399
5400
4.32k
    free(priv);
5401
4.32k
    card->drv_data = NULL; /* priv */
5402
4.32k
  }
5403
8.09k
  return 0;
5404
8.09k
}
5405
5406
static int
5407
piv_match_card(sc_card_t *card)
5408
4.32k
{
5409
4.32k
  int r = 0;
5410
5411
4.32k
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d\n", card->type);
5412
  /* piv_match_card may be called with card->type, set by opensc.conf */
5413
  /* user provided card type must be one we know */
5414
4.32k
  switch (card->type) {
5415
4.32k
  case -1:
5416
4.32k
  case SC_CARD_TYPE_PIV_II_BASE:
5417
4.32k
  case SC_CARD_TYPE_PIV_II_GENERIC:
5418
4.32k
  case SC_CARD_TYPE_PIV_II_HIST:
5419
4.32k
  case SC_CARD_TYPE_PIV_II_NEO:
5420
4.32k
  case SC_CARD_TYPE_PIV_II_YUBIKEY4:
5421
4.32k
  case SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC:
5422
4.32k
  case SC_CARD_TYPE_PIV_II_GI_DE:
5423
4.32k
  case SC_CARD_TYPE_PIV_II_GEMALTO_DUAL_CAC:
5424
4.32k
  case SC_CARD_TYPE_PIV_II_GEMALTO:
5425
4.32k
  case SC_CARD_TYPE_PIV_II_OBERTHUR_DUAL_CAC:
5426
4.32k
  case SC_CARD_TYPE_PIV_II_OBERTHUR:
5427
4.32k
  case SC_CARD_TYPE_PIV_II_PIVKEY:
5428
4.32k
  case SC_CARD_TYPE_PIV_II_SWISSBIT:
5429
4.32k
  case SC_CARD_TYPE_PIV_II_800_73_4:
5430
4.32k
  case SC_CARD_TYPE_PIV_II_NITROKEY:
5431
4.32k
  case SC_CARD_TYPE_PIV_II_TOKEN2:
5432
4.32k
  case SC_CARD_TYPE_PIV_II_PIVAPPLET:
5433
4.32k
    break;
5434
0
  default:
5435
0
    return 0; /* can not handle the card */
5436
4.32k
  }
5437
5438
4.32k
  r = sc_lock(card);
5439
4.32k
  if (r < 0)
5440
0
    return 0;
5441
  /* its one we know, or we can test for it in piv_init */
5442
4.32k
  r = piv_match_card_continued(card);
5443
4.32k
  sc_unlock(card);
5444
5445
4.32k
  if (r < 0 || !card->drv_data) {
5446
    /* clean up what we left in card */
5447
3.77k
    piv_finish(card);
5448
3.77k
    return 0; /* match failed */
5449
3.77k
  }
5450
5451
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r:%d\n", card->type, r);
5452
550
  return 1; /* matched */
5453
4.32k
}
5454
5455
static int
5456
piv_match_card_continued(sc_card_t *card)
5457
4.32k
{
5458
4.32k
  int i, r = 0, r2 = 0;
5459
4.32k
  int type = -1;
5460
4.32k
  piv_private_data_t *priv = NULL;
5461
4.32k
  int saved_type = card->type;
5462
4.32k
  sc_apdu_t apdu;
5463
4.32k
  u8 yubico_version_buf[3] = {0};
5464
5465
  /* piv_match_card may be called with card->type, set by opensc.conf */
5466
  /* User provided card type must be one we know */
5467
5468
4.32k
  switch (card->type) {
5469
4.32k
  case -1:
5470
4.32k
  case SC_CARD_TYPE_PIV_II_BASE:
5471
4.32k
  case SC_CARD_TYPE_PIV_II_GENERIC:
5472
4.32k
  case SC_CARD_TYPE_PIV_II_HIST:
5473
4.32k
  case SC_CARD_TYPE_PIV_II_NEO:
5474
4.32k
  case SC_CARD_TYPE_PIV_II_YUBIKEY4:
5475
4.32k
  case SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC:
5476
4.32k
  case SC_CARD_TYPE_PIV_II_GI_DE:
5477
4.32k
  case SC_CARD_TYPE_PIV_II_GEMALTO_DUAL_CAC:
5478
4.32k
  case SC_CARD_TYPE_PIV_II_GEMALTO:
5479
4.32k
  case SC_CARD_TYPE_PIV_II_OBERTHUR_DUAL_CAC:
5480
4.32k
  case SC_CARD_TYPE_PIV_II_OBERTHUR:
5481
4.32k
  case SC_CARD_TYPE_PIV_II_PIVKEY:
5482
4.32k
  case SC_CARD_TYPE_PIV_II_SWISSBIT:
5483
4.32k
  case SC_CARD_TYPE_PIV_II_800_73_4:
5484
4.32k
  case SC_CARD_TYPE_PIV_II_NITROKEY:
5485
4.32k
  case SC_CARD_TYPE_PIV_II_TOKEN2:
5486
4.32k
  case SC_CARD_TYPE_PIV_II_PIVAPPLET:
5487
4.32k
    type = card->type;
5488
4.32k
    break;
5489
0
  default:
5490
0
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_WRONG_CARD);
5491
4.32k
  }
5492
4.32k
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d type:%d r:%d\n", card->type, type, r);
5493
4.32k
  if (type == -1) {
5494
    /*
5495
     * Try to identify card by ATR or historical data in ATR
5496
     * currently all PIV card will respond to piv_find_aid
5497
     * the same. But in future may need to know card type first,
5498
     * so do it here.
5499
     */
5500
5501
4.32k
    if (card->reader->atr_info.hist_bytes != NULL) {
5502
1.11k
      if (card->reader->atr_info.hist_bytes_len == 8 &&
5503
93
          !(memcmp(card->reader->atr_info.hist_bytes, "Yubikey4", 8))) {
5504
25
        type = SC_CARD_TYPE_PIV_II_YUBIKEY4;
5505
1.08k
      } else if (card->reader->atr_info.hist_bytes_len >= 7 &&
5506
681
          !(memcmp(card->reader->atr_info.hist_bytes, "Yubikey", 7))) {
5507
79
        type = SC_CARD_TYPE_PIV_II_NEO;
5508
1.00k
      } else if (card->reader->atr_info.hist_bytes_len >= 6 &&
5509
648
          !(memcmp(card->reader->atr_info.hist_bytes, "PIVKEY", 6))) {
5510
21
        type = SC_CARD_TYPE_PIV_II_PIVKEY;
5511
986
      } else if (card->reader->atr_info.hist_bytes_len >= 6 &&
5512
627
          !(memcmp(card->reader->atr_info.hist_bytes, (u8 *)"TK\x00PIV", 6))) {
5513
9
        type = SC_CARD_TYPE_PIV_II_TOKEN2;
5514
9
      }
5515
      /* look for TLV historic data */
5516
977
      else if (card->reader->atr_info.hist_bytes_len > 0 &&
5517
962
          card->reader->atr_info.hist_bytes[0] == 0x80u) { /* compact TLV */
5518
239
        size_t datalen;
5519
239
        const u8 *data;
5520
5521
        /* look for card issuer's data:  tag 5X where X is datalen */
5522
239
        if ((data = sc_compacttlv_find_tag(card->reader->atr_info.hist_bytes + 1,
5523
239
                 card->reader->atr_info.hist_bytes_len - 1, 0x50, &datalen))) {
5524
124
          if (datalen >= 8 && !(memcmp(data, "Nitrokey", 8))) { /* first 8 are Nitrokey */
5525
14
            type = SC_CARD_TYPE_PIV_II_NITROKEY;
5526
110
          } else if (datalen == 7 && !(memcmp(data, "YubiKey", 7))) {
5527
89
            type = SC_CARD_TYPE_PIV_II_YUBIKEY4; /* reader says 4  really 5 */
5528
89
          }
5529
          /* Yubikey 5 NFC ATR using ACR122 contactless reader does not match
5530
           * https://developers.yubico.com/PIV/Introduction/Yubico_extensions.html
5531
           * On Windows 10, using Omnikey 5021, the ATR is correct
5532
           * will look at only 6 bytes that do match
5533
           */
5534
21
          else if (datalen == 7 && !(memcmp(data, "YubiKe", 6))) {
5535
3
            type = SC_CARD_TYPE_PIV_II_YUBIKEY4; /* reader says 4 really 5 */
5536
3
          }
5537
124
        } else if ((data = sc_compacttlv_find_tag(card->reader->atr_info.hist_bytes + 1,
5538
115
                  card->reader->atr_info.hist_bytes_len - 1, 0xF0, &datalen))) {
5539
54
          int k;
5540
5541
106
          for (k = 0; piv_aids[k].len_long != 0; k++) {
5542
54
            if (datalen == piv_aids[k].len_long &&
5543
48
                !memcmp(data, piv_aids[k].value, datalen)) {
5544
2
              type = SC_CARD_TYPE_PIV_II_HIST;
5545
2
              break;
5546
2
            }
5547
54
          }
5548
54
        }
5549
239
      }
5550
1.11k
    }
5551
5552
4.32k
    sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d type:%d r:%d\n", card->type, type, r);
5553
5554
4.32k
    if (type == -1) {
5555
      /* use known ATRs  which changes the type */
5556
4.07k
      i = _sc_match_atr(card, piv_atrs, &type);
5557
4.07k
      if (i < 0)
5558
4.07k
        type = SC_CARD_TYPE_PIV_II_BASE; /* May be some newer unknown card including CAC or PIV-like card */
5559
4.07k
    }
5560
4.32k
  }
5561
5562
4.32k
  card->type = type;
5563
5564
  /* we either found via ATR historic bytes or ATR directly */
5565
4.32k
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d type:%d r:%d\n", card->type, type, r);
5566
5567
  /* allocate and init basic fields */
5568
4.32k
  priv = calloc(1, sizeof(piv_private_data_t));
5569
5570
4.32k
  if (!priv)
5571
4.32k
    LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
5572
5573
4.32k
  card->drv_data = priv; /* will free if no match, or pass on to piv_init */
5574
  /*
5575
   * Largest object defined in NIST sp800-73-3 and sp800-73-4 is 12710 bytes
5576
   * If for some reason future cards have larger objects, this value needs to
5577
   * be increased here.
5578
   */
5579
4.32k
  priv->max_object_size = MAX_FILE_SIZE - 256; /* fix SM apdu resplen issue */
5580
4.32k
  priv->selected_obj = -1;
5581
4.32k
  priv->pin_preference = 0x80; /* 800-73-3 part 1, table 3 */
5582
4.32k
  priv->logged_in = SC_PIN_STATE_UNKNOWN;
5583
4.32k
  priv->pstate = PIV_STATE_MATCH;
5584
5585
#ifdef ENABLE_PIV_SM
5586
  memset(&card->sm_ctx, 0, sizeof card->sm_ctx);
5587
  card->sm_ctx.ops.open = piv_sm_open;
5588
  card->sm_ctx.ops.get_sm_apdu = piv_get_sm_apdu;
5589
  card->sm_ctx.ops.free_sm_apdu = piv_free_sm_apdu;
5590
  card->sm_ctx.ops.close = piv_sm_close;
5591
#endif /* ENABLE_PIV_SM */
5592
5593
  /* See if contactless  or run as virtual card */
5594
  /* PivApplet in .github/test_piv.sh for example */
5595
4.32k
  if (card->reader->atr.len >= 4 &&
5596
1.24k
      card->reader->atr.value[0] == 0x3b &&
5597
1.13k
      (card->reader->atr.value[1] & 0xF0) == 0x80 &&
5598
73
      card->reader->atr.value[2] == 0x80 &&
5599
39
      card->reader->atr.value[3] == 0x01) {
5600
26
    priv->init_flags |= PIV_INIT_CONTACTLESS;
5601
26
  }
5602
5603
263k
  for (i = 0; i < PIV_OBJ_LAST_ENUM - 1; i++)
5604
259k
    if (piv_objects[i].flags & PIV_OBJECT_NOT_PRESENT)
5605
86.4k
      priv->obj_cache[i].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
5606
  /*
5607
   * Detect if active AID is PIV. NIST 800-73 says only one PIV application per card
5608
   * and PIV must be the default application.
5609
   * Try to avoid doing a select_aid and losing the login state on some cards.
5610
   * We may get interference on some cards by other drivers trying SELECT_AID before
5611
   * we get to see if PIV application is still active. Putting PIV driver first might help.
5612
   *
5613
   * Discovery Object introduced in 800-73-3 so will return OK if found and PIV applet active.
5614
   * Will fail with SC_ERROR_FILE_NOT_FOUND if 800-73-3 and no Discovery object.
5615
   * But some other card could also return SC_ERROR_FILE_NOT_FOUND.
5616
   * Will fail for other reasons if wrong applet is selected or bad PIV implementation.
5617
   */
5618
5619
  /*
5620
   * if ATR matched or user forced card type
5621
   * test if PIV is active applet without using AID If fails try AID
5622
   */
5623
5624
4.32k
  if (card->type > SC_CARD_TYPE_PIV_II_BASE &&
5625
245
      !(priv->init_flags & PIV_INIT_CONTACTLESS)) {
5626
244
    r = piv_find_discovery(card);
5627
244
    if (r < 0) {
5628
215
      piv_obj_cache_free_entry(card, PIV_OBJ_DISCOVERY, 0); /* don't cache  on failure */
5629
215
      r = piv_find_aid(card);
5630
215
      LOG_TEST_GOTO_ERR(card->ctx, r, "Not a PIV card");
5631
215
    }
5632
4.07k
  } else {
5633
    /* piv_find_aid saves al_label from response */
5634
4.07k
    r = piv_find_aid(card);
5635
4.07k
    LOG_TEST_GOTO_ERR(card->ctx, r, "Not a PIV card");
5636
4.07k
  }
5637
5638
  /* Know to be PIV card but not the type */
5639
5640
  /* If card type still unknown, see if Application Label is known and set card->type */
5641
550
  if (priv->al_label && priv->al_labellen) {
5642
2
    switch (card->type) {
5643
2
    case SC_CARD_TYPE_PIV_II_BASE:
5644
2
    case SC_CARD_TYPE_PIV_II_GENERIC:
5645
4
      for (i = 0; al_map[i].al_label; i++) {
5646
2
        if ((priv->al_labellen >= al_map[i].al_labellen) &&
5647
0
            (!memcmp(priv->al_label, al_map[i].al_label, al_map[i].al_labellen))) {
5648
0
          sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "AL match i:%d type:%d", i, al_map[i].al_type);
5649
0
          card->type = al_map[i].al_type;
5650
0
          break;
5651
0
        }
5652
2
      }
5653
2
      break;
5654
2
    }
5655
2
  }
5656
5657
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d  CI:%08x r:%d AI:%08x\n",
5658
550
      card->type, priv->card_issues, r, priv->alg_ids);
5659
5660
  /*
5661
   * Assumes all Yubikey/Nitrokey/Token2/PivApplet cards are all "Yubico like"
5662
   * via ATR, ATR Historic bytes or Application Label
5663
   * Will also check if BASE cards are Yubikey like card and return a version number
5664
   * SC_CARD_TYPE_PIV_II_GENERIC can be set by user to not test for Yubico version
5665
   */
5666
550
  switch (card->type) {
5667
59
  case SC_CARD_TYPE_PIV_II_NEO:
5668
143
  case SC_CARD_TYPE_PIV_II_YUBIKEY4:
5669
156
  case SC_CARD_TYPE_PIV_II_NITROKEY:
5670
160
  case SC_CARD_TYPE_PIV_II_TOKEN2:
5671
160
  case SC_CARD_TYPE_PIV_II_PIVAPPLET:
5672
160
  case SC_CARD_TYPE_PIV_II_GENERIC:
5673
546
  case SC_CARD_TYPE_PIV_II_BASE: /* unknown PIV card */
5674
546
    sc_format_apdu(card, &apdu, SC_APDU_CASE_2_SHORT, 0xFD, 0x00, 0x00);
5675
546
    apdu.lc = 0;
5676
546
    apdu.data = NULL;
5677
546
    apdu.datalen = 0;
5678
546
    apdu.resp = yubico_version_buf;
5679
546
    apdu.resplen = sizeof(yubico_version_buf);
5680
546
    apdu.le = apdu.resplen;
5681
546
    r2 = sc_transmit_apdu(card, &apdu); /* if not supported yubico_version == 0 */
5682
546
    if (apdu.resplen == 3) {
5683
313
      priv->yubico_version = (yubico_version_buf[0] << 16) | (yubico_version_buf[1] << 8) | yubico_version_buf[2];
5684
313
      sc_log(card->ctx, "Yubikey version test card->type=%d, r=0x%08x version=0x%08x", card->type, r, priv->yubico_version);
5685
313
    }
5686
546
    break;
5687
550
  }
5688
5689
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5690
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5691
5692
  /* We now know PIV AID is active, test CCC object. 800-73-* say CCC is required */
5693
  /* CCC not readable over contactless, unless using VCI. but dont need CCC for SC_CARD_TYPE_PIV_II_800_73_4 */
5694
550
  switch (card->type) {
5695
  /*
5696
   * For cards that may also be CAC, try and read the CCC
5697
   * CCC is required and all Dual PIV/CAC will have a CCC
5698
   * Currently Dual PIV/CAC are based on NIST 800-73-1 which does not have Discovery or History
5699
   */
5700
386
  case SC_CARD_TYPE_PIV_II_BASE: /* i.e. really dont know what this is */
5701
386
  case SC_CARD_TYPE_PIV_II_GENERIC:
5702
387
  case SC_CARD_TYPE_PIV_II_HIST:
5703
387
  case SC_CARD_TYPE_PIV_II_GI_DE:
5704
387
  case SC_CARD_TYPE_PIV_II_GEMALTO:
5705
387
  case SC_CARD_TYPE_PIV_II_OBERTHUR:
5706
387
    r2 = piv_process_ccc(card);
5707
387
    sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5708
387
        card->type, r2, priv->card_issues, r, priv->alg_ids);
5709
    /* Ignore any error. */
5710
    /* If CCC says it has CAC with PKI on card set to one of the SC_CARD_TYPE_PIV_II_*_DUAL_CAC */
5711
387
    if (priv->ccc_flags & PIV_CCC_F3_CAC_PKI) {
5712
4
      switch (card->type) {
5713
4
      case SC_CARD_TYPE_PIV_II_BASE:
5714
4
      case SC_CARD_TYPE_PIV_II_GENERIC:
5715
4
      case SC_CARD_TYPE_PIV_II_HIST:
5716
4
      case SC_CARD_TYPE_PIV_II_GI_DE:
5717
4
        card->type = SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC;
5718
4
        priv->card_issues |= CI_DISCOVERY_USELESS;
5719
4
        priv->obj_cache[PIV_OBJ_DISCOVERY].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
5720
4
        break;
5721
0
      case SC_CARD_TYPE_PIV_II_GEMALTO:
5722
0
        card->type = SC_CARD_TYPE_PIV_II_GEMALTO_DUAL_CAC;
5723
0
        priv->card_issues |= CI_DISCOVERY_USELESS;
5724
0
        priv->obj_cache[PIV_OBJ_DISCOVERY].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
5725
0
        break;
5726
0
      case SC_CARD_TYPE_PIV_II_OBERTHUR:
5727
0
        card->type = SC_CARD_TYPE_PIV_II_OBERTHUR_DUAL_CAC;
5728
0
        priv->card_issues |= CI_DISCOVERY_USELESS;
5729
0
        priv->obj_cache[PIV_OBJ_DISCOVERY].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
5730
0
        break;
5731
4
      }
5732
4
    }
5733
387
    break;
5734
5735
387
  case SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC:
5736
0
  case SC_CARD_TYPE_PIV_II_GEMALTO_DUAL_CAC:
5737
0
  case SC_CARD_TYPE_PIV_II_OBERTHUR_DUAL_CAC:
5738
0
    priv->card_issues |= CI_DISCOVERY_USELESS;
5739
0
    priv->obj_cache[PIV_OBJ_DISCOVERY].flags |= PIV_OBJ_CACHE_NOT_PRESENT;
5740
0
    break;
5741
550
  }
5742
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5743
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5744
5745
  /* If contactless or run as virtual card try to get alg refs from AC entries in response to SELECT AID */
5746
550
  if (!(priv->init_flags & PIV_INIT_AID_PARSED) && priv->init_flags & PIV_INIT_CONTACTLESS) {
5747
0
    r2 = piv_find_aid(card);
5748
0
  }
5749
5750
  /* Read AID if needed for these cards types */
5751
550
  if (!(priv->init_flags & PIV_INIT_AID_PARSED)) {
5752
29
    switch (card->type) {
5753
0
    case SC_CARD_TYPE_PIV_II_BASE:
5754
0
    case SC_CARD_TYPE_PIV_II_GENERIC:
5755
0
    case SC_CARD_TYPE_PIV_II_800_73_4:
5756
0
    case SC_CARD_TYPE_PIV_II_NITROKEY:
5757
1
    case SC_CARD_TYPE_PIV_II_TOKEN2:
5758
1
    case SC_CARD_TYPE_PIV_II_PIVAPPLET:
5759
1
      r2 = piv_find_aid(card);
5760
29
    }
5761
29
  }
5762
5763
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5764
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5765
5766
  /* if card did not specify any of the NIST basic alg_ids add them */
5767
550
  if ((priv->alg_ids & AI_NIST) == 0) {
5768
548
    priv->alg_ids |= AI_NIST;
5769
548
  }
5770
5771
  /* If unknown card has 800-73-4 features, it must be based on 800-73-4 or above */
5772
550
  switch (card->type) {
5773
382
  case SC_CARD_TYPE_PIV_II_BASE:
5774
382
  case SC_CARD_TYPE_PIV_II_GENERIC:
5775
382
    if (priv->init_flags & PIV_INIT_AID_AC_SM) {
5776
0
      card->type = SC_CARD_TYPE_PIV_II_800_73_4;
5777
0
    }
5778
5779
#ifdef ENABLE_PIV_SM
5780
    /* Discovery object has pin policy. 800-74-4 bits, its at least SC_CARD_TYPE_PIV_II_800_73_4 */
5781
    if ((priv->pin_policy & (PIV_PP_OCC | PIV_PP_VCI_IMPL | PIV_PP_VCI_WITHOUT_PC)) != 0) {
5782
      card->type = SC_CARD_TYPE_PIV_II_800_73_4;
5783
    }
5784
#endif
5785
382
    break;
5786
550
  }
5787
5788
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5789
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5790
5791
  /*
5792
   * Set card_issues flags based card->type and version numbers if available.
5793
   *
5794
   * YubiKey NEO, Yubikey 4 and other devices with PIV applets, have compliance
5795
   * issues with the NIST 800-73-3 specs. The OpenSC developers do not have
5796
   * access to all the different devices or versions of the devices.
5797
   * Vendor and user input is welcome on any compliance issues.
5798
   *
5799
   * For the Yubico devices The assumption is also made that if a bug is
5800
   * fixed in a Yubico version that means it is fixed on both NEO and Yubikey 4.
5801
   *
5802
   * The flags CI_CANT_USE_GETDATA_FOR_STATE and CI_DISCOVERY_USELESS
5803
   * may be set earlier or later then in the following code.
5804
   */
5805
5806
550
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5807
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5808
550
  switch (card->type) {
5809
59
  case SC_CARD_TYPE_PIV_II_NEO:
5810
59
    priv->card_issues |= CI_NO_EC384 |
5811
59
             CI_VERIFY_630X |
5812
59
             CI_OTHER_AID_LOSE_STATE |
5813
59
             CI_LEAKS_FILE_NOT_FOUND |
5814
59
             CI_NFC_EXPOSE_TOO_MUCH;
5815
59
    if (priv->yubico_version < 0x00040302)
5816
29
      priv->card_issues |= CI_VERIFY_LC0_FAIL;
5817
59
    break;
5818
5819
84
  case SC_CARD_TYPE_PIV_II_YUBIKEY4:
5820
84
  case SC_CARD_TYPE_PIV_II_PIVAPPLET:
5821
88
  case SC_CARD_TYPE_PIV_II_TOKEN2:
5822
88
    priv->card_issues |= CI_OTHER_AID_LOSE_STATE |
5823
88
             CI_LEAKS_FILE_NOT_FOUND;
5824
88
    if (priv->yubico_version < 0x00040302)
5825
49
      priv->card_issues |= CI_VERIFY_LC0_FAIL;
5826
88
    if (priv->yubico_version >= 0x00050700) /* Also used by Token2 */
5827
36
      priv->alg_ids |= AI_RSA_4096 | AI_25519;
5828
88
    break;
5829
5830
13
  case SC_CARD_TYPE_PIV_II_NITROKEY:
5831
13
    priv->card_issues |= CI_OTHER_AID_LOSE_STATE;
5832
13
    if (priv->yubico_version >= 0x00010802) /* use for NitroKey too */
5833
11
      priv->alg_ids |= AI_RSA_4096;
5834
13
    break;
5835
5836
0
  case SC_CARD_TYPE_PIV_II_GI_DE:
5837
0
  case SC_CARD_TYPE_PIV_II_OBERTHUR:
5838
0
  case SC_CARD_TYPE_PIV_II_GEMALTO:
5839
0
  case SC_CARD_TYPE_PIV_II_SWISSBIT:
5840
0
    priv->card_issues |= 0; /* could add others here */
5841
0
    break;
5842
5843
382
  case SC_CARD_TYPE_PIV_II_BASE:
5844
383
  case SC_CARD_TYPE_PIV_II_HIST:
5845
384
  case SC_CARD_TYPE_PIV_II_800_73_4:
5846
384
    priv->card_issues |= 0; /* could add others here */
5847
384
    break;
5848
5849
4
  case SC_CARD_TYPE_PIV_II_GI_DE_DUAL_CAC:
5850
4
  case SC_CARD_TYPE_PIV_II_GEMALTO_DUAL_CAC:
5851
4
  case SC_CARD_TYPE_PIV_II_OBERTHUR_DUAL_CAC:
5852
4
    priv->card_issues |= CI_VERIFY_LC0_FAIL |
5853
4
             CI_PIV_AID_LOSE_STATE |
5854
4
             CI_NO_RANDOM |
5855
4
             CI_OTHER_AID_LOSE_STATE;
5856
4
    break;
5857
5858
0
  case SC_CARD_TYPE_PIV_II_GENERIC:
5859
0
    priv->card_issues |= CI_VERIFY_LC0_FAIL |
5860
0
             CI_OTHER_AID_LOSE_STATE;
5861
0
    break;
5862
5863
2
  case SC_CARD_TYPE_PIV_II_PIVKEY:
5864
2
    priv->card_issues |= CI_VERIFY_LC0_FAIL |
5865
2
             CI_PIV_AID_LOSE_STATE | /* be conservative */
5866
2
             CI_NO_RANDOM;      /* does not have 9B key */
5867
                   /* Discovery object returns 6A 82 so is not on card by default */
5868
2
    break;
5869
5870
0
  default:
5871
0
    priv->card_issues |= CI_VERIFY_LC0_FAIL |
5872
0
             CI_OTHER_AID_LOSE_STATE;
5873
    /* opensc.conf may have it wrong, continue anyway */
5874
0
    sc_log(card->ctx, "Unknown PIV card->type %d", card->type);
5875
0
    card->type = SC_CARD_TYPE_PIV_II_GENERIC;
5876
550
  }
5877
550
  sc_log(card->ctx, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5878
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5879
5880
550
  if (!(priv->card_issues & CI_DISCOVERY_USELESS) && !(priv->init_flags & PIV_INIT_DISCOVERY_PARSED)) {
5881
    /*
5882
     * We now know PIV AID is active, test DISCOVERY object again
5883
     * Some PIV don't support DISCOVERY and return
5884
     * SC_ERROR_INCORRECT_PARAMETERS. Any error
5885
     * including SC_ERROR_FILE_NOT_FOUND means we cannot use discovery
5886
     * to test for active AID.
5887
     */
5888
517
    r2 = piv_find_discovery(card);
5889
5890
517
    if (r2 < 0) {
5891
503
      priv->card_issues |= CI_DISCOVERY_USELESS;
5892
503
      piv_obj_cache_free_entry(card, PIV_OBJ_DISCOVERY, PIV_OBJ_CACHE_NOT_PRESENT);
5893
503
    }
5894
517
  }
5895
5896
550
  sc_log(card->ctx, "PIV_MATCH card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5897
550
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5898
  /* Matched, caller will use or free priv and sc_lock as needed */
5899
550
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
5900
5901
3.77k
err:
5902
3.77k
  sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "PIV_MATCH failed card->type:%d r2:%d CI:%08x r:%d AI:%08x\n",
5903
3.77k
      card->type, r2, priv->card_issues, r, priv->alg_ids);
5904
  /* don't match. Does not have a PIV applet. */
5905
3.77k
  piv_finish(card);
5906
3.77k
  card->type = saved_type;
5907
3.77k
  LOG_FUNC_RETURN(card->ctx, r);
5908
3.77k
}
5909
5910
static int
5911
piv_init(sc_card_t *card)
5912
550
{
5913
550
  int r = 0;
5914
550
  piv_private_data_t *priv = PIV_DATA(card);
5915
550
  unsigned long flags;
5916
550
  unsigned long flags_eddsa;
5917
550
  unsigned long flags_xeddsa;
5918
550
  unsigned long ext_flags;
5919
5920
550
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
5921
5922
550
  r = sc_lock(card); /* hold until match or init is complete */
5923
550
  LOG_TEST_RET(card->ctx, r, "sc_lock failed");
5924
5925
  /* piv_match_card_continued called from card match should have left card->drv_data */
5926
550
  if (priv == NULL) {
5927
0
    r = piv_match_card_continued(card);
5928
0
    priv = PIV_DATA(card);
5929
0
    if (r < 0 || !priv) {
5930
0
      sc_log(card->ctx, "piv_match_card_continued failed card->type:%d", card->type);
5931
0
      sc_unlock(card);
5932
0
      piv_finish(card);
5933
      /* tell sc_connect_card to try other driver */
5934
0
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_CARD);
5935
0
    }
5936
0
  }
5937
5938
  /* read "card_driver PIV-II" opensc.conf options, and env parameters */
5939
550
  piv_load_options(card);
5940
5941
550
  priv->pstate = PIV_STATE_INIT;
5942
5943
550
  sc_log(card->ctx,
5944
550
      "Max send = %" SC_FORMAT_LEN_SIZE_T "u recv = %" SC_FORMAT_LEN_SIZE_T "u card->type:%d, CI:%08x AI:%08x",
5945
550
      card->max_send_size, card->max_recv_size, card->type, priv->card_issues, priv->alg_ids);
5946
550
  card->cla = 0x00;
5947
550
  if (card->name == NULL)
5948
550
    card->name = card->driver->name;
5949
5950
550
  priv->enumtag = piv_aids[0].enumtag;
5951
5952
  /* PKCS#11 may try to generate session keys, and get confused
5953
   * if SC_ALGORITHM_ONBOARD_KEY_GEN is present
5954
   * piv-tool can still do this, just don't tell PKCS#11
5955
   */
5956
5957
550
  flags = SC_ALGORITHM_RSA_RAW;
5958
5959
550
  if (card->type == SC_CARD_TYPE_PIV_II_SWISSBIT) {
5960
0
    flags |= SC_ALGORITHM_ONBOARD_KEY_GEN;
5961
0
  }
5962
5963
550
  if (priv->alg_ids & AI_RSA_1024)
5964
549
    _sc_card_add_rsa_alg(card, 1024, flags, 0); /* mandatory */
5965
5966
550
  if (priv->alg_ids & AI_RSA_2048)
5967
549
    _sc_card_add_rsa_alg(card, 2048, flags, 0); /* optional */
5968
5969
550
  if (priv->alg_ids & AI_RSA_3072)
5970
548
    _sc_card_add_rsa_alg(card, 3072, flags, 0); /* optional */
5971
5972
550
  if (priv->alg_ids & AI_RSA_4096)
5973
47
    _sc_card_add_rsa_alg(card, 4096, flags, 0); /* non standard */
5974
5975
550
  if (!(priv->card_issues & CI_NO_EC)) {
5976
550
    int i;
5977
550
    flags = SC_ALGORITHM_ECDSA_RAW | SC_ALGORITHM_ECDH_CDH_RAW | SC_ALGORITHM_ECDSA_HASH_NONE;
5978
550
    ext_flags = SC_ALGORITHM_EXT_EC_NAMEDCURVE | SC_ALGORITHM_EXT_EC_UNCOMPRESES;
5979
550
    flags_eddsa = SC_ALGORITHM_EDDSA_RAW;
5980
550
    flags_xeddsa = SC_ALGORITHM_XEDDSA_RAW;
5981
5982
2.75k
    for (i = 0; ec_curves[i].oid.value[0] >= 0; i++) {
5983
2.20k
      if (ec_curves[i].key_type == SC_ALGORITHM_EC) {
5984
1.10k
        if (!(priv->card_issues & CI_NO_EC384 && ec_curves[i].size == 384))
5985
1.04k
          _sc_card_add_ec_alg(card, ec_curves[i].size, flags, ext_flags, &ec_curves[i].oid);
5986
1.10k
      }
5987
5988
1.10k
      else if (priv->alg_ids & AI_25519 && ec_curves[i].key_type == SC_ALGORITHM_EDDSA)
5989
36
        _sc_card_add_eddsa_alg(card, ec_curves[i].size, flags_eddsa, ext_flags, &ec_curves[i].oid);
5990
5991
1.06k
      else if (priv->alg_ids & AI_X25519 && ec_curves[i].key_type == SC_ALGORITHM_XEDDSA)
5992
0
        _sc_card_add_xeddsa_alg(card, ec_curves[i].size, flags_xeddsa, ext_flags, &ec_curves[i].oid);
5993
2.20k
    }
5994
550
  }
5995
5996
550
  if (!(priv->card_issues & CI_NO_RANDOM))
5997
544
    card->caps |= SC_CARD_CAP_RNG;
5998
5999
  /* May turn off SC_CARD_CAP_ISO7816_PIN_INFO later */
6000
550
  card->caps |= SC_CARD_CAP_ISO7816_PIN_INFO;
6001
6002
  /*
6003
   * 800-73-3 cards may have discovery. "piv-like cards may or may not.
6004
   * 800-73-4 with VCI must have it as it has the pin policy needed for VCI .
6005
   */
6006
6007
#ifdef ENABLE_PIV_SM
6008
  /*
6009
   * 800-73-4
6010
   * Response of AID says if SM is supported. Look for Cipher Suite
6011
   */
6012
  if (priv->csID && priv->cs != NULL) {
6013
    /*
6014
     * Main point in SM and VCI is to allow contactless access
6015
     */
6016
    /* Only piv_init and piv_reader_lock_obtained should call piv_sm_open */
6017
6018
    /* If user said PIV_SM_FLAGS_NEVER, dont start SM; implies limited contatless access */
6019
    if (priv->sm_flags & PIV_SM_FLAGS_NEVER) {
6020
      sc_log(card->ctx, "User has requested PIV_SM_FLAGS_NEVER");
6021
      r = SC_SUCCESS; /* Users choice */
6022
6023
    } else if ((priv->init_flags & PIV_INIT_CONTACTLESS) &&
6024
        !(priv->pin_policy & PIV_PP_VCI_IMPL)) {
6025
      sc_log(card->ctx, "Contactless and no card support for VCI");
6026
      r = SC_SUCCESS; /* User should know VCI is not possible with their card; use like 800-73-3 contactless  */
6027
6028
    } else if ((priv->init_flags & PIV_INIT_CONTACTLESS) &&
6029
        !(priv->pin_policy & PIV_PP_VCI_WITHOUT_PC) &&
6030
        (priv->pairing_code[0] == 0x00)) {
6031
      sc_log(card->ctx, "Contactless, pairing_code required and no pairing code");
6032
      r = SC_ERROR_PIN_CODE_INCORRECT; /* User should know they need to set pairing code */
6033
6034
    } else {
6035
      priv->sm_flags |= PIV_SM_FLAGS_DEFER_OPEN; /* tell priv_sm_open, OK to open */
6036
      r = piv_sm_open(card);
6037
      sc_log(card->ctx, "piv_sm_open returned:%d", r);
6038
    }
6039
6040
    /* If failed, and user said PIV_SM_FLAGS_ALWAYS quit */
6041
    if (priv->sm_flags & PIV_SM_FLAGS_ALWAYS && r < 0) {
6042
      sc_log(card->ctx, "User has requested PIV_SM_FLAGS_ALWAYS, SM has failed to start, don't use the card");
6043
      LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_ALLOWED);
6044
    }
6045
6046
    /* user has wrong or no required pairing code */
6047
    if (r == SC_ERROR_PIN_CODE_INCORRECT)
6048
      LOG_FUNC_RETURN(card->ctx, r);
6049
6050
    /* If SM did not start, or is not expected to start, continue on without it */
6051
  }
6052
#endif /* ENABLE_PIV_SM */
6053
6054
  /*
6055
   * 800-73-3 cards may have a history object
6056
   * We want to process it now as this has information on what
6057
   * keys and certs. "piv like" cards may or may not have history
6058
   */
6059
550
  piv_process_history(card);
6060
6061
550
  priv->pstate = PIV_STATE_NORMAL;
6062
550
  sc_unlock(card);
6063
550
  LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
6064
550
}
6065
6066
static int
6067
piv_check_sw(struct sc_card *card, unsigned int sw1, unsigned int sw2)
6068
10.3k
{
6069
10.3k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
6070
6071
10.3k
  int r;
6072
#ifdef ENABLE_PIV_SM
6073
  int i;
6074
#endif
6075
10.3k
  piv_private_data_t *priv = PIV_DATA(card);
6076
6077
10.3k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
6078
6079
  /* may be called before piv_init has allocated priv */
6080
10.3k
  if (priv) {
6081
    /* need to save sw1 and sw2 if trying to determine card_state from pin_cmd */
6082
6083
10.3k
    if (priv->pin_cmd_verify) {
6084
58
      priv->pin_cmd_verify_sw1 = sw1;
6085
58
      priv->pin_cmd_verify_sw2 = sw2;
6086
10.3k
    } else {
6087
      /* a command has completed and it is not verify
6088
       * If we are in a context_specific sequence, unlock
6089
       * This just decrements the extra lock count
6090
       */
6091
10.3k
      if (priv->context_specific) {
6092
7
        sc_log(card->ctx, "Clearing CONTEXT_SPECIFIC lock");
6093
7
        priv->context_specific = 0;
6094
7
        sc_unlock(card);
6095
7
      }
6096
10.3k
    }
6097
6098
10.3k
    if (priv->card_issues & CI_VERIFY_630X) {
6099
6100
      /* Handle the Yubikey NEO or any other PIV card which returns in response to a verify
6101
       * 63 0X rather than 63 CX indicate the number of remaining PIN retries.
6102
       * Perhaps they misread the spec and thought 0xCX meant "clear" or "don't care", not a literal 0xC!
6103
       */
6104
629
      if (priv->pin_cmd_verify && sw1 == 0x63U) {
6105
1
        priv->pin_cmd_verify_sw2 |= 0xC0U; /* make it easier to test in other code */
6106
1
        if ((sw2 & ~0x0fU) == 0x00U) {
6107
1
          sc_log(card->ctx, "Verification failed (remaining tries: %d)", (sw2 & 0x0f));
6108
1
          return SC_ERROR_PIN_CODE_INCORRECT;
6109
          /* this is what the iso_check_sw returns for 63 C0 */
6110
1
        }
6111
1
      }
6112
629
    }
6113
10.3k
  }
6114
#ifdef ENABLE_PIV_SM
6115
  /* Note 6982 is map to SC_ERROR_SM_NO_SESSION_KEYS but iso maps it to SC_ERROR_SECURITY_STATUS_NOT_SATISFIED */
6116
  /* we do this because 6982 could also mean a verify is not allowed over contactless without VCI */
6117
  /* we stashed the sw1 and sw2 above for verify */
6118
  /* Check specific NIST sp800-73-4 SM  errors */
6119
  for (i = 0; piv_sm_errors[i].SWs != 0; i++) {
6120
    if (piv_sm_errors[i].SWs == ((sw1 << 8) | sw2)) {
6121
      sc_log(card->ctx, "%s", piv_sm_errors[i].errorstr);
6122
      return piv_sm_errors[i].errorno;
6123
    }
6124
  }
6125
#endif
6126
10.3k
  r = iso_drv->ops->check_sw(card, sw1, sw2);
6127
10.3k
  return r;
6128
10.3k
}
6129
6130
static int
6131
piv_check_protected_objects(sc_card_t *card)
6132
0
{
6133
0
  int r = 0;
6134
0
  int i;
6135
0
  piv_private_data_t *priv = PIV_DATA(card);
6136
0
  u8 buf[8]; /* tag of 53 with 82 xx xx  will fit in 4 */
6137
0
  u8 *rbuf;
6138
0
  size_t buf_len;
6139
0
  static int protected_objects[] = {PIV_OBJ_PI, PIV_OBJ_CHF, PIV_OBJ_IRIS_IMAGE};
6140
6141
0
  LOG_FUNC_CALLED(card->ctx);
6142
  /*
6143
   * routine only called from piv_pin_cmd after verify lc=0 did not return 90 00
6144
   * We will test for a protected object using GET DATA.
6145
   *
6146
   * Based on observations, of cards using the GET DATA APDU,
6147
   * SC_ERROR_SECURITY_STATUS_NOT_SATISFIED  means the PIN not verified,
6148
   * SC_SUCCESS means PIN has been verified even if it has length 0
6149
   * SC_ERROR_FILE_NOT_FOUND (which is the bug) does not tell us anything
6150
   * about the state of the PIN and we will try the next object.
6151
   *
6152
   * If we can't determine the security state from this process,
6153
   * set card_issues CI_CANT_USE_GETDATA_FOR_STATE
6154
   * and return SC_ERROR_PIN_CODE_INCORRECT
6155
   * The circumvention is to add a dummy Printed Info object in the card.
6156
   * so we will have an object to test.
6157
   *
6158
   * We save the object's number to use in the future.
6159
   *
6160
   */
6161
0
  if (priv->object_test_verify == 0) {
6162
0
    for (i = 0; i < (int)(sizeof(protected_objects) / sizeof(int)); i++) {
6163
0
      buf_len = sizeof(buf);
6164
0
      rbuf = buf;
6165
0
      r = piv_get_data(card, protected_objects[i], &rbuf, &buf_len);
6166
0
      if (r >= 0 || r == SC_ERROR_SECURITY_STATUS_NOT_SATISFIED) {
6167
6168
        /* we can use this object next time if needed */
6169
0
        priv->object_test_verify = protected_objects[i];
6170
0
        break;
6171
0
      }
6172
0
    }
6173
0
    if (priv->object_test_verify == 0) {
6174
      /*
6175
       * none of the objects returned acceptable sw1, sw2
6176
       */
6177
0
      sc_log(card->ctx, "No protected objects found, setting CI_CANT_USE_GETDATA_FOR_STATE");
6178
0
      priv->card_issues |= CI_CANT_USE_GETDATA_FOR_STATE;
6179
0
      r = SC_ERROR_PIN_CODE_INCORRECT;
6180
0
    }
6181
0
  } else {
6182
    /* use the one object we found earlier. Test is security status has changed */
6183
0
    buf_len = sizeof(buf);
6184
0
    rbuf = buf;
6185
0
    r = piv_get_data(card, priv->object_test_verify, &rbuf, &buf_len);
6186
0
  }
6187
0
  if (r == SC_ERROR_FILE_NOT_FOUND)
6188
0
    r = SC_ERROR_PIN_CODE_INCORRECT;
6189
0
  else if (r == SC_ERROR_SECURITY_STATUS_NOT_SATISFIED)
6190
0
    r = SC_ERROR_PIN_CODE_INCORRECT;
6191
0
  else if (r > 0)
6192
0
    r = SC_SUCCESS;
6193
6194
0
  sc_log(card->ctx, "card->type:%d CI:%08x r:%d AI:%08x\n",
6195
0
      card->type, priv->card_issues, r, priv->alg_ids);
6196
0
  LOG_FUNC_RETURN(card->ctx, r);
6197
0
}
6198
6199
static int
6200
piv_pin_cmd(sc_card_t *card, struct sc_pin_cmd_data *data)
6201
45
{
6202
45
  int r = 0;
6203
45
  piv_private_data_t *priv = PIV_DATA(card);
6204
6205
  /* Extra validation of (new) PIN during a PIN change request, to
6206
   * ensure it's not outside the FIPS 201 4.1.6.1 (numeric only) and
6207
   * FIPS 140-2 (6 character minimum) requirements.
6208
   */
6209
45
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
6210
6211
45
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
6212
45
  sc_log(card->ctx, "piv_pin_cmd tries_left=%d, logged_in=%d", priv->tries_left, priv->logged_in);
6213
45
  if (data->cmd == SC_PIN_CMD_CHANGE) {
6214
0
    size_t i = 0;
6215
0
    if (data->pin2.len < 6) {
6216
0
      return SC_ERROR_INVALID_PIN_LENGTH;
6217
0
    }
6218
0
    for (i = 0; i < data->pin2.len; ++i) {
6219
0
      if (!isdigit(data->pin2.data[i])) {
6220
0
        return SC_ERROR_INVALID_DATA;
6221
0
      }
6222
0
    }
6223
0
  }
6224
6225
45
  priv->pin_cmd_verify_sw1 = 0x00U;
6226
6227
45
  if (data->cmd == SC_PIN_CMD_GET_INFO) { /* fill in what we think it should be */
6228
0
    data->pin1.logged_in = priv->logged_in;
6229
0
    data->pin1.tries_left = priv->tries_left;
6230
6231
    /*
6232
     * If called to check on the login state for a context specific login
6233
     * return not logged in. Needed because of logic in e6f7373ef066
6234
     */
6235
0
    if (data->pin_type == SC_AC_CONTEXT_SPECIFIC) {
6236
0
      data->pin1.logged_in = SC_PIN_STATE_LOGGED_OUT;
6237
0
      LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
6238
0
    }
6239
6240
0
    if (priv->logged_in & SC_PIN_STATE_LOGGED_IN) {
6241
      /* Avoid status requests when the user is logged in to handle NIST
6242
       * 800-73-4 Part 2:
6243
       * The PKI cryptographic function (see Table 4b) is protected with
6244
       * a “PIN Always” or “OCC Always” access rule. In other words, the
6245
       * PIN or OCC data must be submitted and verified every time
6246
       * immediately before a digital signature key operation.  This
6247
       * ensures cardholder participation every time the private key is
6248
       * used for digital signature generation */
6249
0
      LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
6250
0
    }
6251
0
  }
6252
6253
  /*
6254
   * If this was for a CKU_CONTEXT_SPECFIC login, lock the card one more time.
6255
   * to avoid any interference from other applications.
6256
   * Sc_unlock will be called at a later time after the next card command
6257
   * that should be a crypto operation. If its not then it is a error by the
6258
   * calling application.
6259
   */
6260
45
  if (data->cmd == SC_PIN_CMD_VERIFY && data->pin_type == SC_AC_CONTEXT_SPECIFIC) {
6261
15
    priv->context_specific = 1;
6262
15
    sc_log(card->ctx, "Starting CONTEXT_SPECIFIC verify");
6263
15
    r = sc_lock(card);
6264
15
    if (r != SC_SUCCESS) {
6265
0
      sc_log(card->ctx, "sc_lock failed");
6266
0
      return r;
6267
0
    }
6268
15
  }
6269
6270
45
  priv->pin_cmd_verify = 1; /* tell piv_check_sw its a verify to save sw1, sw2 */
6271
45
  r = iso_drv->ops->pin_cmd(card, data);
6272
45
  priv->pin_cmd_verify = 0;
6273
6274
  /* tell user verify not supported on contactless without VCI */
6275
45
  if (priv->pin_cmd_verify_sw1 == 0x69 && priv->pin_cmd_verify_sw2 == 0x82 &&
6276
1
      priv->init_flags & PIV_INIT_CONTACTLESS &&
6277
0
      card->type == SC_CARD_TYPE_PIV_II_800_73_4) {
6278
0
    sc_log(card->ctx, "Token does not support pin verify over contacless reader");
6279
0
    r = SC_ERROR_NOT_SUPPORTED;
6280
0
  }
6281
6282
  /* if verify failed, release the lock */
6283
45
  if (data->cmd == SC_PIN_CMD_VERIFY && r < 0 && priv->context_specific) {
6284
7
    sc_log(card->ctx, "Clearing CONTEXT_SPECIFIC");
6285
7
    priv->context_specific = 0;
6286
7
    sc_unlock(card);
6287
7
  }
6288
6289
  /* if access to applet is know to be reset by other driver  we select_aid and try again */
6290
45
  if (priv->card_issues & CI_OTHER_AID_LOSE_STATE && priv->pin_cmd_verify_sw1 == 0x6DU) {
6291
13
    sc_log(card->ctx, "AID may be lost doing piv_find_aid and retry pin_cmd");
6292
13
    piv_find_aid(card);
6293
6294
13
    priv->pin_cmd_verify = 1; /* tell piv_check_sw its a verify to save sw1, sw2 */
6295
13
    r = iso_drv->ops->pin_cmd(card, data);
6296
13
    priv->pin_cmd_verify = 0;
6297
13
  }
6298
6299
  /* If verify worked, we are logged_in */
6300
45
  if (data->cmd == SC_PIN_CMD_VERIFY) {
6301
45
    if (r >= 0)
6302
19
      priv->logged_in = SC_PIN_STATE_LOGGED_IN;
6303
26
    else
6304
26
      priv->logged_in = SC_PIN_STATE_LOGGED_OUT;
6305
45
  }
6306
6307
  /* Some cards never return 90 00  for SC_PIN_CMD_GET_INFO even if the card state is verified */
6308
  /* PR 797 has changed the return codes from pin_cmd, and added a data->pin1.logged_in flag */
6309
6310
45
  if (data->cmd == SC_PIN_CMD_GET_INFO) {
6311
0
    if (priv->card_issues & CI_CANT_USE_GETDATA_FOR_STATE) {
6312
0
      sc_log(card->ctx, "CI_CANT_USE_GETDATA_FOR_STATE set, assume logged_in=%d", priv->logged_in);
6313
0
      data->pin1.logged_in = priv->logged_in; /* use what ever we saw last */
6314
0
    } else if (priv->card_issues & CI_VERIFY_LC0_FAIL &&
6315
0
        priv->pin_cmd_verify_sw1 == 0x63U) { /* can not use modified return codes from iso->drv->pin_cmd */
6316
      /* try another method, looking at a protected object this may require adding one of these to NEO */
6317
0
      r = piv_check_protected_objects(card);
6318
0
      if (r == SC_SUCCESS)
6319
0
        data->pin1.logged_in = SC_PIN_STATE_LOGGED_IN;
6320
0
      else if (r == SC_ERROR_PIN_CODE_INCORRECT) {
6321
0
        if (priv->card_issues & CI_CANT_USE_GETDATA_FOR_STATE) { /* we still can not determine login state */
6322
6323
0
          data->pin1.logged_in = priv->logged_in; /* may have be set from SC_PIN_CMD_VERIFY */
6324
0
        } else {
6325
0
          data->pin1.logged_in = SC_PIN_STATE_LOGGED_OUT;
6326
0
        }
6327
0
        r = SC_SUCCESS;
6328
0
      }
6329
0
    }
6330
0
    priv->logged_in = data->pin1.logged_in;
6331
0
    priv->tries_left = data->pin1.tries_left;
6332
0
  }
6333
6334
45
  sc_log(card->ctx, "piv_pin_cmd tries_left=%d, logged_in=%d", priv->tries_left, priv->logged_in);
6335
45
  LOG_FUNC_RETURN(card->ctx, r);
6336
45
}
6337
6338
static int
6339
piv_logout(sc_card_t *card)
6340
0
{
6341
0
  int r = SC_ERROR_INTERNAL;
6342
0
  piv_private_data_t *priv = PIV_DATA(card);
6343
6344
0
  LOG_FUNC_CALLED(card->ctx);
6345
6346
0
  if (priv) {
6347
    /* logout defined since 800-73-4 */
6348
0
    r = iso7816_logout(card, priv->pin_preference);
6349
0
    if (r == SC_SUCCESS) {
6350
0
      priv->logged_in = SC_PIN_STATE_LOGGED_OUT;
6351
0
    }
6352
0
  }
6353
6354
0
  LOG_FUNC_RETURN(card->ctx, r);
6355
0
}
6356
6357
/*
6358
 * Called when a sc_lock gets a reader lock and PCSC SCardBeginTransaction
6359
 * If SCardBeginTransaction may pass back that a card reset was seen since
6360
 * the last transaction  completed.
6361
 * There may have been one or more resets, by other card drivers in different
6362
 * processes, and they may have taken action already
6363
 * and changed the AID and or may have sent a  VERIFY with PIN
6364
 * So test the state of the card.
6365
 * this is very similar to what the piv_match routine does,
6366
 */
6367
6368
/* card.c also calls piv_sm_open before this if a reset was done, but
6369
 * does not say if a reset was done or not. May need to ignore the call
6370
 * the piv_sm_open in this case, but how? may need a open is active flag,
6371
 * in case it is the APDU done from open caused  triggered the case.
6372
 */
6373
static int
6374
piv_card_reader_lock_obtained(sc_card_t *card, int was_reset)
6375
5.47k
{
6376
5.47k
  int r = 0;
6377
5.47k
  u8 temp[SC_MAX_APDU_BUFFER_SIZE];
6378
5.47k
  size_t templen = sizeof(temp);
6379
5.47k
  piv_private_data_t *priv = PIV_DATA(card); /* may be null */
6380
6381
5.47k
  SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE);
6382
6383
  /* We have a PCSC transaction and sc_lock */
6384
5.47k
  if (priv == NULL || priv->pstate == PIV_STATE_MATCH) {
6385
4.87k
    sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE,
6386
4.87k
        priv ? "PIV_STATE_MATCH" : "priv==NULL");
6387
4.87k
    r = 0; /* do nothing, piv_match will take care of it */
6388
4.87k
    goto err;
6389
4.87k
  }
6390
6391
602
  priv->init_flags |= PIV_INIT_IN_READER_LOCK_OBTAINED;
6392
6393
  /* make sure our application is active */
6394
6395
  /* first see if AID is active AID by reading discovery object '7E' */
6396
  /* If not try selecting AID */
6397
6398
  /* but if card does not support DISCOVERY object we can not use it */
6399
602
  if (priv->card_issues & CI_DISCOVERY_USELESS) {
6400
555
    r = SC_ERROR_NO_CARD_SUPPORT;
6401
555
  } else {
6402
47
    r = piv_find_discovery(card);
6403
#ifdef ENABLE_PIV_SM
6404
    /*
6405
     * All 800-73-4 cards that support SM, also have a discovery object with
6406
     * the pin_policy, so can not have CI_DISCOVERY_USELESS
6407
     * Discovery object can be read with contact or contactless
6408
     * If read with SM and fails with 69 88  SC_ERROR_SM_INVALID_SESSION_KEY
6409
     * sm.c will close the SM connectrion, and set defer
6410
     */
6411
    if (was_reset == 0 && (r == SC_ERROR_SM_INVALID_SESSION_KEY || priv->sm_flags & PIV_SM_FLAGS_DEFER_OPEN)) {
6412
      sc_log(card->ctx, "SC_ERROR_SM_INVALID_SESSION_KEY || PIV_SM_FLAGS_DEFER_OPEN");
6413
      piv_sm_open(card);
6414
      r = piv_find_discovery(card);
6415
    }
6416
#endif /* ENABLE_PIV_SM */
6417
47
  }
6418
6419
602
  if (r < 0) {
6420
593
    if (was_reset > 0 || !(priv->card_issues & CI_PIV_AID_LOSE_STATE)) {
6421
586
      r = iso7816_select_aid(card, piv_aids[0].value, piv_aids[0].len_short, temp, &templen);
6422
586
      sc_debug(card->ctx, SC_LOG_DEBUG_MATCH, "iso7816_select_aid card->type:%d r:%d\n", card->type, r);
6423
586
    } else {
6424
7
      r = 0; /* can't do anything with this card, hope there was no interference */
6425
7
    }
6426
593
  }
6427
6428
602
  if (r < 0) /* bad error return will show up in sc_lock as error*/
6429
462
    goto err;
6430
6431
140
  if (was_reset > 0)
6432
0
    priv->logged_in = SC_PIN_STATE_UNKNOWN;
6433
6434
140
  r = 0;
6435
6436
5.47k
err:
6437
5.47k
  if (priv)
6438
1.15k
    priv->init_flags &= ~PIV_INIT_IN_READER_LOCK_OBTAINED;
6439
5.47k
  LOG_FUNC_RETURN(card->ctx, r);
6440
5.47k
}
6441
6442
static struct sc_card_driver *
6443
sc_get_driver(void)
6444
12.7k
{
6445
12.7k
  struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
6446
6447
12.7k
  piv_ops = *iso_drv->ops;
6448
12.7k
  piv_ops.match_card = piv_match_card;
6449
12.7k
  piv_ops.init = piv_init;
6450
12.7k
  piv_ops.finish = piv_finish;
6451
6452
12.7k
  piv_ops.select_file = piv_select_file; /* must use get/put, could emulate? */
6453
12.7k
  piv_ops.get_challenge = piv_get_challenge;
6454
12.7k
  piv_ops.logout = piv_logout;
6455
12.7k
  piv_ops.read_binary = piv_read_binary;
6456
12.7k
  piv_ops.write_binary = piv_write_binary;
6457
12.7k
  piv_ops.set_security_env = piv_set_security_env;
6458
12.7k
  piv_ops.restore_security_env = piv_restore_security_env;
6459
12.7k
  piv_ops.compute_signature = piv_compute_signature;
6460
12.7k
  piv_ops.decipher = piv_decipher;
6461
12.7k
  piv_ops.check_sw = piv_check_sw;
6462
12.7k
  piv_ops.card_ctl = piv_card_ctl;
6463
12.7k
  piv_ops.pin_cmd = piv_pin_cmd;
6464
12.7k
  piv_ops.card_reader_lock_obtained = piv_card_reader_lock_obtained;
6465
6466
12.7k
  return &piv_drv;
6467
12.7k
}
6468
6469
struct sc_card_driver *
6470
sc_get_piv_driver(void)
6471
12.7k
{
6472
12.7k
  return sc_get_driver();
6473
12.7k
}