From 3ff1f7234abb4c42273adedbe06d9e7f9f3a5f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Thu, 11 Apr 2013 16:18:31 +0700 Subject: [PATCH 14/26] OpenPGP: Overcome the restriction of even data length of Gnuk. When write certificate with odd length to Gnuk, we add zero padding to make it even. --- src/libopensc/card-openpgp.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) Index: opensc-20150513/src/libopensc/card-openpgp.c =================================================================== --- opensc-20150513.orig/src/libopensc/card-openpgp.c +++ opensc-20150513/src/libopensc/card-openpgp.c @@ -1216,6 +1216,10 @@ static int gnuk_write_certificate(sc_car sc_apdu_t apdu; u8 *part; size_t plen; + /* Two round_ variables below are to build APDU data + * with even length for Gnuk */ + u8 roundbuf[256]; + size_t roundlen = 0; int r = SC_SUCCESS; LOG_FUNC_CALLED(ctx); @@ -1246,8 +1250,20 @@ static int gnuk_write_certificate(sc_car sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xD6, i, 0); } apdu.flags |= SC_APDU_FLAGS_CHAINING; - apdu.data = part; - apdu.datalen = apdu.lc = plen; + + /* If the last part has odd length, we add zero padding to make it even. + * Gnuk does not allow data with odd length */ + if (plen < 256 && (plen % 2) != 0) { + roundlen = plen + 1; + memset(roundbuf, 0, roundlen); + memcpy(roundbuf, part, plen); + apdu.data = roundbuf; + apdu.datalen = apdu.lc = roundlen; + } + else { + apdu.data = part; + apdu.datalen = apdu.lc = plen; + } r = sc_transmit_apdu(card, &apdu); LOG_TEST_RET(card->ctx, r, "APDU transmit failed");