Ticket #176: bug.1

File bug.1, 12.4 KB (added by ken, 8 months ago)

Here are some more patches related to this bug. Other than the previously mentioned patch to pkcs15-tool.c, I have no demonstration that these changes are strictly necessary. This is also not an exhaustive review of where more careful size_t vs. int changes need to be made.

Line 
1diff -ru opensc-0.11.4-orig/src/libopensc/card-akis.c opensc-0.11.4/src/libopensc/card-akis.c
2--- opensc-0.11.4-orig/src/libopensc/card-akis.c        2007-08-02 11:52:05.000000000 -0700
3+++ opensc-0.11.4/src/libopensc/card-akis.c     2008-04-27 17:41:00.000000000 -0700
4@@ -371,7 +371,7 @@
5 akis_get_serialnr(sc_card_t *card, sc_serial_number_t *serial)
6 {
7        int r;
8-       u8 system[128];
9+       u8 syst[128];
10 
11        if (!serial)
12                return SC_ERROR_INVALID_ARGUMENTS;
13@@ -380,11 +380,11 @@
14        if (card->serialnr.len) goto end;
15 
16        /* read serial number */
17-       r = akis_get_data(card, 6, system, 0x4D);
18+       r = akis_get_data(card, 6, syst, 0x4D);
19        SC_TEST_RET(card->ctx, r, "GET_DATA failed");
20 
21        card->serialnr.len = 12;
22-       memcpy(card->serialnr.value, system+55, 12);
23+       memcpy(card->serialnr.value, syst+55, 12);
24 
25 end:
26        memcpy(serial, &card->serialnr, sizeof(*serial));
27diff -ru opensc-0.11.4-orig/src/libopensc/muscle-filesystem.h opensc-0.11.4/src/libopensc/muscle-filesystem.h
28--- opensc-0.11.4-orig/src/libopensc/muscle-filesystem.h        2007-04-20 00:12:54.000000000 -0700
29+++ opensc-0.11.4/src/libopensc/muscle-filesystem.h     2008-04-27 17:24:33.000000000 -0700
30@@ -51,7 +51,7 @@
31        int (*listFile)(mscfs_file_t *fileOut, int reset, void* udata);
32 } mscfs_t;
33 
34-mscfs_t *mscfs_new();
35+mscfs_t *mscfs_new(void);
36 void mscfs_free(mscfs_t *fs);
37 void mscfs_clear_cache(mscfs_t* fs);
38 int mscfs_push_file(mscfs_t* fs, mscfs_file_t *file);
39diff -ru opensc-0.11.4-orig/src/libopensc/muscle.c opensc-0.11.4/src/libopensc/muscle.c
40--- opensc-0.11.4-orig/src/libopensc/muscle.c   2007-06-21 00:13:27.000000000 -0700
41+++ opensc-0.11.4/src/libopensc/muscle.c        2008-04-27 17:38:41.000000000 -0700
42@@ -316,8 +316,8 @@
43 void msc_verify_pin_apdu(sc_card_t *card, sc_apdu_t *apdu, u8* buffer, size_t bufferLength, int pinNumber, const u8 *pinValue, int pinLength)
44 {
45        assert(buffer);
46-       assert(bufferLength >= pinLength);
47-       assert(pinLength <= MSC_MAX_PIN_LENGTH);
48+       assert((size_t)pinLength <= bufferLength);
49+       assert((size_t)pinLength <= MSC_MAX_PIN_LENGTH);
50 
51        truncatePinNulls(pinValue, &pinLength);
52 
53@@ -360,8 +360,8 @@
54 void msc_unblock_pin_apdu(sc_card_t *card, sc_apdu_t *apdu, u8* buffer, size_t bufferLength, int pinNumber, const u8 *pukValue, int pukLength)
55 {
56        assert(buffer);
57-       assert(bufferLength >= pukLength);
58-       assert(pukLength <= MSC_MAX_PIN_LENGTH);
59+       assert((size_t)pukLength <= bufferLength);
60+       assert((size_t)pukLength <= MSC_MAX_PIN_LENGTH);
61 
62        truncatePinNulls(pukValue, &pukLength);
63 
64@@ -403,10 +403,10 @@
65 void msc_change_pin_apdu(sc_card_t *card, sc_apdu_t *apdu, u8* buffer, size_t bufferLength, int pinNumber, const u8 *pinValue, int pinLength, const u8 *newPin, int newPinLength)
66 {
67        u8 *ptr;
68-       assert(pinLength <= MSC_MAX_PIN_LENGTH);
69-       assert(newPinLength <= MSC_MAX_PIN_LENGTH);
70+       assert((size_t)pinLength <= MSC_MAX_PIN_LENGTH);
71+       assert((size_t)newPinLength <= MSC_MAX_PIN_LENGTH);
72        assert(buffer);
73-       assert(bufferLength >= pinLength + newPinLength + 2);
74+       assert((size_t)pinLength + (size_t)newPinLength + 2 <= bufferLength);
75 
76        truncatePinNulls(pinValue, &pinLength);
77        truncatePinNulls(newPin, &newPinLength);
78@@ -433,12 +433,12 @@
79        int r, location, cse, len;
80        u8 *buffer, *ptr;
81       
82-       location = (dataLength < MSC_MAX_READ) ? 1 : 2; /* 1 == APDU, 2 == (seed in 0xFFFFFFFE, out in 0xFFFFFFFF) */
83+       location = ((size_t)dataLength < MSC_MAX_READ) ? 1 : 2; /* 1 == APDU, 2 == (seed in 0xFFFFFFFE, out in 0xFFFFFFFF) */
84        cse = (location == 1) ? SC_APDU_CASE_4_SHORT : SC_APDU_CASE_3_SHORT;
85        len = seedLength + 4;
86       
87-       assert(seedLength < MSC_MAX_SEND - 4);
88-       assert(dataLength < MSC_MAX_READ - 9); /* Output buffer doesn't seem to operate as desired.... nobody can read/delete */
89+       assert((size_t)seedLength < MSC_MAX_SEND - 4);
90+       assert((size_t)dataLength < MSC_MAX_READ - 9); /* Output buffer doesn't seem to operate as desired.... nobody can read/delete */
91       
92        buffer = malloc(len);
93        if(!buffer) SC_FUNC_RETURN(card->ctx, 0, SC_ERROR_OUT_OF_MEMORY);
94@@ -806,7 +806,7 @@
95                        size_t dataLength,
96                        size_t outputDataLength)
97 {
98-       int left = dataLength;
99+       size_t left = dataLength;
100        const u8* inPtr = data;
101        u8* outPtr = outputData;
102        int toSend;
103diff -ru opensc-0.11.4-orig/src/pkcs11/framework-pkcs15.c opensc-0.11.4/src/pkcs11/framework-pkcs15.c
104--- opensc-0.11.4-orig/src/pkcs11/framework-pkcs15.c    2007-06-21 12:37:59.000000000 -0700
105+++ opensc-0.11.4/src/pkcs11/framework-pkcs15.c 2008-04-27 17:52:29.000000000 -0700
106@@ -263,7 +263,7 @@
107                              struct pkcs15_any_object **obj2)
108 {
109        int found = 0;
110-       int ii=0;
111+       unsigned int ii=0;
112 
113        while(ii<num_objects && !found) {
114                if (!fw_data->objects[ii]->p15_object) {
115diff -ru opensc-0.11.4-orig/src/pkcs11/openssl.c opensc-0.11.4/src/pkcs11/openssl.c
116--- opensc-0.11.4-orig/src/pkcs11/openssl.c     2007-06-21 12:37:59.000000000 -0700
117+++ opensc-0.11.4/src/pkcs11/openssl.c  2008-04-27 18:12:41.000000000 -0700
118@@ -136,9 +136,9 @@
119                                CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
120 {
121        EVP_MD_CTX      *md_ctx = DIGEST_CTX(op);
122-       unsigned int    len = *pulDigestLen;
123+       CK_ULONG        len = *pulDigestLen;
124 
125-       if (len < EVP_MD_CTX_size(md_ctx)) {
126+       if (len < (CK_ULONG)EVP_MD_CTX_size(md_ctx)) {
127                *pulDigestLen = EVP_MD_CTX_size(md_ctx);
128                return CKR_BUFFER_TOO_SMALL;
129        }
130diff -ru opensc-0.11.4-orig/src/pkcs11/pkcs11-display.c opensc-0.11.4/src/pkcs11/pkcs11-display.c
131--- opensc-0.11.4-orig/src/pkcs11/pkcs11-display.c      2007-06-21 12:37:59.000000000 -0700
132+++ opensc-0.11.4/src/pkcs11/pkcs11-display.c   2008-04-27 17:53:59.000000000 -0700
133@@ -713,7 +713,7 @@
134 
135 void print_slot_info(FILE *f, CK_SLOT_INFO *info)
136 {
137-  int            i;
138+  size_t i;
139   enum_specs ck_flags[] = {
140     { CKF_TOKEN_PRESENT    , "CKF_TOKEN_PRESENT                " },
141     { CKF_REMOVABLE_DEVICE , "CKF_REMOVABLE_DEVICE             " },
142@@ -899,7 +899,7 @@
143 
144 void print_session_info(FILE *f, CK_SESSION_INFO *info)
145 {
146-  int            i;
147+  size_t i;
148   enum_specs ck_flags[] = {
149     { CKF_RW_SESSION     , "CKF_RW_SESSION                   " },
150     { CKF_SERIAL_SESSION , "CKF_SERIAL_SESSION               " }
151diff -ru opensc-0.11.4-orig/src/scconf/parse.c opensc-0.11.4/src/scconf/parse.c
152--- opensc-0.11.4-orig/src/scconf/parse.c       2006-07-23 03:10:45.000000000 -0700
153+++ opensc-0.11.4/src/scconf/parse.c    2008-04-27 17:06:13.000000000 -0700
154@@ -150,7 +150,7 @@
155        scconf_item_add_internal(&parser, type);
156        switch (parser.current_item->type) {
157        case SCCONF_ITEM_TYPE_COMMENT:
158-               parser.current_item->value.comment = strdup((char *) data);
159+               parser.current_item->value.comment = strdup((const char *) data);
160                break;
161        case SCCONF_ITEM_TYPE_BLOCK:
162                if (!dst)
163diff -ru opensc-0.11.4-orig/src/scconf/sclex.c opensc-0.11.4/src/scconf/sclex.c
164--- opensc-0.11.4-orig/src/scconf/sclex.c       2005-12-29 04:36:29.000000000 -0800
165+++ opensc-0.11.4/src/scconf/sclex.c    2008-04-27 17:07:23.000000000 -0700
166@@ -92,7 +92,7 @@
167        }
168 }
169 
170-static void buf_eat_till(BUFHAN * bp, char start, char *end)
171+static void buf_eat_till(BUFHAN * bp, char start, const char *end)
172 {
173        int i;
174 
175diff -ru opensc-0.11.4-orig/src/signer/dialog.c opensc-0.11.4/src/signer/dialog.c
176--- opensc-0.11.4-orig/src/signer/dialog.c      2007-06-21 12:37:59.000000000 -0700
177+++ opensc-0.11.4/src/signer/dialog.c   2008-04-27 18:06:28.000000000 -0700
178@@ -36,6 +36,7 @@
179                            struct sc_pkcs15_object *pin)
180 {
181        int r;
182+       size_t len;
183        const char *argv[3];
184        const char *pgmname = PIN_ENTRY;
185        ASSUAN_CONTEXT ctx;
186@@ -78,12 +79,12 @@
187                        printf("GETPIN: %s\n", assuan_strerror((AssuanError) r));
188                        goto err;
189                }
190-               r = strlen(buf);
191-               if (r < pinfo->min_length) {
192+               len = strlen(buf);
193+               if (len < pinfo->min_length) {
194                        sprintf(errtext, "PIN code too short, min. %lu digits", (unsigned long) pinfo->min_length);
195                        continue;
196                }
197-               if (r > pinfo->max_length) {
198+               if (len > pinfo->max_length) {
199                        sprintf(errtext, "PIN code too long, max. %lu digits", (unsigned long) pinfo->max_length);
200                        continue;
201                }
202diff -ru opensc-0.11.4-orig/src/signer/opensc-crypto.h opensc-0.11.4/src/signer/opensc-crypto.h
203--- opensc-0.11.4-orig/src/signer/opensc-crypto.h       2005-12-29 04:36:28.000000000 -0800
204+++ opensc-0.11.4/src/signer/opensc-crypto.h    2008-04-27 18:02:51.000000000 -0700
205@@ -13,6 +13,6 @@
206         int ref_count, reader_id;
207 };
208 
209-extern RSA_METHOD * sc_get_method();
210+extern RSA_METHOD * sc_get_method(void);
211 
212 #endif
213diff -ru opensc-0.11.4-orig/src/tests/print.c opensc-0.11.4/src/tests/print.c
214--- opensc-0.11.4-orig/src/tests/print.c        2007-06-21 12:37:59.000000000 -0700
215+++ opensc-0.11.4/src/tests/print.c     2008-04-27 17:56:52.000000000 -0700
216@@ -95,6 +95,7 @@
217 static void print_prkey(const struct sc_pkcs15_object *obj)
218 {
219        int i;
220+       size_t j;
221        const char *usages[] =
222        {
223                "encrypt", "decrypt", "sign", "signRecover",
224@@ -131,8 +132,8 @@
225        printf("\tNative      : %s\n", prkey->native ? "yes" : "no");
226        if (prkey->path.len) {
227                printf("\tPath        : ");
228-               for (i = 0; i < prkey->path.len; i++)
229-                       printf("%02X", prkey->path.value[i]);
230+               for (j = 0; j < prkey->path.len; j++)
231+                       printf("%02X", prkey->path.value[j]);
232                if (prkey->path.type == SC_PATH_TYPE_PATH_PROT)
233                        printf(" (protected)");
234                printf("\n");
235@@ -143,6 +144,7 @@
236 static void print_pubkey(const struct sc_pkcs15_object *obj)
237 {
238        int i;
239+       size_t j;
240        const char *usages[] =
241        {
242                "encrypt", "decrypt", "sign", "signRecover",
243@@ -178,8 +180,8 @@
244        printf("\tKey ref     : %d\n", pubkey->key_reference);
245        printf("\tNative      : %s\n", pubkey->native ? "yes" : "no");
246        printf("\tPath        : ");
247-       for (i = 0; i < pubkey->path.len; i++)
248-               printf("%02X", pubkey->path.value[i]);
249+       for (j = 0; j < pubkey->path.len; j++)
250+               printf("%02X", pubkey->path.value[j]);
251        printf("\n");
252        printf("\tID          : %s\n", sc_pkcs15_print_id(&pubkey->id));
253 }
254diff -ru opensc-0.11.4-orig/src/tests/sc-test.h opensc-0.11.4/src/tests/sc-test.h
255--- opensc-0.11.4-orig/src/tests/sc-test.h      2007-06-21 12:37:59.000000000 -0700
256+++ opensc-0.11.4/src/tests/sc-test.h   2008-04-27 17:55:25.000000000 -0700
257@@ -13,7 +13,7 @@
258 struct sc_pkcs15_object;
259 
260 int sc_test_init(int *argc, char *argv[]);
261-void sc_test_cleanup();
262+void sc_test_cleanup(void);
263 void sc_test_print_card(const sc_pkcs15_card_t *);
264 void sc_test_print_object(const struct sc_pkcs15_object *);
265 
266diff -ru opensc-0.11.4-orig/src/tools/opensc-explorer.c opensc-0.11.4/src/tools/opensc-explorer.c
267--- opensc-0.11.4-orig/src/tools/opensc-explorer.c      2007-08-19 12:04:07.000000000 -0700
268+++ opensc-0.11.4/src/tools/opensc-explorer.c   2008-04-27 17:59:09.000000000 -0700
269@@ -128,7 +128,7 @@
270                path->type = SC_PATH_TYPE_DF_NAME;
271        } else {
272                /* file id */
273-               int buf[2];
274+               unsigned int buf[2];
275                u8 cbuf[2];
276       
277                if (strlen(arg) != 4) {
278@@ -531,7 +531,7 @@
279        if (arg_to_path(argv[0], &path, 1) != 0)
280                goto usage;
281        /* %z isn't supported everywhere */
282-       if (sscanf(argv[1], "%d", &size) != 1)
283+       if (sscanf(argv[1], "%u", &size) != 1)
284                goto usage;
285        file = sc_file_new();
286        file->id = (path.value[0] << 8) | path.value[1];
287@@ -561,7 +561,7 @@
288                goto usage;
289        if (arg_to_path(argv[0], &path, 1) != 0)
290                goto usage;
291-       if (sscanf(argv[1], "%d", &size) != 1)
292+       if (sscanf(argv[1], "%u", &size) != 1)
293                goto usage;
294        file = sc_file_new();
295        file->id = (path.value[0] << 8) | path.value[1];
296diff -ru opensc-0.11.4-orig/src/tools/piv-tool.c opensc-0.11.4/src/tools/piv-tool.c
297--- opensc-0.11.4-orig/src/tools/piv-tool.c     2007-06-30 01:55:58.000000000 -0700
298+++ opensc-0.11.4/src/tools/piv-tool.c  2008-04-27 20:02:23.000000000 -0700
299@@ -121,7 +121,8 @@
300                derlen = stat_buf.st_size;
301                der = malloc(derlen);
302                if (der == NULL) {
303-                       printf("file %s is too big, %d\n", cert_file, derlen);
304+                       printf("file %s is too big, %lu\n",
305+                               cert_file, (unsigned long)derlen);
306                        return-1 ;
307                }
308                if (1 != fread(der, derlen, 1, fp)) {
309diff -ru opensc-0.11.4-orig/src/tools/pkcs15-init.c opensc-0.11.4/src/tools/pkcs15-init.c
310--- opensc-0.11.4-orig/src/tools/pkcs15-init.c  2007-07-09 07:50:53.000000000 -0700
311+++ opensc-0.11.4/src/tools/pkcs15-init.c       2008-04-27 18:01:20.000000000 -0700
312@@ -106,7 +106,7 @@
313 static void    parse_commandline(int argc, char **argv);
314 static void    read_options_file(const char *);
315 static void    ossl_print_errors(void);
316-static void set_userpin_ref();
317+static void set_userpin_ref(void);
318 
319 
320 enum {
321diff -ru opensc-0.11.4-orig/src/tools/pkcs15-tool.c opensc-0.11.4/src/tools/pkcs15-tool.c
322--- opensc-0.11.4-orig/src/tools/pkcs15-tool.c  2007-06-30 01:55:58.000000000 -0700
323+++ opensc-0.11.4/src/tools/pkcs15-tool.c       2008-04-27 19:56:02.000000000 -0700
324@@ -1419,7 +1419,7 @@
325        struct sc_asn1_entry    asn1_pem_key[2],
326                                asn1_pem_key_items[3];
327        struct sc_algorithm_id algorithm;
328-       int key_len;
329+       size_t key_len;
330 
331        memset(&algorithm, 0, sizeof(algorithm));
332        algorithm.algorithm = alg_id;