removed old flag
[gnutls.git] / tests / rsa-encrypt-decrypt.c
blob376736f0da80d49d53db1803e986ff037ecc612b
1 /*
2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos, Simon Josefsson
6 * This file is part of GnuTLS.
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.
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.
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
23 /* Parts copied from GnuTLS example programs. */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
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 #include <arpa/inet.h>
36 #include <unistd.h>
37 #include <gnutls/gnutls.h>
38 #include <gnutls/x509.h>
39 #include <gnutls/abstract.h>
41 #include "utils.h"
43 /* sha1 hash of "hello" string */
44 const gnutls_datum_t hash_data = {
45 (void *)
46 "\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe"
47 "\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
51 const gnutls_datum_t raw_data = {
52 (void *) "hello there",
56 static char pem1_cert[] =
57 "-----BEGIN CERTIFICATE-----\n"
58 "MIICHjCCAYmgAwIBAgIERiYdNzALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
59 "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTI3WhcNMDgwNDE3MTMyOTI3WjAdMRsw\n"
60 "GQYDVQQDExJHbnVUTFMgdGVzdCBjbGllbnQwgZwwCwYJKoZIhvcNAQEBA4GMADCB\n"
61 "iAKBgLtmQ/Xyxde2jMzF3/WIO7HJS2oOoa0gUEAIgKFPXKPQ+GzP5jz37AR2ExeL\n"
62 "ZIkiW8DdU3w77XwEu4C5KL6Om8aOoKUSy/VXHqLnu7czSZ/ju0quak1o/8kR4jKN\n"
63 "zj2AC41179gAgY8oBAOgIo1hBAf6tjd9IQdJ0glhaZiQo1ipAgMBAAGjdjB0MAwG\n"
64 "A1UdEwEB/wQCMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDwYDVR0PAQH/BAUDAweg\n"
65 "ADAdBgNVHQ4EFgQUTLkKm/odNON+3svSBxX+odrLaJEwHwYDVR0jBBgwFoAU6Twc\n"
66 "+62SbuYGpFYsouHAUyfI8pUwCwYJKoZIhvcNAQEFA4GBALujmBJVZnvaTXr9cFRJ\n"
67 "jpfc/3X7sLUsMvumcDE01ls/cG5mIatmiyEU9qI3jbgUf82z23ON/acwJf875D3/\n"
68 "U7jyOsBJ44SEQITbin2yUeJMIm1tievvdNXBDfW95AM507ShzP12sfiJkJfjjdhy\n"
69 "dc8Siq5JojruiMizAf0pA7in\n" "-----END CERTIFICATE-----\n";
71 static char pem1_key[] =
72 "-----BEGIN RSA PRIVATE KEY-----\n"
73 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
74 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
75 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
76 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
77 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
78 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
79 "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
80 "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
81 "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
82 "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
83 "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
84 "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
85 "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
86 "-----END RSA PRIVATE KEY-----\n";
88 const gnutls_datum_t cert_dat[] = {
89 {(void*)pem1_cert, sizeof (pem1_cert)}
92 const gnutls_datum_t key_dat[] = {
93 {(void*)pem1_key, sizeof (pem1_key)}
96 void
97 doit (void)
99 gnutls_x509_privkey_t key;
100 gnutls_x509_crt_t crt;
101 gnutls_pubkey_t pubkey;
102 gnutls_privkey_t privkey;
103 gnutls_datum_t out, out2;
104 int ret;
105 size_t i;
107 gnutls_global_init ();
109 for (i = 0; i < sizeof (key_dat) / sizeof (key_dat[0]); i++)
111 if (debug)
112 success ("loop %d\n", (int) i);
114 ret = gnutls_x509_privkey_init (&key);
115 if (ret < 0)
116 fail ("gnutls_x509_privkey_init\n");
118 ret =
119 gnutls_x509_privkey_import (key, &key_dat[i], GNUTLS_X509_FMT_PEM);
120 if (ret < 0)
121 fail ("gnutls_x509_privkey_import\n");
123 ret = gnutls_pubkey_init (&pubkey);
124 if (ret < 0)
125 fail ("gnutls_privkey_init\n");
127 ret = gnutls_privkey_init (&privkey);
128 if (ret < 0)
129 fail ("gnutls_pubkey_init\n");
131 ret = gnutls_privkey_import_x509 (privkey, key, 0);
132 if (ret < 0)
133 fail ("gnutls_privkey_import_x509\n");
135 ret = gnutls_x509_crt_init (&crt);
136 if (ret < 0)
137 fail ("gnutls_x509_crt_init\n");
139 ret = gnutls_x509_crt_import (crt, &cert_dat[i], GNUTLS_X509_FMT_PEM);
140 if (ret < 0)
141 fail ("gnutls_x509_crt_import\n");
143 ret =
144 gnutls_pubkey_import_x509 (pubkey, crt, 0);
145 if (ret < 0)
146 fail ("gnutls_x509_pubkey_import\n");
149 ret = gnutls_pubkey_encrypt_data(pubkey, 0, &hash_data, &out);
150 if (ret < 0)
151 fail ("gnutls_pubkey_encrypt_data\n");
154 ret = gnutls_privkey_decrypt_data (privkey, 0,
155 &out, &out2);
156 if (ret < 0)
157 fail ("gnutls_privkey_decrypt_data\n");
159 if (out2.size != hash_data.size)
160 fail ("Decrypted data don't match original (1)\n");
162 if (memcmp(out2.data, hash_data.data, hash_data.size) != 0)
163 fail ("Decrypted data don't match original (2)\n");
165 gnutls_free(out.data);
166 gnutls_free(out2.data);
168 ret = gnutls_pubkey_encrypt_data(pubkey, 0, &raw_data, &out);
169 if (ret < 0)
170 fail ("gnutls_pubkey_encrypt_data\n");
172 ret = gnutls_privkey_decrypt_data (privkey, 0,
173 &out, &out2);
174 if (ret < 0)
175 fail ("gnutls_privkey_decrypt_data\n");
177 if (out2.size != raw_data.size)
178 fail ("Decrypted data don't match original (3)\n");
180 if (memcmp(out2.data, raw_data.data, raw_data.size) != 0)
181 fail ("Decrypted data don't match original (4)\n");
183 if (debug) success("ok\n");
185 gnutls_free(out.data);
186 gnutls_free(out2.data);
187 gnutls_x509_privkey_deinit (key);
188 gnutls_x509_crt_deinit (crt);
189 gnutls_privkey_deinit (privkey);
190 gnutls_pubkey_deinit (pubkey);
193 gnutls_global_deinit ();