Coverage Report

Created: 2025-07-01 06:08

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