Remove.
[gnutls.git] / lib / gnutls_rsa_export.c
blob0f5a1ef7424b8160f4784ea46019382197623cd6
1 /*
2 * Copyright (C) 2002, 2003, 2004, 2005, 2008, 2009, 2010 Free Software
3 * Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
26 /* This file contains code for RSA temporary keys. These keys are
27 * only used in export cipher suites.
30 #include <gnutls_int.h>
31 #include <gnutls_errors.h>
32 #include <gnutls_datum.h>
33 #include <gnutls_rsa_export.h>
34 #include "x509/x509_int.h"
35 #include "debug.h"
37 /* returns e and m, depends on the requested bits.
38 * We only support limited key sizes.
40 const bigint_t *
41 _gnutls_rsa_params_to_mpi (gnutls_rsa_params_t rsa_params)
43 if (rsa_params == NULL)
45 return NULL;
48 return rsa_params->params;
51 /**
52 * gnutls_rsa_params_import_raw:
53 * @rsa_params: Is a structure will hold the parameters
54 * @m: holds the modulus
55 * @e: holds the public exponent
56 * @d: holds the private exponent
57 * @p: holds the first prime (p)
58 * @q: holds the second prime (q)
59 * @u: holds the coefficient
61 * This function will replace the parameters in the given structure.
62 * The new parameters should be stored in the appropriate
63 * gnutls_datum.
65 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
66 **/
67 int
68 gnutls_rsa_params_import_raw (gnutls_rsa_params_t rsa_params,
69 const gnutls_datum_t * m,
70 const gnutls_datum_t * e,
71 const gnutls_datum_t * d,
72 const gnutls_datum_t * p,
73 const gnutls_datum_t * q,
74 const gnutls_datum_t * u)
76 return gnutls_x509_privkey_import_rsa_raw (rsa_params, m, e, d, p, q, u);
79 /**
80 * gnutls_rsa_params_init:
81 * @rsa_params: Is a structure that will hold the parameters
83 * This function will initialize the temporary RSA parameters structure.
85 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
86 **/
87 int
88 gnutls_rsa_params_init (gnutls_rsa_params_t * rsa_params)
90 int ret;
92 ret = gnutls_x509_privkey_init (rsa_params);
93 if (ret < 0)
95 gnutls_assert ();
96 return ret;
99 (*rsa_params)->crippled = 1;
101 return 0;
105 * gnutls_rsa_params_deinit:
106 * @rsa_params: Is a structure that holds the parameters
108 * This function will deinitialize the RSA parameters structure.
110 void
111 gnutls_rsa_params_deinit (gnutls_rsa_params_t rsa_params)
113 gnutls_x509_privkey_deinit (rsa_params);
117 * gnutls_rsa_params_cpy:
118 * @dst: Is the destination structure, which should be initialized.
119 * @src: Is the source structure
121 * This function will copy the RSA parameters structure from source
122 * to destination.
124 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
127 gnutls_rsa_params_cpy (gnutls_rsa_params_t dst, gnutls_rsa_params_t src)
129 return gnutls_x509_privkey_cpy (dst, src);
133 * gnutls_rsa_params_generate2:
134 * @params: The structure where the parameters will be stored
135 * @bits: is the prime's number of bits
137 * This function will generate new temporary RSA parameters for use in
138 * RSA-EXPORT ciphersuites. This function is normally slow.
140 * Note that if the parameters are to be used in export cipher suites the
141 * bits value should be 512 or less.
142 * Also note that the generation of new RSA parameters is only useful
143 * to servers. Clients use the parameters sent by the server, thus it's
144 * no use calling this in client side.
146 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
149 gnutls_rsa_params_generate2 (gnutls_rsa_params_t params, unsigned int bits)
151 return gnutls_x509_privkey_generate (params, GNUTLS_PK_RSA, bits, 0);
155 * gnutls_rsa_params_import_pkcs1:
156 * @params: A structure where the parameters will be copied to
157 * @pkcs1_params: should contain a PKCS1 RSAPublicKey structure PEM or DER encoded
158 * @format: the format of params. PEM or DER.
160 * This function will extract the RSAPublicKey found in a PKCS1 formatted
161 * structure.
163 * If the structure is PEM encoded, it should have a header
164 * of "BEGIN RSA PRIVATE KEY".
166 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
169 gnutls_rsa_params_import_pkcs1 (gnutls_rsa_params_t params,
170 const gnutls_datum_t * pkcs1_params,
171 gnutls_x509_crt_fmt_t format)
173 return gnutls_x509_privkey_import (params, pkcs1_params, format);
177 * gnutls_rsa_params_export_pkcs1:
178 * @params: Holds the RSA parameters
179 * @format: the format of output params. One of PEM or DER.
180 * @params_data: will contain a PKCS1 RSAPublicKey structure PEM or DER encoded
181 * @params_data_size: holds the size of params_data (and will be replaced by the actual size of parameters)
183 * This function will export the given RSA parameters to a PKCS1
184 * RSAPublicKey structure. If the buffer provided is not long enough to
185 * hold the output, then GNUTLS_E_SHORT_MEMORY_BUFFER will be returned.
187 * If the structure is PEM encoded, it will have a header
188 * of "BEGIN RSA PRIVATE KEY".
190 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
193 gnutls_rsa_params_export_pkcs1 (gnutls_rsa_params_t params,
194 gnutls_x509_crt_fmt_t format,
195 unsigned char *params_data,
196 size_t * params_data_size)
198 return gnutls_x509_privkey_export (params, format,
199 params_data, params_data_size);
203 * gnutls_rsa_params_export_raw:
204 * @params: a structure that holds the rsa parameters
205 * @m: will hold the modulus
206 * @e: will hold the public exponent
207 * @d: will hold the private exponent
208 * @p: will hold the first prime (p)
209 * @q: will hold the second prime (q)
210 * @u: will hold the coefficient
211 * @bits: if non null will hold the prime's number of bits
213 * This function will export the RSA parameters found in the given
214 * structure. The new parameters will be allocated using
215 * gnutls_malloc() and will be stored in the appropriate datum.
217 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
220 gnutls_rsa_params_export_raw (gnutls_rsa_params_t params,
221 gnutls_datum_t * m, gnutls_datum_t * e,
222 gnutls_datum_t * d, gnutls_datum_t * p,
223 gnutls_datum_t * q, gnutls_datum_t * u,
224 unsigned int *bits)
226 int ret;
228 ret = gnutls_x509_privkey_export_rsa_raw (params, m, e, d, p, q, u);
229 if (ret < 0)
231 gnutls_assert ();
232 return ret;
235 if (bits)
236 *bits = _gnutls_mpi_get_nbits (params->params[3]);
238 return 0;