Debian Codegraph @

src/bin/keyconv/softhsm2-keyconv-botan.cpp

raw
1 /*
2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 softhsm2-keyconv-botan.cpp
29
30 Code specific for Botan
31 *****************************************************************************/
32
33 #include <config.h>
34 #define KEYCONV_BOTAN
35 #include "softhsm2-keyconv.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <iostream>
41 #include <fstream>
42
43 #include <botan/auto_rng.h>
44 #include <botan/pkcs8.h>
45 #include <botan/rsa.h>
46 #include <botan/dsa.h>
47 #include <botan/bigint.h>
48 #include <botan/version.h>
49
50 // Init Botan
51 void crypto_init()
52 {
53 }
54
55 // Final Botan
56 void crypto_final()
57 {
58 }
59
60 // Save the RSA key as a PKCS#8 file
61 int save_rsa_pkcs8(char* out_path, char* file_pin, key_material_t* pkey)
62 {
63 int result = 0;
64 Botan::Private_Key* priv_key = NULL;
65 Botan::AutoSeeded_RNG* rng = NULL;
66 Botan::BigInt bigE, bigP, bigQ, bigN, bigD;
67
68 // See if the key material was found.
69 if
70 (
71 pkey[TAG_MODULUS].size <= 0 ||
72 pkey[TAG_PUBEXP].size <= 0 ||
73 pkey[TAG_PRIVEXP].size <= 0 ||
74 pkey[TAG_PRIME1].size <= 0 ||
75 pkey[TAG_PRIME2].size <= 0
76 )
77 {
78 fprintf(stderr, "ERROR: Some parts of the key material is missing in the input file.\n");
79 return 1;
80 }
81
82 bigE = Botan::BigInt((Botan::byte*)pkey[TAG_PUBEXP].big, pkey[TAG_PUBEXP].size);
83 bigP = Botan::BigInt((Botan::byte*)pkey[TAG_PRIME1].big, pkey[TAG_PRIME1].size);
84 bigQ = Botan::BigInt((Botan::byte*)pkey[TAG_PRIME2].big, pkey[TAG_PRIME2].size);
85 bigN = Botan::BigInt((Botan::byte*)pkey[TAG_MODULUS].big, pkey[TAG_MODULUS].size);
86 bigD = Botan::BigInt((Botan::byte*)pkey[TAG_PRIVEXP].big, pkey[TAG_PRIVEXP].size);
87
88 rng = new Botan::AutoSeeded_RNG();
89
90 try
91 {
92 priv_key = new Botan::RSA_PrivateKey(bigP, bigQ, bigE, bigD, bigN);
93 }
94 catch(std::exception& e)
95 {
96 fprintf(stderr, "%s\n", e.what());
97 fprintf(stderr, "ERROR: Could not extract the private key from the file.\n");
98 delete rng;
99 return 1;
100 }
101
102 std::ofstream priv_file(out_path);
103 if (!priv_file.is_open())
104 {
105 fprintf(stderr, "ERROR: Could not open file for output.\n");
106 delete rng;
107 delete priv_key;
108 return 1;
109 }
110
111 try
112 {
113 if (file_pin == NULL)
114 {
115 priv_file << Botan::PKCS8::PEM_encode(*priv_key);
116 }
117 else
118 {
119 priv_file << Botan::PKCS8::PEM_encode(*priv_key, *rng, file_pin, std::chrono::milliseconds(300), "PBE-PKCS5v15(MD5,DES/CBC)");
120 }
121
122 printf("The key has been written to %s\n", out_path);
123 }
124 catch(std::exception& e)
125 {
126 fprintf(stderr, "%s\n", e.what());
127 fprintf(stderr, "ERROR: Could not write to file.\n");
128 result = 1;
129 }
130
131 delete rng;
132 delete priv_key;
133 priv_file.close();
134
135 return result;
136 }
137
138 // Save the DSA key as a PKCS#8 file
139 int save_dsa_pkcs8(char* out_path, char* file_pin, key_material_t* pkey)
140 {
141 int result = 0;
142 Botan::Private_Key* priv_key = NULL;
143 Botan::AutoSeeded_RNG* rng = NULL;
144 Botan::BigInt bigDP, bigDQ, bigDG, bigDX;
145
146 // See if the key material was found.
147 if
148 (
149 pkey[TAG_PRIME].size <= 0 ||
150 pkey[TAG_SUBPRIME].size <= 0 ||
151 pkey[TAG_BASE].size <= 0 ||
152 pkey[TAG_PRIVVAL].size <= 0
153 )
154 {
155 fprintf(stderr, "ERROR: Some parts of the key material is missing in the input file.\n");
156 return 1;
157 }
158
159 bigDP = Botan::BigInt((Botan::byte*)pkey[TAG_PRIME].big, pkey[TAG_PRIME].size);
160 bigDQ = Botan::BigInt((Botan::byte*)pkey[TAG_SUBPRIME].big, pkey[TAG_SUBPRIME].size);
161 bigDG = Botan::BigInt((Botan::byte*)pkey[TAG_BASE].big, pkey[TAG_BASE].size);
162 bigDX = Botan::BigInt((Botan::byte*)pkey[TAG_PRIVVAL].big, pkey[TAG_PRIVVAL].size);
163
164 rng = new Botan::AutoSeeded_RNG();
165
166 try
167 {
168 priv_key = new Botan::DSA_PrivateKey(*rng, Botan::DL_Group(bigDP, bigDQ, bigDG), bigDX);
169 }
170 catch (std::exception& e)
171 {
172 fprintf(stderr, "%s\n", e.what());
173 fprintf(stderr, "ERROR: Could not extract the private key from the file.\n");
174 delete rng;
175 return 1;
176 }
177
178 std::ofstream priv_file(out_path);
179 if (!priv_file.is_open())
180 {
181 fprintf(stderr, "ERROR: Could not open file for output.\n");
182 delete rng;
183 delete priv_key;
184 return 1;
185 }
186
187 try
188 {
189 if (file_pin == NULL)
190 {
191 priv_file << Botan::PKCS8::PEM_encode(*priv_key);
192 }
193 else
194 {
195 priv_file << Botan::PKCS8::PEM_encode(*priv_key, *rng, file_pin, std::chrono::milliseconds(300), "PBE-PKCS5v15(MD5,DES/CBC)");
196 }
197
198 printf("The key has been written to %s\n", out_path);
199 }
200 catch (std::exception& e)
201 {
202 fprintf(stderr, "%s\n", e.what());
203 fprintf(stderr, "ERROR: Could not write to file.\n");
204 result = 1;
205 }
206
207 delete rng;
208 delete priv_key;
209 priv_file.close();
210
211 return result;
212 }