Revert "Imported Upstream version 3.4.11"
[platform/upstream/gnutls.git] / tests / rsa-encrypt-decrypt.c
1 /*
2  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos, 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 /* Parts copied from GnuTLS example programs. */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
35 #ifndef _WIN32
36 # include <arpa/inet.h>
37 #endif
38 #include <unistd.h>
39 #include <gnutls/gnutls.h>
40 #include <gnutls/x509.h>
41 #include <gnutls/abstract.h>
42
43 #include "utils.h"
44
45 /* sha1 hash of "hello" string */
46 const gnutls_datum_t hash_data = {
47         (void *)
48             "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe"
49             "\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
50         20
51 };
52
53 const gnutls_datum_t raw_data = {
54         (void *) "hello there",
55         11
56 };
57
58 static char pem1_cert[] =
59     "-----BEGIN CERTIFICATE-----\n"
60     "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
61     "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
62     "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
63     "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
64     "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
65     "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
66     "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
67     "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
68     "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
69     "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
70     "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
71     "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
72
73 static char pem1_key[] =
74     "-----BEGIN RSA PRIVATE KEY-----\n"
75     "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
76     "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
77     "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
78     "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
79     "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
80     "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
81     "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
82     "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
83     "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
84     "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
85     "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
86     "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
87     "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
88     "-----END RSA PRIVATE KEY-----\n";
89
90 const gnutls_datum_t cert_dat[] = {
91         {(void *) pem1_cert, sizeof(pem1_cert)}
92 };
93
94 const gnutls_datum_t key_dat[] = {
95         {(void *) pem1_key, sizeof(pem1_key)}
96 };
97
98 void doit(void)
99 {
100         gnutls_x509_privkey_t key;
101         gnutls_x509_crt_t crt;
102         gnutls_pubkey_t pubkey;
103         gnutls_privkey_t privkey;
104         gnutls_datum_t out, out2;
105         int ret;
106         size_t i;
107
108         global_init();
109
110         for (i = 0; i < sizeof(key_dat) / sizeof(key_dat[0]); i++) {
111                 if (debug)
112                         success("loop %d\n", (int) i);
113
114                 ret = gnutls_x509_privkey_init(&key);
115                 if (ret < 0)
116                         fail("gnutls_x509_privkey_init\n");
117
118                 ret =
119                     gnutls_x509_privkey_import(key, &key_dat[i],
120                                                GNUTLS_X509_FMT_PEM);
121                 if (ret < 0)
122                         fail("gnutls_x509_privkey_import\n");
123
124                 ret = gnutls_pubkey_init(&pubkey);
125                 if (ret < 0)
126                         fail("gnutls_privkey_init\n");
127
128                 ret = gnutls_privkey_init(&privkey);
129                 if (ret < 0)
130                         fail("gnutls_pubkey_init\n");
131
132                 ret = gnutls_privkey_import_x509(privkey, key, 0);
133                 if (ret < 0)
134                         fail("gnutls_privkey_import_x509\n");
135
136                 ret = gnutls_x509_crt_init(&crt);
137                 if (ret < 0)
138                         fail("gnutls_x509_crt_init\n");
139
140                 ret =
141                     gnutls_x509_crt_import(crt, &cert_dat[i],
142                                            GNUTLS_X509_FMT_PEM);
143                 if (ret < 0)
144                         fail("gnutls_x509_crt_import\n");
145
146                 ret = gnutls_pubkey_import_x509(pubkey, crt, 0);
147                 if (ret < 0)
148                         fail("gnutls_x509_pubkey_import\n");
149
150
151                 ret =
152                     gnutls_pubkey_encrypt_data(pubkey, 0, &hash_data,
153                                                &out);
154                 if (ret < 0)
155                         fail("gnutls_pubkey_encrypt_data\n");
156
157
158                 ret = gnutls_privkey_decrypt_data(privkey, 0, &out, &out2);
159                 if (ret < 0)
160                         fail("gnutls_privkey_decrypt_data\n");
161
162                 if (out2.size != hash_data.size)
163                         fail("Decrypted data don't match original (1)\n");
164
165                 if (memcmp(out2.data, hash_data.data, hash_data.size) != 0)
166                         fail("Decrypted data don't match original (2)\n");
167
168                 gnutls_free(out.data);
169                 gnutls_free(out2.data);
170
171                 ret =
172                     gnutls_pubkey_encrypt_data(pubkey, 0, &raw_data, &out);
173                 if (ret < 0)
174                         fail("gnutls_pubkey_encrypt_data\n");
175
176                 ret = gnutls_privkey_decrypt_data(privkey, 0, &out, &out2);
177                 if (ret < 0)
178                         fail("gnutls_privkey_decrypt_data\n");
179
180                 if (out2.size != raw_data.size)
181                         fail("Decrypted data don't match original (3)\n");
182
183                 if (memcmp(out2.data, raw_data.data, raw_data.size) != 0)
184                         fail("Decrypted data don't match original (4)\n");
185
186                 if (debug)
187                         success("ok\n");
188
189                 gnutls_free(out.data);
190                 gnutls_free(out2.data);
191                 gnutls_x509_privkey_deinit(key);
192                 gnutls_x509_crt_deinit(crt);
193                 gnutls_privkey_deinit(privkey);
194                 gnutls_pubkey_deinit(pubkey);
195         }
196
197         gnutls_global_deinit();
198 }