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.
 
 
 
 
 
 

53 lines
1.7 KiB

From 3ff1f7234abb4c42273adedbe06d9e7f9f3a5f9d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?=
<ng.hong.quan@gmail.com>
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(-)
diff --git a/src/libopensc/card-openpgp.c b/src/libopensc/card-openpgp.c
index 7cea84f..7a77a71 100644
--- a/src/libopensc/card-openpgp.c
+++ b/src/libopensc/card-openpgp.c
@@ -1208,6 +1208,10 @@ static int gnuk_write_certificate(sc_card_t *card, const u8 *buf, size_t length)
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);
@@ -1238,8 +1242,20 @@ static int gnuk_write_certificate(sc_card_t *card, const u8 *buf, size_t length)
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");
--
2.1.3