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