Tizen 2.0 Release
[external/libgnutls26.git] / tests / cve-2009-1416.c
1 /*
2  * Copyright (C) 2009, 2010, 2011 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
45 #include <gnutls/gnutls.h>
46 #include <gnutls/x509.h>
47
48 int
49 main (void)
50 {
51   gnutls_x509_privkey_t key;
52   gnutls_datum_t p, q, g, y, x;
53   int ret;
54
55   gnutls_global_init ();
56
57   ret = gnutls_x509_privkey_init (&key);
58   if (ret < 0)
59     return 1;
60
61   ret = gnutls_x509_privkey_generate (key, GNUTLS_PK_DSA, 512, 0);
62   if (ret < 0)
63     return 1;
64
65   ret = gnutls_x509_privkey_export_dsa_raw (key, &p, &q, &g, &y, &x);
66   if (ret < 0)
67     return 1;
68
69   if (q.size == 3 && memcmp (q.data, "\x01\x00\x01", 3) == 0)
70     {
71       printf ("buggy\n");
72       return 1;
73     }
74   else
75     printf ("success!\n");
76
77   gnutls_free (p.data);
78   gnutls_free (q.data);
79   gnutls_free (g.data);
80   gnutls_free (y.data);
81   gnutls_free (x.data);
82
83   gnutls_x509_privkey_deinit (key);
84   gnutls_global_deinit ();
85
86   return 0;
87 }