Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / cve-2009-1416.c
1 /*
2  * Copyright (C) 2009-2012 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 /*
28  * Small code to reproduce the CVE-2009-1416 bad DSA key problem.
29  *
30  * Build it using:
31  *
32  *  gcc -o cve-2009-1416 cve-2009-1416.c -lgnutls
33  *
34  * If your gnutls library is OK then running it will print 'success!'.
35  *
36  * If your gnutls library is buggy then running it will print 'buggy'.
37  *
38  */
39
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <utils.h>
45
46 #include <gnutls/gnutls.h>
47 #include <gnutls/x509.h>
48
49 int main(void)
50 {
51 #ifdef ENABLE_FIPS140
52         /* Cannot generate a 512-bit DSA key */
53         return 77;
54 #else
55         gnutls_x509_privkey_t key;
56         gnutls_datum_t p, q, g, y, x;
57         int ret;
58
59         global_init();
60
61         ret = gnutls_x509_privkey_init(&key);
62         if (ret < 0)
63                 return 1;
64
65         ret = gnutls_x509_privkey_generate(key, GNUTLS_PK_DSA, 512, 0);
66         if (ret < 0)
67                 return 1;
68
69         ret = gnutls_x509_privkey_export_dsa_raw(key, &p, &q, &g, &y, &x);
70         if (ret < 0)
71                 return 1;
72
73         if (q.size == 3 && memcmp(q.data, "\x01\x00\x01", 3) == 0) {
74                 printf("buggy\n");
75                 return 1;
76         }
77
78         gnutls_free(p.data);
79         gnutls_free(q.data);
80         gnutls_free(g.data);
81         gnutls_free(y.data);
82         gnutls_free(x.data);
83
84         gnutls_x509_privkey_deinit(key);
85         gnutls_global_deinit();
86
87         return 0;
88 #endif
89 }