1 /*
2  * Copyright (C) 2015 Nikos Mavrogiannopoulos
3  *
4  * This file is part of GnuTLS.
5  *
6  * GnuTLS is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuTLS is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GnuTLS; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <utils.h>
30 #include <assert.h>
31 
32 #include <gnutls/gnutls.h>
33 #include <gnutls/x509.h>
34 #include <gnutls/abstract.h>
35 
36 #include "cert-common.h"
37 
38 static char rsa_key_pem[] =
39     "-----BEGIN RSA PRIVATE KEY-----\n"
40     "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
41     "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
42     "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
43     "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
44     "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
45     "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
46     "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
47     "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
48     "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
49     "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
50     "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
51     "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
52     "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
53     "-----END RSA PRIVATE KEY-----\n";
54 
55 const gnutls_datum_t rsa_key = { (void*)rsa_key_pem,
56 	sizeof(rsa_key_pem)
57 };
58 
dump(const char * name,unsigned char * buf,int buf_size)59 static void dump(const char *name, unsigned char *buf, int buf_size)
60 {
61 	int i;
62 	fprintf(stderr, "%s: ", name);
63 	for (i = 0; i < buf_size; i++)
64 		fprintf(stderr, "\\x%.2x", buf[i]);
65 	fprintf(stderr, "\n");
66 }
67 
68 unsigned char dsa_p[] = "\x00\xb9\x84\xf5\x5a\x81\xbe\x1a\x0d\xc5\x8a\x73\x8f\x0c\x9b\x2f\x9b\xb6\x0e\x4b\xc3\x74\x1a\x7f\x64\xad\x9d\xf3\x28\xc5\xa0\x47\xbc\x9b\x57\x56\xf1\x97\xd5\x7e\x37\x03\xe9\xf2\x4c\xf4\xe3\x8b\x7f\x30\xa3\x5d\x2f\xbb\xa1\xa2\x37\xc2\xea\x35\x8f\x1f\xb1\x5f\xa6\xa2\x5f\x01\xf1\x23\x36\x2b\xe4\x4f\x2f\x2d\xdd\x9d\xd5\x3a\xa6\x39\xaf\x7a\x51\x7c\xd2\x25\x8e\x97\x74\xcf\x1e\xc5\x7b\x4b\x76\x43\x81\x07\x1f\x06\x14\xb8\x6e\x58\x12\xe1\x90\xe2\x37\x6f\xd2\x1b\xec\x68\xc5\x58\xe2\xe6\x30\xe0\x6a\x5e\x2c\x63\x78\xec\x07";
69 unsigned char dsa_q[] = "\x00\x9f\x56\x8c\x48\x64\x2f\xfe\x8d\xaa\x7a\x6d\x96\xdb\x04\x5d\x16\xef\x08\xa5\x71";
70 unsigned char dsa_g[] = "\x62\x06\x7e\xe4\x5c\x76\x08\xb7\x46\x1a\x5d\xd7\x97\xd4\x2a\x21\xfb\x1f\x31\xc9\xd2\xf4\xfa\x39\xd8\x27\xd1\x9b\xfc\x27\x5d\xa7\x0a\xa7\x1a\xfc\x53\xc1\x2f\x43\xc2\x37\xc8\x85\x7f\x3d\x4c\xab\x5d\x81\x32\xfb\x1d\x5e\x1e\x54\x11\x16\x20\xc6\x80\x5a\xd9\x8c\x9b\x43\xf0\xdd\x6b\xa0\xf4\xc3\xf2\x8a\x9c\x39\xd2\x1c\x7b\x0f\xef\xfa\x28\x93\x8f\xd2\xa1\x22\xeb\xdc\xe0\x8a\x8b\xad\x28\x0e\xcf\xef\x09\x85\xe9\x36\xbd\x8b\x7a\x50\xd5\x7b\xf7\x25\x0d\x6c\x60\x11\xc4\xef\x70\x90\xcf\xd6\x1b\xeb\xbb\x8e\xc6\x3e\x3a\x97";
71 unsigned char dsa_y[] = "\x0f\x8a\x87\x57\xf2\xd1\xc2\xdc\xac\xdf\x4b\x8b\x0f\x8b\xba\x29\xf7\xe1\x03\xe4\x55\xfa\xb2\x98\x07\xd6\xfd\x12\xb1\x80\xbc\xf5\xba\xb4\x50\xd4\x7f\xa0\x0e\x43\xe7\x9f\xc9\x78\x11\x5f\xe5\xe4\x0c\x2c\x6b\x6a\xa4\x35\xdc\xbd\x54\xe5\x60\x36\x9a\x31\xd1\x8a\x59\x6e\x6b\x1c\xba\xbd\x2e\xba\xeb\x7c\x87\xef\xda\xc8\xdd\xa1\xeb\xa4\x83\xe6\x8b\xad\xfa\xfa\x8e\x5b\xd7\x37\xc8\x32\x3e\x96\xc2\x3e\xf4\x43\xda\x7d\x91\x02\x0f\xb7\xbc\xf8\xef\x8f\xf7\x41\x00\x5e\x96\xdf\x0f\x08\x96\xdc\xea\xb2\xe9\x06\x82\xaf\xd2\x2f";
72 unsigned char dsa_x[] = "\x4b\x9f\xeb\xff\x6c\x9a\x02\x83\x41\x5e\x37\x81\x8e\x00\x86\x31\xe8\xb6\x9b\xc1";
73 
74 unsigned char rsa_m[] = "\x00\xbb\x66\x43\xf5\xf2\xc5\xd7\xb6\x8c\xcc\xc5\xdf\xf5\x88\x3b\xb1\xc9\x4b\x6a\x0e\xa1\xad\x20\x50\x40\x08\x80\xa1\x4f\x5c\xa3\xd0\xf8\x6c\xcf\xe6\x3c\xf7\xec\x04\x76\x13\x17\x8b\x64\x89\x22\x5b\xc0\xdd\x53\x7c\x3b\xed\x7c\x04\xbb\x80\xb9\x28\xbe\x8e\x9b\xc6\x8e\xa0\xa5\x12\xcb\xf5\x57\x1e\xa2\xe7\xbb\xb7\x33\x49\x9f\xe3\xbb\x4a\xae\x6a\x4d\x68\xff\xc9\x11\xe2\x32\x8d\xce\x3d\x80\x0b\x8d\x75\xef\xd8\x00\x81\x8f\x28\x04\x03\xa0\x22\x8d\x61\x04\x07\xfa\xb6\x37\x7d\x21\x07\x49\xd2\x09\x61\x69\x98\x90\xa3\x58\xa9";
75 unsigned char rsa_e[] = "\x01\x00\x01";
76 unsigned char rsa_d[] = "\x0e\x99\x80\x44\x6e\x42\x43\x14\xbe\x01\xeb\x0d\x90\x69\xa9\x6a\xe7\xa9\x88\x2c\xf5\x24\x11\x7f\x27\x09\xf2\x89\x7e\xaf\x13\x35\x21\xd1\x8a\x5d\xdf\xd4\x99\xce\xdc\x2b\x0f\x1b\xc5\x3c\x98\xd0\x68\xa5\x65\x8e\x69\x75\xce\x42\x69\x20\x35\x6c\xaa\xf1\xdd\xc9\x57\x6c\x7b\xc3\x3e\x42\x7e\xa1\xc3\x8c\x76\xa7\x9a\xe8\x81\xdb\xe1\x84\x82\xf5\x99\xd5\xa8\xee\x35\x9e\x54\x94\xc5\x44\xa0\x7b\xcc\xb7\x4c\x3e\xcd\xf2\x49\xdb\x5c\x21\x06\x85\xf6\x75\x00\x43\x62\x89\x12\xf9\x5d\x90\xed\xe6\xfd\xb4\x49\x14\x4a\x79\xe2\x4d";
77 unsigned char rsa_p[] = "\x00\xd8\xcb\xe4\x65\x4e\x6c\x11\x0f\xa8\x72\xed\x4b\x4c\x8d\x1d\x07\xdc\x24\x99\x25\xe4\x3c\xb2\xf3\x02\xc4\x72\xe6\x3a\x5b\x86\xf4\x7d\x54\x2a\x4e\x79\x64\x16\x1f\x45\x3b\x17\x9e\x2a\x94\x90\x90\x59\xe7\x0b\x95\xd4\xbf\xa9\x47\xd1\x0a\x71\xaf\x3d\x6b\xed\x55";
78 unsigned char rsa_q[] = "\x00\xdd\x49\x81\x7a\x5c\x04\xbf\x6b\xbd\x70\x05\x35\x42\x32\xa3\x9b\x08\xee\xd4\x98\x17\x6e\xb8\xc4\xa2\x12\xbe\xdc\x1e\x72\xd0\x44\x84\x5c\xf0\x30\x35\x04\xfd\x4e\xb0\xcc\xd6\x6f\x40\xcb\x16\x13\x58\xbc\x57\xf7\x77\x48\xe5\x0c\x0d\x14\x9b\x66\x6e\xd8\xde\x05";
79 unsigned char rsa_u[] = "\x4a\x74\x5c\x95\x83\x54\xa3\xb0\x71\x35\xba\x02\x3a\x7d\x4a\x8c\x2d\x9a\x26\x77\x60\x36\x28\xd4\xb1\x7d\x8a\x06\xf8\x89\xa2\xef\xb1\x66\x46\x7d\xb9\xd4\xde\xbc\xa3\xbe\x46\xfa\x62\xe1\x63\x82\xdc\xdb\x64\x36\x47\x59\x00\xa8\xf3\xf7\x0e\xb4\xe0\x66\x3d\xd9";
80 unsigned char rsa_e1[] = "\x45\x20\x96\x5e\x1b\x28\x68\x34\x46\xf1\x06\x6b\x09\x28\xc1\xc5\xfc\xd3\x0a\xa6\x43\x65\x7b\x65\xf3\x4e\xf2\x98\x28\xa9\x80\x99\xba\xd0\xb8\x80\xb7\x42\x4b\xaf\x82\xe2\xb9\xc0\x2c\x31\x9c\xfa\xfa\x3f\xaa\xb9\x06\xd2\x6a\x46\xc5\x08\x00\x81\xf1\x22\xd5\xd5";
81 unsigned char rsa_e2[] = "\x00\xa6\x50\x60\xa7\xfe\x10\xf3\x6d\x9e\x6b\x5a\xfe\xb4\x4a\x2a\xfc\x92\xb2\x2d\xc6\x41\x96\x4d\xf8\x3b\x77\xab\x4a\xf4\xf7\x85\xe0\x79\x3b\x00\xaa\xba\xae\x8d\x53\x5f\x3e\x14\xcc\x78\xfe\x2a\x11\x50\x57\xfe\x25\x57\xd9\xc9\x8c\x4d\x28\x77\xc3\x7c\xfc\x31\xa1";
82 
83 unsigned char ecc_x[] = "\x3c\x15\x6f\x1d\x48\x3e\x64\x59\x13\x2c\x6d\x04\x1a\x38\x0d\x30\x5c\xe4\x3f\x55\xcb\xd9\x17\x15\x46\x72\x71\x92\xc1\xf8\xc6\x33";
84 unsigned char ecc_y[] = "\x3d\x04\x2e\xc8\xc1\x0f\xc0\x50\x04\x7b\x9f\xc9\x48\xb5\x40\xfa\x6f\x93\x82\x59\x61\x5e\x72\x57\xcb\x83\x06\xbd\xcc\x82\x94\xc1";
85 unsigned char ecc_k[] = "\x00\xfd\x2b\x00\x80\xf3\x36\x5f\x11\x32\x65\xe3\x8d\x30\x33\x3b\x47\xf5\xce\xf8\x13\xe5\x4c\xc2\xcf\xfd\xe8\x05\x6a\xca\xc9\x41\xb1";
86 
87 unsigned char false_ed25519_x[] = "\xac\xac\x9a\xb3\xc3\x41\x8d\x41\x22\x21\xc1\x84\xa7\xb8\x70\xfb\x44\x6e\xc7\x7e\x20\x87\x7b\xd9\x22\xa4\x5d\xd2\x97\x09\xd5\x48";
88 unsigned char ed25519_x[] = "\xab\xaf\x98\xb3\xc3\x41\x8d\x41\x22\x21\xc1\x86\xa7\xb8\x70\xfb\x44\x6e\xc7\x7e\x20\x87\x7b\xd9\x22\xa4\x5d\xd2\x97\x09\xd5\x48";
89 unsigned char ed25519_k[] = "\x1c\xa9\x23\xdc\x35\xa8\xfd\xd6\x2d\xa8\x98\xb9\x60\x7b\xce\x10\x3d\xf4\x64\xc6\xe5\x4b\x0a\x65\x56\x6a\x3c\x73\x65\x51\xa2\x2f";
90 
91 unsigned char gost_x[] = "\xd0\xbb\xe9\xf4\xc6\xa8\x60\x3c\x73\x91\x44\x55\xcf\xbd\x50\xdd\x2c\x3d\x5a\xbc\x1a\xd8\x5e\x3c\xdf\x10\xdd\xd2\x63\x88\x0f\xc0";
92 unsigned char gost_y[] = "\x8a\xec\x96\x3c\x0b\xc8\x33\xff\x57\x5f\x66\x78\x94\x39\xb4\xf5\x24\xc6\xba\x86\x41\xac\x43\x21\x6f\x3c\xb0\xfa\x56\xbd\x5b\x37";
93 unsigned char gost_k[] = "\x47\x59\x41\x2c\x8a\xf8\x58\x1a\x67\xe0\xc3\x82\x1f\xca\x31\x19\x66\xf9\xd8\x43\xcd\x2f\x78\x23\x34\x98\x90\xb8\x14\x2e\x7f\xa5";
94 
95 gnutls_datum_t _dsa_p = {dsa_p, sizeof(dsa_p)-1};
96 gnutls_datum_t _dsa_q = {dsa_q, sizeof(dsa_q)-1};
97 gnutls_datum_t _dsa_g = {dsa_g, sizeof(dsa_g)-1};
98 gnutls_datum_t _dsa_y = {dsa_y, sizeof(dsa_y)-1};
99 gnutls_datum_t _dsa_x = {dsa_x, sizeof(dsa_x)-1};
100 
101 gnutls_datum_t _rsa_m = {rsa_m, sizeof(rsa_m)-1};
102 gnutls_datum_t _rsa_e = {rsa_e, sizeof(rsa_e)-1};
103 gnutls_datum_t _rsa_d = {rsa_d, sizeof(rsa_d)-1};
104 gnutls_datum_t _rsa_p = {rsa_p, sizeof(rsa_p)-1};
105 gnutls_datum_t _rsa_q = {rsa_q, sizeof(rsa_q)-1};
106 gnutls_datum_t _rsa_u = {rsa_u, sizeof(rsa_u)-1};
107 gnutls_datum_t _rsa_e1 = {rsa_e1, sizeof(rsa_e1)-1};
108 gnutls_datum_t _rsa_e2 = {rsa_e2, sizeof(rsa_e2)-1};
109 
110 gnutls_datum_t _ecc_x = {ecc_x, sizeof(ecc_x)-1};
111 gnutls_datum_t _ecc_y = {ecc_y, sizeof(ecc_y)-1};
112 gnutls_datum_t _ecc_k = {ecc_k, sizeof(ecc_k)-1};
113 
114 gnutls_datum_t _false_ed25519_x = {false_ed25519_x, sizeof(false_ed25519_x)-1};
115 gnutls_datum_t _ed25519_x = {ed25519_x, sizeof(ed25519_x)-1};
116 gnutls_datum_t _ed25519_k = {ed25519_k, sizeof(ed25519_k)-1};
117 
118 gnutls_datum_t _gost_x = {gost_x, sizeof(gost_x)-1};
119 gnutls_datum_t _gost_y = {gost_y, sizeof(gost_y)-1};
120 gnutls_datum_t _gost_k = {gost_k, sizeof(gost_k)-1};
121 
122 unsigned char ecc_params[] = "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07";
123 unsigned char ecc_point[] = "\x04\x41\x04\x3c\x15\x6f\x1d\x48\x3e\x64\x59\x13\x2c\x6d\x04\x1a\x38\x0d\x30\x5c\xe4\x3f\x55\xcb\xd9\x17\x15\x46\x72\x71\x92\xc1\xf8\xc6\x33\x3d\x04\x2e\xc8\xc1\x0f\xc0\x50\x04\x7b\x9f\xc9\x48\xb5\x40\xfa\x6f\x93\x82\x59\x61\x5e\x72\x57\xcb\x83\x06\xbd\xcc\x82\x94\xc1";
124 
_gnutls_privkey_export2_pkcs8(gnutls_privkey_t key,gnutls_x509_crt_fmt_t f,const char * password,unsigned flags,gnutls_datum_t * out)125 static int _gnutls_privkey_export2_pkcs8(gnutls_privkey_t key, gnutls_x509_crt_fmt_t f,
126 					 const char *password, unsigned flags, gnutls_datum_t *out)
127 {
128 	gnutls_x509_privkey_t xkey;
129 	int ret;
130 
131 	ret = gnutls_privkey_export_x509(key, &xkey);
132 	if (ret < 0)
133 		fail("error in gnutls_privkey_export_x509\n");
134 
135 	assert(gnutls_x509_privkey_fix(xkey)>=0);
136 
137 	ret = gnutls_x509_privkey_export2_pkcs8(xkey, f, password, 0, out);
138 	gnutls_x509_privkey_deinit(xkey);
139 
140 	return ret;
141 }
142 
143 #define CMP(name, dat, v) cmp(name, __LINE__, dat, v, sizeof(v)-1)
cmp(const char * name,int line,gnutls_datum_t * v1,unsigned char * v2,unsigned size)144 static int cmp(const char *name, int line, gnutls_datum_t *v1, unsigned char *v2, unsigned size)
145 {
146 	if (size != v1->size) {
147 		fprintf(stderr, "error in %s:%d size\n", name, line);
148 		dump("expected", v2, size);
149 		dump("got", v1->data, v1->size);
150 		exit(1);
151 	}
152 
153 	if (memcmp(v1->data, v2, size) != 0) {
154 		fprintf(stderr, "error in %s:%d\n", name, line);
155 		dump("expected", v2, size);
156 		dump("got", v1->data, v1->size);
157 		exit(1);
158 	}
159 	return 0;
160 }
161 
162 /* leading zero on v2 is ignored */
163 #define CMP_NO_LZ(name, dat, v) cmp_no_lz(name, __LINE__, dat, v, sizeof(v)-1)
cmp_no_lz(const char * name,int line,gnutls_datum_t * v1,unsigned char * i2,unsigned size)164 static int cmp_no_lz(const char *name, int line, gnutls_datum_t *v1, unsigned char *i2, unsigned size)
165 {
166 	gnutls_datum_t v2;
167 	if (size > 0 && i2[0] == 0) {
168 		v2.data = &i2[1];
169 		v2.size = size-1;
170 	} else {
171 		v2.data = i2;
172 		v2.size = size;
173 	}
174 
175 	if (v2.size != v1->size) {
176 		fprintf(stderr, "error in %s:%d size\n", name, line);
177 		dump("expected", v2.data, v2.size);
178 		dump("got", v1->data, v1->size);
179 		exit(1);
180 	}
181 
182 	if (memcmp(v1->data, v2.data, v2.size) != 0) {
183 		fprintf(stderr, "error in %s:%d\n", name, line);
184 		dump("expected", v2.data, v2.size);
185 		dump("got", v1->data, v1->size);
186 		exit(1);
187 	}
188 	return 0;
189 }
190 
191 static
check_x509_privkey(void)192 int check_x509_privkey(void)
193 {
194 	gnutls_x509_privkey_t key;
195 	gnutls_datum_t p, q, g, y, x;
196 	gnutls_datum_t m, e, u, e1, e2, d;
197 	gnutls_ecc_curve_t curve;
198 	int ret;
199 
200 	global_init();
201 
202 	ret = gnutls_x509_privkey_init(&key);
203 	if (ret < 0)
204 		fail("error\n");
205 
206 	ret = gnutls_x509_privkey_import(key, &dsa_key, GNUTLS_X509_FMT_PEM);
207 	if (ret < 0)
208 		fail("error\n");
209 
210 	ret = gnutls_x509_privkey_export_dsa_raw(key, &p, &q, &g, &y, &x);
211 	if (ret < 0)
212 		fail("error\n");
213 
214 	CMP("p", &p, dsa_p);
215 	CMP("q", &q, dsa_q);
216 	CMP("g", &g, dsa_g);
217 	CMP("y", &y, dsa_y);
218 	CMP("x", &x, dsa_x);
219 	gnutls_free(p.data);
220 	gnutls_free(q.data);
221 	gnutls_free(g.data);
222 	gnutls_free(y.data);
223 	gnutls_free(x.data);
224 	gnutls_x509_privkey_deinit(key);
225 
226 	/* RSA */
227 	ret = gnutls_x509_privkey_init(&key);
228 	if (ret < 0)
229 		fail("error\n");
230 
231 	ret = gnutls_x509_privkey_import(key, &rsa_key, GNUTLS_X509_FMT_PEM);
232 	if (ret < 0)
233 		fail("error\n");
234 
235 	ret = gnutls_x509_privkey_export_rsa_raw2(key, &m, &e, &d, &p, &q, &u, &e1, &e2);
236 	if (ret < 0)
237 		fail("error\n");
238 
239 	CMP("m", &m, rsa_m);
240 	CMP("e", &e, rsa_e);
241 	CMP("d", &d, rsa_d);
242 	CMP("p", &p, rsa_p);
243 	CMP("q", &q, rsa_q);
244 	CMP("u", &u, rsa_u);
245 	CMP("e1", &e1, rsa_e1);
246 	CMP("e2", &e2, rsa_e2);
247 	gnutls_free(m.data);
248 	gnutls_free(e.data);
249 	gnutls_free(d.data);
250 	gnutls_free(p.data);
251 	gnutls_free(q.data);
252 	gnutls_free(u.data);
253 	gnutls_free(e1.data);
254 	gnutls_free(e2.data);
255 	gnutls_x509_privkey_deinit(key);
256 
257 	/* ECC */
258 	ret = gnutls_x509_privkey_init(&key);
259 	if (ret < 0)
260 		fail("error\n");
261 
262 	ret = gnutls_x509_privkey_import(key, &server_ecc_key, GNUTLS_X509_FMT_PEM);
263 	if (ret < 0)
264 		fail("error\n");
265 
266 	ret = gnutls_x509_privkey_export_ecc_raw(key, &curve, &x, &y, &p);
267 	if (ret < 0)
268 		fail("error\n");
269 
270 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
271 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
272 		exit(1);
273 	}
274 	CMP("x", &x, ecc_x);
275 	CMP("y", &y, ecc_y);
276 	CMP("k", &p, ecc_k);
277 	gnutls_free(x.data);
278 	gnutls_free(y.data);
279 	gnutls_free(p.data);
280 	gnutls_x509_privkey_deinit(key);
281 
282 	return 0;
283 }
284 
285 static
check_privkey_import_export(void)286 int check_privkey_import_export(void)
287 {
288 	gnutls_privkey_t key;
289 	gnutls_datum_t p, q, g, y, x;
290 	gnutls_datum_t m, e, u, e1, e2, d;
291 	gnutls_ecc_curve_t curve;
292 #ifdef ENABLE_GOST
293 	gnutls_digest_algorithm_t digest;
294 	gnutls_gost_paramset_t paramset;
295 #endif
296 	int ret;
297 
298 	global_init();
299 
300 	ret = gnutls_privkey_init(&key);
301 	if (ret < 0)
302 		fail("error\n");
303 
304 	ret = gnutls_privkey_import_dsa_raw(key, &_dsa_p, &_dsa_q, &_dsa_g, &_dsa_y, &_dsa_x);
305 	if (ret < 0)
306 		fail("error\n");
307 
308 	ret = gnutls_privkey_export_dsa_raw2(key, &p, &q, &g, &y, &x, 0);
309 	if (ret < 0)
310 		fail("error: %s\n", gnutls_strerror(ret));
311 
312 	CMP("p", &p, dsa_p);
313 	CMP("q", &q, dsa_q);
314 	CMP("g", &g, dsa_g);
315 	CMP("y", &y, dsa_y);
316 	CMP("x", &x, dsa_x);
317 	gnutls_free(p.data);
318 	gnutls_free(q.data);
319 	gnutls_free(g.data);
320 	gnutls_free(y.data);
321 	gnutls_free(x.data);
322 
323 	ret = gnutls_privkey_export_dsa_raw2(key, &p, &q, &g, &y, &x, GNUTLS_EXPORT_FLAG_NO_LZ);
324 	if (ret < 0)
325 		fail("error: %s\n", gnutls_strerror(ret));
326 
327 	CMP_NO_LZ("p", &p, dsa_p);
328 	CMP_NO_LZ("q", &q, dsa_q);
329 	CMP_NO_LZ("g", &g, dsa_g);
330 	CMP_NO_LZ("y", &y, dsa_y);
331 	CMP_NO_LZ("x", &x, dsa_x);
332 	gnutls_free(p.data);
333 	gnutls_free(q.data);
334 	gnutls_free(g.data);
335 	gnutls_free(y.data);
336 	gnutls_free(x.data);
337 	gnutls_privkey_deinit(key);
338 
339 	/* RSA */
340 	ret = gnutls_privkey_init(&key);
341 	if (ret < 0)
342 		fail("error\n");
343 
344 	ret = gnutls_privkey_import_rsa_raw(key, &_rsa_m, &_rsa_e, &_rsa_d, &_rsa_p, &_rsa_q, &_rsa_u, &_rsa_e1, &_rsa_e2);
345 	if (ret < 0)
346 		fail("error\n");
347 
348 	ret = gnutls_privkey_export_rsa_raw2(key, &m, &e, &d, &p, &q, &u, &e1, &e2, 0);
349 	if (ret < 0)
350 		fail("error\n");
351 
352 	CMP("m", &m, rsa_m);
353 	CMP("e", &e, rsa_e);
354 	CMP("d", &d, rsa_d);
355 	CMP("p", &p, rsa_p);
356 	CMP("q", &q, rsa_q);
357 	CMP("u", &u, rsa_u);
358 	CMP("e1", &e1, rsa_e1);
359 	CMP("e2", &e2, rsa_e2);
360 	gnutls_free(m.data);
361 	gnutls_free(e.data);
362 	gnutls_free(d.data);
363 	gnutls_free(p.data);
364 	gnutls_free(q.data);
365 	gnutls_free(u.data);
366 	gnutls_free(e1.data);
367 	gnutls_free(e2.data);
368 
369 	ret = gnutls_privkey_export_rsa_raw2(key, &m, &e, &d, &p, &q, &u, &e1, &e2, GNUTLS_EXPORT_FLAG_NO_LZ);
370 	if (ret < 0)
371 		fail("error\n");
372 
373 	CMP_NO_LZ("m", &m, rsa_m);
374 	CMP_NO_LZ("e", &e, rsa_e);
375 	CMP_NO_LZ("d", &d, rsa_d);
376 	CMP_NO_LZ("p", &p, rsa_p);
377 	CMP_NO_LZ("q", &q, rsa_q);
378 	CMP_NO_LZ("u", &u, rsa_u);
379 	CMP_NO_LZ("e1", &e1, rsa_e1);
380 	CMP_NO_LZ("e2", &e2, rsa_e2);
381 	gnutls_free(m.data);
382 	gnutls_free(e.data);
383 	gnutls_free(d.data);
384 	gnutls_free(p.data);
385 	gnutls_free(q.data);
386 	gnutls_free(u.data);
387 	gnutls_free(e1.data);
388 	gnutls_free(e2.data);
389 	gnutls_privkey_deinit(key);
390 
391 	/* ECC */
392 	ret = gnutls_privkey_init(&key);
393 	if (ret < 0)
394 		fail("error\n");
395 
396 	ret = gnutls_privkey_import_ecc_raw(key, GNUTLS_ECC_CURVE_SECP256R1, &_ecc_x, &_ecc_y, &_ecc_k);
397 	if (ret < 0)
398 		fail("error\n");
399 
400 	ret = gnutls_privkey_export_ecc_raw2(key, &curve, &x, &y, &p, 0);
401 	if (ret < 0)
402 		fail("error\n");
403 
404 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
405 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
406 		exit(1);
407 	}
408 	CMP("x", &x, ecc_x);
409 	CMP("y", &y, ecc_y);
410 	CMP("k", &p, ecc_k);
411 	gnutls_free(x.data);
412 	gnutls_free(y.data);
413 	gnutls_free(p.data);
414 
415 	ret = gnutls_privkey_export_ecc_raw2(key, &curve, &x, &y, &p, GNUTLS_EXPORT_FLAG_NO_LZ);
416 	if (ret < 0)
417 		fail("error\n");
418 
419 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
420 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
421 		exit(1);
422 	}
423 	CMP_NO_LZ("x", &x, ecc_x);
424 	CMP_NO_LZ("y", &y, ecc_y);
425 	CMP_NO_LZ("k", &p, ecc_k);
426 	gnutls_free(x.data);
427 	gnutls_free(y.data);
428 	gnutls_free(p.data);
429 	gnutls_privkey_deinit(key);
430 
431 	/* Ed25519 */
432 	ret = gnutls_privkey_init(&key);
433 	if (ret < 0)
434 		fail("error\n");
435 
436 	/* test whether an invalid size would fail */
437 	ret = gnutls_privkey_import_ecc_raw(key, GNUTLS_ECC_CURVE_ED25519, &_rsa_m, NULL, &_rsa_m);
438 	if (ret != GNUTLS_E_INVALID_REQUEST)
439 		fail("error\n");
440 
441 	ret = gnutls_privkey_import_ecc_raw(key, GNUTLS_ECC_CURVE_ED25519, &_ed25519_x, NULL, &_ed25519_k);
442 	if (ret < 0)
443 		fail("error\n");
444 
445 	ret = gnutls_privkey_verify_params(key);
446 	if (ret != 0)
447 		fail("error: %s\n", gnutls_strerror(ret));
448 
449 	ret = gnutls_privkey_export_ecc_raw(key, &curve, &x, NULL, &p);
450 	if (ret < 0)
451 		fail("error\n");
452 
453 	if (curve != GNUTLS_ECC_CURVE_ED25519) {
454 		fail("unexpected curve value: %d\n", (int)curve);
455 	}
456 	CMP("x", &x, ed25519_x);
457 	CMP("k", &p, ed25519_k);
458 	gnutls_free(x.data);
459 	gnutls_free(p.data);
460 	gnutls_privkey_deinit(key);
461 
462 	/* Ed25519 with incorrect public key */
463 	ret = gnutls_privkey_init(&key);
464 	if (ret < 0)
465 		fail("error\n");
466 
467 	ret = gnutls_privkey_import_ecc_raw(key, GNUTLS_ECC_CURVE_ED25519, &_false_ed25519_x, NULL, &_ed25519_k);
468 	if (ret < 0)
469 		fail("error\n");
470 
471 	ret = gnutls_privkey_verify_params(key);
472 	if (ret != GNUTLS_E_ILLEGAL_PARAMETER)
473 		fail("error: %s\n", gnutls_strerror(ret));
474 
475 	gnutls_privkey_deinit(key);
476 
477 	/* GOST */
478 #ifdef ENABLE_GOST
479 	ret = gnutls_privkey_init(&key);
480 	if (ret < 0)
481 		fail("error\n");
482 
483 	ret = gnutls_privkey_import_gost_raw(key, GNUTLS_ECC_CURVE_GOST256CPXA, GNUTLS_DIG_GOSTR_94, GNUTLS_GOST_PARAMSET_CP_A, &_gost_x, &_gost_y, &_gost_k);
484 	if (ret < 0)
485 		fail("error\n");
486 
487 	ret = gnutls_privkey_export_gost_raw2(key, &curve, &digest, &paramset, &x, &y, &p, 0);
488 	if (ret < 0)
489 		fail("error\n");
490 
491 	if (curve != GNUTLS_ECC_CURVE_GOST256CPXA) {
492 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
493 		exit(1);
494 	}
495 	if (digest != GNUTLS_DIG_GOSTR_94) {
496 		fprintf(stderr, "unexpected digest value: %d\n", (int)digest);
497 		exit(1);
498 	}
499 	if (paramset != GNUTLS_GOST_PARAMSET_CP_A) {
500 		fprintf(stderr, "unexpected paramset value: %d\n", (int)paramset);
501 		exit(1);
502 	}
503 	CMP("x", &x, gost_x);
504 	CMP("y", &y, gost_y);
505 	CMP("k", &p, gost_k);
506 	gnutls_free(x.data);
507 	gnutls_free(y.data);
508 	gnutls_free(p.data);
509 
510 	ret = gnutls_privkey_export_gost_raw2(key, &curve, &digest, &paramset, &x, &y, &p, GNUTLS_EXPORT_FLAG_NO_LZ);
511 	if (ret < 0)
512 		fail("error\n");
513 
514 	if (curve != GNUTLS_ECC_CURVE_GOST256CPXA) {
515 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
516 		exit(1);
517 	}
518 	if (digest != GNUTLS_DIG_GOSTR_94) {
519 		fprintf(stderr, "unexpected digest value: %d\n", (int)digest);
520 		exit(1);
521 	}
522 	if (paramset != GNUTLS_GOST_PARAMSET_CP_A) {
523 		fprintf(stderr, "unexpected paramset value: %d\n", (int)paramset);
524 		exit(1);
525 	}
526 	CMP_NO_LZ("x", &x, gost_x);
527 	CMP_NO_LZ("y", &y, gost_y);
528 	CMP_NO_LZ("k", &p, gost_k);
529 	gnutls_free(x.data);
530 	gnutls_free(y.data);
531 	gnutls_free(p.data);
532 	gnutls_privkey_deinit(key);
533 #endif
534 
535 	return 0;
536 }
537 
538 static
check_dsa(void)539 int check_dsa(void)
540 {
541 	gnutls_privkey_t key;
542 	gnutls_pubkey_t pub;
543 	gnutls_datum_t p, q, g, y, x;
544 	int ret;
545 
546 	global_init();
547 
548 	success("Checking DSA key operations\n");
549 
550 	ret = gnutls_privkey_init(&key);
551 	if (ret < 0)
552 		fail("error\n");
553 
554 	ret = gnutls_pubkey_init(&pub);
555 	if (ret < 0)
556 		fail("error\n");
557 
558 	ret = gnutls_privkey_import_x509_raw(key, &dsa_key, GNUTLS_X509_FMT_PEM, 0, 0);
559 	if (ret < 0)
560 		fail("error\n");
561 
562 	ret = gnutls_pubkey_import_privkey(pub, key, 0, 0);
563 	if (ret < 0)
564 		fail("error\n");
565 
566 	ret = gnutls_pubkey_export_dsa_raw2(pub, &p, &q, &g, &y, 0);
567 	if (ret < 0)
568 		fail("error\n");
569 
570 	CMP("p", &p, dsa_p);
571 	CMP("q", &q, dsa_q);
572 	CMP("g", &g, dsa_g);
573 	CMP("y", &y, dsa_y);
574 	gnutls_free(p.data);
575 	gnutls_free(q.data);
576 	gnutls_free(g.data);
577 	gnutls_free(y.data);
578 
579 	ret = gnutls_pubkey_export_dsa_raw2(pub, &p, &q, &g, &y, GNUTLS_EXPORT_FLAG_NO_LZ);
580 	if (ret < 0)
581 		fail("error\n");
582 
583 	CMP_NO_LZ("p", &p, dsa_p);
584 	CMP_NO_LZ("q", &q, dsa_q);
585 	CMP_NO_LZ("g", &g, dsa_g);
586 	CMP_NO_LZ("y", &y, dsa_y);
587 	gnutls_free(p.data);
588 	gnutls_free(q.data);
589 	gnutls_free(g.data);
590 	gnutls_free(y.data);
591 
592 	ret = gnutls_privkey_export_dsa_raw(key, &p, &q, &g, &y, &x);
593 	if (ret < 0)
594 		fail("error\n");
595 	CMP("p", &p, dsa_p);
596 	CMP("q", &q, dsa_q);
597 	CMP("g", &g, dsa_g);
598 	CMP("y", &y, dsa_y);
599 	CMP("x", &x, dsa_x);
600 	gnutls_free(p.data);
601 	gnutls_free(q.data);
602 	gnutls_free(g.data);
603 	gnutls_free(y.data);
604 	gnutls_free(x.data);
605 
606 	ret = _gnutls_privkey_export2_pkcs8(key, GNUTLS_X509_FMT_DER, NULL, 0, &x);
607 	if (ret < 0 || x.size == 0)
608 		fail("error in pkcs8 export\n");
609 	gnutls_free(x.data);
610 
611 	gnutls_privkey_deinit(key);
612 	gnutls_pubkey_deinit(pub);
613 
614 	return 0;
615 }
616 
617 static
check_rsa(void)618 int check_rsa(void)
619 {
620 	gnutls_privkey_t key;
621 	gnutls_pubkey_t pub;
622 	gnutls_datum_t m, e, d, p, q, u, e1, e2;
623 	int ret;
624 
625 	success("Checking RSA key operations\n");
626 
627 	/* RSA */
628 	ret = gnutls_privkey_init(&key);
629 	if (ret < 0)
630 		fail("error\n");
631 
632 	ret = gnutls_pubkey_init(&pub);
633 	if (ret < 0)
634 		fail("error\n");
635 
636 	ret = gnutls_privkey_import_x509_raw(key, &rsa_key, GNUTLS_X509_FMT_PEM, 0, 0);
637 	if (ret < 0)
638 		fail("error\n");
639 
640 	ret = gnutls_pubkey_import_privkey(pub, key, 0, 0);
641 	if (ret < 0)
642 		fail("error\n");
643 
644 	ret = gnutls_pubkey_export_rsa_raw2(pub, &m, &e, 0);
645 	if (ret < 0)
646 		fail("error\n");
647 
648 	CMP("m", &m, rsa_m);
649 	CMP("e", &e, rsa_e);
650 	gnutls_free(m.data);
651 	gnutls_free(e.data);
652 
653 	ret = gnutls_pubkey_export_rsa_raw2(pub, &m, &e, GNUTLS_EXPORT_FLAG_NO_LZ);
654 	if (ret < 0)
655 		fail("error\n");
656 
657 	CMP_NO_LZ("m", &m, rsa_m);
658 	CMP_NO_LZ("e", &e, rsa_e);
659 	gnutls_free(m.data);
660 	gnutls_free(e.data);
661 
662 	ret = gnutls_privkey_export_rsa_raw(key, &m, &e, &d, &p, &q, &u, &e1, &e2);
663 	if (ret < 0)
664 		fail("error\n");
665 
666 	CMP("m", &m, rsa_m);
667 	CMP("e", &e, rsa_e);
668 	CMP("d", &d, rsa_d);
669 	CMP("p", &p, rsa_p);
670 	CMP("q", &q, rsa_q);
671 	CMP("u", &u, rsa_u);
672 	CMP("e1", &e1, rsa_e1);
673 	CMP("e2", &e2, rsa_e2);
674 	gnutls_free(m.data);
675 	gnutls_free(e.data);
676 	gnutls_free(d.data);
677 	gnutls_free(p.data);
678 	gnutls_free(q.data);
679 	gnutls_free(u.data);
680 	gnutls_free(e1.data);
681 	gnutls_free(e2.data);
682 
683 	ret = _gnutls_privkey_export2_pkcs8(key, GNUTLS_X509_FMT_DER, NULL, 0, &m);
684 	if (ret < 0 || m.size == 0)
685 		fail("error in pkcs8 export\n");
686 	gnutls_free(m.data);
687 
688 	gnutls_privkey_deinit(key);
689 	gnutls_pubkey_deinit(pub);
690 
691 	return 0;
692 }
693 
694 static
check_ecc(void)695 int check_ecc(void)
696 {
697 	gnutls_privkey_t key;
698 	gnutls_pubkey_t pub;
699 	gnutls_datum_t y, x, k;
700 	gnutls_ecc_curve_t curve;
701 	int ret;
702 
703 	success("Checking SECP256R1 key operations\n");
704 
705 	/* ECC */
706 	ret = gnutls_privkey_init(&key);
707 	if (ret < 0)
708 		fail("error\n");
709 
710 	ret = gnutls_pubkey_init(&pub);
711 	if (ret < 0)
712 		fail("error\n");
713 
714 	ret = gnutls_privkey_import_x509_raw(key, &server_ecc_key, GNUTLS_X509_FMT_PEM, 0, 0);
715 	if (ret < 0)
716 		fail("error\n");
717 
718 	ret = gnutls_pubkey_import_privkey(pub, key, 0, 0);
719 	if (ret < 0)
720 		fail("error\n");
721 
722 	ret = gnutls_pubkey_export_ecc_raw2(pub, &curve, &x, &y, 0);
723 	if (ret < 0)
724 		fail("error\n");
725 
726 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
727 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
728 		exit(1);
729 	}
730 	CMP("x", &x, ecc_x);
731 	CMP("y", &y, ecc_y);
732 	gnutls_free(x.data);
733 	gnutls_free(y.data);
734 
735 	ret = gnutls_pubkey_export_ecc_raw2(pub, &curve, &x, &y, GNUTLS_EXPORT_FLAG_NO_LZ);
736 	if (ret < 0)
737 		fail("error\n");
738 
739 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
740 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
741 		exit(1);
742 	}
743 	CMP_NO_LZ("x", &x, ecc_x);
744 	CMP_NO_LZ("y", &y, ecc_y);
745 	gnutls_free(x.data);
746 	gnutls_free(y.data);
747 
748 
749 	/* check the private key export */
750 	ret = gnutls_privkey_export_ecc_raw(key, &curve, &x, &y, &k);
751 	if (ret < 0)
752 		fail("error\n");
753 
754 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
755 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
756 		exit(1);
757 	}
758 	CMP("x", &x, ecc_x);
759 	CMP("y", &y, ecc_y);
760 	CMP("k", &k, ecc_k);
761 	gnutls_free(x.data);
762 	gnutls_free(y.data);
763 	gnutls_free(k.data);
764 
765 	ret = _gnutls_privkey_export2_pkcs8(key, GNUTLS_X509_FMT_DER, NULL, 0, &x);
766 	if (ret < 0 || x.size == 0)
767 		fail("error in pkcs8 export\n");
768 	gnutls_free(x.data);
769 
770 	gnutls_privkey_deinit(key);
771 
772 	/* More public key ops */
773 
774 	ret = gnutls_pubkey_export_ecc_x962(pub, &x, &y);
775 	if (ret < 0)
776 		fail("error\n");
777 
778 	CMP("parameters", &x, ecc_params);
779 	CMP("ecpoint", &y, ecc_point);
780 
781 	ret = gnutls_pubkey_import_ecc_x962(pub, &x, &y);
782 	if (ret < 0)
783 		fail("error\n");
784 	gnutls_free(x.data);
785 	gnutls_free(y.data);
786 
787 	/* check again */
788 	ret = gnutls_pubkey_export_ecc_raw(pub, &curve, &x, &y);
789 	if (ret < 0)
790 		fail("error\n");
791 
792 	if (curve != GNUTLS_ECC_CURVE_SECP256R1) {
793 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
794 		fail("error\n");
795 	}
796 	CMP("x", &x, ecc_x);
797 	CMP("y", &y, ecc_y);
798 	gnutls_free(x.data);
799 	gnutls_free(y.data);
800 
801 	gnutls_pubkey_deinit(pub);
802 
803 	return 0;
804 }
805 
806 static
check_ed25519(void)807 int check_ed25519(void)
808 {
809 	gnutls_privkey_t key;
810 	gnutls_pubkey_t pub;
811 	gnutls_datum_t y, x, k;
812 	gnutls_ecc_curve_t curve;
813 	int ret;
814 
815 	success("Checking ed25519 key operations\n");
816 
817 	/* ECC */
818 	ret = gnutls_privkey_init(&key);
819 	if (ret < 0)
820 		fail("error\n");
821 
822 	ret = gnutls_pubkey_init(&pub);
823 	if (ret < 0)
824 		fail("error\n");
825 
826 	ret = gnutls_privkey_import_x509_raw(key, &server_ca3_eddsa_key, GNUTLS_X509_FMT_PEM, 0, 0);
827 	if (ret < 0)
828 		fail("error\n");
829 
830 	ret = gnutls_pubkey_import_privkey(pub, key, 0, 0);
831 	if (ret < 0)
832 		fail("error\n");
833 
834 	ret = gnutls_pubkey_export_ecc_raw(pub, &curve, &x, NULL);
835 	if (ret < 0)
836 		fail("error\n");
837 	gnutls_free(x.data);
838 
839 	ret = gnutls_pubkey_export_ecc_raw(pub, &curve, &x, &y);
840 	if (ret < 0)
841 		fail("error\n");
842 
843 	if (curve != GNUTLS_ECC_CURVE_ED25519) {
844 		fail("unexpected curve value: %d\n", (int)curve);
845 	}
846 	CMP("x", &x, ed25519_x);
847 
848 	if (y.data != NULL) {
849 		fail("expected NULL value in Y\n");
850 	}
851 	gnutls_free(x.data);
852 
853 
854 	/* check the private key export */
855 	ret = gnutls_privkey_export_ecc_raw(key, &curve, &x, NULL, &k);
856 	if (ret < 0)
857 		fail("error\n");
858 	gnutls_free(x.data);
859 	gnutls_free(k.data);
860 
861 	ret = gnutls_privkey_export_ecc_raw(key, &curve, &x, &y, &k);
862 	if (ret < 0)
863 		fail("error\n");
864 
865 	if (curve != GNUTLS_ECC_CURVE_ED25519) {
866 		fail("unexpected curve value: %d\n", (int)curve);
867 	}
868 	CMP("x", &x, ed25519_x);
869 	CMP("k", &k, ed25519_k);
870 	gnutls_free(x.data);
871 	gnutls_free(k.data);
872 
873 	if (y.data != NULL) {
874 		fail("expected NULL value in Y\n");
875 	}
876 
877 	ret = _gnutls_privkey_export2_pkcs8(key, GNUTLS_X509_FMT_DER, NULL, 0, &x);
878 	if (ret < 0 || x.size == 0)
879 		fail("error in pkcs8 export\n");
880 	gnutls_free(x.data);
881 
882 	gnutls_privkey_deinit(key);
883 
884 	/* More public key ops */
885 
886 	ret = gnutls_pubkey_export_ecc_x962(pub, &x, &y);
887 	if (ret != GNUTLS_E_INVALID_REQUEST)
888 		fail("error\n");
889 
890 	gnutls_pubkey_deinit(pub);
891 
892 	return 0;
893 }
894 
895 static
check_gost(void)896 int check_gost(void)
897 {
898 #ifdef ENABLE_GOST
899 	gnutls_privkey_t key;
900 	gnutls_pubkey_t pub;
901 	gnutls_datum_t y, x, k;
902 	gnutls_ecc_curve_t curve;
903 	gnutls_digest_algorithm_t digest;
904 	gnutls_gost_paramset_t paramset;
905 	int ret;
906 
907 	success("Checking GOST key operations\n");
908 
909 	/* ECC */
910 	ret = gnutls_privkey_init(&key);
911 	if (ret < 0)
912 		fail("error\n");
913 
914 	ret = gnutls_pubkey_init(&pub);
915 	if (ret < 0)
916 		fail("error\n");
917 
918 	ret = gnutls_privkey_import_x509_raw(key, &server_ca3_gost01_key, GNUTLS_X509_FMT_PEM, 0, 0);
919 	if (ret < 0)
920 		fail("error\n");
921 
922 	ret = gnutls_pubkey_import_privkey(pub, key, 0, 0);
923 	if (ret < 0)
924 		fail("error\n");
925 
926 	ret = gnutls_pubkey_export_gost_raw2(pub, &curve, &digest, &paramset, &x, &y, 0);
927 	if (ret < 0)
928 		fail("error\n");
929 
930 	if (curve != GNUTLS_ECC_CURVE_GOST256CPXA) {
931 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
932 		exit(1);
933 	}
934 	if (digest != GNUTLS_DIG_GOSTR_94) {
935 		fprintf(stderr, "unexpected digest value: %d\n", (int)digest);
936 		exit(1);
937 	}
938 	if (paramset != GNUTLS_GOST_PARAMSET_CP_A) {
939 		fprintf(stderr, "unexpected paramset value: %d\n", (int)paramset);
940 		exit(1);
941 	}
942 	CMP("x", &x, gost_x);
943 	CMP("y", &y, gost_y);
944 	gnutls_free(x.data);
945 	gnutls_free(y.data);
946 
947 	ret = gnutls_pubkey_export_gost_raw2(pub, &curve, &digest, &paramset, &x, &y, GNUTLS_EXPORT_FLAG_NO_LZ);
948 	if (ret < 0)
949 		fail("error\n");
950 
951 	if (curve != GNUTLS_ECC_CURVE_GOST256CPXA) {
952 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
953 		exit(1);
954 	}
955 	if (digest != GNUTLS_DIG_GOSTR_94) {
956 		fprintf(stderr, "unexpected digest value: %d\n", (int)digest);
957 		exit(1);
958 	}
959 	if (paramset != GNUTLS_GOST_PARAMSET_CP_A) {
960 		fprintf(stderr, "unexpected paramset value: %d\n", (int)paramset);
961 		exit(1);
962 	}
963 	CMP_NO_LZ("x", &x, gost_x);
964 	CMP_NO_LZ("y", &y, gost_y);
965 	gnutls_free(x.data);
966 	gnutls_free(y.data);
967 
968 
969 	/* check the private key export */
970 	ret = gnutls_privkey_export_gost_raw2(key, &curve, &digest, &paramset, &x, &y, &k, 0);
971 	if (ret < 0)
972 		fail("error\n");
973 
974 	if (curve != GNUTLS_ECC_CURVE_GOST256CPXA) {
975 		fprintf(stderr, "unexpected curve value: %d\n", (int)curve);
976 		exit(1);
977 	}
978 	if (digest != GNUTLS_DIG_GOSTR_94) {
979 		fprintf(stderr, "unexpected digest value: %d\n", (int)digest);
980 		exit(1);
981 	}
982 	if (paramset != GNUTLS_GOST_PARAMSET_CP_A) {
983 		fprintf(stderr, "unexpected paramset value: %d\n", (int)paramset);
984 		exit(1);
985 	}
986 	CMP("x", &x, gost_x);
987 	CMP("y", &y, gost_y);
988 	CMP("k", &k, gost_k);
989 	gnutls_free(x.data);
990 	gnutls_free(y.data);
991 	gnutls_free(k.data);
992 
993 	ret = _gnutls_privkey_export2_pkcs8(key, GNUTLS_X509_FMT_DER, NULL, 0, &x);
994 	if (ret < 0 || x.size == 0)
995 		fail("error in pkcs8 export\n");
996 	gnutls_free(x.data);
997 
998 	gnutls_privkey_deinit(key);
999 
1000 	gnutls_pubkey_deinit(pub);
1001 #endif
1002 
1003 	return 0;
1004 }
1005 
doit(void)1006 void doit(void)
1007 {
1008 	if (check_x509_privkey() != 0) {
1009 		fail("error in privkey check\n");
1010 		exit(1);
1011 	}
1012 
1013 	if (check_privkey_import_export() != 0) {
1014 		fail("error in privkey import/export check\n");
1015 		exit(1);
1016 	}
1017 
1018 	if (check_dsa() != 0) {
1019 		fail("error in DSA check\n");
1020 		exit(1);
1021 	}
1022 
1023 	if (check_rsa() != 0) {
1024 		fail("error in RSA check\n");
1025 		exit(1);
1026 	}
1027 
1028 	if (check_ecc() != 0) {
1029 		fail("error in ecdsa check\n");
1030 	}
1031 
1032 	if (check_ed25519() != 0) {
1033 		fail("error in ed25519 check\n");
1034 	}
1035 
1036 	if (check_gost() != 0) {
1037 		fail("error in gost check\n");
1038 	}
1039 }
1040