Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / pkcs8-key-decode.c
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  *
4  * Author: Daniel Berrange
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 #include <gnutls/gnutls.h>
24 #include <gnutls/x509.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "utils.h"
30
31 # define PRIVATE_KEY                                          \
32     "-----BEGIN PRIVATE KEY-----\n"                             \
33     "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALVcr\n"     \
34     "BL40Tm6yq88FBhJNw1aaoCjmtg0l4dWQZ/e9Fimx4ARxFpT+ji4FE\n"     \
35     "Cgl9s/SGqC+1nvlkm9ViSo0j7MKDbnDB+VRHDvMAzQhA2X7e8M0n9\n"     \
36     "rPolUY2lIVC83q0BBaOBkCj2RSmT2xTEbbC2xLukSrg2WP/ihVOxc\n"     \
37     "kXRuyFtzAgMBAAECgYB7slBexDwXrtItAMIH6m/U+LUpNe0Xx48OL\n"     \
38     "IOn4a4whNgO/o84uIwygUK27ZGFZT0kAGAk8CdF9hA6ArcbQ62s1H\n"     \
39     "myxrUbF9/mrLsQw1NEqpuUk9Ay2Tx5U/wPx35S3W/X2AvR/ZpTnCn\n"     \
40     "2q/7ym9fyiSoj86drD7BTvmKXlOnOwQJBAPOFMp4mMa9NGpGuEssO\n"     \
41     "m3Uwbp6lhcP0cA9MK+iOmeANpoKWfBdk5O34VbmeXnGYWEkrnX+9J\n"     \
42     "bM4wVhnnBWtgBMCQQC+qAEmvwcfhauERKYznMVUVksyeuhxhCe7EK\n"     \
43     "mPh+U2+g0WwdKvGDgO0PPt1gq0ILEjspMDeMHVdTwkaVBo/uMhAkA\n"     \
44     "Z5SsZyCP2aTOPFDypXRdI4eqRcjaEPOUBq27r3uYb/jeboVb2weLa\n"     \
45     "L1MmVuHiIHoa5clswPdWVI2y0em2IGoDAkBPSp/v9VKJEZabk9Frd\n"     \
46     "a+7u4fanrM9QrEjY3KhduslSilXZZSxrWjjAJPyPiqFb3M8XXA26W\n"     \
47     "nz1KYGnqYKhLcBAkB7dt57n9xfrhDpuyVEv+Uv1D3VVAhZlsaZ5Pp\n"     \
48     "dcrhrkJn2sa/+O8OKvdrPSeeu/N5WwYhJf61+CPoenMp7IFci\n"         \
49     "-----END PRIVATE KEY-----\n"
50
51 static int test_load(void)
52 {
53   gnutls_x509_privkey_t key;
54   const gnutls_datum_t data = {
55     (unsigned char *)PRIVATE_KEY,
56     strlen(PRIVATE_KEY)
57   };
58   int err;
59
60   if ((err = gnutls_x509_privkey_init(&key)) < 0) {
61     fail("Failed to init key %s\n", gnutls_strerror(err));
62     exit(1);
63   }
64
65   if ((err = gnutls_x509_privkey_import(key, &data,
66                                         GNUTLS_X509_FMT_PEM)) < 0) {
67     fail("Failed to import key %s\n", gnutls_strerror(err));
68     exit(1);
69   }
70
71   success("Loaded key\n%s", PRIVATE_KEY);
72
73   gnutls_x509_privkey_deinit(key);
74   return 0;
75 }
76
77 void doit(void)
78 {
79         test_load();
80 }