Merge pull request #339 from libtom/minor_improvements
Minor improvements (cherry picked from commit df8ed5c76b66f5891c8eeb274769a3f8f7dad086)
This commit is contained in:
parent
f4d2b37cf4
commit
5501572b35
@ -31,7 +31,7 @@ enum public_key_algorithms {
|
|||||||
|
|
||||||
typedef struct Oid {
|
typedef struct Oid {
|
||||||
unsigned long OID[16];
|
unsigned long OID[16];
|
||||||
/** Length of DER encoding */
|
/** Number of OID digits in use */
|
||||||
unsigned long OIDlen;
|
unsigned long OIDlen;
|
||||||
} oid_st;
|
} oid_st;
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ int der_decode_object_identifier(const unsigned char *in, unsigned long inle
|
|||||||
unsigned long *words, unsigned long *outlen)
|
unsigned long *words, unsigned long *outlen)
|
||||||
{
|
{
|
||||||
unsigned long x, y, t, len;
|
unsigned long x, y, t, len;
|
||||||
|
int err;
|
||||||
|
|
||||||
LTC_ARGCHK(in != NULL);
|
LTC_ARGCHK(in != NULL);
|
||||||
LTC_ARGCHK(words != NULL);
|
LTC_ARGCHK(words != NULL);
|
||||||
@ -38,6 +39,7 @@ int der_decode_object_identifier(const unsigned char *in, unsigned long inle
|
|||||||
|
|
||||||
/* must be room for at least two words */
|
/* must be room for at least two words */
|
||||||
if (*outlen < 2) {
|
if (*outlen < 2) {
|
||||||
|
*outlen = 2;
|
||||||
return CRYPT_BUFFER_OVERFLOW;
|
return CRYPT_BUFFER_OVERFLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,21 +75,28 @@ int der_decode_object_identifier(const unsigned char *in, unsigned long inle
|
|||||||
if (!(in[x++] & 0x80)) {
|
if (!(in[x++] & 0x80)) {
|
||||||
/* store t */
|
/* store t */
|
||||||
if (y >= *outlen) {
|
if (y >= *outlen) {
|
||||||
return CRYPT_BUFFER_OVERFLOW;
|
y++;
|
||||||
}
|
|
||||||
if (y == 0) {
|
|
||||||
words[0] = t / 40;
|
|
||||||
words[1] = t % 40;
|
|
||||||
y = 2;
|
|
||||||
} else {
|
} else {
|
||||||
words[y++] = t;
|
if (y == 0) {
|
||||||
|
words[0] = t / 40;
|
||||||
|
words[1] = t % 40;
|
||||||
|
y = 2;
|
||||||
|
} else {
|
||||||
|
words[y++] = t;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
t = 0;
|
t = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (y > *outlen) {
|
||||||
|
err = CRYPT_BUFFER_OVERFLOW;
|
||||||
|
} else {
|
||||||
|
err = CRYPT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
*outlen = y;
|
*outlen = y;
|
||||||
return CRYPT_OK;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -29,6 +29,7 @@ int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
|||||||
{
|
{
|
||||||
wchar_t tmp;
|
wchar_t tmp;
|
||||||
unsigned long x, y, z, len;
|
unsigned long x, y, z, len;
|
||||||
|
int err;
|
||||||
|
|
||||||
LTC_ARGCHK(in != NULL);
|
LTC_ARGCHK(in != NULL);
|
||||||
LTC_ARGCHK(out != NULL);
|
LTC_ARGCHK(out != NULL);
|
||||||
@ -91,15 +92,19 @@ int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
|||||||
tmp = (tmp << 6) | ((wchar_t)in[x++] & 0x3F);
|
tmp = (tmp << 6) | ((wchar_t)in[x++] & 0x3F);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (y > *outlen) {
|
if (y < *outlen) {
|
||||||
*outlen = y;
|
out[y] = tmp;
|
||||||
return CRYPT_BUFFER_OVERFLOW;
|
|
||||||
}
|
}
|
||||||
out[y++] = tmp;
|
y++;
|
||||||
|
}
|
||||||
|
if (y > *outlen) {
|
||||||
|
err = CRYPT_BUFFER_OVERFLOW;
|
||||||
|
} else {
|
||||||
|
err = CRYPT_OK;
|
||||||
}
|
}
|
||||||
*outlen = y;
|
*outlen = y;
|
||||||
|
|
||||||
return CRYPT_OK;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -53,7 +53,7 @@ int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
|||||||
|
|
||||||
/* too big? */
|
/* too big? */
|
||||||
if (y > *outlen) {
|
if (y > *outlen) {
|
||||||
*outlen = len;
|
*outlen = y;
|
||||||
return CRYPT_BUFFER_OVERFLOW;
|
return CRYPT_BUFFER_OVERFLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,7 +286,7 @@ static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
|
|||||||
for (n = 0; n < l->size; ++n) {
|
for (n = 0; n < l->size; ++n) {
|
||||||
r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
|
r = snprintf(s, sz, "%02X", ((unsigned char*)l->data)[n]);
|
||||||
if (r < 0 || r >= sz) {
|
if (r < 0 || r >= sz) {
|
||||||
printf("Octet string boom");
|
fprintf(stderr, "%s boom\n", name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
s += r;
|
s += r;
|
||||||
@ -310,7 +310,7 @@ static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
|
|||||||
for (i = 0; i < l->size; ++i) {
|
for (i = 0; i < l->size; ++i) {
|
||||||
r = snprintf(s, sz, "%lu.", ((unsigned long*)l->data)[i]);
|
r = snprintf(s, sz, "%lu.", ((unsigned long*)l->data)[i]);
|
||||||
if (r < 0 || r >= sz) {
|
if (r < 0 || r >= sz) {
|
||||||
printf("OID boom");
|
fprintf(stderr, "%s boom\n", name);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
s += r;
|
s += r;
|
||||||
@ -413,16 +413,16 @@ static void _der_tests_print_flexi(ltc_asn1_list* l, unsigned int level)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (n = 0; n < level; ++n) {
|
for (n = 0; n < level; ++n) {
|
||||||
printf(" ");
|
fprintf(stderr, " ");
|
||||||
}
|
}
|
||||||
if (name) {
|
if (name) {
|
||||||
if (text)
|
if (text)
|
||||||
printf("%s %s\n", name, text);
|
fprintf(stderr, "%s %s\n", name, text);
|
||||||
else
|
else
|
||||||
printf("%s <missing decoding>\n", name);
|
fprintf(stderr, "%s <missing decoding>\n", name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
printf("WTF type=%i\n", l->type);
|
fprintf(stderr, "WTF type=%i\n", l->type);
|
||||||
|
|
||||||
if (ostring) {
|
if (ostring) {
|
||||||
_der_tests_print_flexi(ostring, level + 1);
|
_der_tests_print_flexi(ostring, level + 1);
|
||||||
|
@ -143,14 +143,14 @@ static int _dsa_compat_test(void)
|
|||||||
x = sizeof(tmp);
|
x = sizeof(tmp);
|
||||||
DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
|
DO(dsa_export(tmp, &x, PK_PRIVATE | PK_STD, &key));
|
||||||
if (compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
|
if (compare_testvector(tmp, x, openssl_priv_dsa, sizeof(openssl_priv_dsa),
|
||||||
"DSA private export failed from dsa_import(priv_key)\n", 0)) {
|
"DSA private export failed from dsa_import(priv_key)\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
x = sizeof(tmp);
|
x = sizeof(tmp);
|
||||||
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
|
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
|
||||||
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
||||||
"DSA public export failed from dsa_import(priv_key)\n", 0)) {
|
"DSA public export failed from dsa_import(priv_key)\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
dsa_free(&key);
|
dsa_free(&key);
|
||||||
@ -160,7 +160,7 @@ static int _dsa_compat_test(void)
|
|||||||
x = sizeof(tmp);
|
x = sizeof(tmp);
|
||||||
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
|
DO(dsa_export(tmp, &x, PK_PUBLIC | PK_STD, &key));
|
||||||
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
if (compare_testvector(tmp, x, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
||||||
"DSA public export failed from dsa_import(pub_key)\n", 0)) {
|
"DSA public export failed from dsa_import(pub_key)\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
dsa_free(&key);
|
dsa_free(&key);
|
||||||
@ -185,7 +185,7 @@ static int _dsa_compat_test(void)
|
|||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
|
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
|
||||||
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
|
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
|
||||||
"DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", 0)) {
|
"DSA private export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
dsa_free(&key);
|
dsa_free(&key);
|
||||||
@ -201,7 +201,7 @@ static int _dsa_compat_test(void)
|
|||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
|
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
|
||||||
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
||||||
"DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", 0)) {
|
"DSA public export failed from dsa_set_pqg() & dsa_set_key()\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
dsa_free(&key);
|
dsa_free(&key);
|
||||||
@ -225,7 +225,7 @@ static int _dsa_compat_test(void)
|
|||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
|
DO(dsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
|
||||||
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
if (compare_testvector(buf, len, openssl_pub_dsa, sizeof(openssl_pub_dsa),
|
||||||
"DSA public export failed from dsa_set_pqg_dsaparam()\n", 0)) {
|
"DSA public export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
dsa_free(&key);
|
dsa_free(&key);
|
||||||
@ -238,7 +238,7 @@ static int _dsa_compat_test(void)
|
|||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
|
DO(dsa_export(buf, &len, PK_PRIVATE | PK_STD, &key));
|
||||||
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
|
if (compare_testvector(buf, len, openssl_priv_dsa, sizeof(openssl_priv_dsa),
|
||||||
"DSA private export failed from dsa_set_pqg_dsaparam()\n", 0)) {
|
"DSA private export failed from dsa_set_pqg_dsaparam()\n", __LINE__)) {
|
||||||
return CRYPT_FAIL_TESTVECTOR;
|
return CRYPT_FAIL_TESTVECTOR;
|
||||||
}
|
}
|
||||||
dsa_free(&key);
|
dsa_free(&key);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user