Guile: Fix `x509-certificate-dn-oid' and related functions.
[gnutls.git] / lib / gnutls_rsa_export.c
blob8344fe99ab51f4e16bbee05e49e38a66549302e0
1 /*
2 * Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation
4 * Author: Nikos Mavroyanopoulos
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.h"
34 #include "x509/privkey.h"
35 #include "debug.h"
37 /* This function takes a number of bits and returns a supported
38 * number of bits. Ie a number of bits that we have a prime in the
39 * dh_primes structure.
42 #define MAX_SUPPORTED_BITS 512
44 /* returns e and m, depends on the requested bits.
45 * We only support limited key sizes.
47 const mpi_t *
48 _gnutls_rsa_params_to_mpi (gnutls_rsa_params_t rsa_params)
50 if (rsa_params == NULL)
52 return NULL;
55 return rsa_params->params;
59 /* resarr will contain: modulus(0), public exponent(1), private exponent(2),
60 * prime1 - p (3), prime2 - q(4), u (5).
62 int
63 _gnutls_rsa_generate_params (mpi_t * resarr, int *resarr_len, int bits)
66 int ret;
67 gcry_sexp_t parms, key, list;
69 ret = gcry_sexp_build (&parms, NULL, "(genkey(rsa(nbits %d)))", bits);
70 if (ret != 0)
72 gnutls_assert ();
73 return GNUTLS_E_INTERNAL_ERROR;
76 /* generate the RSA key */
77 ret = gcry_pk_genkey (&key, parms);
78 gcry_sexp_release (parms);
80 if (ret != 0)
82 gnutls_assert ();
83 return GNUTLS_E_INTERNAL_ERROR;
86 list = gcry_sexp_find_token (key, "n", 0);
87 if (list == NULL)
89 gnutls_assert ();
90 gcry_sexp_release (key);
91 return GNUTLS_E_INTERNAL_ERROR;
94 resarr[0] = gcry_sexp_nth_mpi (list, 1, 0);
95 gcry_sexp_release (list);
97 list = gcry_sexp_find_token (key, "e", 0);
98 if (list == NULL)
100 gnutls_assert ();
101 gcry_sexp_release (key);
102 return GNUTLS_E_INTERNAL_ERROR;
105 resarr[1] = gcry_sexp_nth_mpi (list, 1, 0);
106 gcry_sexp_release (list);
108 list = gcry_sexp_find_token (key, "d", 0);
109 if (list == NULL)
111 gnutls_assert ();
112 gcry_sexp_release (key);
113 return GNUTLS_E_INTERNAL_ERROR;
116 resarr[2] = gcry_sexp_nth_mpi (list, 1, 0);
117 gcry_sexp_release (list);
119 list = gcry_sexp_find_token (key, "p", 0);
120 if (list == NULL)
122 gnutls_assert ();
123 gcry_sexp_release (key);
124 return GNUTLS_E_INTERNAL_ERROR;
127 resarr[3] = gcry_sexp_nth_mpi (list, 1, 0);
128 gcry_sexp_release (list);
131 list = gcry_sexp_find_token (key, "q", 0);
132 if (list == NULL)
134 gnutls_assert ();
135 gcry_sexp_release (key);
136 return GNUTLS_E_INTERNAL_ERROR;
139 resarr[4] = gcry_sexp_nth_mpi (list, 1, 0);
140 gcry_sexp_release (list);
143 list = gcry_sexp_find_token (key, "u", 0);
144 if (list == NULL)
146 gnutls_assert ();
147 gcry_sexp_release (key);
148 return GNUTLS_E_INTERNAL_ERROR;
151 resarr[5] = gcry_sexp_nth_mpi (list, 1, 0);
152 gcry_sexp_release (list);
154 gcry_sexp_release (key);
156 _gnutls_dump_mpi ("n: ", resarr[0]);
157 _gnutls_dump_mpi ("e: ", resarr[1]);
158 _gnutls_dump_mpi ("d: ", resarr[2]);
159 _gnutls_dump_mpi ("p: ", resarr[3]);
160 _gnutls_dump_mpi ("q: ", resarr[4]);
161 _gnutls_dump_mpi ("u: ", resarr[5]);
163 *resarr_len = 6;
165 return 0;
171 * gnutls_rsa_params_import_raw - This function will replace the old RSA parameters
172 * @rsa_params: Is a structure will hold the parameters
173 * @m: holds the modulus
174 * @e: holds the public exponent
175 * @d: holds the private exponent
176 * @p: holds the first prime (p)
177 * @q: holds the second prime (q)
178 * @u: holds the coefficient
180 * This function will replace the parameters in the given structure.
181 * The new parameters should be stored in the appropriate gnutls_datum.
185 gnutls_rsa_params_import_raw (gnutls_rsa_params_t rsa_params,
186 const gnutls_datum_t * m,
187 const gnutls_datum_t * e,
188 const gnutls_datum_t * d,
189 const gnutls_datum_t * p,
190 const gnutls_datum_t * q,
191 const gnutls_datum_t * u)
193 return gnutls_x509_privkey_import_rsa_raw (rsa_params, m, e, d, p, q, u);
197 * gnutls_rsa_params_init - This function will initialize the temporary RSA parameters
198 * @rsa_params: Is a structure that will hold the parameters
200 * This function will initialize the temporary RSA parameters structure.
204 gnutls_rsa_params_init (gnutls_rsa_params_t * rsa_params)
206 int ret;
208 ret = gnutls_x509_privkey_init (rsa_params);
209 if (ret < 0)
211 gnutls_assert ();
212 return ret;
215 (*rsa_params)->crippled = 1;
217 return 0;
221 * gnutls_rsa_params_deinit - This function will deinitialize the RSA parameters
222 * @rsa_params: Is a structure that holds the parameters
224 * This function will deinitialize the RSA parameters structure.
227 void
228 gnutls_rsa_params_deinit (gnutls_rsa_params_t rsa_params)
230 gnutls_x509_privkey_deinit (rsa_params);
234 * gnutls_rsa_params_cpy - This function will copy an RSA parameters structure
235 * @dst: Is the destination structure, which should be initialized.
236 * @src: Is the source structure
238 * This function will copy the RSA parameters structure from source
239 * to destination.
243 gnutls_rsa_params_cpy (gnutls_rsa_params_t dst, gnutls_rsa_params_t src)
245 return gnutls_x509_privkey_cpy (dst, src);
249 * gnutls_rsa_params_generate2 - This function will generate temporary RSA parameters
250 * @params: The structure where the parameters will be stored
251 * @bits: is the prime's number of bits
253 * This function will generate new temporary RSA parameters for use in
254 * RSA-EXPORT ciphersuites. This function is normally slow.
256 * Note that if the parameters are to be used in export cipher suites the
257 * bits value should be 512 or less.
258 * Also note that the generation of new RSA parameters is only useful
259 * to servers. Clients use the parameters sent by the server, thus it's
260 * no use calling this in client side.
264 gnutls_rsa_params_generate2 (gnutls_rsa_params_t params, unsigned int bits)
266 return gnutls_x509_privkey_generate (params, GNUTLS_PK_RSA, bits, 0);
270 * gnutls_rsa_params_import_pkcs1 - This function will import RSA params from a pkcs1 structure
271 * @params: A structure where the parameters will be copied to
272 * @pkcs1_params: should contain a PKCS1 RSAPublicKey structure PEM or DER encoded
273 * @format: the format of params. PEM or DER.
275 * This function will extract the RSAPublicKey found in a PKCS1 formatted
276 * structure.
278 * If the structure is PEM encoded, it should have a header
279 * of "BEGIN RSA PRIVATE KEY".
281 * In case of failure a negative value will be returned, and
282 * 0 on success.
286 gnutls_rsa_params_import_pkcs1 (gnutls_rsa_params_t params,
287 const gnutls_datum_t * pkcs1_params,
288 gnutls_x509_crt_fmt_t format)
290 return gnutls_x509_privkey_import (params, pkcs1_params, format);
295 * gnutls_rsa_params_export_pkcs1 - This function will export RSA params to a pkcs1 structure
296 * @params: Holds the RSA parameters
297 * @format: the format of output params. One of PEM or DER.
298 * @params_data: will contain a PKCS1 RSAPublicKey structure PEM or DER encoded
299 * @params_data_size: holds the size of params_data (and will be replaced by the actual size of parameters)
301 * This function will export the given RSA parameters to a PKCS1
302 * RSAPublicKey structure. If the buffer provided is not long enough to
303 * hold the output, then GNUTLS_E_SHORT_MEMORY_BUFFER will be returned.
305 * If the structure is PEM encoded, it will have a header
306 * of "BEGIN RSA PRIVATE KEY".
308 * In case of failure a negative value will be returned, and
309 * 0 on success.
313 gnutls_rsa_params_export_pkcs1 (gnutls_rsa_params_t params,
314 gnutls_x509_crt_fmt_t format,
315 unsigned char *params_data,
316 size_t * params_data_size)
318 return gnutls_x509_privkey_export (params, format,
319 params_data, params_data_size);
324 * gnutls_rsa_params_export_raw - This function will export the RSA parameters
325 * @params: a structure that holds the rsa parameters
326 * @m: will hold the modulus
327 * @e: will hold the public exponent
328 * @d: will hold the private exponent
329 * @p: will hold the first prime (p)
330 * @q: will hold the second prime (q)
331 * @u: will hold the coefficient
332 * @bits: if non null will hold the prime's number of bits
334 * This function will export the RSA parameters found in the given
335 * structure. The new parameters will be allocated using
336 * gnutls_malloc() and will be stored in the appropriate datum.
340 gnutls_rsa_params_export_raw (gnutls_rsa_params_t params,
341 gnutls_datum_t * m, gnutls_datum_t * e,
342 gnutls_datum_t * d, gnutls_datum_t * p,
343 gnutls_datum_t * q, gnutls_datum_t * u,
344 unsigned int *bits)
346 int ret;
348 ret = gnutls_x509_privkey_export_rsa_raw (params, m, e, d, p, q, u);
349 if (ret < 0)
351 gnutls_assert ();
352 return ret;
355 if (bits)
356 *bits = _gnutls_mpi_get_nbits (params->params[3]);
358 return 0;