You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.2 KiB

  1. From 0cd2a488d86006bb2740a4e73e7a0d859e1bf33c Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?=
  3. <ng.hong.quan@gmail.com>
  4. Date: Sun, 13 Jul 2014 17:37:59 +0800
  5. Subject: [PATCH 22/26] OpenPGP: Use directly binary array of APDUs for ERASE
  6. command.
  7. I used a string presentation before and it needed an extra conversion step.
  8. ---
  9. src/libopensc/card-openpgp.c | 47 +++++++++++++++++++++++---------------------
  10. 1 file changed, 25 insertions(+), 22 deletions(-)
  11. Index: opensc-20150513/src/libopensc/card-openpgp.c
  12. ===================================================================
  13. --- opensc-20150513.orig/src/libopensc/card-openpgp.c
  14. +++ opensc-20150513/src/libopensc/card-openpgp.c
  15. @@ -2358,24 +2358,27 @@ out:
  16. static int pgp_erase_card(sc_card_t *card)
  17. {
  18. sc_context_t *ctx = card->ctx;
  19. - u8 *apdustring[10] = {
  20. - "00:20:00:81:08:40:40:40:40:40:40:40:40",
  21. - "00:20:00:81:08:40:40:40:40:40:40:40:40",
  22. - "00:20:00:81:08:40:40:40:40:40:40:40:40",
  23. - "00:20:00:81:08:40:40:40:40:40:40:40:40",
  24. - "00:20:00:83:08:40:40:40:40:40:40:40:40",
  25. - "00:20:00:83:08:40:40:40:40:40:40:40:40",
  26. - "00:20:00:83:08:40:40:40:40:40:40:40:40",
  27. - "00:20:00:83:08:40:40:40:40:40:40:40:40",
  28. - "00:e6:00:00",
  29. - "00:44:00:00"
  30. + /* Special series of commands to erase OpenPGP card,
  31. + * according to https://www.crypto-stick.com/en/faq
  32. + * (How to reset a Crypto Stick? question).
  33. + * Gnuk is known not to support this feature. */
  34. + u8 apdu_binaries[10][13] = {
  35. + {0, 0x20, 0, 0x81, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  36. + {0, 0x20, 0, 0x81, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  37. + {0, 0x20, 0, 0x81, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  38. + {0, 0x20, 0, 0x81, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  39. + {0, 0x20, 0, 0x83, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  40. + {0, 0x20, 0, 0x83, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  41. + {0, 0x20, 0, 0x83, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  42. + {0, 0x20, 0, 0x83, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40},
  43. + {0, 0xe6, 0, 0},
  44. + {0, 0x44, 0, 0}
  45. };
  46. + u8 apdu_lens[10] = {13, 13, 13, 13, 13, 13, 13, 13, 4, 4};
  47. u8 buf[SC_MAX_APDU_BUFFER_SIZE];
  48. u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
  49. sc_apdu_t apdu;
  50. - size_t len0;
  51. - int commandsnum = 10;
  52. - int i, r;
  53. + int i, l, r;
  54. LOG_FUNC_CALLED(ctx);
  55. @@ -2387,17 +2390,17 @@ static int pgp_erase_card(sc_card_t *car
  56. sc_log(ctx, "Card is OpenPGP v2. Erase card.");
  57. /* Iterate over 10 commands above */
  58. - for (i = 0; i < commandsnum; i++) {
  59. - /* Convert the string to binary array */
  60. - len0 = sizeof(buf);
  61. - sc_hex_to_bin(apdustring[i], buf, &len0);
  62. - printf("Sending: ");
  63. - for (r = 0; r < len0; r++)
  64. - printf("%02X ", buf[r]);
  65. + for (i = 0; i < sizeof(apdu_lens); i++) {
  66. + /* Length of the binary array of the current command */
  67. + l = apdu_lens[i];
  68. + /* Print the command to console */
  69. + printf("Sending %d: ", i);
  70. + for (r = 0; r < l; r++)
  71. + printf("%02X ", apdu_binaries[i][r]);
  72. printf("\n");
  73. /* Build APDU from binary array */
  74. - r = sc_bytes2apdu(card->ctx, buf, len0, &apdu);
  75. + r = sc_bytes2apdu(card->ctx, apdu_binaries[i], l, &apdu);
  76. if (r) {
  77. sc_log(ctx, "Failed to build APDU");
  78. LOG_FUNC_RETURN(ctx, SC_ERROR_INTERNAL);