Tizen 2.0 Release
[external/libgnutls26.git] / tests / cve-2009-1415.c
1 /*
2  * Copyright (C) 2009, 2010 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-1415 double-free problem.
29  *
30  * Build it using:
31  *
32  *  gcc -o cve-2009-1415 cve-2009-1415.c -lgnutls
33  *
34  * If your gnutls library is OK then running it will just print 'success!'.
35  *
36  * If your gnutls library is buggy, then running it will crash like this:
37  *
38  * ** glibc detected *** ./cve-2009-1415: munmap_chunk(): invalid pointer: 0xb7f80a9c ***
39  * ======= Backtrace: =========
40  * ...
41  */
42
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46
47 #include <gnutls/gnutls.h>
48 #include <gnutls/x509.h>
49 #include <gnutls/abstract.h>
50
51 static char dsa_cert[] =
52   "-----BEGIN CERTIFICATE-----\n"
53   "MIIDbzCCAtqgAwIBAgIERiYdRTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
54   "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTQxWhcNMDgwNDE3MTMyOTQxWjA3MRsw\n"
55   "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
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dM4MJFB4W\n"
71   "QcDzQ8REwidsfh9uKAluk1c/KQ==\n" "-----END CERTIFICATE-----\n";
72
73 const gnutls_datum_t dsa_cert_dat = {
74   dsa_cert, sizeof (dsa_cert)
75 };
76
77 int
78 main (void)
79 {
80   gnutls_x509_crt_t crt;
81   gnutls_pubkey_t pubkey;
82   gnutls_datum_t data = { (char *) "foo", 3 };
83   gnutls_datum_t sig = { (char *) "bar", 3 };
84   int ret;
85
86   gnutls_global_init ();
87
88   ret = gnutls_x509_crt_init (&crt);
89   if (ret < 0)
90     return 1;
91
92   ret = gnutls_pubkey_init (&pubkey);
93   if (ret < 0)
94     return 1;
95
96   ret = gnutls_x509_crt_import (crt, &dsa_cert_dat, GNUTLS_X509_FMT_PEM);
97   if (ret < 0)
98     return 1;
99
100   ret = gnutls_pubkey_import_x509( pubkey, crt, 0);
101   if (ret < 0)
102     return 1;
103
104   ret = gnutls_pubkey_verify_data (pubkey, 0, &data, &sig);
105   if (ret < 0 && ret != GNUTLS_E_PK_SIG_VERIFY_FAILED)
106     return 1;
107
108   //printf ("success!\n");
109
110   gnutls_x509_crt_deinit (crt);
111   gnutls_pubkey_deinit( pubkey);
112   gnutls_global_deinit ();
113
114   return 0;
115 }