Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -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 | * @author Zhiyi Zhang <dreamerbarrychang@gmail.com> |
| 22 | */ |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/security/safe-bag.hpp" |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace tests { |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(Security) |
| 33 | BOOST_AUTO_TEST_SUITE(TestSafeBag) |
| 34 | |
| 35 | const uint8_t CERT[] = { |
| 36 | 0x06, 0xc8, // Data |
| 37 | 0x07, 0x14, // Name |
| 38 | 0x08, 0x05, |
| 39 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 40 | 0x08, 0x03, |
| 41 | 0x6e, 0x64, 0x6e, |
| 42 | 0x08, 0x06, |
| 43 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 44 | 0x14, 0x07, // MetaInfo |
| 45 | 0x18, 0x01, // ContentType |
| 46 | 0x02, |
| 47 | 0x19, 0x02, // FreshnessPeriod |
| 48 | 0x27, 0x10, |
| 49 | 0x15, 0x08, // Content |
| 50 | 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21, |
| 51 | 0x16, 0x1b, // SignatureInfo |
| 52 | 0x1b, 0x01, // SignatureType |
| 53 | 0x01, |
| 54 | 0x1c, 0x16, // KeyLocator |
| 55 | 0x07, 0x14, // Name |
| 56 | 0x08, 0x04, |
| 57 | 0x74, 0x65, 0x73, 0x74, |
| 58 | 0x08, 0x03, |
| 59 | 0x6b, 0x65, 0x79, |
| 60 | 0x08, 0x07, |
| 61 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 62 | 0x17, 0x80, // SignatureValue |
| 63 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 64 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 65 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 66 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 67 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 68 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 69 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 70 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 71 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 72 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1 |
| 73 | }; |
| 74 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 75 | const uint8_t ENCRYPTED_KEY[] = { |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 76 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe |
| 77 | }; |
| 78 | |
| 79 | const uint8_t SAFE_BAG[] = { |
| 80 | 0x80, 0xd4, // SafeBag |
| 81 | 0x06, 0xc8, // Data |
| 82 | 0x07, 0x14, // Name |
| 83 | 0x08, 0x05, |
| 84 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 85 | 0x08, 0x03, |
| 86 | 0x6e, 0x64, 0x6e, |
| 87 | 0x08, 0x06, |
| 88 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 89 | 0x14, 0x07, // MetaInfo |
| 90 | 0x18, 0x01, // ContentType |
| 91 | 0x02, |
| 92 | 0x19, 0x02, // FreshnessPeriod |
| 93 | 0x27, 0x10, |
| 94 | 0x15, 0x08, // Content |
| 95 | 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21, |
| 96 | 0x16, 0x1b, // SignatureInfo |
| 97 | 0x1b, 0x01, // SignatureType |
| 98 | 0x01, |
| 99 | 0x1c, 0x16, // KeyLocator |
| 100 | 0x07, 0x14, // Name |
| 101 | 0x08, 0x04, |
| 102 | 0x74, 0x65, 0x73, 0x74, |
| 103 | 0x08, 0x03, |
| 104 | 0x6b, 0x65, 0x79, |
| 105 | 0x08, 0x07, |
| 106 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 107 | 0x17, 0x80, // SignatureValue |
| 108 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 109 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 110 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 111 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 112 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 113 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 114 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 115 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 116 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 117 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1, |
Davide Pesavento | 93101df | 2021-06-15 18:33:09 -0400 | [diff] [blame] | 118 | 0x81, 0x08, // EncryptedKey |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 119 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe |
| 120 | }; |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(Constructor) |
| 123 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 124 | Data data(Block{CERT}); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 125 | auto encKey = make_span(ENCRYPTED_KEY); |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 126 | SafeBag safeBag1(data, encKey); |
| 127 | SafeBag safeBag2(Block{SAFE_BAG}); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 128 | |
| 129 | BOOST_CHECK(safeBag1.getCertificate() == data); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL_COLLECTIONS(safeBag1.getEncryptedKey().begin(), safeBag1.getEncryptedKey().end(), |
| 131 | encKey.begin(), encKey.end()); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 132 | BOOST_CHECK(safeBag2.getCertificate() == data); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL_COLLECTIONS(safeBag2.getEncryptedKey().begin(), safeBag2.getEncryptedKey().end(), |
| 134 | encKey.begin(), encKey.end()); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(EncoderAndDecoder) |
| 138 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 139 | Data data(Block{CERT}); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 140 | SafeBag safeBag(data, ENCRYPTED_KEY); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 141 | |
| 142 | // wire encode |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 143 | const auto& wireBlock = safeBag.wireEncode(); |
| 144 | Block block(SAFE_BAG); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 145 | |
| 146 | // check safe bag block |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 147 | BOOST_CHECK_EQUAL(wireBlock, block); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 148 | |
| 149 | // wire decode |
| 150 | SafeBag safeBag2; |
| 151 | safeBag2.wireDecode(wireBlock); |
| 152 | |
| 153 | // check equal |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 154 | Buffer buffer1(safeBag2.getEncryptedKey().begin(), safeBag2.getEncryptedKey().end()); |
| 155 | Buffer buffer2(ENCRYPTED_KEY, sizeof(ENCRYPTED_KEY)); |
Zhiyi Zhang | 3e62a83 | 2015-07-20 18:36:31 -0700 | [diff] [blame] | 156 | BOOST_CHECK(buffer1 == buffer2); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_SUITE_END() // TestSafeBag |
| 160 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 161 | |
| 162 | } // namespace tests |
| 163 | } // namespace security |
| 164 | } // namespace ndn |