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
|
/*
* Copyright (C) 2001 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* GNUTLS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNUTLS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <gnutls_int.h>
#include <gnutls_errors.h>
#include <cert_b64.h>
#include <auth_x509.h>
#include <gnutls_cert.h>
#include <cert_asn1.h>
#include <cert_der.h>
#include <gnutls_datum.h>
#include <gnutls_gcry.h>
/* Converts an RSA PKCS#1 key to
* an internal structure (gnutls_private_key)
*/
int _gnutls_pkcs1key2gnutlsKey(gnutls_private_key * pkey, gnutls_datum cert) {
int result;
opaque str[5*1024];
int len = sizeof(str);
pkey->pk_algorithm = GNUTLS_PK_RSA;
/* we do return 2 MPIs
*/
pkey->params = gnutls_malloc(2*sizeof(MPI));
if (create_structure("rsakey", "PKCS-1.RSAPrivateKey")!=ASN_OK) {
gnutls_assert();
return GNUTLS_E_ASN1_ERROR;
}
result = get_der("rsakey", cert.data, cert.size);
if (result != ASN_OK) {
gnutls_assert();
return GNUTLS_E_ASN1_PARSING_ERROR;
}
len = sizeof(str);
result =
read_value("rsakey.privateExponent", str, &len);
if (result != ASN_OK) {
gnutls_assert();
delete_structure("rsakey");
return GNUTLS_E_ASN1_PARSING_ERROR;
}
if (gcry_mpi_scan( &pkey->params[0], /* u */
GCRYMPI_FMT_USG, str, &len) != 0) {
gnutls_assert();
delete_structure("rsakey");
return GNUTLS_E_MPI_SCAN_FAILED;
}
len = sizeof(str);
result =
read_value("rsakey.modulus", str, &len);
if (result != ASN_OK) {
gnutls_assert();
delete_structure("rsakey");
_gnutls_mpi_release( &pkey->params[0]);
return GNUTLS_E_ASN1_PARSING_ERROR;
}
if (gcry_mpi_scan( &pkey->params[1], /* A */
GCRYMPI_FMT_USG, str, &len) != 0) {
gnutls_assert();
delete_structure("rsakey");
_gnutls_mpi_release( &pkey->params[0]);
return GNUTLS_E_MPI_SCAN_FAILED;
}
delete_structure("rsakey");
if (gnutls_set_datum( &pkey->raw, cert.data, cert.size) < 0) {
_gnutls_mpi_release(&pkey->params[0]);
_gnutls_mpi_release(&pkey->params[1]);
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
return 0;
}
void _gnutls_free_private_key( gnutls_private_key pkey) {
int n, i;
switch( pkey.pk_algorithm) {
case GNUTLS_PK_RSA:
n = 2;/* the number of parameters in MPI* */
break;
default:
n=0;
}
for (i=0;i<n;i++) {
_gnutls_mpi_release( &pkey.params[i]);
}
gnutls_free_datum( &pkey.raw);
}
|