2 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library 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 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* This file contains code for RSA temporary keys. These keys are
24 * only used in export cipher suites.
27 #include <gnutls_int.h>
28 #include <gnutls_errors.h>
29 #include <gnutls_datum.h>
30 #include "x509/x509_int.h"
33 #ifdef ENABLE_RSA_EXPORT
35 /* The are included for binary compatibility with previous versions
39 * gnutls_rsa_params_import_raw:
40 * @rsa_params: Is a structure will hold the parameters
41 * @m: holds the modulus
42 * @e: holds the public exponent
43 * @d: holds the private exponent
44 * @p: holds the first prime (p)
45 * @q: holds the second prime (q)
46 * @u: holds the coefficient
48 * This function will replace the parameters in the given structure.
49 * The new parameters should be stored in the appropriate
52 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
55 gnutls_rsa_params_import_raw(gnutls_rsa_params_t rsa_params,
56 const gnutls_datum_t * m,
57 const gnutls_datum_t * e,
58 const gnutls_datum_t * d,
59 const gnutls_datum_t * p,
60 const gnutls_datum_t * q,
61 const gnutls_datum_t * u)
63 return gnutls_x509_privkey_import_rsa_raw(rsa_params, m, e, d, p,
68 * gnutls_rsa_params_init:
69 * @rsa_params: Is a structure that will hold the parameters
71 * This function will initialize the temporary RSA parameters structure.
73 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
75 int gnutls_rsa_params_init(gnutls_rsa_params_t * rsa_params)
79 ret = gnutls_x509_privkey_init(rsa_params);
89 * gnutls_rsa_params_deinit:
90 * @rsa_params: Is a structure that holds the parameters
92 * This function will deinitialize the RSA parameters structure.
94 void gnutls_rsa_params_deinit(gnutls_rsa_params_t rsa_params)
96 gnutls_x509_privkey_deinit(rsa_params);
100 * gnutls_rsa_params_cpy:
101 * @dst: Is the destination structure, which should be initialized.
102 * @src: Is the source structure
104 * This function will copy the RSA parameters structure from source
107 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
109 int gnutls_rsa_params_cpy(gnutls_rsa_params_t dst, gnutls_rsa_params_t src)
111 return gnutls_x509_privkey_cpy(dst, src);
115 * gnutls_rsa_params_generate2:
116 * @params: The structure where the parameters will be stored
117 * @bits: is the prime's number of bits
119 * This function will generate new temporary RSA parameters for use in
120 * RSA-EXPORT ciphersuites. This function is normally slow.
122 * Note that if the parameters are to be used in export cipher suites the
123 * bits value should be 512 or less.
124 * Also note that the generation of new RSA parameters is only useful
125 * to servers. Clients use the parameters sent by the server, thus it's
126 * no use calling this in client side.
128 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
131 gnutls_rsa_params_generate2(gnutls_rsa_params_t params, unsigned int bits)
133 return gnutls_x509_privkey_generate(params, GNUTLS_PK_RSA, bits,
138 * gnutls_rsa_params_import_pkcs1:
139 * @params: A structure where the parameters will be copied to
140 * @pkcs1_params: should contain a PKCS1 RSAPrivateKey structure PEM or DER encoded
141 * @format: the format of params. PEM or DER.
143 * This function will extract the RSAPrivateKey found in a PKCS1 formatted
146 * If the structure is PEM encoded, it should have a header
147 * of "BEGIN RSA PRIVATE KEY".
149 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
152 gnutls_rsa_params_import_pkcs1(gnutls_rsa_params_t params,
153 const gnutls_datum_t * pkcs1_params,
154 gnutls_x509_crt_fmt_t format)
156 return gnutls_x509_privkey_import(params, pkcs1_params, format);
160 * gnutls_rsa_params_export_pkcs1:
161 * @params: Holds the RSA parameters
162 * @format: the format of output params. One of PEM or DER.
163 * @params_data: will contain a PKCS1 RSAPrivateKey structure PEM or DER encoded
164 * @params_data_size: holds the size of params_data (and will be replaced by the actual size of parameters)
166 * This function will export the given RSA parameters to a PKCS1
167 * RSAPrivateKey structure. If the buffer provided is not long enough to
168 * hold the output, then GNUTLS_E_SHORT_MEMORY_BUFFER will be returned.
170 * If the structure is PEM encoded, it will have a header
171 * of "BEGIN RSA PRIVATE KEY".
173 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
176 gnutls_rsa_params_export_pkcs1(gnutls_rsa_params_t params,
177 gnutls_x509_crt_fmt_t format,
178 unsigned char *params_data,
179 size_t * params_data_size)
181 return gnutls_x509_privkey_export(params, format,
182 params_data, params_data_size);
186 * gnutls_rsa_params_export_raw:
187 * @rsa: a structure that holds the rsa parameters
188 * @m: will hold the modulus
189 * @e: will hold the public exponent
190 * @d: will hold the private exponent
191 * @p: will hold the first prime (p)
192 * @q: will hold the second prime (q)
193 * @u: will hold the coefficient
194 * @bits: if non null will hold the prime's number of bits
196 * This function will export the RSA parameters found in the given
197 * structure. The new parameters will be allocated using
198 * gnutls_malloc() and will be stored in the appropriate datum.
200 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
203 gnutls_rsa_params_export_raw(gnutls_rsa_params_t rsa,
204 gnutls_datum_t * m, gnutls_datum_t * e,
205 gnutls_datum_t * d, gnutls_datum_t * p,
206 gnutls_datum_t * q, gnutls_datum_t * u,
211 ret = gnutls_x509_privkey_export_rsa_raw(rsa, m, e, d, p, q, u);
218 *bits = _gnutls_mpi_get_nbits(rsa->params.params[3]);
223 #endif /* ENABLE_RSA_EXPORT */