blob: f8932dede42fe62e7ed5ae0f93c9e677a35a77f1 [file] [log] [blame]
Yingdi Yu202a2e92015-07-12 16:49:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu202a2e92015-07-12 16:49:25 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "security/transform/private-key.hpp"
23#include "security/transform.hpp"
24#include "security/key-params.hpp"
25#include "encoding/buffer-stream.hpp"
26#include <iostream>
27
28#include <boost/mpl/list.hpp>
29#include "boost-test.hpp"
30
31namespace ndn {
32namespace security {
33namespace transform {
34namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(Transform)
38BOOST_AUTO_TEST_SUITE(TestPrivateKey)
39
40class RsaKeyTestData
41{
42public:
43 RsaKeyTestData()
44 {
45 privateKeyPkcs1 =
46 "MIIEpAIBAAKCAQEAw0WM1/WhAxyLtEqsiAJgWDZWuzkYpeYVdeeZcqRZzzfRgBQT\n"
47 "sNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobws3iigohnM9yTK+KKiayPhIAm/+5H\n"
48 "GT6SgFJhYhqo1/upWdueojil6RP4/AgavH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0JIjdc6+3RgIlGsXc8BwiSjDSI2XW+\n"
96 "vo75/2zPU9t8OeXEIJk2CQGyqLwUJ6dyi/yDRrvZAgjrUvbpcxydnBAHrLbLUGXJ\n"
97 "aEHH2tjEtnTqVyTchr1yHoupcFOCkA0dAA66XqwcssQxJiMGrWTpCbgd9mrTXQaZ\n"
98 "U7afFN1jpO78tgBQUUpImXdHLLsqdN5tefqjileZGZ9x3/C6TNAfDwYJdsicNNn5\n"
99 "y+JVsbltfLWlJxb9teb3dtQiFlJ7ofprLJnJVqI/Js8lozY+KaxV2vtbZkcD4dM=\n";
100
101 publicKeyPkcs8 =
102 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0WM1/WhAxyLtEqsiAJg\n"
103 "WDZWuzkYpeYVdeeZcqRZzzfRgBQTsNozS5t4HnwTZhwwXbH7k3QN0kRTV826Xobw\n"
104 "s3iigohnM9yTK+KKiayPhIAm/+5HGT6SgFJhYhqo1/upWdueojil6RP4/AgavHho\n"
105 "pxlAVbk6G9VdVnlQcQ5Zv0OcGi73c+EnYD/YgURYGSngUi/Ynsh779p2U69/te9g\n"
106 "ZwIL5PuE9BiO6I39cL9z7EK1SfZhOWvDe/qH7YhD/BHwcWit8FjRww1glwRVTJsA\n"
107 "9rH58ynaAix0tcR/nBMRLUX+e3rURHg6UbSjJbdb9qmKM1fTGHKUzL/5pMG6uBU0\n"
108 "ywIDAQAB\n";
109 }
110
111public:
112 std::string privateKeyPkcs1;
113 std::string privateKeyPkcs8;
114 std::string publicKeyPkcs8;
115};
116
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700117class EcKeyTestData
Yingdi Yu202a2e92015-07-12 16:49:25 -0700118{
119public:
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700120 EcKeyTestData()
Yingdi Yu202a2e92015-07-12 16:49:25 -0700121 {
122 privateKeyPkcs1 =
123 "MIIBaAIBAQQgRxwcbzK9RV6AHYFsDcykI86o3M/a1KlJn0z8PcLMBZOggfowgfcC\n"
124 "AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////\n"
125 "MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr\n"
126 "vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE\n"
127 "axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W\n"
128 "K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8\n"
129 "YyVRAgEBoUQDQgAEaG4WJuDAt0QkEM4t29KDUdzkQlMPGrqWzkWhgt9OGnwc6O7A\n"
130 "ZLPSrDyhwyrKS7XLRXml5DisQ93RvByll32y8A==\n";
131
132 privateKeyPkcs8 =
133 "MIIBwzA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIVHkBzLGtDvICAggA\n"
134 "MBEGBSsOAwIHBAhk6g9eI3toNwSCAYDd+LWPDBTrKV7vUyxTvDbpUd0eXfh73DKA\n"
135 "MHkdHuVmhpmpBbsF9XvaFuL8J/1xi1Yl2XGw8j3WyrprD2YEhl/+zKjNbdTDJmNO\n"
136 "SlomuwWb5AVCJ9reT94zIXKCnexUcyBFS7ep+P4dwuef0VjzprjfmnAZHrP+u594\n"
137 "ELHpKwi0ZpQLtcJjjud13bn43vbXb+aU7jmPV5lU2XP8TxaQJiYIibNEh1Y3TZGr\n"
138 "akJormYvhaYbiZkKLHQ9AvQMEjhoIW5WCB3q+tKZUKTzcQpjNnf9FOTeKN3jk3Kd\n"
139 "2OmibPZcbMJdgCD/nRVn1cBo7Hjn3IMjgtszQHtEUphOQiAkOJUnKmy9MTYqtcNN\n"
140 "6cuFItbu4QvbVwailgdUjOYwIJCmIxExlPV0ohS24pFGsO03Yn7W8rBB9VWENYmG\n"
141 "HkZIbGsHv7O9Wy7fv+FJgZkjeti0807IsNXSJl8LUK0ZIhAR7OU8uONWMsbHdQnk\n"
142 "q1HB1ZKa52ugACl7g/DF9b7CoSAjFeE=\n";
143
144 publicKeyPkcs8 =
145 "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA\n"
146 "AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////\n"
147 "///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd\n"
148 "NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5\n"
149 "RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA\n"
150 "//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABGhuFibgwLdEJBDOLdvSg1Hc\n"
151 "5EJTDxq6ls5FoYLfThp8HOjuwGSz0qw8ocMqyku1y0V5peQ4rEPd0bwcpZd9svA=\n";
152 }
153
154public:
155 std::string privateKeyPkcs1;
156 std::string privateKeyPkcs8;
157 std::string publicKeyPkcs8;
158};
159
160typedef boost::mpl::list<RsaKeyTestData,
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700161 EcKeyTestData> KeyTestDataSets;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700162
163void
164checkPkcs8Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
165{
166 PrivateKey sKey;
167 BOOST_REQUIRE_NO_THROW(sKey.loadPkcs8(encoding->buf(), encoding->size(),
168 password.c_str(), password.size()));
169 OBufferStream os;
170 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os));
171 ConstBufferPtr keyPkcs1Str = os.buf();
172 BOOST_CHECK_EQUAL_COLLECTIONS(pkcs1->begin(), pkcs1->end(), keyPkcs1Str->begin(), keyPkcs1Str->end());
173}
174
175void
176checkPkcs8Base64Encoding(ConstBufferPtr encoding, const std::string& password, ConstBufferPtr pkcs1)
177{
178 OBufferStream os;
179 bufferSource(*encoding) >> base64Decode() >> streamSink(os);
180
181 checkPkcs8Encoding(os.buf(), password, pkcs1);
182}
183
184BOOST_AUTO_TEST_CASE_TEMPLATE(SaveLoad, T, KeyTestDataSets)
185{
186 T dataSet;
187
188 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str());
189 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
190
191 OBufferStream os;
192 bufferSource(sKeyPkcs1Base64, sKeyPkcs1Base64Len) >> base64Decode() >> streamSink(os);
193 ConstBufferPtr sKeyPkcs1Buf = os.buf();
194 const uint8_t* sKeyPkcs1 = sKeyPkcs1Buf->buf();
195 size_t sKeyPkcs1Len = sKeyPkcs1Buf->size();
196
197 // load key in base64 encoded pkcs1 format
198 PrivateKey sKey;
199 BOOST_REQUIRE_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
200
201 std::stringstream ss2(dataSet.privateKeyPkcs1);
202 PrivateKey sKey2;
203 BOOST_REQUIRE_NO_THROW(sKey2.loadPkcs1Base64(ss2));
204
205 // load key in pkcs1 format
206 PrivateKey sKey3;
207 BOOST_REQUIRE_NO_THROW(sKey3.loadPkcs1(sKeyPkcs1, sKeyPkcs1Len));
208
209 std::stringstream ss4;
210 ss4.write(reinterpret_cast<const char*>(sKeyPkcs1), sKeyPkcs1Len);
211 PrivateKey sKey4;
212 BOOST_REQUIRE_NO_THROW(sKey4.loadPkcs1(ss4));
213
214 // save key in base64 encoded pkcs1 format
215 OBufferStream os2;
216 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1Base64(os2));
217 ConstBufferPtr keyPkcs1Base64Str = os2.buf();
218 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1Base64, sKeyPkcs1Base64 + sKeyPkcs1Base64Len,
219 keyPkcs1Base64Str->begin(), keyPkcs1Base64Str->end());
220
221 // save key in pkcs1 format
222 OBufferStream os3;
223 BOOST_REQUIRE_NO_THROW(sKey.savePkcs1(os3));
224 ConstBufferPtr keyPkcs1Str = os3.buf();
225 BOOST_CHECK_EQUAL_COLLECTIONS(sKeyPkcs1, sKeyPkcs1 + sKeyPkcs1Len,
226 keyPkcs1Str->begin(), keyPkcs1Str->end());
227
228
229
230 const uint8_t* sKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs8.c_str());
231 size_t sKeyPkcs8Base64Len = dataSet.privateKeyPkcs8.size();
232
233 OBufferStream os4;
234 bufferSource(sKeyPkcs8Base64, sKeyPkcs8Base64Len) >> base64Decode() >> streamSink(os4);
235 ConstBufferPtr sKeyPkcs8Buf = os4.buf();
236 const uint8_t* sKeyPkcs8 = sKeyPkcs8Buf->buf();
237 size_t sKeyPkcs8Len = sKeyPkcs8Buf->size();
238
239 std::string password("password");
240 std::string wrongpw("wrongpw");
241 auto pwCallback = [&] (char* buf, size_t size, int rwflag) -> int {
242 std::copy(password.begin(), password.end(), buf);
243 return password.size();
244 };
245
246
247 // load key in base64 encoded pkcs8 format
248 PrivateKey sKey5;
249 BOOST_REQUIRE_NO_THROW(sKey5.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len,
250 password.c_str(), password.size()));
251
252 PrivateKey sKey6;
253 BOOST_REQUIRE_NO_THROW(sKey6.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, pwCallback));
254
255 std::stringstream ss7(dataSet.privateKeyPkcs8);
256 PrivateKey sKey7;
257 BOOST_REQUIRE_NO_THROW(sKey7.loadPkcs8Base64(ss7, password.c_str(), password.size()));
258
259 std::stringstream ss8(dataSet.privateKeyPkcs8);
260 PrivateKey sKey8;
261 BOOST_REQUIRE_NO_THROW(sKey8.loadPkcs8Base64(ss8, pwCallback));
262
263 // load key in pkcs8 format
264 PrivateKey sKey9;
265 BOOST_REQUIRE_NO_THROW(sKey9.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len,
266 password.c_str(), password.size()));
267
268 PrivateKey sKey10;
269 BOOST_REQUIRE_NO_THROW(sKey10.loadPkcs8(sKeyPkcs8, sKeyPkcs8Len, pwCallback));
270
271 std::stringstream ss11;
272 ss11.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
273 PrivateKey sKey11;
274 BOOST_REQUIRE_NO_THROW(sKey11.loadPkcs8(ss11, password.c_str(), password.size()));
275
276 std::stringstream ss12;
277 ss12.write(reinterpret_cast<const char*>(sKeyPkcs8), sKeyPkcs8Len);
278 PrivateKey sKey12;
279 BOOST_REQUIRE_NO_THROW(sKey12.loadPkcs8(ss12, pwCallback));
280
281 // load key using wrong password, Error is expected.
282 PrivateKey sKey13;
283 BOOST_REQUIRE_THROW(sKey13.loadPkcs8Base64(sKeyPkcs8Base64, sKeyPkcs8Base64Len, wrongpw.c_str(), wrongpw.size()),
284 PrivateKey::Error);
285
286 // save key in base64 encoded pkcs8 format
287 OBufferStream os14;
288 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os14, password.c_str(), password.size()));
289 ConstBufferPtr encoded14 = os14.buf();
290 checkPkcs8Base64Encoding(encoded14, password, sKeyPkcs1Buf);
291
292 OBufferStream os15;
293 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8Base64(os15, pwCallback));
294 ConstBufferPtr encoded15 = os15.buf();
295 checkPkcs8Base64Encoding(encoded15, password, sKeyPkcs1Buf);
296
297 // save key in pkcs8 format
298 OBufferStream os16;
299 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os16, password.c_str(), password.size()));
300 ConstBufferPtr encoded16 = os16.buf();
301 checkPkcs8Encoding(encoded16, password, sKeyPkcs1Buf);
302
303 OBufferStream os17;
304 BOOST_REQUIRE_NO_THROW(sKey.savePkcs8(os17, pwCallback));
305 ConstBufferPtr encoded17 = os17.buf();
306 checkPkcs8Encoding(encoded17, password, sKeyPkcs1Buf);
307}
308
309BOOST_AUTO_TEST_CASE_TEMPLATE(DerivePublicKey, T, KeyTestDataSets)
310{
311 T dataSet;
312
313 const uint8_t* sKeyPkcs1Base64 = reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str());
314 size_t sKeyPkcs1Base64Len = dataSet.privateKeyPkcs1.size();
315
316 PrivateKey sKey;
317 BOOST_REQUIRE_NO_THROW(sKey.loadPkcs1Base64(sKeyPkcs1Base64, sKeyPkcs1Base64Len));
318
319 // derive public key and compare
320 ConstBufferPtr pKeyBits= sKey.derivePublicKey();
321 OBufferStream os;
322 bufferSource(dataSet.publicKeyPkcs8) >> base64Decode() >> streamSink(os);
323 BOOST_CHECK_EQUAL_COLLECTIONS(pKeyBits->begin(), pKeyBits->end(),
324 os.buf()->begin(), os.buf()->end());
325}
326
327BOOST_AUTO_TEST_CASE(RsaDecryption)
328{
329 RsaKeyTestData dataSet;
330
331 PrivateKey sKey;
332 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
333 dataSet.privateKeyPkcs1.size());
334
335 const uint8_t plainText[] = {
336 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
337 };
338
339 const std::string cipherTextBase64 =
340 "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
341 "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
342 "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
343 "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
344 "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
345 "PmTiSdeDQXZ94U5boDQ4Aw==\n";
346
347 OBufferStream os;
348 bufferSource(cipherTextBase64) >> base64Decode() >> streamSink(os);
349
350 ConstBufferPtr decryptText = sKey.decrypt(os.buf()->buf(), os.buf()->size());
351
352 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
353 decryptText->begin(), decryptText->end());
354}
355
356BOOST_AUTO_TEST_CASE(RsaEncryption)
357{
358 RsaKeyTestData dataSet;
359
360 PublicKey pKey;
361 pKey.loadPkcs8Base64(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.c_str()),
362 dataSet.publicKeyPkcs8.size());
363
364 PrivateKey sKey;
365 sKey.loadPkcs1Base64(reinterpret_cast<const uint8_t*>(dataSet.privateKeyPkcs1.c_str()),
366 dataSet.privateKeyPkcs1.size());
367
368 const uint8_t plainText[] = {
369 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
370 };
371
372 ConstBufferPtr cipherText = pKey.encrypt(plainText, sizeof(plainText));
373 ConstBufferPtr decryptText = sKey.decrypt(cipherText->buf(), cipherText->size());
374
375 BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
376 decryptText->begin(), decryptText->end());
377}
378
379typedef boost::mpl::list<RsaKeyParams,
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700380 EcKeyParams> TestKeyParams;
Yingdi Yu202a2e92015-07-12 16:49:25 -0700381
382BOOST_AUTO_TEST_CASE_TEMPLATE(GenerateKey, T, TestKeyParams)
383{
384 BOOST_REQUIRE_NO_THROW(generatePrivateKey(T()));
385
386 unique_ptr<PrivateKey> sKey = generatePrivateKey(T());
387 PublicKey pKey;
388 ConstBufferPtr pKeyBits = sKey->derivePublicKey();
389 pKey.loadPkcs8(pKeyBits->buf(), pKeyBits->size());
390
Yingdi Yub263f152015-07-12 16:50:13 -0700391 uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
392
393 OBufferStream os;
394 BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
395 signerFilter(DigestAlgorithm::SHA256, *sKey) >>
396 streamSink(os));
397
398 ConstBufferPtr sig = os.buf();
399 bool result = false;
400 BOOST_REQUIRE_NO_THROW(bufferSource(data, sizeof(data)) >>
401 verifierFilter(DigestAlgorithm::SHA256, pKey, sig->buf(), sig->size()) >>
402 boolSink(result));
403
404 BOOST_CHECK(result);
405
Yingdi Yu202a2e92015-07-12 16:49:25 -0700406
407 unique_ptr<PrivateKey> sKey2 = generatePrivateKey(T());
408
409 OBufferStream os1;
410 sKey->savePkcs1(os1);
411 ConstBufferPtr key1Pkcs1 = os1.buf();
412
413 OBufferStream os2;
414 sKey2->savePkcs1(os2);
415 ConstBufferPtr key2Pkcs1 = os2.buf();
416
417 BOOST_CHECK(*key1Pkcs1 != *key2Pkcs1);
418}
419
420BOOST_AUTO_TEST_SUITE_END() // TestPrivateKey
421BOOST_AUTO_TEST_SUITE_END() // Transform
422BOOST_AUTO_TEST_SUITE_END() // Security
423
424} // namespace tests
425} // namespace transform
426} // namespace security
427} // namespace ndn