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
8 When write certificate with odd length to Gnuk, we add zero padding to make it even.
10 src/libopensc/card-openpgp.c | 20 ++++++++++++++++++--
11 1 file changed, 18 insertions(+), 2 deletions(-)
13 diff --git a/src/libopensc/card-openpgp.c b/src/libopensc/card-openpgp.c
14 index 7cea84f..7a77a71 100644
15 --- a/src/libopensc/card-openpgp.c
16 +++ b/src/libopensc/card-openpgp.c
17 @@ -1208,6 +1208,10 @@ static int gnuk_write_certificate(sc_card_t *card, const u8 *buf, size_t length)
21 + /* Two round_ variables below are to build APDU data
22 + * with even length for Gnuk */
24 + size_t roundlen = 0;
28 @@ -1238,8 +1242,20 @@ static int gnuk_write_certificate(sc_card_t *card, const u8 *buf, size_t length)
29 sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6, i, 0);
31 apdu.flags |= SC_APDU_FLAGS_CHAINING;
33 - apdu.datalen = apdu.lc = plen;
35 + /* If the last part has odd length, we add zero padding to make it even.
36 + * Gnuk does not allow data with odd length */
37 + if (plen < 256 && (plen % 2) != 0) {
38 + roundlen = plen + 1;
39 + memset(roundbuf, 0, roundlen);
40 + memcpy(roundbuf, part, plen);
41 + apdu.data = roundbuf;
42 + apdu.datalen = apdu.lc = roundlen;
46 + apdu.datalen = apdu.lc = plen;
49 r = sc_transmit_apdu(card, &apdu);
50 LOG_TEST_RET(card->ctx, r, "APDU transmit failed");