Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 22 | #include "security/validator-null.hpp" |
| 23 | #include "security/key-chain.hpp" |
| 24 | #include "util/time.hpp" |
| 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 27 | |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 28 | namespace ndn { |
| 29 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 30 | using std::string; |
| 31 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(SecurityTestValidator) |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 33 | |
| 34 | void |
| 35 | onValidated(const shared_ptr<const Data>& data) |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 36 | { |
| 37 | BOOST_CHECK(true); |
| 38 | } |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 39 | |
| 40 | void |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 41 | onValidationFailed(const shared_ptr<const Data>& data, const string& failureInfo) |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 42 | { |
| 43 | BOOST_CHECK(false); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 44 | } |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 45 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 46 | BOOST_AUTO_TEST_CASE(Null) |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 47 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 48 | BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file")); |
| 49 | KeyChain keyChain("sqlite3", "file"); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 51 | Name identity("/TestValidator/Null"); |
| 52 | identity.appendVersion(); |
| 53 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 54 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 55 | |
| 56 | Name dataName = identity; |
| 57 | dataName.append("1"); |
| 58 | shared_ptr<Data> data = make_shared<Data>(dataName); |
| 59 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 60 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data, identity)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 62 | ValidatorNull validator; |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 63 | |
| 64 | // data must be a shared pointer |
| 65 | validator.validate(*data, |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 66 | bind(&onValidated, _1), |
| 67 | bind(&onValidationFailed, _1, _2)); |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 68 | |
| 69 | keyChain.deleteIdentity(identity); |
| 70 | } |
| 71 | |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 72 | const uint8_t ecdsaSigInfo[] = { |
| 73 | 0x16, 0x1b, // SignatureInfo |
| 74 | 0x1b, 0x01, // SignatureType |
| 75 | 0x03, |
| 76 | 0x1c, 0x16, // KeyLocator |
| 77 | 0x07, 0x14, // Name |
| 78 | 0x08, 0x04, |
| 79 | 0x74, 0x65, 0x73, 0x74, |
| 80 | 0x08, 0x03, |
| 81 | 0x6b, 0x65, 0x79, |
| 82 | 0x08, 0x07, |
| 83 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 84 | }; |
| 85 | |
| 86 | const uint8_t ecdsaSigValue[] = { |
| 87 | 0x17, 0x40, // SignatureValue |
| 88 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 89 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 90 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 91 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 92 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b |
| 93 | }; |
| 94 | |
| 95 | BOOST_AUTO_TEST_CASE(RsaSignatureVerification) |
| 96 | { |
| 97 | BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file")); |
| 98 | KeyChain keyChain("sqlite3", "file"); |
| 99 | |
| 100 | Name identity("/TestValidator/RsaSignatureVerification"); |
| 101 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
| 102 | Name keyName = keyChain.getDefaultKeyNameForIdentity(identity); |
| 103 | shared_ptr<PublicKey> publicKey = keyChain.getPublicKey(keyName); |
| 104 | |
| 105 | Name identity2("/TestValidator/RsaSignatureVerification/id2"); |
| 106 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity2)); |
| 107 | Name keyName2 = keyChain.getDefaultKeyNameForIdentity(identity2); |
| 108 | shared_ptr<PublicKey> publicKey2 = keyChain.getPublicKey(keyName2); |
| 109 | |
| 110 | |
| 111 | Data data("/TestData/1"); |
| 112 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(data, identity)); |
| 113 | BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey), true); |
| 114 | BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey2), false); |
| 115 | |
| 116 | Interest interest("/TestInterest/1"); |
| 117 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identity)); |
| 118 | BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey), true); |
| 119 | BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey2), false); |
| 120 | |
| 121 | Data wrongData("/TestData/2"); |
| 122 | Block ecdsaSigInfoBlock(ecdsaSigInfo, sizeof(ecdsaSigInfo)); |
| 123 | Block ecdsaSigValueBlock(ecdsaSigValue, sizeof(ecdsaSigValue)); |
| 124 | Signature ecdsaSig(ecdsaSigInfoBlock, ecdsaSigValueBlock); |
| 125 | wrongData.setSignature(ecdsaSig); |
| 126 | BOOST_CHECK_EQUAL(Validator::verifySignature(wrongData, *publicKey), false); |
| 127 | |
| 128 | keyChain.deleteIdentity(identity); |
| 129 | keyChain.deleteIdentity(identity2); |
| 130 | } |
| 131 | |
| 132 | const uint8_t rsaSigInfo[] = { |
| 133 | 0x16, 0x1b, // SignatureInfo |
| 134 | 0x1b, 0x01, // SignatureType |
| 135 | 0x01, |
| 136 | 0x1c, 0x16, // KeyLocator |
| 137 | 0x07, 0x14, // Name |
| 138 | 0x08, 0x04, |
| 139 | 0x74, 0x65, 0x73, 0x74, |
| 140 | 0x08, 0x03, |
| 141 | 0x6b, 0x65, 0x79, |
| 142 | 0x08, 0x07, |
| 143 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 144 | }; |
| 145 | |
| 146 | const uint8_t rsaSigValue[] = { |
| 147 | 0x17, 0x80, // SignatureValue |
| 148 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 149 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 150 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 151 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 152 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 153 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 154 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 155 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 156 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 157 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1 |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | BOOST_AUTO_TEST_CASE(EcdsaSignatureVerification) |
| 162 | { |
| 163 | BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file")); |
| 164 | KeyChain keyChain("sqlite3", "file"); |
| 165 | |
| 166 | Name identity("/TestValidator/EcdsaSignatureVerification"); |
| 167 | EcdsaKeyParams params; |
| 168 | // BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity, params)); |
| 169 | try |
| 170 | { |
| 171 | keyChain.createIdentity(identity, params); |
| 172 | } |
| 173 | catch (std::runtime_error& e) |
| 174 | { |
| 175 | std::cerr << e.what() << std::endl; |
| 176 | } |
| 177 | Name keyName = keyChain.getDefaultKeyNameForIdentity(identity); |
| 178 | shared_ptr<PublicKey> publicKey = keyChain.getPublicKey(keyName); |
| 179 | |
| 180 | Name identity2("/TestValidator/EcdsaSignatureVerification/id2"); |
| 181 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity2, params)); |
| 182 | Name keyName2 = keyChain.getDefaultKeyNameForIdentity(identity2); |
| 183 | shared_ptr<PublicKey> publicKey2 = keyChain.getPublicKey(keyName2); |
| 184 | |
| 185 | |
| 186 | Data data("/TestData/1"); |
| 187 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(data, identity)); |
| 188 | BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey), true); |
| 189 | BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey2), false); |
| 190 | |
| 191 | Interest interest("/TestInterest/1"); |
| 192 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identity)); |
| 193 | BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey), true); |
| 194 | BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey2), false); |
| 195 | |
| 196 | Data wrongData("/TestData/2"); |
| 197 | Block rsaSigInfoBlock(rsaSigInfo, sizeof(rsaSigInfo)); |
| 198 | Block rsaSigValueBlock(rsaSigValue, sizeof(rsaSigValue)); |
| 199 | Signature rsaSig(rsaSigInfoBlock, rsaSigValueBlock); |
| 200 | wrongData.setSignature(rsaSig); |
| 201 | BOOST_CHECK_EQUAL(Validator::verifySignature(wrongData, *publicKey), false); |
| 202 | |
| 203 | keyChain.deleteIdentity(identity); |
| 204 | keyChain.deleteIdentity(identity2); |
| 205 | } |
| 206 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame^] | 207 | BOOST_AUTO_TEST_CASE(EcdsaSignatureVerification2) |
| 208 | { |
| 209 | KeyChain keyChain("sqlite3", "file"); |
| 210 | |
| 211 | EcdsaKeyParams params; |
| 212 | |
| 213 | Name ecdsaIdentity("/SecurityTestValidator/EcdsaSignatureVerification2/ecdsa"); |
| 214 | Name ecdsaCertName = keyChain.createIdentity(ecdsaIdentity, params); |
| 215 | shared_ptr<IdentityCertificate> ecdsaCert = keyChain.getCertificate(ecdsaCertName); |
| 216 | |
| 217 | Name rsaIdentity("/SecurityTestValidator/EcdsaSignatureVerification2/rsa"); |
| 218 | Name rsaCertName = keyChain.createIdentity(rsaIdentity); |
| 219 | shared_ptr<IdentityCertificate> rsaCert = keyChain.getCertificate(rsaCertName); |
| 220 | |
| 221 | Name packetName("/Test/Packet/Name"); |
| 222 | |
| 223 | shared_ptr<Data> testDataRsa = make_shared<Data>(packetName); |
| 224 | keyChain.signByIdentity(*testDataRsa, rsaIdentity); |
| 225 | shared_ptr<Data> testDataEcdsa = make_shared<Data>(packetName); |
| 226 | keyChain.signByIdentity(*testDataEcdsa, ecdsaIdentity); |
| 227 | shared_ptr<Interest> testInterestRsa = make_shared<Interest>(packetName); |
| 228 | keyChain.signByIdentity(*testInterestRsa, rsaIdentity); |
| 229 | shared_ptr<Interest> testInterestEcdsa = make_shared<Interest>(packetName); |
| 230 | keyChain.signByIdentity(*testInterestEcdsa, ecdsaIdentity); |
| 231 | |
| 232 | BOOST_CHECK(Validator::verifySignature(*ecdsaCert, ecdsaCert->getPublicKeyInfo())); |
| 233 | BOOST_CHECK_EQUAL(Validator::verifySignature(*ecdsaCert, rsaCert->getPublicKeyInfo()), false); |
| 234 | BOOST_CHECK_EQUAL(Validator::verifySignature(*rsaCert, ecdsaCert->getPublicKeyInfo()), false); |
| 235 | BOOST_CHECK(Validator::verifySignature(*rsaCert, rsaCert->getPublicKeyInfo())); |
| 236 | |
| 237 | BOOST_CHECK(Validator::verifySignature(*testDataEcdsa, ecdsaCert->getPublicKeyInfo())); |
| 238 | BOOST_CHECK_EQUAL(Validator::verifySignature(*testDataEcdsa, rsaCert->getPublicKeyInfo()), false); |
| 239 | BOOST_CHECK_EQUAL(Validator::verifySignature(*testDataRsa, ecdsaCert->getPublicKeyInfo()), false); |
| 240 | BOOST_CHECK(Validator::verifySignature(*testDataRsa, rsaCert->getPublicKeyInfo())); |
| 241 | |
| 242 | BOOST_CHECK(Validator::verifySignature(*testInterestEcdsa, ecdsaCert->getPublicKeyInfo())); |
| 243 | BOOST_CHECK_EQUAL(Validator::verifySignature(*testInterestEcdsa, rsaCert->getPublicKeyInfo()), |
| 244 | false); |
| 245 | BOOST_CHECK_EQUAL(Validator::verifySignature(*testInterestRsa, ecdsaCert->getPublicKeyInfo()), |
| 246 | false); |
| 247 | BOOST_CHECK(Validator::verifySignature(*testInterestRsa, rsaCert->getPublicKeyInfo())); |
| 248 | |
| 249 | keyChain.deleteIdentity(ecdsaIdentity); |
| 250 | keyChain.deleteIdentity(rsaIdentity); |
| 251 | } |
| 252 | |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 253 | BOOST_AUTO_TEST_SUITE_END() |
| 254 | |
| 255 | } // namespace ndn |