| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include "internal.h" |
|---|
| 22 | #include "cardctl.h" |
|---|
| 23 | #include "pkcs15.h" |
|---|
| 24 | #ifdef ENABLE_OPENSSL |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <openssl/evp.h> |
|---|
| 28 | #include <openssl/rand.h> |
|---|
| 29 | |
|---|
| 30 | #define GPK_SEL_MF       0x00 |
|---|
| 31 | #define GPK_SEL_DF       0x01 |
|---|
| 32 | #define GPK_SEL_EF       0x02 |
|---|
| 33 | #define GPK_SEL_AID       0x04 |
|---|
| 34 | #define GPK_FID_MF       0x3F00 |
|---|
| 35 | |
|---|
| 36 | #define GPK_FTYPE_SC      0x21 |
|---|
| 37 | |
|---|
| 38 | #define GPK_SIGN_RSA_MD5    0x11 |
|---|
| 39 | #define GPK_SIGN_RSA_SHA    0x12 |
|---|
| 40 | #define GPK_SIGN_RSA_SSL    0x18 |
|---|
| 41 | #define GPK_VERIFY_RSA_MD5   0x21 |
|---|
| 42 | #define GPK_VERIFY_RSA_SHA   0x22 |
|---|
| 43 | #define GPK_AUTH_RSA_MD5    0x31 |
|---|
| 44 | #define GPK_AUTH_RSA_SHA    0x32 |
|---|
| 45 | #define GPK_AUTH_RSA_SSL    0x38 |
|---|
| 46 | #define GPK_UNWRAP_RSA     0x77 |
|---|
| 47 | |
|---|
| 48 | #define GPK_MAX_PINS      8 |
|---|
| 49 | #define GPK_HASH_CHUNK     62 |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | struct gpk_private_data { |
|---|
| 55 | Â Â Â Â |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 |     unsigned int  offset_shift; |
|---|
| 59 |     unsigned int  offset_mask; |
|---|
| 60 |     unsigned int  locked : 1, |
|---|
| 61 | Â Â Â Â Â Â Â Â Â Â Â Â sample_card : 1; |
|---|
| 62 | |
|---|
| 63 | Â Â Â Â |
|---|
| 64 |     unsigned short int ac[3]; |
|---|
| 65 | |
|---|
| 66 | Â Â Â Â |
|---|
| 67 |     unsigned    key_set  : 1; |
|---|
| 68 |     unsigned int  key_reference; |
|---|
| 69 | Â Â Â Â u8Â Â Â Â Â Â Â key[16]; |
|---|
| 70 | |
|---|
| 71 | Â Â Â Â |
|---|
| 72 |     unsigned int  sec_algorithm; |
|---|
| 73 |     unsigned int  sec_hash_len; |
|---|
| 74 |     unsigned int  sec_mod_len; |
|---|
| 75 |     unsigned int  sec_padding; |
|---|
| 76 | }; |
|---|
| 77 | #define DRVDATA(card)  ((struct gpk_private_data *) ((card)->drv_data)) |
|---|
| 78 | |
|---|
| 79 | static int   gpk_get_info(sc_card_t *, int, int, u8 *, size_t); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | static struct sc_atr_table gpk_atrs[] = { |
|---|
| 85 | Â Â Â Â { "3B:27:00:80:65:A2:04:01:01:37", NULL, "GPK 4K", SC_CARD_TYPE_GPK_GPK4000_s, 0, NULL }, |
|---|
| 86 | Â Â Â Â { "3B:27:00:80:65:A2:05:01:01:37", NULL, "GPK 4K", SC_CARD_TYPE_GPK_GPK4000_sp, 0, NULL }, |
|---|
| 87 | Â Â Â Â { "3B:27:00:80:65:A2:0C:01:01:37", NULL, "GPK 4K", SC_CARD_TYPE_GPK_GPK4000_su256, 0, NULL }, |
|---|
| 88 | Â Â Â Â { "3B:A7:00:40:14:80:65:A2:14:01:01:37", NULL, "GPK 4K", SC_CARD_TYPE_GPK_GPK4000_sdo, 0, NULL }, |
|---|
| 89 | Â Â Â Â { "3B:A7:00:40:18:80:65:A2:08:01:01:52", NULL, "GPK 8K", SC_CARD_TYPE_GPK_GPK8000_8K, 0, NULL }, |
|---|
| 90 | Â Â Â Â { "3B:A7:00:40:18:80:65:A2:09:01:01:52", NULL, "GPK 8K", SC_CARD_TYPE_GPK_GPK8000_16K, 0, NULL }, |
|---|
| 91 | Â Â Â Â { "3B:A7:00:40:18:80:65:A2:09:01:02:52", NULL, "GPK 16K", SC_CARD_TYPE_GPK_GPK16000, 0, NULL }, |
|---|
| 92 | Â Â Â Â { "3B:A7:00:40:18:80:65:A2:09:01:03:52", NULL, "GPK 16K", SC_CARD_TYPE_GPK_GPK16000, 0, NULL }, |
|---|
| 93 | Â Â Â Â { NULL, NULL, NULL, 0, 0, NULL } |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | static struct sc_card_operations    gpk_ops, *iso_ops; |
|---|
| 100 | static struct sc_card_driver gpk_drv = { |
|---|
| 101 | Â Â Â Â "Gemplus GPK", |
|---|
| 102 | Â Â Â Â "gpk", |
|---|
| 103 | Â Â Â Â &gpk_ops, |
|---|
| 104 | Â Â Â Â NULL, 0, NULL |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | static int |
|---|
| 111 | gpk_match_card(sc_card_t *card) |
|---|
| 112 | { |
|---|
| 113 |     int i; |
|---|
| 114 | |
|---|
| 115 | Â Â Â Â i = _sc_match_atr(card, gpk_atrs, &card->type); |
|---|
| 116 |     if (i < 0) { |
|---|
| 117 |         const u8 *hist_bytes = card->slot->atr_info.hist_bytes; |
|---|
| 118 | |
|---|
| 119 | Â Â Â Â Â Â Â Â |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 |         if ((card->slot->atr_info.hist_bytes_len >= 7) |
|---|
| 127 | Â Â Â Â Â Â Â Â Â Â Â Â && (hist_bytes[0] == 0x80) |
|---|
| 128 | Â Â Â Â Â Â Â Â Â Â Â Â && (hist_bytes[1] == 0x65) |
|---|
| 129 | Â Â Â Â Â Â Â Â Â Â Â Â && (hist_bytes[2] == 0xa2)) {Â Â |
|---|
| 130 |             if (hist_bytes[3] == 0x08) {  |
|---|
| 131 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â card->type = SC_CARD_TYPE_GPK_GPK8000; |
|---|
| 132 |                 return 1; |
|---|
| 133 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 134 |             if (hist_bytes[3] == 0x09) {  |
|---|
| 135 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â card->type = SC_CARD_TYPE_GPK_GPK16000; |
|---|
| 136 |                 return 1; |
|---|
| 137 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 138 | Â Â Â Â Â Â Â Â } |
|---|
| 139 |         return 0; |
|---|
| 140 | Â Â Â Â } |
|---|
| 141 |     return 1; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | static int |
|---|
| 148 | gpk_init(sc_card_t *card) |
|---|
| 149 | { |
|---|
| 150 |     struct gpk_private_data *priv; |
|---|
| 151 |     unsigned long  exponent, flags, kg; |
|---|
| 152 |     unsigned char info[13]; |
|---|
| 153 | |
|---|
| 154 |     card->drv_data = priv = (struct gpk_private_data *) calloc(1, sizeof(*priv)); |
|---|
| 155 |     if (card->drv_data == NULL) |
|---|
| 156 |         return SC_ERROR_OUT_OF_MEMORY; |
|---|
| 157 | |
|---|
| 158 | Â Â Â Â |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | Â Â Â Â priv->offset_shift = 2; |
|---|
| 163 | Â Â Â Â priv->offset_mask = 3; |
|---|
| 164 | Â Â Â Â card->cla = 0x00; |
|---|
| 165 | |
|---|
| 166 | Â Â Â Â |
|---|
| 167 | |
|---|
| 168 | Â Â Â Â flags = SC_ALGORITHM_RSA_HASH_MD5 | SC_ALGORITHM_RSA_HASH_SHA1 |
|---|
| 169 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_HASH_MD5_SHA1; |
|---|
| 170 | Â Â Â Â flags |= SC_ALGORITHM_RSA_PAD_PKCS1 | SC_ALGORITHM_RSA_PAD_ANSI |
|---|
| 171 | Â Â Â Â Â Â Â Â | SC_ALGORITHM_RSA_PAD_ISO9796; |
|---|
| 172 | Â Â Â Â exponent = (card->type < SC_CARD_TYPE_GPK_GPK16000) ? 0x10001 : 0; |
|---|
| 173 | Â Â Â Â kg = (card->type >= SC_CARD_TYPE_GPK_GPK8000) ? SC_ALGORITHM_ONBOARD_KEY_GEN : 0; |
|---|
| 174 |     _sc_card_add_rsa_alg(card, 512, flags|kg, exponent); |
|---|
| 175 |     _sc_card_add_rsa_alg(card, 768, flags, exponent); |
|---|
| 176 | Â Â Â Â _sc_card_add_rsa_alg(card, 1024, flags|kg, exponent); |
|---|
| 177 | |
|---|
| 178 | Â Â Â Â |
|---|
| 179 |     if (gpk_get_info(card, 0x02, 0xA4, info, sizeof(info)) >= 0) { |
|---|
| 180 |         if (info[12] & 0x40) { |
|---|
| 181 | Â Â Â Â Â Â Â Â Â Â Â Â priv->offset_shift = 0; |
|---|
| 182 | Â Â Â Â Â Â Â Â Â Â Â Â priv->offset_mask = 0; |
|---|
| 183 | Â Â Â Â Â Â Â Â } |
|---|
| 184 |         if (info[12] & 0x10) { |
|---|
| 185 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | Â Â Â Â Â Â Â Â } |
|---|
| 189 |         if (info[12] & 0x08) { |
|---|
| 190 | Â Â Â Â Â Â Â Â Â Â Â Â priv->locked = 1; |
|---|
| 191 | Â Â Â Â Â Â Â Â } |
|---|
| 192 | Â Â Â Â Â Â Â Â |
|---|
| 193 |         if (!memcmp(info+5, "\x00\xff\x00", 3)) { |
|---|
| 194 | Â Â Â Â Â Â Â Â Â Â Â Â priv->sample_card = 1; |
|---|
| 195 | Â Â Â Â Â Â Â Â } |
|---|
| 196 | Â Â Â Â } |
|---|
| 197 | |
|---|
| 198 | Â Â Â Â |
|---|
| 199 | Â Â Â Â card->caps |= SC_CARD_CAP_RNG; |
|---|
| 200 | |
|---|
| 201 | Â Â Â Â |
|---|
| 202 | Â Â Â Â card->max_send_size &= ~3; |
|---|
| 203 |     if (card->max_recv_size >= 256) |
|---|
| 204 | Â Â Â Â Â Â Â Â card->max_recv_size = 252; |
|---|
| 205 | Â Â Â Â else |
|---|
| 206 | Â Â Â Â Â Â Â Â card->max_recv_size &= ~3; |
|---|
| 207 | |
|---|
| 208 |     return 0; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | static int |
|---|
| 215 | gpk_finish(sc_card_t *card) |
|---|
| 216 | { |
|---|
| 217 |     if (card->drv_data) |
|---|
| 218 | Â Â Â Â Â Â Â Â free(card->drv_data); |
|---|
| 219 | Â Â Â Â card->drv_data = NULL; |
|---|
| 220 |     return 0; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | #if 0 |
|---|
| 229 | static int |
|---|
| 230 | gpk_check_sw(sc_card_t *card, u8 sw1, u8 sw2) |
|---|
| 231 | { |
|---|
| 232 |     unsigned short int   sw = (sw1 << 8) | sw2; |
|---|
| 233 | |
|---|
| 234 |     if ((sw & 0xFFF0) == 0x63C0) { |
|---|
| 235 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "wrong PIN, %u tries left\n", sw&0xf); |
|---|
| 236 |         return SC_ERROR_PIN_CODE_INCORRECT; |
|---|
| 237 | Â Â Â Â } |
|---|
| 238 | |
|---|
| 239 |     switch (sw) { |
|---|
| 240 |     case 0x6400: |
|---|
| 241 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "wrong crypto context\n"); |
|---|
| 242 |         return SC_ERROR_OBJECT_NOT_VALID; |
|---|
| 243 | |
|---|
| 244 | Â Â Â Â |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 |     case 0x6983: |
|---|
| 248 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "PIN is blocked\n"); |
|---|
| 249 |         return SC_ERROR_PIN_CODE_INCORRECT; |
|---|
| 250 |     case 0x6581: |
|---|
| 251 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "out of space on card or file\n"); |
|---|
| 252 |         return SC_ERROR_OUT_OF_MEMORY; |
|---|
| 253 |     case 0x6981: |
|---|
| 254 |         return SC_ERROR_FILE_NOT_FOUND; |
|---|
| 255 |     case 0x6A80: |
|---|
| 256 |     case 0x6b00: |
|---|
| 257 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 258 | Â Â Â Â } |
|---|
| 259 | |
|---|
| 260 |     return iso7816_check_sw(card, sw1, sw2); |
|---|
| 261 | } |
|---|
| 262 | #endif |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | static int |
|---|
| 268 | match_path(sc_card_t *card, unsigned short int **pathptr, size_t *pathlen, |
|---|
| 269 |         int need_info) |
|---|
| 270 | { |
|---|
| 271 |     unsigned short int   *curptr, *ptr; |
|---|
| 272 |     size_t     curlen, len; |
|---|
| 273 |     size_t     i; |
|---|
| 274 | |
|---|
| 275 |     curptr = (unsigned short int *) card->cache.current_path.value; |
|---|
| 276 | Â Â Â Â curlen = card->cache.current_path.len; |
|---|
| 277 |     ptr  = *pathptr; |
|---|
| 278 |     len  = *pathlen; |
|---|
| 279 | |
|---|
| 280 |     if (curlen < 1 || len < 1) |
|---|
| 281 |         return 0; |
|---|
| 282 | |
|---|
| 283 | Â Â Â Â |
|---|
| 284 | |
|---|
| 285 |     if (ptr[0] != GPK_FID_MF || curptr[0] != GPK_FID_MF) |
|---|
| 286 |         return 0; |
|---|
| 287 | |
|---|
| 288 |     for (i = 1; i < len && i < curlen; i++) { |
|---|
| 289 |         if (ptr[i] != curptr[i]) |
|---|
| 290 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 291 | Â Â Â Â } |
|---|
| 292 | |
|---|
| 293 |     if (len < curlen) { |
|---|
| 294 | Â Â Â Â Â Â Â Â |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 |         if (len == 2) |
|---|
| 298 |             goto okay; |
|---|
| 299 | Â Â Â Â Â Â Â Â |
|---|
| 300 |         return 0; |
|---|
| 301 | Â Â Â Â } |
|---|
| 302 | |
|---|
| 303 | Â Â Â Â |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 |     if (i == len && need_info) { |
|---|
| 309 |         if (i > 1) { |
|---|
| 310 | Â Â Â Â Â Â Â Â Â Â Â Â *pathptr = ptr + len - 1; |
|---|
| 311 | Â Â Â Â Â Â Â Â Â Â Â Â *pathlen = len - 1; |
|---|
| 312 |             return 1; |
|---|
| 313 | Â Â Â Â Â Â Â Â } |
|---|
| 314 | Â Â Â Â Â Â Â Â |
|---|
| 315 |         return 0; |
|---|
| 316 | Â Â Â Â } |
|---|
| 317 | |
|---|
| 318 | okay: |
|---|
| 319 | Â Â Â Â *pathptr = ptr + i; |
|---|
| 320 | Â Â Â Â *pathlen = len - i; |
|---|
| 321 |     return 1; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | static void |
|---|
| 325 | ac_to_acl(unsigned int ac, sc_file_t *file, unsigned int op) |
|---|
| 326 | { |
|---|
| 327 |     unsigned int  npins, pin; |
|---|
| 328 | |
|---|
| 329 | Â Â Â Â npins = (ac >> 14) & 3; |
|---|
| 330 |     if (npins == 3) { |
|---|
| 331 | Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, op, SC_AC_NEVER, |
|---|
| 332 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_AC_KEY_REF_NONE); |
|---|
| 333 | Â Â Â Â Â Â Â Â return; |
|---|
| 334 | Â Â Â Â } |
|---|
| 335 | |
|---|
| 336 | Â Â Â Â sc_file_add_acl_entry(file, op, SC_AC_NONE, SC_AC_KEY_REF_NONE); |
|---|
| 337 | Â Â Â Â pin = ac & 0xFF; |
|---|
| 338 |     if (npins >= 1) |
|---|
| 339 | Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, op, SC_AC_CHV, (pin >> 4) & 0xF); |
|---|
| 340 |     if (npins == 2) |
|---|
| 341 | Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, op, SC_AC_CHV, pin & 0xF); |
|---|
| 342 | |
|---|
| 343 | Â Â Â Â |
|---|
| 344 |     if (ac & 0x3F00) |
|---|
| 345 | Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, op, SC_AC_PRO, (ac & 0x3F00) >> 8); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | static void |
|---|
| 354 | acl_to_ac(sc_file_t *file, unsigned int op, u8 *ac) |
|---|
| 355 | { |
|---|
| 356 |     const sc_acl_entry_t *acl; |
|---|
| 357 |     unsigned int  npins = 0; |
|---|
| 358 | |
|---|
| 359 | Â Â Â Â ac[0] = ac[1] = 0; |
|---|
| 360 | |
|---|
| 361 |     if ((acl = sc_file_get_acl_entry(file, op)) == NULL) |
|---|
| 362 | Â Â Â Â Â Â Â Â return; |
|---|
| 363 | |
|---|
| 364 | Â Â Â Â assert(acl->method != SC_AC_UNKNOWN); |
|---|
| 365 |     switch (acl->method) { |
|---|
| 366 |     case SC_AC_NEVER: |
|---|
| 367 | Â Â Â Â Â Â Â Â ac[0] = 0xC0; |
|---|
| 368 | Â Â Â Â Â Â Â Â return; |
|---|
| 369 |     case SC_AC_NONE: |
|---|
| 370 | Â Â Â Â Â Â Â Â return; |
|---|
| 371 | Â Â Â Â } |
|---|
| 372 | |
|---|
| 373 |     while (acl) { |
|---|
| 374 |         if (acl->method == SC_AC_CHV) { |
|---|
| 375 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 376 |             if (++npins >= 2) |
|---|
| 377 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â continue; |
|---|
| 378 | Â Â Â Â Â Â Â Â Â Â Â Â ac[1] >>= 4; |
|---|
| 379 | Â Â Â Â Â Â Â Â Â Â Â Â ac[1] |= acl->key_ref << 4; |
|---|
| 380 | Â Â Â Â Â Â Â Â Â Â Â Â ac[0] += 0x40; |
|---|
| 381 | Â Â Â Â Â Â Â Â } |
|---|
| 382 |         if (acl->method == SC_AC_PRO) { |
|---|
| 383 | Â Â Â Â Â Â Â Â Â Â Â Â ac[0] |= acl->key_ref & 0x1f; |
|---|
| 384 | Â Â Â Â Â Â Â Â } |
|---|
| 385 | Â Â Â Â Â Â Â Â acl = acl->next; |
|---|
| 386 | Â Â Â Â } |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | static int |
|---|
| 390 | gpk_parse_fci(sc_card_t *card, |
|---|
| 391 |         const u8 *buf, size_t buflen, |
|---|
| 392 | Â Â Â Â Â Â Â Â sc_file_t *file) |
|---|
| 393 | { |
|---|
| 394 |     const u8    *end, *next; |
|---|
| 395 |     unsigned int  tag, len; |
|---|
| 396 | |
|---|
| 397 | Â Â Â Â end = buf + buflen; |
|---|
| 398 |     for (; buf + 2 < end; buf = next) { |
|---|
| 399 | Â Â Â Â Â Â Â Â next = buf + 2 + buf[1]; |
|---|
| 400 |         if (next > end) |
|---|
| 401 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 402 | Â Â Â Â Â Â Â Â tag = *buf++; |
|---|
| 403 | Â Â Â Â Â Â Â Â len = *buf++; |
|---|
| 404 |         if (tag == 0x84) { |
|---|
| 405 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | Â Â Â Â Â Â Â Â } else |
|---|
| 410 |         if (tag == 0xC1 && len >= 2) { |
|---|
| 411 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 412 | |
|---|
| 413 | Â Â Â Â Â Â Â Â Â Â Â Â file->id = (buf[0] << 8) | buf[1]; |
|---|
| 414 | Â Â Â Â Â Â Â Â } else |
|---|
| 415 |         if (tag == 0xC2) { |
|---|
| 416 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | Â Â Â Â Â Â Â Â } |
|---|
| 420 | Â Â Â Â } |
|---|
| 421 | |
|---|
| 422 |     return 0; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | static int |
|---|
| 426 | gpk_parse_fileinfo(sc_card_t *card, |
|---|
| 427 |         const u8 *buf, size_t buflen, |
|---|
| 428 | Â Â Â Â Â Â Â Â sc_file_t *file) |
|---|
| 429 | { |
|---|
| 430 |     const u8    *sp, *end, *next; |
|---|
| 431 |     int       i, rc; |
|---|
| 432 | |
|---|
| 433 | Â Â Â Â memset(file, 0, sizeof(*file)); |
|---|
| 434 |     for (i = 0; i < SC_MAX_AC_OPS; i++) |
|---|
| 435 | Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, i, SC_AC_UNKNOWN, SC_AC_KEY_REF_NONE); |
|---|
| 436 | |
|---|
| 437 | Â Â Â Â end = buf + buflen; |
|---|
| 438 |     for (sp = buf; sp + 2 < end; sp = next) { |
|---|
| 439 | Â Â Â Â Â Â Â Â next = sp + 2 + sp[1]; |
|---|
| 440 |         if (next > end) |
|---|
| 441 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 442 |         if (sp[0] == 0x84) { |
|---|
| 443 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 444 |             if (sp[1] > sizeof(file->name)) |
|---|
| 445 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â continue; |
|---|
| 446 | Â Â Â Â Â Â Â Â Â Â Â Â memset(file->name, 0, sizeof(file->name)); |
|---|
| 447 | Â Â Â Â Â Â Â Â Â Â Â Â memcpy(file->name, sp+2, sp[1]); |
|---|
| 448 | Â Â Â Â Â Â Â Â } else |
|---|
| 449 |         if (sp[0] == 0x85) { |
|---|
| 450 |             unsigned int  ac[3], n; |
|---|
| 451 | |
|---|
| 452 | Â Â Â Â Â Â Â Â Â Â Â Â file->id = (sp[4] << 8) | sp[5]; |
|---|
| 453 | Â Â Â Â Â Â Â Â Â Â Â Â file->size = (sp[8] << 8) | sp[9]; |
|---|
| 454 | Â Â Â Â Â Â Â Â Â Â Â Â file->record_length = sp[7]; |
|---|
| 455 | |
|---|
| 456 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 457 | |
|---|
| 458 |             for (n = 0; n < 3; n++) |
|---|
| 459 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ac[n] = (sp[10+2*n] << 8) | sp[11+2*n]; |
|---|
| 460 | |
|---|
| 461 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 462 |             switch (sp[6] & 7) { |
|---|
| 463 |             case 0x01: case 0x02: case 0x03: case 0x04: |
|---|
| 464 |             case 0x05: case 0x06: case 0x07: |
|---|
| 465 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_WORKING_EF; |
|---|
| 466 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->ef_structure = sp[6] & 7; |
|---|
| 467 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ac_to_acl(ac[0], file, SC_AC_OP_UPDATE); |
|---|
| 468 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ac_to_acl(ac[1], file, SC_AC_OP_WRITE); |
|---|
| 469 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ac_to_acl(ac[2], file, SC_AC_OP_READ); |
|---|
| 470 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 471 |             case 0x00: |
|---|
| 472 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_DF; |
|---|
| 473 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ac_to_acl(ac[0], file, SC_AC_OP_LOCK); |
|---|
| 477 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ac_to_acl(ac[1], file, SC_AC_OP_CREATE); |
|---|
| 478 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, SC_AC_OP_SELECT, |
|---|
| 479 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_AC_NONE, SC_AC_KEY_REF_NONE); |
|---|
| 480 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, SC_AC_OP_DELETE, |
|---|
| 481 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_AC_NEVER, SC_AC_KEY_REF_NONE); |
|---|
| 482 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, SC_AC_OP_REHABILITATE, |
|---|
| 483 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_AC_NEVER, SC_AC_KEY_REF_NONE); |
|---|
| 484 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, SC_AC_OP_INVALIDATE, |
|---|
| 485 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_AC_NEVER, SC_AC_KEY_REF_NONE); |
|---|
| 486 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_file_add_acl_entry(file, SC_AC_OP_LIST_FILES, |
|---|
| 487 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SC_AC_NEVER, SC_AC_KEY_REF_NONE); |
|---|
| 488 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 489 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 490 | Â Â Â Â Â Â Â Â } else |
|---|
| 491 |         if (sp[0] == 0x6f) { |
|---|
| 492 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | Â Â Â Â Â Â Â Â Â Â Â Â file->type = SC_FILE_TYPE_DF; |
|---|
| 496 | Â Â Â Â Â Â Â Â Â Â Â Â rc = gpk_parse_fci(card, sp + 2, sp[1], file); |
|---|
| 497 |             if (rc < 0) |
|---|
| 498 |                 return rc; |
|---|
| 499 | Â Â Â Â Â Â Â Â } |
|---|
| 500 | Â Â Â Â } |
|---|
| 501 | |
|---|
| 502 |     if (file->record_length) |
|---|
| 503 | Â Â Â Â Â Â Â Â file->record_count = file->size / file->record_length; |
|---|
| 504 | Â Â Â Â file->magic = SC_FILE_MAGIC; |
|---|
| 505 | |
|---|
| 506 |     return 0; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | static int |
|---|
| 510 | gpk_select(sc_card_t *card, int kind, |
|---|
| 511 |         const u8 *buf, size_t buflen, |
|---|
| 512 | Â Â Â Â Â Â Â Â sc_file_t **file) |
|---|
| 513 | { |
|---|
| 514 |     struct gpk_private_data *priv = DRVDATA(card); |
|---|
| 515 |     sc_apdu_t    apdu; |
|---|
| 516 | Â Â Â Â u8Â Â Â Â Â Â Â resbuf[256]; |
|---|
| 517 |     int       r; |
|---|
| 518 | |
|---|
| 519 | Â Â Â Â |
|---|
| 520 |     if (kind == GPK_SEL_MF || kind == GPK_SEL_DF) { |
|---|
| 521 | Â Â Â Â Â Â Â Â memset(priv->key, 0, sizeof(priv->key)); |
|---|
| 522 | Â Â Â Â Â Â Â Â priv->key_set = 0; |
|---|
| 523 | Â Â Â Â } |
|---|
| 524 | |
|---|
| 525 | Â Â Â Â |
|---|
| 526 | Â Â Â Â memset(&apdu, 0, sizeof(apdu)); |
|---|
| 527 | Â Â Â Â apdu.cla = 0x00; |
|---|
| 528 | Â Â Â Â apdu.cse = SC_APDU_CASE_3_SHORT; |
|---|
| 529 | Â Â Â Â apdu.ins = 0xA4; |
|---|
| 530 | Â Â Â Â apdu.p1 = kind; |
|---|
| 531 | Â Â Â Â apdu.p2 = 0; |
|---|
| 532 | Â Â Â Â apdu.data = buf; |
|---|
| 533 | Â Â Â Â apdu.datalen = buflen; |
|---|
| 534 | Â Â Â Â apdu.lc = apdu.datalen; |
|---|
| 535 | |
|---|
| 536 |     if (file) { |
|---|
| 537 | Â Â Â Â Â Â Â Â apdu.cse = SC_APDU_CASE_4_SHORT; |
|---|
| 538 | Â Â Â Â Â Â Â Â apdu.resp = resbuf; |
|---|
| 539 | Â Â Â Â Â Â Â Â apdu.resplen = sizeof(resbuf); |
|---|
| 540 | Â Â Â Â Â Â Â Â apdu.le = sizeof(resbuf); |
|---|
| 541 | Â Â Â Â } |
|---|
| 542 | |
|---|
| 543 | Â Â Â Â r = sc_transmit_apdu(card, &apdu); |
|---|
| 544 | Â Â Â Â SC_TEST_RET(card->ctx, r, "APDU transmit failed"); |
|---|
| 545 | Â Â Â Â r = sc_check_sw(card, apdu.sw1, apdu.sw2); |
|---|
| 546 | Â Â Â Â SC_TEST_RET(card->ctx, r, "Card returned error"); |
|---|
| 547 | |
|---|
| 548 | Â Â Â Â |
|---|
| 549 | |
|---|
| 550 |     if (kind == GPK_SEL_AID) { |
|---|
| 551 | Â Â Â Â Â Â Â Â card->cache.current_path.len = 0; |
|---|
| 552 | Â Â Â Â } |
|---|
| 553 | |
|---|
| 554 |     if (file == NULL) |
|---|
| 555 |         return 0; |
|---|
| 556 | Â Â Â Â *file = sc_file_new(); |
|---|
| 557 | |
|---|
| 558 | Â Â Â Â r = gpk_parse_fileinfo(card, apdu.resp, apdu.resplen, *file); |
|---|
| 559 |     if (r < 0) { |
|---|
| 560 | Â Â Â Â Â Â Â Â sc_file_free(*file); |
|---|
| 561 | Â Â Â Â Â Â Â Â *file = NULL; |
|---|
| 562 | Â Â Â Â } |
|---|
| 563 |     return r; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | static int |
|---|
| 567 | gpk_select_id(sc_card_t *card, int kind, unsigned int fid, |
|---|
| 568 | Â Â Â Â Â Â Â Â sc_file_t **file) |
|---|
| 569 | { |
|---|
| 570 |     sc_path_t    *cp = &card->cache.current_path; |
|---|
| 571 | Â Â Â Â u8Â Â Â Â Â Â Â fbuf[2]; |
|---|
| 572 |     int       r; |
|---|
| 573 | |
|---|
| 574 |     if (card->ctx->debug) |
|---|
| 575 | Â Â Â Â Â Â Â Â sc_debug(card->ctx, "gpk_select_id(0x%04X, kind=%u)\n", fid, kind); |
|---|
| 576 | |
|---|
| 577 | Â Â Â Â fbuf[0] = fid >> 8; |
|---|
| 578 | Â Â Â Â fbuf[1] = fid & 0xff; |
|---|
| 579 | |
|---|
| 580 | Â Â Â Â sc_ctx_suppress_errors_on(card->ctx); |
|---|
| 581 | Â Â Â Â r = gpk_select(card, kind, fbuf, 2, file); |
|---|
| 582 | Â Â Â Â sc_ctx_suppress_errors_off(card->ctx); |
|---|
| 583 | |
|---|
| 584 | Â Â Â Â |
|---|
| 585 | |
|---|
| 586 |     if (r == 0) { |
|---|
| 587 |         unsigned short int   *path; |
|---|
| 588 | |
|---|
| 589 |         switch (kind) { |
|---|
| 590 |         case GPK_SEL_MF: |
|---|
| 591 | Â Â Â Â Â Â Â Â Â Â Â Â cp->len = 0; |
|---|
| 592 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 593 |         case GPK_SEL_DF: |
|---|
| 594 | Â Â Â Â Â Â Â Â Â Â Â Â assert(cp->len + 1 <= SC_MAX_PATH_SIZE / 2); |
|---|
| 595 |             path = (unsigned short int *) cp->value; |
|---|
| 596 | Â Â Â Â Â Â Â Â Â Â Â Â path[cp->len++] = fid; |
|---|
| 597 | Â Â Â Â Â Â Â Â } |
|---|
| 598 |     } else { |
|---|
| 599 | Â Â Â Â Â Â Â Â cp->len = 0; |
|---|
| 600 | Â Â Â Â } |
|---|
| 601 |     return r; |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | static int |
|---|
| 605 | gpk_select_file(sc_card_t *card, const sc_path_t *path, |
|---|
| 606 | Â Â Â Â Â Â Â Â sc_file_t **file) |
|---|
| 607 | { |
|---|
| 608 |     unsigned short int   pathtmp[SC_MAX_PATH_SIZE/2]; |
|---|
| 609 |     unsigned short int   *pathptr; |
|---|
| 610 |     size_t         pathlen, n; |
|---|
| 611 |     int           locked = 0, r = 0, use_relative = 0, retry = 1; |
|---|
| 612 | Â Â Â Â u8Â Â Â Â Â Â Â Â Â Â Â leaf_type; |
|---|
| 613 | |
|---|
| 614 | Â Â Â Â SC_FUNC_CALLED(card->ctx, 1); |
|---|
| 615 | |
|---|
| 616 | Â Â Â Â |
|---|
| 617 |     if (path->type == SC_PATH_TYPE_DF_NAME) { |
|---|
| 618 |         if (path->len > 16) |
|---|
| 619 |             return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 620 | Â Â Â Â Â Â Â Â r = gpk_select(card, GPK_SEL_AID, |
|---|
| 621 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â path->value, path->len, file); |
|---|
| 622 |         goto done; |
|---|
| 623 | Â Â Â Â } |
|---|
| 624 | |
|---|
| 625 | Â Â Â Â |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | Â Â Â Â leaf_type = GPK_SEL_EF; |
|---|
| 633 | |
|---|
| 634 | try_again: |
|---|
| 635 |     if ((path->len & 1) || path->len > sizeof(pathtmp)) |
|---|
| 636 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 637 | Â Â Â Â pathptr = pathtmp; |
|---|
| 638 |     for (n = 0; n < path->len; n += 2) |
|---|
| 639 | Â Â Â Â Â Â Â Â pathptr[n>>1] = (path->value[n] << 8)|path->value[n+1]; |
|---|
| 640 | Â Â Â Â pathlen = path->len >> 1; |
|---|
| 641 | |
|---|
| 642 | Â Â Â Â |
|---|
| 643 | |
|---|
| 644 |     if (path->type == SC_PATH_TYPE_PATH) { |
|---|
| 645 | Â Â Â Â Â Â Â Â |
|---|
| 646 |         if ((pathptr[0] == GPK_FID_MF && pathlen > 2) |
|---|
| 647 | Â Â Â Â Â Â Â Â Â || (pathptr[0] != GPK_FID_MF && pathlen > 1)) |
|---|
| 648 | Â Â Â Â Â Â Â Â Â Â Â Â retry = 0; |
|---|
| 649 | Â Â Â Â Â Â Â Â use_relative = match_path(card, &pathptr, &pathlen, file != 0); |
|---|
| 650 |         if (pathlen == 0) |
|---|
| 651 |             goto done; |
|---|
| 652 |     } else { |
|---|
| 653 | Â Â Â Â Â Â Â Â |
|---|
| 654 |         if (pathlen > 1) |
|---|
| 655 |             return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 656 | Â Â Â Â Â Â Â Â use_relative = 1; |
|---|
| 657 | Â Â Â Â } |
|---|
| 658 | |
|---|
| 659 |     if (pathlen == 1 && pathptr[0] == GPK_FID_MF) { |
|---|
| 660 | Â Â Â Â Â Â Â Â |
|---|
| 661 | Â Â Â Â Â Â Â Â leaf_type = GPK_SEL_MF; |
|---|
| 662 |     } else { |
|---|
| 663 |         if (!locked++) { |
|---|
| 664 | Â Â Â Â Â Â Â Â Â Â Â Â r = sc_lock(card); |
|---|
| 665 | Â Â Â Â Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "sc_lock() failed"); |
|---|
| 666 | Â Â Â Â Â Â Â Â } |
|---|
| 667 | |
|---|
| 668 | Â Â Â Â Â Â Â Â |
|---|
| 669 |         if (!use_relative) { |
|---|
| 670 | Â Â Â Â Â Â Â Â Â Â Â Â r = gpk_select_id(card, GPK_SEL_MF, GPK_FID_MF, NULL); |
|---|
| 671 |             if (r) |
|---|
| 672 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_unlock(card); |
|---|
| 673 | Â Â Â Â Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "Unable to select MF"); |
|---|
| 674 | |
|---|
| 675 | Â Â Â Â Â Â Â Â Â Â Â Â |
|---|
| 676 |             if (pathptr[0] == GPK_FID_MF) { |
|---|
| 677 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pathptr++; |
|---|
| 678 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pathlen--; |
|---|
| 679 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 680 |             if (pathlen == 0) |
|---|
| 681 |                 goto done; |
|---|
| 682 | Â Â Â Â Â Â Â Â } |
|---|
| 683 | |
|---|
| 684 | Â Â Â Â Â Â Â Â |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 |         while (pathlen > 1) { |
|---|
| 688 | Â Â Â Â Â Â Â Â Â Â Â Â r = gpk_select_id(card, GPK_SEL_DF, pathptr[0], NULL); |
|---|
| 689 |             if (r) |
|---|
| 690 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sc_unlock(card); |
|---|
| 691 | Â Â Â Â Â Â Â Â Â Â Â Â SC_TEST_RET(card->ctx, r, "Unable to select DF"); |
|---|
| 692 | Â Â Â Â Â Â Â Â Â Â Â Â pathptr++; |
|---|
| 693 | Â Â Â Â Â Â Â Â Â Â Â Â pathlen--; |
|---|
| 694 | Â Â Â Â Â Â Â Â } |
|---|
| 695 | Â Â Â Â } |
|---|
| 696 | |
|---|
| 697 | Â Â Â Â |
|---|
| 698 | |
|---|
| 699 | Â Â Â Â r = gpk_select_id(card, leaf_type, pathptr[0], file); |
|---|
| 700 |     if (r) { |
|---|
| 701 | Â Â Â Â Â Â Â Â |
|---|
| 702 | |
|---|
| 703 |         if (leaf_type == GPK_SEL_EF && retry) { |
|---|
| 704 | Â Â Â Â Â Â Â Â Â Â Â Â card->cache.current_path.len = 0; |
|---|
| 705 | Â Â Â Â Â Â Â Â Â Â Â Â leaf_type = GPK_SEL_DF; |
|---|
| 706 |             goto try_again; |
|---|
| 707 | Â Â Â Â Â Â Â Â } |
|---|
| 708 | Â Â Â Â } |
|---|
| 709 | |
|---|
| 710 | done: |
|---|
| 711 |     if (locked) |
|---|
| 712 | Â Â Â Â Â Â Â Â sc_unlock(card); |
|---|
| 713 |     return r; |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | static int |
|---|
| 721 | gpk_read_binary(sc_card_t *card, unsigned int offset, |
|---|
| 722 |         u8 *buf, size_t count, unsigned long flags) |
|---|
| 723 | { |
|---|
| 724 |     struct gpk_private_data *priv = DRVDATA(card); |
|---|
| 725 | |
|---|
| 726 |     if (offset & priv->offset_mask) { |
|---|
| 727 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "Invalid file offset (not a multiple of %d)", |
|---|
| 728 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â priv->offset_mask + 1); |
|---|
| 729 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 730 | Â Â Â Â } |
|---|
| 731 |     return iso_ops->read_binary(card, offset >> priv->offset_shift, |
|---|
| 732 | Â Â Â Â Â Â Â Â Â Â Â Â buf, count, flags); |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | static int |
|---|
| 736 | gpk_write_binary(sc_card_t *card, unsigned int offset, |
|---|
| 737 |         const u8 *buf, size_t count, unsigned long flags) |
|---|
| 738 | { |
|---|
| 739 |     struct gpk_private_data *priv = DRVDATA(card); |
|---|
| 740 | |
|---|
| 741 |     if (offset & priv->offset_mask) { |
|---|
| 742 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "Invalid file offset (not a multiple of %d)", |
|---|
| 743 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â priv->offset_mask + 1); |
|---|
| 744 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 745 | Â Â Â Â } |
|---|
| 746 |     return iso_ops->write_binary(card, offset >> priv->offset_shift, |
|---|
| 747 | Â Â Â Â Â Â Â Â Â Â Â Â buf, count, flags); |
|---|
| 748 | } |
|---|
| 749 | |
|---|
| 750 | static int |
|---|
| 751 | gpk_update_binary(sc_card_t *card, unsigned int offset, |
|---|
| 752 |         const u8 *buf, size_t count, unsigned long flags) |
|---|
| 753 | { |
|---|
| 754 |     struct gpk_private_data *priv = DRVDATA(card); |
|---|
| 755 | |
|---|
| 756 |     if (offset & priv->offset_mask) { |
|---|
| 757 | Â Â Â Â Â Â Â Â sc_error(card->ctx, "Invalid file offset (not a multiple of %d)", |
|---|
| 758 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â priv->offset_mask + 1); |
|---|
| 759 |         return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 760 | Â Â Â Â } |
|---|
| 761 |     return iso_ops->update_binary(card, offset >> priv->offset_shift, |
|---|
| 762 | Â Â Â Â Â Â Â Â Â Â Â Â buf, count, flags); |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | static int |
|---|
| 769 | gpk_compute_crycks(sc_card_t *card, sc_apdu_t *apdu, |
|---|
| 770 | Â Â Â Â Â Â Â Â Â Â Â Â u8 *crycks1) |
|---|
| 771 | { |
|---|
| 772 |     struct gpk_private_data *priv = DRVDATA(card); |
|---|
| 773 | Â Â Â Â u8Â Â Â Â Â Â Â in[8], out[8], block[64]; |
|---|
| 774 |     unsigned int  len = 0, i; |
|---|
| 775 |     int       r = SC_SUCCESS, outl; |
|---|
| 776 | Â Â Â Â EVP_CIPHER_CTXÂ ctx; |
|---|
| 777 | |
|---|
| 778 | Â Â Â Â |
|---|
| 779 | Â Â Â Â memset(block, 0x00, sizeof(block)); |
|---|
| 780 | Â Â Â Â block[len++] = apdu->cla; |
|---|
| 781 | Â Â Â Â block[len++] = apdu->ins; |
|---|
| 782 | Â Â Â Â block[len++] = apdu->p1; |
|---|
| 783 | Â Â Â Â block[len++] = apdu->p2; |
|---|
| 784 | Â Â Â Â block[len++] = apdu->lc + 3; |
|---|
| 785 |     if ((i = apdu->datalen) + len > sizeof(block)) |
|---|
| 786 | Â Â Â Â Â Â Â Â i = sizeof(block) - len; |
|---|
| 787 | Â Â Â Â memcpy(block+len, apdu->data, i); |
|---|
| 788 | Â Â Â Â len += i; |
|---|
| 789 | |
|---|
| 790 | Â Â Â Â |
|---|
| 791 | Â Â Â Â memset(in, 0x00, 8); |
|---|
| 792 | |
|---|
| 793 | Â Â Â Â EVP_CIPHER_CTX_init(&ctx); |
|---|
| 794 | Â Â Â Â EVP_EncryptInit_ex(&ctx, EVP_des_ede_cbc(), NULL, priv->key, in); |
|---|
| 795 |     for (i = 0; i < len; i += 8) { |
|---|
| 796 |         if (!EVP_EncryptUpdate(&ctx, out, &outl, &block[i], 8)) { |
|---|
| 797 | Â Â Â Â Â Â Â Â Â Â Â Â r = SC_ERROR_INTERNAL; |
|---|
| 798 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 799 | Â Â Â Â Â Â Â Â } |
|---|
| 800 | Â Â Â Â } |
|---|
| 801 |     if (!EVP_CIPHER_CTX_cleanup(&ctx)) |
|---|
| 802 | Â Â Â Â Â Â Â Â r = SC_ERROR_INTERNAL; |
|---|
| 803 | |
|---|
| 804 | Â Â Â Â memcpy((u8 *) (apdu->data + apdu->datalen), out + 5, 3); |
|---|
| 805 | Â Â Â Â apdu->datalen += 3; |
|---|
| 806 | Â Â Â Â apdu->lc += 3; |
|---|
| 807 | Â Â Â Â apdu->le += 3; |
|---|
| 808 |     if (crycks1) |
|---|
| 809 | Â Â Â Â Â Â Â Â memcpy(crycks1, out, 3); |
|---|
| 810 | Â Â Â Â sc_mem_clear(in, sizeof(in)); |
|---|
| 811 | Â Â Â Â sc_mem_clear(out, sizeof(out)); |
|---|
| 812 | Â Â Â Â sc_mem_clear(block, sizeof(block)); |
|---|
| 813 |     return r; |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | static int |
|---|
| 820 | gpk_verify_crycks(sc_card_t *card, sc_apdu_t *apdu, u8 *crycks) |
|---|
| 821 | { |
|---|
| 822 |     if (apdu->resplen < 3 |
|---|
| 823 | Â Â Â Â Â || memcmp(apdu->resp + apdu->resplen - 3, crycks, 3)) { |
|---|
| 824 |         if (card->ctx->debug) |
|---|
| 825 | Â Â Â Â Â Â Â Â Â Â Â Â sc_debug(card->ctx, "Invalid secure messaging reply\n"); |
|---|
| 826 |         return SC_ERROR_UNKNOWN_DATA_RECEIVED; |
|---|
| 827 | Â Â Â Â } |
|---|
| 828 | Â Â Â Â apdu->resplen -= 3; |
|---|
| 829 |     return 0; |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | static int |
|---|
| 839 | gpk_create_file(sc_card_t *card, sc_file_t *file) |
|---|
| 840 | { |
|---|
| 841 |     struct gpk_private_data *priv = DRVDATA(card); |
|---|
| 842 |     sc_apdu_t    apdu; |
|---|
| 843 | Â Â Â Â u8Â Â Â Â Â Â Â data[28+3], crycks[3], resp[3]; |
|---|
| 844 |     size_t     datalen, namelen; |
|---|
| 845 |     int       r; |
|---|
| 846 | |
|---|
| 847 |     if (card->ctx->debug) |
|---|
| 848 | Â Â Â Â Â Â Â Â sc_debug(card->ctx, "gpk_create_file(0x%04X)\n", file->id); |
|---|
| 849 | |
|---|
| 850 | Â Â Â Â |
|---|
| 851 | Â Â Â Â memset(&apdu, 0, sizeof(apdu)); |
|---|
| 852 | Â Â Â Â apdu.cla = 0x80;Â Â Â Â |
|---|
| 853 | Â Â Â Â apdu.cse = SC_APDU_CASE_3_SHORT; |
|---|
| 854 | Â Â Â Â apdu.ins = 0xE0; |
|---|
| 855 | Â Â Â Â apdu.p2Â = 0x00; |
|---|
| 856 | |
|---|
| 857 | Â Â Â Â |
|---|
| 858 | Â Â Â Â memset(data, 0, sizeof(data)); |
|---|
| 859 | Â Â Â Â datalen = 12; |
|---|
| 860 | |
|---|
| 861 | Â Â Â Â |
|---|
| 862 | Â Â Â Â data[0] = file->id >> 8; |
|---|
| 863 | Â Â Â Â data[1] = file->id & 0xFF; |
|---|
| 864 | |
|---|
| 865 | Â Â Â Â |
|---|
| 866 |     if (file->type == SC_FILE_TYPE_DF) { |
|---|
| 867 | Â Â Â Â Â Â Â Â |
|---|
| 868 | |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | Â Â Â Â Â Â Â Â apdu.p1 = 0x01; |
|---|
| 873 | Â Â Â Â Â Â Â Â data[2] = 0x38; |
|---|
| 874 | Â Â Â Â Â Â Â Â acl_to_ac(file, SC_AC_OP_CREATE, data + 6); |
|---|
| 875 | Â Â Â Â Â Â Â Â acl_to_ac(file, SC_AC_OP_CREATE, data + 8); |
|---|
| 876 |         if ((namelen = file->namelen) != 0) { |
|---|
| 877 |             if (namelen > 16) |
|---|
| 878 |                 return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 879 | Â Â Â Â Â Â Â Â Â Â Â Â memcpy(data+datalen, file->name, namelen); |
|---|
| 880 | Â Â Â Â Â Â Â Â Â Â Â Â data[5] = namelen; |
|---|
| 881 | Â Â Â Â Â Â Â Â Â Â Â Â datalen += namelen; |
|---|
| 882 | Â Â Â Â Â Â Â Â } |
|---|
| 883 |     } else { |
|---|
| 884 | Â Â Â Â Â Â Â Â apdu.p1 = 0x02; |
|---|
| 885 | Â Â Â Â Â Â Â Â data[2] = file->ef_structure; |
|---|
| 886 | Â Â Â Â Â Â Â Â data[3] = file->record_length; |
|---|
| 887 | Â Â Â Â Â Â Â Â data[4] = file->size >> 8; |
|---|
| 888 | Â Â Â Â Â Â Â Â data[5] = file->size & 0xff; |
|---|
| 889 | Â Â Â Â Â Â Â Â acl_to_ac(file, SC_AC_OP_UPDATE, data + 6); |
|---|
| 890 | Â Â Â Â Â Â Â Â acl_to_ac(file, SC_AC_OP_WRITE, data + 8); |
|---|
| 891 | Â Â Â Â Â Â Â Â acl_to_ac(file, SC_AC_OP_READ, data + 10); |
|---|
| 892 | Â Â Â Â } |
|---|
| 893 | |
|---|
| 894 | Â Â Â Â apdu.data = data; |
|---|
| 895 | Â Â Â Â apdu.datalen = datalen; |
|---|
| 896 | Â Â Â Â apdu.lc = datalen; |
|---|
| 897 | |
|---|
| 898 |     if (priv->key_set) { |
|---|
| 899 | Â Â Â Â Â Â Â Â apdu.cla = 0x84; |
|---|
| 900 | Â Â Â Â Â Â Â Â apdu.cse = SC_APDU_CASE_4_SHORT; |
|---|
| 901 | Â Â Â Â Â Â Â Â r = gpk_compute_crycks(card, &apdu, crycks); |
|---|
| 902 |         if (r) |
|---|
| 903 |             return r; |
|---|
| 904 | Â Â Â Â |
|---|