Debian Codegraph @

src/lib/crypto/BotanDHPrivateKey.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 BotanDHPrivateKey.cpp
29
30 Botan Diffie-Hellman private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "BotanDHPrivateKey.h"
36 #include "BotanCryptoFactory.h"
37 #include "BotanRNG.h"
38 #include "BotanUtil.h"
39 #include <string.h>
40 #include <botan/pkcs8.h>
41 #include <botan/ber_dec.h>
42 #include <botan/der_enc.h>
43 #include <botan/oids.h>
44 #include <botan/version.h>
45
46 std::vector<uint8_t> BotanDH_PrivateKey::public_value() const
47 {
48 return impl->public_value();
49 }
50
51 // Redefine of DH_PrivateKey constructor with the correct format
52 BotanDH_PrivateKey::BotanDH_PrivateKey(
53 const Botan::AlgorithmIdentifier& alg_id,
54 const Botan::secure_vector<uint8_t>& key_bits,
55 Botan::RandomNumberGenerator& rng) :
56 Botan::DL_Scheme_PrivateKey(alg_id, key_bits, Botan::DL_Group::PKCS3_DH_PARAMETERS)
57 {
58 impl = new Botan::DH_PrivateKey(rng, m_group, m_x);
59 }
60
61 BotanDH_PrivateKey::BotanDH_PrivateKey(Botan::RandomNumberGenerator& rng,
62 const Botan::DL_Group& grp,
63 const Botan::BigInt& x_arg)
64 {
65 impl = new Botan::DH_PrivateKey(rng, grp, x_arg);
66 m_group = grp;
67 m_x = x_arg;
68 m_y = impl->get_y();
69 }
70
71 BotanDH_PrivateKey::~BotanDH_PrivateKey()
72 {
73 delete impl;
74 }
75
76 // Constructors
77 BotanDHPrivateKey::BotanDHPrivateKey()
78 {
79 dh = NULL;
80 }
81
82 BotanDHPrivateKey::BotanDHPrivateKey(const BotanDH_PrivateKey* inDH)
83 {
84 dh = NULL;
85
86 setFromBotan(inDH);
87 }
88
89 // Destructor
90 BotanDHPrivateKey::~BotanDHPrivateKey()
91 {
92 delete dh;
93 }
94
95 // The type
96 /*static*/ const char* BotanDHPrivateKey::type = "Botan DH Private Key";
97
98 // Set from Botan representation
99 void BotanDHPrivateKey::setFromBotan(const BotanDH_PrivateKey* inDH)
100 {
101 ByteString inP = BotanUtil::bigInt2ByteString(inDH->impl->group_p());
102 setP(inP);
103 ByteString inG = BotanUtil::bigInt2ByteString(inDH->impl->group_g());
104 setG(inG);
105 ByteString inX = BotanUtil::bigInt2ByteString(inDH->impl->get_x());
106 setX(inX);
107 }
108
109 // Check if the key is of the given type
110 bool BotanDHPrivateKey::isOfType(const char* inType)
111 {
112 return !strcmp(type, inType);
113 }
114
115 // Setters for the DH private key components
116 void BotanDHPrivateKey::setX(const ByteString& inX)
117 {
118 DHPrivateKey::setX(inX);
119
120 if (dh)
121 {
122 delete dh;
123 dh = NULL;
124 }
125 }
126
127 // Setters for the DH public key components
128 void BotanDHPrivateKey::setP(const ByteString& inP)
129 {
130 DHPrivateKey::setP(inP);
131
132 if (dh)
133 {
134 delete dh;
135 dh = NULL;
136 }
137 }
138
139 void BotanDHPrivateKey::setG(const ByteString& inG)
140 {
141 DHPrivateKey::setG(inG);
142
143 if (dh)
144 {
145 delete dh;
146 dh = NULL;
147 }
148 }
149
150 // Encode into PKCS#8 DER
151 ByteString BotanDHPrivateKey::PKCS8Encode()
152 {
153 ByteString der;
154 createBotanKey();
155 if (dh == NULL) return der;
156 // Force PKCS3_DH_PARAMETERS for p, g and no q.
157 const size_t PKCS8_VERSION = 0;
158 const std::vector<uint8_t> parameters = dh->impl->get_domain().DER_encode(Botan::DL_Group::PKCS3_DH_PARAMETERS);
159 const Botan::AlgorithmIdentifier alg_id(dh->impl->get_oid(), parameters);
160 const Botan::secure_vector<uint8_t> ber =
161 Botan::DER_Encoder()
162 .start_cons(Botan::SEQUENCE)
163 .encode(PKCS8_VERSION)
164 .encode(alg_id)
165 .encode(dh->impl->private_key_bits(), Botan::OCTET_STRING)
166 .end_cons()
167 .get_contents();
168 der.resize(ber.size());
169 memcpy(&der[0], &ber[0], ber.size());
170 return der;
171 }
172
173 // Decode from PKCS#8 BER
174 bool BotanDHPrivateKey::PKCS8Decode(const ByteString& ber)
175 {
176 Botan::DataSource_Memory source(ber.const_byte_str(), ber.size());
177 if (source.end_of_data()) return false;
178 Botan::secure_vector<uint8_t> keydata;
179 Botan::AlgorithmIdentifier alg_id;
180 BotanDH_PrivateKey* key = NULL;
181 try
182 {
183 Botan::BER_Decoder(source)
184 .start_cons(Botan::SEQUENCE)
185 .decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
186 .decode(alg_id)
187 .decode(keydata, Botan::OCTET_STRING)
188 .discard_remaining()
189 .end_cons();
190 if (keydata.empty())
191 throw Botan::Decoding_Error("PKCS #8 private key decoding failed");
192 if (Botan::OIDS::lookup(alg_id.oid).compare("DH"))
193 {
194 ERROR_MSG("Decoded private key not DH");
195
196 return false;
197 }
198 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG();
199 key = new BotanDH_PrivateKey(alg_id, keydata, *rng->getRNG());
200 if (key == NULL) return false;
201
202 setFromBotan(key);
203
204 delete key;
205 }
206 catch (std::exception& e)
207 {
208 ERROR_MSG("Decode failed on %s", e.what());
209
210 return false;
211 }
212
213 return true;
214 }
215
216 // Retrieve the Botan representation of the key
217 BotanDH_PrivateKey* BotanDHPrivateKey::getBotanKey()
218 {
219 if (!dh)
220 {
221 createBotanKey();
222 }
223
224 return dh;
225 }
226
227 // Create the Botan representation of the key
228 void BotanDHPrivateKey::createBotanKey()
229 {
230 // y is not needed
231 if (p.size() != 0 &&
232 g.size() != 0 &&
233 x.size() != 0)
234 {
235 if (dh)
236 {
237 delete dh;
238 dh = NULL;
239 }
240
241 try
242 {
243 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG();
244 dh = new BotanDH_PrivateKey(*rng->getRNG(),
245 Botan::DL_Group(BotanUtil::byteString2bigInt(p),
246 BotanUtil::byteString2bigInt(g)),
247 BotanUtil::byteString2bigInt(x));
248 }
249 catch (...)
250 {
251 ERROR_MSG("Could not create the Botan public key");
252 }
253 }
254 }