Skip to content

Commit 5501572

Browse files
committed
Merge pull request #339 from libtom/minor_improvements
Minor improvements (cherry picked from commit df8ed5c)
1 parent f4d2b37 commit 5501572

6 files changed

Lines changed: 43 additions & 29 deletions

File tree

src/headers/tomcrypt_pk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum public_key_algorithms {
3131

3232
typedef struct Oid {
3333
unsigned long OID[16];
34-
/** Length of DER encoding */
34+
/** Number of OID digits in use */
3535
unsigned long OIDlen;
3636
} oid_st;
3737

src/pk/asn1/der/object_identifier/der_decode_object_identifier.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int der_decode_object_identifier(const unsigned char *in, unsigned long inle
2626
unsigned long *words, unsigned long *outlen)
2727
{
2828
unsigned long x, y, t, len;
29+
int err;
2930

3031
LTC_ARGCHK(in != NULL);
3132
LTC_ARGCHK(words != NULL);
@@ -38,6 +39,7 @@ int der_decode_object_identifier(const unsigned char *in, unsigned long inle
3839

3940
/* must be room for at least two words */
4041
if (*outlen < 2) {
42+
*outlen = 2;
4143
return CRYPT_BUFFER_OVERFLOW;
4244
}
4345

@@ -73,21 +75,28 @@ int der_decode_object_identifier(const unsigned char *in, unsigned long inle
7375
if (!(in[x++] & 0x80)) {
7476
/* store t */
7577
if (y >= *outlen) {
76-
return CRYPT_BUFFER_OVERFLOW;
77-
}
78-
if (y == 0) {
79-
words[0] = t / 40;
80-
words[1] = t % 40;
81-
y = 2;
78+
y++;
8279
} else {
83-
words[y++] = t;
80+
if (y == 0) {
81+
words[0] = t / 40;
82+
words[1] = t % 40;
83+
y = 2;
84+
} else {
85+
words[y++] = t;
86+
}
8487
}
85-
t = 0;
88+
t = 0;
8689
}
8790
}
8891

92+
if (y > *outlen) {
93+
err = CRYPT_BUFFER_OVERFLOW;
94+
} else {
95+
err = CRYPT_OK;
96+
}
97+
8998
*outlen = y;
90-
return CRYPT_OK;
99+
return err;
91100
}
92101

93102
#endif

src/pk/asn1/der/utf8/der_decode_utf8_string.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
2929
{
3030
wchar_t tmp;
3131
unsigned long x, y, z, len;
32+
int err;
3233

3334
LTC_ARGCHK(in != NULL);
3435
LTC_ARGCHK(out != NULL);
@@ -91,15 +92,19 @@ int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
9192
tmp = (tmp << 6) | ((wchar_t)in[x++] & 0x3F);
9293
}
9394

94-
if (y > *outlen) {
95-
*outlen = y;
96-
return CRYPT_BUFFER_OVERFLOW;
95+
if (y < *outlen) {
96+
out[y] = tmp;
9797
}
98-
out[y++] = tmp;
98+
y++;
99+
}
100+
if (y > *outlen) {
101+
err = CRYPT_BUFFER_OVERFLOW;
102+
} else {
103+
err = CRYPT_OK;
99104
}
100105
*outlen = y;
101106

102-
return CRYPT_OK;
107+
return err;
103108
}
104109

105110
#endif

src/pk/asn1/der/utf8/der_encode_utf8_string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
5353

5454
/* too big? */
5555
if (y > *outlen) {
56-
*outlen = len;
56+
*outlen = y;
5757
return CRYPT_BUFFER_OVERFLOW;
5858
}
5959

tests/der_test.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
286286
for (n = 0; n < l->size; ++n) {
287287
r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
288288
if (r < 0 || r >= sz) {
289-
printf("Octet string boom");
289+
fprintf(stderr, "%s boom\n", name);
290290
exit(EXIT_FAILURE);
291291
}
292292
s += r;
@@ -310,7 +310,7 @@ static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
310310
for (i = 0; i < l->size; ++i) {
311311
r = snprintf(s, sz, "%lu.", ((unsigned long*)l->data)[i]);
312312
if (r < 0 || r >= sz) {
313-
printf("OID boom");
313+
fprintf(stderr, "%s boom\n", name);
314314
exit(EXIT_FAILURE);
315315
}
316316
s += r;
@@ -413,16 +413,16 @@ static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
413413
}
414414

415415
for (n = 0; n < level; ++n) {
416-
printf(" ");
416+
fprintf(stderr, " ");
417417
}
418418
if (name) {
419419
if (text)
420-
printf("%s %s\n", name, text);
420+
fprintf(stderr, "%s %s\n", name, text);
421421
else
422-
printf("%s <missing decoding>\n", name);
422+
fprintf(stderr, "%s <missing decoding>\n", name);
423423
}
424424
else
425-
printf("WTF type=%i\n", l->type);
425+
fprintf(stderr, "WTF type=%i\n", l->type);
426426

427427
if (ostring) {
428428
_der_tests_print_flexi(ostring, level + 1);

tests/dsa_test.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ static int _dsa_compat_test(void)
143143
x = sizeof(tmp);
144144
DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
145145
if (compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
146-
"DSA private export failed from dsa_import(priv_key)\n", 0)) {
146+
"DSA private export failed from dsa_import(priv_key)\n", __LINE__)) {
147147
return CRYPT_FAIL_TESTVECTOR;
148148
}
149149

150150
x = sizeof(tmp);
151151
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
152152
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
153-
"DSA public export failed from dsa_import(priv_key)\n", 0)) {
153+
"DSA public export failed from dsa_import(priv_key)\n", __LINE__)) {
154154
return CRYPT_FAIL_TESTVECTOR;
155155
}
156156
dsa_free(&key);
@@ -160,7 +160,7 @@ static int _dsa_compat_test(void)
160160
x = sizeof(tmp);
161161
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
162162
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
163-
"DSA public export failed from dsa_import(pub_key)\n", 0)) {
163+
"DSA public export failed from dsa_import(pub_key)\n", __LINE__)) {
164164
return CRYPT_FAIL_TESTVECTOR;
165165
}
166166
dsa_free(&key);
@@ -185,7 +185,7 @@ static int _dsa_compat_test(void)
185185
len = sizeof(buf);
186186
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
187187
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
188-
"DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", 0)) {
188+
"DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
189189
return CRYPT_FAIL_TESTVECTOR;
190190
}
191191
dsa_free(&key);
@@ -201,7 +201,7 @@ static int _dsa_compat_test(void)
201201
len = sizeof(buf);
202202
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
203203
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
204-
"DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", 0)) {
204+
"DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
205205
return CRYPT_FAIL_TESTVECTOR;
206206
}
207207
dsa_free(&key);
@@ -225,7 +225,7 @@ static int _dsa_compat_test(void)
225225
len = sizeof(buf);
226226
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
227227
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
228-
"DSA public export failed from dsa_set_pqg_dsaparam()\n", 0)) {
228+
"DSA public export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
229229
return CRYPT_FAIL_TESTVECTOR;
230230
}
231231
dsa_free(&key);
@@ -238,7 +238,7 @@ static int _dsa_compat_test(void)
238238
len = sizeof(buf);
239239
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
240240
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
241-
"DSA private export failed from dsa_set_pqg_dsaparam()\n", 0)) {
241+
"DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
242242
return CRYPT_FAIL_TESTVECTOR;
243243
}
244244
dsa_free(&key);

0 commit comments

Comments
 (0)