Debian Codegraph @

src/lib/crypto/BotanRSAPrivateKey.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 BotanRSAPrivateKey.cpp
29
30 Botan RSA private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "BotanRSAPrivateKey.h"
36 #include "BotanUtil.h"
37 #include "BotanRNG.h"
38 #include "BotanCryptoFactory.h"
39 #include <string.h>
40 #include <botan/pkcs8.h>
41 #include <botan/pkcs8.h>
42 #include <botan/ber_dec.h>
43 #include <botan/der_enc.h>
44 #include <botan/oids.h>
45 #include <botan/version.h>
46
47 // Constructors
48 BotanRSAPrivateKey::BotanRSAPrivateKey()
49 {
50 rsa = NULL;
51 }
52
53 BotanRSAPrivateKey::BotanRSAPrivateKey(const Botan::RSA_PrivateKey* inRSA)
54 {
55 rsa = NULL;
56
57 setFromBotan(inRSA);
58 }
59
60 // Destructor
61 BotanRSAPrivateKey::~BotanRSAPrivateKey()
62 {
63 delete rsa;
64 }
65
66 // The type
67 /*static*/ const char* BotanRSAPrivateKey::type = "Botan RSA Private Key";
68
69 // Set from Botan representation
70 void BotanRSAPrivateKey::setFromBotan(const Botan::RSA_PrivateKey* inRSA)
71 {
72 ByteString inP = BotanUtil::bigInt2ByteString(inRSA->get_p());
73 setP(inP);
74 ByteString inQ = BotanUtil::bigInt2ByteString(inRSA->get_q());
75 setQ(inQ);
76 ByteString inDP1 = BotanUtil::bigInt2ByteString(inRSA->get_d1());
77 setDP1(inDP1);
78 ByteString inDQ1 = BotanUtil::bigInt2ByteString(inRSA->get_d2());
79 setDQ1(inDQ1);
80 ByteString inPQ = BotanUtil::bigInt2ByteString(inRSA->get_c());
81 setPQ(inPQ);
82 ByteString inD = BotanUtil::bigInt2ByteString(inRSA->get_d());
83 setD(inD);
84 ByteString inN = BotanUtil::bigInt2ByteString(inRSA->get_n());
85 setN(inN);
86 ByteString inE = BotanUtil::bigInt2ByteString(inRSA->get_e());
87 setE(inE);
88 }
89
90 // Check if the key is of the given type
91 bool BotanRSAPrivateKey::isOfType(const char* inType)
92 {
93 return !strcmp(type, inType);
94 }
95
96 // Setters for the RSA private key components
97 void BotanRSAPrivateKey::setP(const ByteString& inP)
98 {
99 RSAPrivateKey::setP(inP);
100
101 if (rsa)
102 {
103 delete rsa;
104 rsa = NULL;
105 }
106 }
107
108 void BotanRSAPrivateKey::setQ(const ByteString& inQ)
109 {
110 RSAPrivateKey::setQ(inQ);
111
112 if (rsa)
113 {
114 delete rsa;
115 rsa = NULL;
116 }
117 }
118
119 void BotanRSAPrivateKey::setPQ(const ByteString& inPQ)
120 {
121 RSAPrivateKey::setPQ(inPQ);
122
123 if (rsa)
124 {
125 delete rsa;
126 rsa = NULL;
127 }
128 }
129
130 void BotanRSAPrivateKey::setDP1(const ByteString& inDP1)
131 {
132 RSAPrivateKey::setDP1(inDP1);
133
134 if (rsa)
135 {
136 delete rsa;
137 rsa = NULL;
138 }
139 }
140
141 void BotanRSAPrivateKey::setDQ1(const ByteString& inDQ1)
142 {
143 RSAPrivateKey::setDQ1(inDQ1);
144
145 if (rsa)
146 {
147 delete rsa;
148 rsa = NULL;
149 }
150 }
151
152 void BotanRSAPrivateKey::setD(const ByteString& inD)
153 {
154 RSAPrivateKey::setD(inD);
155
156 if (rsa)
157 {
158 delete rsa;
159 rsa = NULL;
160 }
161 }
162
163
164 // Setters for the RSA public key components
165 void BotanRSAPrivateKey::setN(const ByteString& inN)
166 {
167 RSAPrivateKey::setN(inN);
168
169 if (rsa)
170 {
171 delete rsa;
172 rsa = NULL;
173 }
174 }
175
176 void BotanRSAPrivateKey::setE(const ByteString& inE)
177 {
178 RSAPrivateKey::setE(inE);
179
180 if (rsa)
181 {
182 delete rsa;
183 rsa = NULL;
184 }
185 }
186
187 // Encode into PKCS#8 DER
188 ByteString BotanRSAPrivateKey::PKCS8Encode()
189 {
190 ByteString der;
191 createBotanKey();
192 if (rsa == NULL) return der;
193 const auto ber = Botan::PKCS8::BER_encode(*rsa);
194 der.resize(ber.size());
195 memcpy(&der[0], &ber[0], ber.size());
196 return der;
197 }
198
199 // Decode from PKCS#8 BER
200 bool BotanRSAPrivateKey::PKCS8Decode(const ByteString& ber)
201 {
202 Botan::DataSource_Memory source(ber.const_byte_str(), ber.size());
203 if (source.end_of_data()) return false;
204 Botan::secure_vector<uint8_t> keydata;
205 Botan::AlgorithmIdentifier alg_id;
206 Botan::RSA_PrivateKey* key = NULL;
207 try
208 {
209
210 Botan::BER_Decoder(source)
211 .start_cons(Botan::SEQUENCE)
212 .decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
213 .decode(alg_id)
214 .decode(keydata, Botan::OCTET_STRING)
215 .discard_remaining()
216 .end_cons();
217 if (keydata.empty())
218 throw Botan::Decoding_Error("PKCS #8 private key decoding failed");
219 if (Botan::OIDS::lookup(alg_id.oid).compare("RSA"))
220 {
221 ERROR_MSG("Decoded private key not RSA");
222
223 return false;
224 }
225 key = new Botan::RSA_PrivateKey(alg_id, keydata);
226 if (key == NULL) return false;
227
228 setFromBotan(key);
229
230 delete key;
231 }
232 catch (std::exception& e)
233 {
234 ERROR_MSG("Decode failed on %s", e.what());
235
236 return false;
237 }
238
239 return true;
240 }
241
242 // Retrieve the Botan representation of the key
243 Botan::RSA_PrivateKey* BotanRSAPrivateKey::getBotanKey()
244 {
245 if (!rsa)
246 {
247 createBotanKey();
248 }
249
250 return rsa;
251 }
252
253 // Create the Botan representation of the key
254 void BotanRSAPrivateKey::createBotanKey()
255 {
256 // d and n is not needed, they can be calculated
257 if (p.size() != 0 &&
258 q.size() != 0 &&
259 e.size() != 0)
260 {
261 if (rsa)
262 {
263 delete rsa;
264 rsa = NULL;
265 }
266
267 try
268 {
269 rsa = new Botan::RSA_PrivateKey(
270 BotanUtil::byteString2bigInt(p),
271 BotanUtil::byteString2bigInt(q),
272 BotanUtil::byteString2bigInt(e),
273 BotanUtil::byteString2bigInt(d),
274 BotanUtil::byteString2bigInt(n));
275 }
276 catch (...)
277 {
278 ERROR_MSG("Could not create the Botan private key");
279 }
280 }
281 }