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.

50 lines
1.7 KiB

  1. From 3ff1f7234abb4c42273adedbe06d9e7f9f3a5f9d 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: Thu, 11 Apr 2013 16:18:31 +0700
  5. Subject: [PATCH 14/26] OpenPGP: Overcome the restriction of even data length
  6. of Gnuk.
  7. When write certificate with odd length to Gnuk, we add zero padding to make it even.
  8. ---
  9. src/libopensc/card-openpgp.c | 20 ++++++++++++++++++--
  10. 1 file changed, 18 insertions(+), 2 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. @@ -1216,6 +1216,10 @@ static int gnuk_write_certificate(sc_car
  16. sc_apdu_t apdu;
  17. u8 *part;
  18. size_t plen;
  19. + /* Two round_ variables below are to build APDU data
  20. + * with even length for Gnuk */
  21. + u8 roundbuf[256];
  22. + size_t roundlen = 0;
  23. int r = SC_SUCCESS;
  24. LOG_FUNC_CALLED(ctx);
  25. @@ -1246,8 +1250,20 @@ static int gnuk_write_certificate(sc_car
  26. sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6, i, 0);
  27. }
  28. apdu.flags |= SC_APDU_FLAGS_CHAINING;
  29. - apdu.data = part;
  30. - apdu.datalen = apdu.lc = plen;
  31. +
  32. + /* If the last part has odd length, we add zero padding to make it even.
  33. + * Gnuk does not allow data with odd length */
  34. + if (plen < 256 && (plen % 2) != 0) {
  35. + roundlen = plen + 1;
  36. + memset(roundbuf, 0, roundlen);
  37. + memcpy(roundbuf, part, plen);
  38. + apdu.data = roundbuf;
  39. + apdu.datalen = apdu.lc = roundlen;
  40. + }
  41. + else {
  42. + apdu.data = part;
  43. + apdu.datalen = apdu.lc = plen;
  44. + }
  45. r = sc_transmit_apdu(card, &apdu);
  46. LOG_TEST_RET(card->ctx, r, "APDU transmit failed");