Imported Upstream version 3.3.5
[platform/upstream/gnutls.git] / lib / gnutls_privkey_raw.c
1 /*
2  * Copyright (C) 2010-2014 Free Software Foundation, Inc.
3  * 
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * The GnuTLS is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>
18  */
19
20 #include <gnutls_int.h>
21 #include <gnutls/pkcs11.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <gnutls_errors.h>
25 #include <gnutls_datum.h>
26 #include <pkcs11_int.h>
27 #include <gnutls/abstract.h>
28 #include <gnutls_pk.h>
29 #include <x509_int.h>
30 #include <openpgp/openpgp_int.h>
31 #include <openpgp/gnutls_openpgp.h>
32 #include <gnutls_sig.h>
33 #include <algorithms.h>
34 #include <fips.h>
35 #include <abstract_int.h>
36
37 /**
38  * gnutls_privkey_export_rsa_raw:
39  * @key: Holds the certificate
40  * @m: will hold the modulus
41  * @e: will hold the public exponent
42  * @d: will hold the private exponent
43  * @p: will hold the first prime (p)
44  * @q: will hold the second prime (q)
45  * @u: will hold the coefficient
46  * @e1: will hold e1 = d mod (p-1)
47  * @e2: will hold e2 = d mod (q-1)
48  *
49  * This function will export the RSA private key's parameters found
50  * in the given structure. The new parameters will be allocated using
51  * gnutls_malloc() and will be stored in the appropriate datum.
52  *
53  * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
54  *
55  * Since: 3.3.0
56  **/
57 int
58 gnutls_privkey_export_rsa_raw(gnutls_privkey_t key,
59                                     gnutls_datum_t * m, gnutls_datum_t * e,
60                                     gnutls_datum_t * d, gnutls_datum_t * p,
61                                     gnutls_datum_t * q, gnutls_datum_t * u,
62                                     gnutls_datum_t * e1,
63                                     gnutls_datum_t * e2)
64 {
65 gnutls_pk_params_st params;
66 int ret;
67
68         if (key == NULL) {
69                 gnutls_assert();
70                 return GNUTLS_E_INVALID_REQUEST;
71         }
72         
73         gnutls_pk_params_init(&params);
74
75         ret = _gnutls_privkey_get_mpis(key, &params);
76         if (ret < 0)
77                 return gnutls_assert_val(ret);
78
79         ret = _gnutls_params_get_rsa_raw(&params, m, e, d, p, q, u, e1, e2);
80
81         gnutls_pk_params_release(&params);
82
83         return ret;
84 }
85
86 /**
87  * gnutls_privkey_export_dsa_raw:
88  * @key: Holds the public key
89  * @p: will hold the p
90  * @q: will hold the q
91  * @g: will hold the g
92  * @y: will hold the y
93  * @x: will hold the x
94  *
95  * This function will export the DSA private key's parameters found
96  * in the given structure. The new parameters will be allocated using
97  * gnutls_malloc() and will be stored in the appropriate datum.
98  *
99  * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
100  *
101  * Since: 3.3.0
102  **/
103 int
104 gnutls_privkey_export_dsa_raw(gnutls_privkey_t key,
105                              gnutls_datum_t * p, gnutls_datum_t * q,
106                              gnutls_datum_t * g, gnutls_datum_t * y,
107                              gnutls_datum_t * x)
108 {
109 gnutls_pk_params_st params;
110 int ret;
111
112         if (key == NULL) {
113                 gnutls_assert();
114                 return GNUTLS_E_INVALID_REQUEST;
115         }
116         
117         gnutls_pk_params_init(&params);
118
119         ret = _gnutls_privkey_get_mpis(key, &params);
120         if (ret < 0)
121                 return gnutls_assert_val(ret);
122
123         ret = _gnutls_params_get_dsa_raw(&params, p, q, g, y, x);
124
125         gnutls_pk_params_release(&params);
126
127         return ret;
128 }
129
130
131 /**
132  * gnutls_privkey_export_ecc_raw:
133  * @key: Holds the public key
134  * @curve: will hold the curve
135  * @x: will hold the x coordinate
136  * @y: will hold the y coordinate
137  * @k: will hold the private key
138  *
139  * This function will export the ECC private key's parameters found
140  * in the given structure. The new parameters will be allocated using
141  * gnutls_malloc() and will be stored in the appropriate datum.
142  *
143  * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
144  *
145  * Since: 3.3.0
146  **/
147 int
148 gnutls_privkey_export_ecc_raw(gnutls_privkey_t key,
149                                        gnutls_ecc_curve_t * curve,
150                                        gnutls_datum_t * x,
151                                        gnutls_datum_t * y,
152                                        gnutls_datum_t * k)
153 {
154 gnutls_pk_params_st params;
155 int ret;
156
157         if (key == NULL) {
158                 gnutls_assert();
159                 return GNUTLS_E_INVALID_REQUEST;
160         }
161         
162         gnutls_pk_params_init(&params);
163
164         ret = _gnutls_privkey_get_mpis(key, &params);
165         if (ret < 0)
166                 return gnutls_assert_val(ret);
167
168         ret = _gnutls_params_get_ecc_raw(&params, curve, x, y, k);
169
170         gnutls_pk_params_release(&params);
171
172         return ret;
173 }
174
175 /**
176  * gnutls_privkey_import_rsa_raw:
177  * @key: The structure to store the parsed key
178  * @m: holds the modulus
179  * @e: holds the public exponent
180  * @d: holds the private exponent
181  * @p: holds the first prime (p)
182  * @q: holds the second prime (q)
183  * @u: holds the coefficient (optional)
184  * @e1: holds e1 = d mod (p-1) (optional)
185  * @e2: holds e2 = d mod (q-1) (optional)
186  *
187  * This function will convert the given RSA raw parameters to the
188  * native #gnutls_privkey_t format.  The output will be stored in
189  * @key.
190  *
191  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
192  *   negative error value.
193  **/
194 int
195 gnutls_privkey_import_rsa_raw(gnutls_privkey_t key,
196                                     const gnutls_datum_t * m,
197                                     const gnutls_datum_t * e,
198                                     const gnutls_datum_t * d,
199                                     const gnutls_datum_t * p,
200                                     const gnutls_datum_t * q,
201                                     const gnutls_datum_t * u,
202                                     const gnutls_datum_t * e1,
203                                     const gnutls_datum_t * e2)
204 {
205 int ret;
206 gnutls_x509_privkey_t xkey;
207
208         ret = gnutls_x509_privkey_init(&xkey);
209         if (ret < 0)
210                 return gnutls_assert_val(ret);
211
212         ret = gnutls_x509_privkey_import_rsa_raw2(xkey, m, e, d, p, q, u, e1, e1);
213         if (ret < 0) {
214                 gnutls_assert();
215                 goto error;
216         }
217         
218         ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
219         if (ret < 0) {
220                 gnutls_assert();
221                 goto error;
222         }
223         
224         return 0;
225
226 error:
227         gnutls_x509_privkey_deinit(xkey);
228         return ret;
229 }
230
231 /**
232  * gnutls_privkey_import_dsa_raw:
233  * @key: The structure to store the parsed key
234  * @p: holds the p
235  * @q: holds the q
236  * @g: holds the g
237  * @y: holds the y
238  * @x: holds the x
239  *
240  * This function will convert the given DSA raw parameters to the
241  * native #gnutls_privkey_t format.  The output will be stored
242  * in @key.
243  *
244  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
245  *   negative error value.
246  **/
247 int
248 gnutls_privkey_import_dsa_raw(gnutls_privkey_t key,
249                                    const gnutls_datum_t * p,
250                                    const gnutls_datum_t * q,
251                                    const gnutls_datum_t * g,
252                                    const gnutls_datum_t * y,
253                                    const gnutls_datum_t * x)
254 {
255 int ret;
256 gnutls_x509_privkey_t xkey;
257
258         ret = gnutls_x509_privkey_init(&xkey);
259         if (ret < 0)
260                 return gnutls_assert_val(ret);
261
262         ret = gnutls_x509_privkey_import_dsa_raw(xkey, p, q, g, y, x);
263         if (ret < 0) {
264                 gnutls_assert();
265                 goto error;
266         }
267         
268         ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
269         if (ret < 0) {
270                 gnutls_assert();
271                 goto error;
272         }
273         
274         return 0;
275
276 error:
277         gnutls_x509_privkey_deinit(xkey);
278         return ret;
279 }
280
281 /**
282  * gnutls_privkey_import_ecc_raw:
283  * @key: The structure to store the parsed key
284  * @curve: holds the curve
285  * @x: holds the x
286  * @y: holds the y
287  * @k: holds the k
288  *
289  * This function will convert the given elliptic curve parameters to the
290  * native #gnutls_privkey_t format.  The output will be stored
291  * in @key.
292  *
293  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
294  *   negative error value.
295  *
296  * Since: 3.0
297  **/
298 int
299 gnutls_privkey_import_ecc_raw(gnutls_privkey_t key,
300                                    gnutls_ecc_curve_t curve,
301                                    const gnutls_datum_t * x,
302                                    const gnutls_datum_t * y,
303                                    const gnutls_datum_t * k)
304 {
305 int ret;
306 gnutls_x509_privkey_t xkey;
307
308         ret = gnutls_x509_privkey_init(&xkey);
309         if (ret < 0)
310                 return gnutls_assert_val(ret);
311
312         ret = gnutls_x509_privkey_import_ecc_raw(xkey, curve, y, x, k);
313         if (ret < 0) {
314                 gnutls_assert();
315                 goto error;
316         }
317         
318         ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
319         if (ret < 0) {
320                 gnutls_assert();
321                 goto error;
322         }
323         
324         return 0;
325
326 error:
327         gnutls_x509_privkey_deinit(xkey);
328         return ret;
329 }
330