asn1_dsa_internal_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/bn.h>
  12. #include "crypto/asn1_dsa.h"
  13. #include "testutil.h"
  14. static unsigned char t_dsa_sig[] = {
  15. 0x30, 0x06, /* SEQUENCE tag + length */
  16. 0x02, 0x01, 0x01, /* INTEGER tag + length + content */
  17. 0x02, 0x01, 0x02 /* INTEGER tag + length + content */
  18. };
  19. static unsigned char t_dsa_sig_extra[] = {
  20. 0x30, 0x06, /* SEQUENCE tag + length */
  21. 0x02, 0x01, 0x01, /* INTEGER tag + length + content */
  22. 0x02, 0x01, 0x02, /* INTEGER tag + length + content */
  23. 0x05, 0x00 /* NULL tag + length */
  24. };
  25. static unsigned char t_dsa_sig_msb[] = {
  26. 0x30, 0x08, /* SEQUENCE tag + length */
  27. 0x02, 0x02, 0x00, 0x81, /* INTEGER tag + length + content */
  28. 0x02, 0x02, 0x00, 0x82 /* INTEGER tag + length + content */
  29. };
  30. static unsigned char t_dsa_sig_two[] = {
  31. 0x30, 0x08, /* SEQUENCE tag + length */
  32. 0x02, 0x02, 0x01, 0x00, /* INTEGER tag + length + content */
  33. 0x02, 0x02, 0x02, 0x00 /* INTEGER tag + length + content */
  34. };
  35. /*
  36. * Badly coded ASN.1 INTEGER zero wrapped in a sequence along with another
  37. * (valid) INTEGER.
  38. */
  39. static unsigned char t_invalid_int_zero[] = {
  40. 0x30, 0x05, /* SEQUENCE tag + length */
  41. 0x02, 0x00, /* INTEGER tag + length */
  42. 0x02, 0x01, 0x2a /* INTEGER tag + length */
  43. };
  44. /*
  45. * Badly coded ASN.1 INTEGER (with leading zeros) wrapped in a sequence along
  46. * with another (valid) INTEGER.
  47. */
  48. static unsigned char t_invalid_int[] = {
  49. 0x30, 0x07, /* SEQUENCE tag + length */
  50. 0x02, 0x02, 0x00, 0x7f, /* INTEGER tag + length */
  51. 0x02, 0x01, 0x2a /* INTEGER tag + length */
  52. };
  53. /*
  54. * Negative ASN.1 INTEGER wrapped in a sequence along with another
  55. * (valid) INTEGER.
  56. */
  57. static unsigned char t_neg_int[] = {
  58. 0x30, 0x06, /* SEQUENCE tag + length */
  59. 0x02, 0x01, 0xaa, /* INTEGER tag + length */
  60. 0x02, 0x01, 0x2a /* INTEGER tag + length */
  61. };
  62. static unsigned char t_trunc_der[] = {
  63. 0x30, 0x08, /* SEQUENCE tag + length */
  64. 0x02, 0x02, 0x00, 0x81, /* INTEGER tag + length */
  65. 0x02, 0x02, 0x00 /* INTEGER tag + length */
  66. };
  67. static unsigned char t_trunc_seq[] = {
  68. 0x30, 0x07, /* SEQUENCE tag + length */
  69. 0x02, 0x02, 0x00, 0x81, /* INTEGER tag + length */
  70. 0x02, 0x02, 0x00, 0x82 /* INTEGER tag + length */
  71. };
  72. static int test_decode(void)
  73. {
  74. int rv = 0;
  75. BIGNUM *r;
  76. BIGNUM *s;
  77. const unsigned char *pder;
  78. r = BN_new();
  79. s = BN_new();
  80. /* Positive tests */
  81. pder = t_dsa_sig;
  82. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig)) == 0
  83. || !TEST_ptr_eq(pder, (t_dsa_sig + sizeof(t_dsa_sig)))
  84. || !TEST_BN_eq_word(r, 1) || !TEST_BN_eq_word(s, 2)) {
  85. TEST_info("asn1_dsa test_decode: t_dsa_sig failed");
  86. goto fail;
  87. }
  88. BN_clear(r);
  89. BN_clear(s);
  90. pder = t_dsa_sig_extra;
  91. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig_extra)) == 0
  92. || !TEST_ptr_eq(pder,
  93. (t_dsa_sig_extra + sizeof(t_dsa_sig_extra) - 2))
  94. || !TEST_BN_eq_word(r, 1) || !TEST_BN_eq_word(s, 2)) {
  95. TEST_info("asn1_dsa test_decode: t_dsa_sig_extra failed");
  96. goto fail;
  97. }
  98. BN_clear(r);
  99. BN_clear(s);
  100. pder = t_dsa_sig_msb;
  101. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig_msb)) == 0
  102. || !TEST_ptr_eq(pder, (t_dsa_sig_msb + sizeof(t_dsa_sig_msb)))
  103. || !TEST_BN_eq_word(r, 0x81) || !TEST_BN_eq_word(s, 0x82)) {
  104. TEST_info("asn1_dsa test_decode: t_dsa_sig_msb failed");
  105. goto fail;
  106. }
  107. BN_clear(r);
  108. BN_clear(s);
  109. pder = t_dsa_sig_two;
  110. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_dsa_sig_two)) == 0
  111. || !TEST_ptr_eq(pder, (t_dsa_sig_two + sizeof(t_dsa_sig_two)))
  112. || !TEST_BN_eq_word(r, 0x100) || !TEST_BN_eq_word(s, 0x200)) {
  113. TEST_info("asn1_dsa test_decode: t_dsa_sig_two failed");
  114. goto fail;
  115. }
  116. /* Negative tests */
  117. pder = t_invalid_int_zero;
  118. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_invalid_int_zero)) != 0) {
  119. TEST_info("asn1_dsa test_decode: Expected t_invalid_int_zero to fail");
  120. goto fail;
  121. }
  122. BN_clear(r);
  123. BN_clear(s);
  124. pder = t_invalid_int;
  125. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_invalid_int)) != 0) {
  126. TEST_info("asn1_dsa test_decode: Expected t_invalid_int to fail");
  127. goto fail;
  128. }
  129. BN_clear(r);
  130. BN_clear(s);
  131. pder = t_neg_int;
  132. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_neg_int)) != 0) {
  133. TEST_info("asn1_dsa test_decode: Expected t_neg_int to fail");
  134. goto fail;
  135. }
  136. BN_clear(r);
  137. BN_clear(s);
  138. pder = t_trunc_der;
  139. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_trunc_der)) != 0) {
  140. TEST_info("asn1_dsa test_decode: Expected fail t_trunc_der");
  141. goto fail;
  142. }
  143. BN_clear(r);
  144. BN_clear(s);
  145. pder = t_trunc_seq;
  146. if (ossl_decode_der_dsa_sig(r, s, &pder, sizeof(t_trunc_seq)) != 0) {
  147. TEST_info("asn1_dsa test_decode: Expected fail t_trunc_seq");
  148. goto fail;
  149. }
  150. rv = 1;
  151. fail:
  152. BN_free(r);
  153. BN_free(s);
  154. return rv;
  155. }
  156. int setup_tests(void)
  157. {
  158. ADD_TEST(test_decode);
  159. return 1;
  160. }