2 * Copyright (C) 2010-2014 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
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.
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.
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/>
20 #include <gnutls_int.h>
21 #include <gnutls/pkcs11.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>
30 #include <openpgp/openpgp_int.h>
31 #include <openpgp/gnutls_openpgp.h>
32 #include <gnutls_sig.h>
33 #include <algorithms.h>
35 #include <abstract_int.h>
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)
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.
53 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
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,
65 gnutls_pk_params_st params;
70 return GNUTLS_E_INVALID_REQUEST;
73 gnutls_pk_params_init(¶ms);
75 ret = _gnutls_privkey_get_mpis(key, ¶ms);
77 return gnutls_assert_val(ret);
79 ret = _gnutls_params_get_rsa_raw(¶ms, m, e, d, p, q, u, e1, e2);
81 gnutls_pk_params_release(¶ms);
87 * gnutls_privkey_export_dsa_raw:
88 * @key: Holds the public key
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.
99 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
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,
109 gnutls_pk_params_st params;
114 return GNUTLS_E_INVALID_REQUEST;
117 gnutls_pk_params_init(¶ms);
119 ret = _gnutls_privkey_get_mpis(key, ¶ms);
121 return gnutls_assert_val(ret);
123 ret = _gnutls_params_get_dsa_raw(¶ms, p, q, g, y, x);
125 gnutls_pk_params_release(¶ms);
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
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.
143 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
148 gnutls_privkey_export_ecc_raw(gnutls_privkey_t key,
149 gnutls_ecc_curve_t * curve,
154 gnutls_pk_params_st params;
159 return GNUTLS_E_INVALID_REQUEST;
162 gnutls_pk_params_init(¶ms);
164 ret = _gnutls_privkey_get_mpis(key, ¶ms);
166 return gnutls_assert_val(ret);
168 ret = _gnutls_params_get_ecc_raw(¶ms, curve, x, y, k);
170 gnutls_pk_params_release(¶ms);
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)
187 * This function will convert the given RSA raw parameters to the
188 * native #gnutls_privkey_t format. The output will be stored in
191 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
192 * negative error value.
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)
206 gnutls_x509_privkey_t xkey;
208 ret = gnutls_x509_privkey_init(&xkey);
210 return gnutls_assert_val(ret);
212 ret = gnutls_x509_privkey_import_rsa_raw2(xkey, m, e, d, p, q, u, e1, e1);
218 ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
227 gnutls_x509_privkey_deinit(xkey);
232 * gnutls_privkey_import_dsa_raw:
233 * @key: The structure to store the parsed key
240 * This function will convert the given DSA raw parameters to the
241 * native #gnutls_privkey_t format. The output will be stored
244 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
245 * negative error value.
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)
256 gnutls_x509_privkey_t xkey;
258 ret = gnutls_x509_privkey_init(&xkey);
260 return gnutls_assert_val(ret);
262 ret = gnutls_x509_privkey_import_dsa_raw(xkey, p, q, g, y, x);
268 ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
277 gnutls_x509_privkey_deinit(xkey);
282 * gnutls_privkey_import_ecc_raw:
283 * @key: The structure to store the parsed key
284 * @curve: holds the curve
289 * This function will convert the given elliptic curve parameters to the
290 * native #gnutls_privkey_t format. The output will be stored
293 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
294 * negative error value.
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)
306 gnutls_x509_privkey_t xkey;
308 ret = gnutls_x509_privkey_init(&xkey);
310 return gnutls_assert_val(ret);
312 ret = gnutls_x509_privkey_import_ecc_raw(xkey, curve, x, y, k);
318 ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
327 gnutls_x509_privkey_deinit(xkey);