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