Imported Upstream version 3.3.5
[platform/upstream/gnutls.git] / lib / gnutls_rsa_export.c
1 /*
2  * Copyright (C) 2002-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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/>
20  *
21  */
22
23 /* This file contains code for RSA temporary keys. These keys are
24  * only used in export cipher suites.
25  */
26
27 #include <gnutls_int.h>
28 #include <gnutls_errors.h>
29 #include <gnutls_datum.h>
30 #include "x509/x509_int.h"
31 #include "debug.h"
32
33 #ifdef ENABLE_RSA_EXPORT
34
35 /* The are included for binary compatibility with previous versions
36  * only */
37
38 /**
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
47  *
48  * This function will replace the parameters in the given structure.
49  * The new parameters should be stored in the appropriate
50  * gnutls_datum.
51  *
52  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
53  **/
54 int
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)
62 {
63         return gnutls_x509_privkey_import_rsa_raw(rsa_params, m, e, d, p,
64                                                   q, u);
65 }
66
67 /**
68  * gnutls_rsa_params_init:
69  * @rsa_params: Is a structure that will hold the parameters
70  *
71  * This function will initialize the temporary RSA parameters structure.
72  *
73  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
74  **/
75 int gnutls_rsa_params_init(gnutls_rsa_params_t * rsa_params)
76 {
77         int ret;
78
79         ret = gnutls_x509_privkey_init(rsa_params);
80         if (ret < 0) {
81                 gnutls_assert();
82                 return ret;
83         }
84
85         return 0;
86 }
87
88 /**
89  * gnutls_rsa_params_deinit:
90  * @rsa_params: Is a structure that holds the parameters
91  *
92  * This function will deinitialize the RSA parameters structure.
93  **/
94 void gnutls_rsa_params_deinit(gnutls_rsa_params_t rsa_params)
95 {
96         gnutls_x509_privkey_deinit(rsa_params);
97 }
98
99 /**
100  * gnutls_rsa_params_cpy:
101  * @dst: Is the destination structure, which should be initialized.
102  * @src: Is the source structure
103  *
104  * This function will copy the RSA parameters structure from source
105  * to destination.
106  *
107  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
108  **/
109 int gnutls_rsa_params_cpy(gnutls_rsa_params_t dst, gnutls_rsa_params_t src)
110 {
111         return gnutls_x509_privkey_cpy(dst, src);
112 }
113
114 /**
115  * gnutls_rsa_params_generate2:
116  * @params: The structure where the parameters will be stored
117  * @bits: is the prime's number of bits
118  *
119  * This function will generate new temporary RSA parameters for use in
120  * RSA-EXPORT ciphersuites.  This function is normally slow.
121  *
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.
127  *
128  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
129  **/
130 int
131 gnutls_rsa_params_generate2(gnutls_rsa_params_t params, unsigned int bits)
132 {
133         return gnutls_x509_privkey_generate(params, GNUTLS_PK_RSA, bits,
134                                             0);
135 }
136
137 /**
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.
142  *
143  * This function will extract the RSAPrivateKey found in a PKCS1 formatted
144  * structure.
145  *
146  * If the structure is PEM encoded, it should have a header
147  * of "BEGIN RSA PRIVATE KEY".
148  *
149  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
150  **/
151 int
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)
155 {
156         return gnutls_x509_privkey_import(params, pkcs1_params, format);
157 }
158
159 /**
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)
165  *
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.
169  *
170  * If the structure is PEM encoded, it will have a header
171  * of "BEGIN RSA PRIVATE KEY".
172  *
173  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
174  **/
175 int
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)
180 {
181         return gnutls_x509_privkey_export(params, format,
182                                           params_data, params_data_size);
183 }
184
185 /**
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
195  *
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.
199  *
200  * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
201  **/
202 int
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,
207                              unsigned int *bits)
208 {
209         int ret;
210
211         ret = gnutls_x509_privkey_export_rsa_raw(rsa, m, e, d, p, q, u);
212         if (ret < 0) {
213                 gnutls_assert();
214                 return ret;
215         }
216
217         if (bits)
218                 *bits = _gnutls_mpi_get_nbits(rsa->params.params[3]);
219
220         return 0;
221 }
222
223 #endif                          /* ENABLE_RSA_EXPORT */