fix things around write_binary
authorNIIBE Yutaka <gniibe@fsij.org>
Wed, 9 Feb 2011 05:06:57 +0000 (14:06 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Wed, 9 Feb 2011 05:06:57 +0000 (14:06 +0900)
AUTHORS
ChangeLog
src/openpgp-do.c
src/openpgp.c
src/usb_prop.c
tool/dfuse.py
tool/gnuk_put_binary.py

diff --git a/AUTHORS b/AUTHORS
index b9270bb..393c3dc 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -27,9 +27,12 @@ NIIBE Yutaka:
        boards/CQ_STARM/board.h
        boards/CQ_STARM/board.mk
        boards/CQ_STARM/mcuconf.h
-    Wrote a tool for DfuSe:
+    Wrote tools for DfuSe:
        tool/dfuse.py
+       tool/dump_mem.py
        tool/intel_hex.py
+    Wrote a tool for Gnuk:
+       tool/gnuk_put_binary.py
     Wrote:
        gnuk.svg
        src/configure
index 54652f9..0fa43d4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2011-02-09  NIIBE Yutaka  <gniibe@fsij.org>
+
+       * src/usb_prop.c (gnukStringSerial): Updated.
+
+       * tool/gnuk_put_binary.py (gnuk_token.__del__): Removed.
+       Releasing the interface is done in PyUSB.
+
+       * tool/dfuse.py (DFU_STM32.__del__): Removed.
+
+       * src/openpgp.c (cmd_write_binary): Support random bits and card
+       holder certificate as well.
+
+       * src/openpgp-do.c (do_openpgpcard_aid): Add volatile to prevent
+       compiler optimization to access AID.
+
 2011-02-08  NIIBE Yutaka  <gniibe@fsij.org>
 
        * tool/gnuk_put_binary.py: Renamed (was: gnuk_update_binary.py).
index d7571ba..0804382 100644 (file)
@@ -416,7 +416,7 @@ const uint8_t openpgpcard_aid[] = {
 static int
 do_openpgpcard_aid (uint16_t tag, int with_tag)
 {
-  const uint16_t *vid_p = (const uint16_t *)&openpgpcard_aid[8];
+  uint16_t vid = *((const volatile uint16_t *)&openpgpcard_aid[8]);
 
   if (with_tag)
     {
@@ -424,7 +424,7 @@ do_openpgpcard_aid (uint16_t tag, int with_tag)
       *res_p++ = 16;
     }
 
-  if (*vid_p == 0xffff || *vid_p == 0x0000)
+  if (vid == 0xffff || vid == 0x0000)
     {
       const uint8_t *u = unique_device_id ();
 
index 8b10e3f..8758358 100644 (file)
@@ -870,7 +870,7 @@ cmd_update_binary (void)
     }
 
   if ((cmd_APDU[2] & 0x80))
-    if ((cmd_APDU[2] & 0x7f) <= 0x01)
+    if ((cmd_APDU[2] & 0x7f) <= FILEID_RANDOM)
       {
        file_selection = FILE_EF_CH_CERTIFICATE + (cmd_APDU[2] & 0x7f);
        r = flash_erase_binary (file_selection - FILE_EF_CH_CERTIFICATE);
@@ -941,7 +941,7 @@ cmd_write_binary (void)
     }
 
   if ((cmd_APDU[2] & 0x80))
-    if ((cmd_APDU[2] & 0x7f) == FILEID_SERIAL_NO)
+    if ((cmd_APDU[2] & 0x7f) <= FILEID_SERIAL_NO)
       {
        file_selection = FILE_EF_CH_CERTIFICATE + (cmd_APDU[2] & 0x7f);
        offset = 0;
@@ -953,7 +953,9 @@ cmd_write_binary (void)
       }
   else
     {
-      if (file_selection != FILEID_SERIAL_NO)
+      if (file_selection != FILE_EF_CH_CERTIFICATE
+         && file_selection != FILE_EF_RANDOM
+         && file_selection != FILEID_SERIAL_NO)
        {
          GPG_COMMAND_NOT_ALLOWED ();
          return;
index 0298729..d71afe0 100644 (file)
 #endif
 
 static uint8_t gnukStringSerial[] = {
-  14*2+2,                      /* bLength */
+  13*2+2,                      /* bLength */
   USB_STRING_DESCRIPTOR_TYPE,  /* bDescriptorType */
-#if defined(SERIAL_NUMBER_IN_AID)
-  'F', 0,                      /* 'F' for Fixed */
-#else
-  'C', 0,                      /* 'C' for Chip uniqure ID */
-#endif
-  '-', 0,
-  '0', 0, '.', 0, '8', 0,      /* Version number of Gnuk */
+  '0', 0, '.', 0, '1', 0, '0', 0, /* Version number of Gnuk */
   '-', 0,
   0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0,
 };
+#define ID_OFFSET 12
 
 static void
 gnuk_device_init (void)
@@ -59,10 +54,10 @@ gnuk_device_init (void)
 
   for (i = 0; i < 4; i++)
     {
-      gnukStringSerial[i*4+14] = (u[i*2] >> 4) + 'A';
-      gnukStringSerial[i*4+15] = 0;
-      gnukStringSerial[i*4+16] = (u[i*2+1] & 0x0f) + 'A';
-      gnukStringSerial[i*4+17] = 0;
+      gnukStringSerial[i*4+ID_OFFSET+0] = (u[i*2] >> 4) + 'A';
+      gnukStringSerial[i*4+ID_OFFSET+1] = 0;
+      gnukStringSerial[i*4+ID_OFFSET+2] = (u[i*2+1] & 0x0f) + 'A';
+      gnukStringSerial[i*4+ID_OFFSET+3] = 0;
     }
 
   pInformation->Current_Configuration = 0;
index 2f9c180..e826af4 100755 (executable)
@@ -119,13 +119,6 @@ class DFU_STM32:
         # Initialize members
         self.__blocknum = 0
 
-    def __del__(self):
-        try:
-            self.__devhandle.releaseInterface()
-            del self.__devhandle
-        except:
-            pass
-
     def ll_getdev(self):
         return self.__devhandle
 
index f824b44..d4a1e87 100755 (executable)
@@ -25,7 +25,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from intel_hex import *
 from struct import *
-import sys, time, os, binascii
+import sys, time, os, binascii, string
 
 # INPUT: binary file
 
@@ -81,14 +81,6 @@ class gnuk_token:
         self.__timeout = 10000
         self.__seq = 0
 
-
-    def __del__(self):
-        try:
-            self.__devhandle.releaseInterface()
-            del self.__devhandle
-        except:
-            pass
-
     def icc_get_result(self):
         msg = self.__devhandle.bulkRead(self.__bulkin, 1024, self.__timeout)
         if len(msg) < 10:
@@ -249,9 +241,10 @@ def main(fileid, is_update, data):
     else:
         icc.cmd_write_binary(fileid, data)
     icc.cmd_select_openpgp()
-    data = data[:-2]
-    data_in_device = icc.cmd_get_data(0x7f, 0x21)
-    compare(data, data_in_device)
+    if fileid == 0:
+        data = data[:-2]
+        data_in_device = icc.cmd_get_data(0x7f, 0x21)
+        compare(data, data_in_device)
     icc.icc_power_off()
     return 0
 
@@ -265,16 +258,16 @@ if __name__ == '__main__':
         fileid = 2              # serial number
         filename = sys.argv[2]
         f = open(filename)
-        email = os.environ['MAIL']
+        email = os.environ['EMAIL']
         serial_data_hex = None
         for line in f.readlines():
             field = string.split(line)
-            if field[0] == os.environ['MAIL']:
+            if field[0] == email:
                 serial_data_hex = field[1].replace(':','')
         f.close()
         if not serial_data_hex:
             print "No serial number"
-            exit 1
+            exit(1)
         print "Writing serial number"
         data = binascii.unhexlify(serial_data_hex)
     elif sys.argv[1] == '-r':
@@ -291,7 +284,7 @@ if __name__ == '__main__':
         print "Updating random bits"
     else:
         fileid = 0              # Card holder certificate
-        filename = sys.argv[2]
+        filename = sys.argv[1]
         f = open(filename)
         data = f.read()
         f.close()