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