Fix --disable-valgrind-tests.
[gnutls.git] / lib / gnutls_rsa_export.c
blob3a606b42fd5b8de50fdf98415f9429881288f37f
1 /*
2 * Copyright (C) 2002, 2003, 2004, 2005, 2008, 2009 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file contains code for RSA temporary keys. These keys are
26 * only used in export cipher suites.
29 #include <gnutls_int.h>
30 #include <gnutls_errors.h>
31 #include <gnutls_datum.h>
32 #include <gnutls_rsa_export.h>
33 #include "x509/x509_int.h"
34 #include "debug.h"
36 /* returns e and m, depends on the requested bits.
37 * We only support limited key sizes.
39 const bigint_t *
40 _gnutls_rsa_params_to_mpi (gnutls_rsa_params_t rsa_params)
42 if (rsa_params == NULL)
44 return NULL;
47 return rsa_params->params;
50 /**
51 * gnutls_rsa_params_import_raw - set the RSA parameters
52 * @rsa_params: Is a structure will hold the parameters
53 * @m: holds the modulus
54 * @e: holds the public exponent
55 * @d: holds the private exponent
56 * @p: holds the first prime (p)
57 * @q: holds the second prime (q)
58 * @u: holds the coefficient
60 * This function will replace the parameters in the given structure.
61 * The new parameters should be stored in the appropriate
62 * gnutls_datum.
64 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
65 **/
66 int
67 gnutls_rsa_params_import_raw (gnutls_rsa_params_t rsa_params,
68 const gnutls_datum_t * m,
69 const gnutls_datum_t * e,
70 const gnutls_datum_t * d,
71 const gnutls_datum_t * p,
72 const gnutls_datum_t * q,
73 const gnutls_datum_t * u)
75 return gnutls_x509_privkey_import_rsa_raw (rsa_params, m, e, d, p, q, u);
78 /**
79 * gnutls_rsa_params_init - initialize the temporary RSA parameters
80 * @rsa_params: Is a structure that will hold the parameters
82 * This function will initialize the temporary RSA parameters structure.
84 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
85 **/
86 int
87 gnutls_rsa_params_init (gnutls_rsa_params_t * rsa_params)
89 int ret;
91 ret = gnutls_x509_privkey_init (rsa_params);
92 if (ret < 0)
94 gnutls_assert ();
95 return ret;
98 (*rsa_params)->crippled = 1;
100 return 0;
104 * gnutls_rsa_params_deinit - deinitialize the RSA parameters
105 * @rsa_params: Is a structure that holds the parameters
107 * This function will deinitialize the RSA parameters structure.
109 void
110 gnutls_rsa_params_deinit (gnutls_rsa_params_t rsa_params)
112 gnutls_x509_privkey_deinit (rsa_params);
116 * gnutls_rsa_params_cpy - copy an RSA parameters structure
117 * @dst: Is the destination structure, which should be initialized.
118 * @src: Is the source structure
120 * This function will copy the RSA parameters structure from source
121 * to destination.
123 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
126 gnutls_rsa_params_cpy (gnutls_rsa_params_t dst, gnutls_rsa_params_t src)
128 return gnutls_x509_privkey_cpy (dst, src);
132 * gnutls_rsa_params_generate2 - generate temporary RSA parameters
133 * @params: The structure where the parameters will be stored
134 * @bits: is the prime's number of bits
136 * This function will generate new temporary RSA parameters for use in
137 * RSA-EXPORT ciphersuites. This function is normally slow.
139 * Note that if the parameters are to be used in export cipher suites the
140 * bits value should be 512 or less.
141 * Also note that the generation of new RSA parameters is only useful
142 * to servers. Clients use the parameters sent by the server, thus it's
143 * no use calling this in client side.
145 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
148 gnutls_rsa_params_generate2 (gnutls_rsa_params_t params, unsigned int bits)
150 return gnutls_x509_privkey_generate (params, GNUTLS_PK_RSA, bits, 0);
154 * gnutls_rsa_params_import_pkcs1 - import RSA params from a pkcs1 structure
155 * @params: A structure where the parameters will be copied to
156 * @pkcs1_params: should contain a PKCS1 RSAPublicKey structure PEM or DER encoded
157 * @format: the format of params. PEM or DER.
159 * This function will extract the RSAPublicKey found in a PKCS1 formatted
160 * structure.
162 * If the structure is PEM encoded, it should have a header
163 * of "BEGIN RSA PRIVATE KEY".
165 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
168 gnutls_rsa_params_import_pkcs1 (gnutls_rsa_params_t params,
169 const gnutls_datum_t * pkcs1_params,
170 gnutls_x509_crt_fmt_t format)
172 return gnutls_x509_privkey_import (params, pkcs1_params, format);
176 * gnutls_rsa_params_export_pkcs1 - export RSA params to a pkcs1 structure
177 * @params: Holds the RSA parameters
178 * @format: the format of output params. One of PEM or DER.
179 * @params_data: will contain a PKCS1 RSAPublicKey structure PEM or DER encoded
180 * @params_data_size: holds the size of params_data (and will be replaced by the actual size of parameters)
182 * This function will export the given RSA parameters to a PKCS1
183 * RSAPublicKey structure. If the buffer provided is not long enough to
184 * hold the output, then GNUTLS_E_SHORT_MEMORY_BUFFER will be returned.
186 * If the structure is PEM encoded, it will have a header
187 * of "BEGIN RSA PRIVATE KEY".
189 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
192 gnutls_rsa_params_export_pkcs1 (gnutls_rsa_params_t params,
193 gnutls_x509_crt_fmt_t format,
194 unsigned char *params_data,
195 size_t * params_data_size)
197 return gnutls_x509_privkey_export (params, format,
198 params_data, params_data_size);
202 * gnutls_rsa_params_export_raw - export the RSA parameters
203 * @params: a structure that holds the rsa parameters
204 * @m: will hold the modulus
205 * @e: will hold the public exponent
206 * @d: will hold the private exponent
207 * @p: will hold the first prime (p)
208 * @q: will hold the second prime (q)
209 * @u: will hold the coefficient
210 * @bits: if non null will hold the prime's number of bits
212 * This function will export the RSA parameters found in the given
213 * structure. The new parameters will be allocated using
214 * gnutls_malloc() and will be stored in the appropriate datum.
216 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
219 gnutls_rsa_params_export_raw (gnutls_rsa_params_t params,
220 gnutls_datum_t * m, gnutls_datum_t * e,
221 gnutls_datum_t * d, gnutls_datum_t * p,
222 gnutls_datum_t * q, gnutls_datum_t * u,
223 unsigned int *bits)
225 int ret;
227 ret = gnutls_x509_privkey_export_rsa_raw (params, m, e, d, p, q, u);
228 if (ret < 0)
230 gnutls_assert ();
231 return ret;
234 if (bits)
235 *bits = _gnutls_mpi_get_nbits (params->params[3]);
237 return 0;