aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/x509/x509_privkey.c
blob: b935c3f8a7e935c65f60ff5d93ce91f52a1e6312 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

#include <gnutls_int.h>
#include <gnutls_datum.h>
#include <gnutls_global.h>
#include <gnutls_errors.h>
#include <gnutls_rsa_export.h>
#include <gnutls_sig.h>
#include <common.h>
#include <gnutls_x509.h>
#include <x509_b64.h>
#include <x509.h>
#include <mpi.h>
#include <extensions.h>

/* remove this when libgcrypt can handle the PKCS #1 coefficients from
 * rsa keys
 */
#define CALC_COEFF 1

/**
 * MHD_gnutls_x509_privkey_init - This function initializes a MHD_gnutls_crl structure
 * @key: The structure to be initialized
 *
 * This function will initialize an private key structure.
 *
 * Returns 0 on success.
 *
 **/
int
MHD_gnutls_x509_privkey_init (MHD_gnutls_x509_privkey_t * key)
{
  *key = MHD_gnutls_calloc (1, sizeof (MHD_gnutls_x509_privkey_int));

  if (*key)
    {
      (*key)->key = ASN1_TYPE_EMPTY;
      (*key)->pk_algorithm = MHD_GNUTLS_PK_UNKNOWN;
      return 0;                 /* success */
    }

  return GNUTLS_E_MEMORY_ERROR;
}

/**
 * MHD_gnutls_x509_privkey_deinit - This function deinitializes memory used by a MHD_gnutls_x509_privkey_t structure
 * @key: The structure to be initialized
 *
 * This function will deinitialize a private key structure.
 *
 **/
void
MHD_gnutls_x509_privkey_deinit (MHD_gnutls_x509_privkey_t key)
{
  int i;

  if (!key)
    return;

  for (i = 0; i < key->params_size; i++)
    {
      MHD_gtls_mpi_release (&key->params[i]);
    }

  MHD__asn1_delete_structure (&key->key);
  MHD_gnutls_free (key);
}


/* Converts an RSA PKCS#1 key to
 * an internal structure (MHD_gnutls_private_key)
 */
ASN1_TYPE
MHD__gnutls_privkey_decode_pkcs1_rsa_key (const MHD_gnutls_datum_t * raw_key,
                                          MHD_gnutls_x509_privkey_t pkey)
{
  int result;
  ASN1_TYPE pkey_asn;

  if ((result = MHD__asn1_create_element (MHD__gnutls_getMHD__gnutls_asn (),
                                          "GNUTLS.RSAPrivateKey",
                                          &pkey_asn)) != ASN1_SUCCESS)
    {
      MHD_gnutls_assert ();
      return NULL;
    }

  if ((sizeof (pkey->params) / sizeof (mpi_t)) < RSA_PRIVATE_PARAMS)
    {
      MHD_gnutls_assert ();
      /* internal error. Increase the mpi_ts in params */
      return NULL;
    }

  result =
    MHD__asn1_der_decoding (&pkey_asn, raw_key->data, raw_key->size, NULL);
  if (result != ASN1_SUCCESS)
    {
      MHD_gnutls_assert ();
      goto error;
    }

  if ((result =
       MHD__gnutls_x509_read_int (pkey_asn, "modulus", &pkey->params[0])) < 0)
    {
      MHD_gnutls_assert ();
      goto error;
    }

  if ((result = MHD__gnutls_x509_read_int (pkey_asn, "publicExponent",
                                           &pkey->params[1])) < 0)
    {
      MHD_gnutls_assert ();
      goto error;
    }

  if ((result = MHD__gnutls_x509_read_int (pkey_asn, "privateExponent",
                                           &pkey->params[2])) < 0)
    {
      MHD_gnutls_assert ();
      goto error;
    }

  if ((result =
       MHD__gnutls_x509_read_int (pkey_asn, "prime1", &pkey->params[3])) < 0)
    {
      MHD_gnutls_assert ();
      goto error;
    }

  if ((result =
       MHD__gnutls_x509_read_int (pkey_asn, "prime2", &pkey->params[4])) < 0)
    {
      MHD_gnutls_assert ();
      goto error;
    }

#ifdef CALC_COEFF
  /* Calculate the coefficient. This is because the gcrypt
   * library is uses the p,q in the reverse order.
   */
  pkey->params[5] =
    MHD__gnutls_mpi_snew (MHD__gnutls_mpi_get_nbits (pkey->params[0]));

  if (pkey->params[5] == NULL)
    {
      MHD_gnutls_assert ();
      goto error;
    }

  MHD__gnutls_mpi_invm (pkey->params[5], pkey->params[3], pkey->params[4]);
  /* p, q */
#else
  if ((result = MHD__gnutls_x509_read_int (pkey_asn, "coefficient",
                                           &pkey->params[5])) < 0)
    {
      MHD_gnutls_assert ();
      goto error;
    }
#endif
  pkey->params_size = 6;

  return pkey_asn;

error:MHD__asn1_delete_structure (&pkey_asn);
  MHD_gtls_mpi_release (&pkey->params[0]);
  MHD_gtls_mpi_release (&pkey->params[1]);
  MHD_gtls_mpi_release (&pkey->params[2]);
  MHD_gtls_mpi_release (&pkey->params[3]);
  MHD_gtls_mpi_release (&pkey->params[4]);
  MHD_gtls_mpi_release (&pkey->params[5]);
  return NULL;

}

#define PEM_KEY_RSA "RSA PRIVATE KEY"

/**
 * MHD_gnutls_x509_privkey_import - This function will import a DER or PEM encoded key
 * @key: The structure to store the parsed key
 * @data: The DER or PEM encoded certificate.
 * @format: One of DER or PEM
 *
 * This function will convert the given DER or PEM encoded key
 * to the native MHD_gnutls_x509_privkey_t format. The output will be stored in @key .
 *
 * If the key is PEM encoded it should have a header of "RSA PRIVATE KEY", or
 * "DSA PRIVATE KEY".
 *
 * Returns 0 on success.
 *
 **/
int
MHD_gnutls_x509_privkey_import (MHD_gnutls_x509_privkey_t key,
                                const MHD_gnutls_datum_t * data,
                                MHD_gnutls_x509_crt_fmt_t format)
{
  int result = 0, need_free = 0;
  MHD_gnutls_datum_t _data;

  if (key == NULL)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_INVALID_REQUEST;
    }

  _data.data = data->data;
  _data.size = data->size;

  key->pk_algorithm = MHD_GNUTLS_PK_UNKNOWN;

  /* If the Certificate is in PEM format then decode it */
  if (format == GNUTLS_X509_FMT_PEM)
    {
      opaque *out;

      /* Try the first header */
      result
        =
        MHD__gnutls_fbase64_decode (PEM_KEY_RSA, data->data, data->size,
                                    &out);
      key->pk_algorithm = MHD_GNUTLS_PK_RSA;

      _data.data = out;
      _data.size = result;

      need_free = 1;
    }

  if (key->pk_algorithm == MHD_GNUTLS_PK_RSA)
    {
      key->key = MHD__gnutls_privkey_decode_pkcs1_rsa_key (&_data, key);
      if (key->key == NULL)
        MHD_gnutls_assert ();
    }
  else
    {
      /* Try decoding with both, and accept the one that succeeds. */
      key->pk_algorithm = MHD_GNUTLS_PK_RSA;
      key->key = MHD__gnutls_privkey_decode_pkcs1_rsa_key (&_data, key);

      // TODO rm
//      if (key->key == NULL)
//        {
//          key->pk_algorithm = GNUTLS_PK_DSA;
//          key->key = decode_dsa_key(&_data, key);
//          if (key->key == NULL)
//            MHD_gnutls_assert();
//        }
    }

  if (key->key == NULL)
    {
      MHD_gnutls_assert ();
      result = GNUTLS_E_ASN1_DER_ERROR;
      key->pk_algorithm = MHD_GNUTLS_PK_UNKNOWN;
      return result;
    }

  if (need_free)
    MHD__gnutls_free_datum (&_data);

  /* The key has now been decoded.
   */

  return 0;
}