Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 4 | * |
| 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 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 22 | #include "security/v1/public-key.hpp" |
| 23 | #include "security/v1/cryptopp.hpp" |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 24 | #include "encoding/buffer-stream.hpp" |
| 25 | |
| 26 | #include "boost-test.hpp" |
| 27 | |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 29 | namespace security { |
| 30 | namespace v1 { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 31 | namespace tests { |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Security) |
| 34 | BOOST_AUTO_TEST_SUITE(V1) |
| 35 | BOOST_AUTO_TEST_SUITE(TestPublicKey) |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 36 | |
| 37 | const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\ |
| 38 | hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\ |
| 39 | sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\ |
| 40 | zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\ |
| 41 | yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\ |
| 42 | y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\ |
| 43 | DwIDAQAB"); |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 44 | |
| 45 | const uint8_t RSA_DER_KEY_DIGEST[] = { |
| 46 | 0x1d, 0x20, |
| 47 | 0x58, 0x72, 0x4c, 0xf7, 0x36, 0x3d, 0xee, 0x4a, |
| 48 | 0x5c, 0x5b, 0x39, 0x44, 0x2d, 0xf6, 0x1a, 0x24, |
| 49 | 0xda, 0x13, 0xac, 0xab, 0x70, 0xf7, 0x74, 0x40, |
| 50 | 0x5a, 0x44, 0xfe, 0xc0, 0xc9, 0x26, 0x58, 0x74 |
| 51 | }; |
| 52 | |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 53 | const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\ |
| 54 | 1Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg=="); |
| 55 | |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 56 | const uint8_t ECDSA_DER_KEY_DIGEST[] = { |
| 57 | 0x1d, 0x20, |
| 58 | 0xaf, 0x82, 0x3f, 0xfc, 0xdc, 0x85, 0xb2, 0xa4, |
| 59 | 0xc8, 0xf5, 0x3b, 0x1a, 0xf8, 0xec, 0x4a, 0x55, |
| 60 | 0x97, 0x55, 0x19, 0x3f, 0x54, 0xdd, 0xd0, 0xfd, |
| 61 | 0xb5, 0x9d, 0x80, 0x65, 0x80, 0x6b, 0x4b, 0x63 |
| 62 | }; |
| 63 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(Rsa) |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 65 | { |
| 66 | using namespace CryptoPP; |
| 67 | |
| 68 | OBufferStream os; |
| 69 | StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(), |
| 70 | true, new Base64Decoder(new FileSink(os))); |
| 71 | |
| 72 | shared_ptr<PublicKey> publicKey; |
| 73 | BOOST_REQUIRE_NO_THROW(publicKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(), |
| 74 | os.buf()->size()))); |
| 75 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(publicKey->getKeyType(), KeyType::RSA); |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 77 | |
| 78 | Block digestBlock(RSA_DER_KEY_DIGEST, sizeof(RSA_DER_KEY_DIGEST)); |
| 79 | const Block& digest = publicKey->computeDigest(); |
| 80 | BOOST_CHECK_EQUAL_COLLECTIONS(digestBlock.wire(), |
| 81 | digestBlock.wire() + digestBlock.size(), |
| 82 | digest.wire(), |
| 83 | digest.wire() + digest.size()); |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame^] | 86 | BOOST_AUTO_TEST_CASE(Ec) |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 87 | { |
| 88 | using namespace CryptoPP; |
| 89 | |
| 90 | OBufferStream os; |
| 91 | StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(), |
| 92 | true, new Base64Decoder(new FileSink(os))); |
| 93 | |
| 94 | shared_ptr<PublicKey> publicKey; |
| 95 | BOOST_REQUIRE_NO_THROW(publicKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(), |
| 96 | os.buf()->size()))); |
| 97 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(publicKey->getKeyType(), KeyType::EC); |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 99 | |
| 100 | Block digestBlock(ECDSA_DER_KEY_DIGEST, sizeof(ECDSA_DER_KEY_DIGEST)); |
| 101 | const Block& digest = publicKey->computeDigest(); |
| 102 | BOOST_CHECK_EQUAL_COLLECTIONS(digestBlock.wire(), |
| 103 | digestBlock.wire() + digestBlock.size(), |
| 104 | digest.wire(), |
| 105 | digest.wire() + digest.size()); |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 108 | BOOST_AUTO_TEST_SUITE_END() // TestPublicKey |
| 109 | BOOST_AUTO_TEST_SUITE_END() // V1 |
| 110 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 111 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 112 | } // namespace tests |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 113 | } // namespace v1 |
| 114 | } // namespace security |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 115 | } // namespace ndn |