blob: 9f9b00dc3716437f38d905319819d03c68f82da0 [file] [log] [blame]
Zhiyi Zhang3e62a832015-07-20 18:36:31 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi72c0c642018-04-20 15:41:09 +00002/*
Davide Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Zhiyi Zhang3e62a832015-07-20 18:36:31 -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 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
22 */
Davide Pesaventoeee3e822016-11-26 19:19:34 +010023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/security/safe-bag.hpp"
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
29
30using ndn::security::SafeBag;
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070031
Davide Pesavento152ef442023-04-22 02:02:29 -040032BOOST_CONCEPT_ASSERT((WireEncodable<SafeBag>));
33BOOST_CONCEPT_ASSERT((WireDecodable<SafeBag>));
34
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070035BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(TestSafeBag)
37
38const uint8_t CERT[] = {
39 0x06, 0xc8, // Data
40 0x07, 0x14, // Name
41 0x08, 0x05,
42 0x6c, 0x6f, 0x63, 0x61, 0x6c,
43 0x08, 0x03,
44 0x6e, 0x64, 0x6e,
45 0x08, 0x06,
46 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
47 0x14, 0x07, // MetaInfo
48 0x18, 0x01, // ContentType
49 0x02,
50 0x19, 0x02, // FreshnessPeriod
51 0x27, 0x10,
52 0x15, 0x08, // Content
53 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
54 0x16, 0x1b, // SignatureInfo
55 0x1b, 0x01, // SignatureType
56 0x01,
57 0x1c, 0x16, // KeyLocator
58 0x07, 0x14, // Name
59 0x08, 0x04,
60 0x74, 0x65, 0x73, 0x74,
61 0x08, 0x03,
62 0x6b, 0x65, 0x79,
63 0x08, 0x07,
64 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
65 0x17, 0x80, // SignatureValue
66 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
67 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
68 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
69 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
70 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
71 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
72 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
73 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
74 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
75 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
76};
77
Davide Pesavento765abc92021-12-27 00:44:04 -050078const uint8_t ENCRYPTED_KEY[] = {
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070079 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
80};
81
82const uint8_t SAFE_BAG[] = {
83 0x80, 0xd4, // SafeBag
84 0x06, 0xc8, // Data
85 0x07, 0x14, // Name
86 0x08, 0x05,
87 0x6c, 0x6f, 0x63, 0x61, 0x6c,
88 0x08, 0x03,
89 0x6e, 0x64, 0x6e,
90 0x08, 0x06,
91 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
92 0x14, 0x07, // MetaInfo
93 0x18, 0x01, // ContentType
94 0x02,
95 0x19, 0x02, // FreshnessPeriod
96 0x27, 0x10,
97 0x15, 0x08, // Content
98 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
99 0x16, 0x1b, // SignatureInfo
100 0x1b, 0x01, // SignatureType
101 0x01,
102 0x1c, 0x16, // KeyLocator
103 0x07, 0x14, // Name
104 0x08, 0x04,
105 0x74, 0x65, 0x73, 0x74,
106 0x08, 0x03,
107 0x6b, 0x65, 0x79,
108 0x08, 0x07,
109 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
110 0x17, 0x80, // SignatureValue
111 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
112 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
113 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
114 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
115 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
116 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
117 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
118 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
119 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
120 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
Davide Pesavento93101df2021-06-15 18:33:09 -0400121 0x81, 0x08, // EncryptedKey
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700122 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
123};
124
125BOOST_AUTO_TEST_CASE(Constructor)
126{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500127 Data data(Block{CERT});
Davide Pesavento765abc92021-12-27 00:44:04 -0500128 auto encKey = make_span(ENCRYPTED_KEY);
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500129 SafeBag safeBag1(data, encKey);
130 SafeBag safeBag2(Block{SAFE_BAG});
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700131
132 BOOST_CHECK(safeBag1.getCertificate() == data);
Davide Pesavento765abc92021-12-27 00:44:04 -0500133 BOOST_CHECK_EQUAL_COLLECTIONS(safeBag1.getEncryptedKey().begin(), safeBag1.getEncryptedKey().end(),
134 encKey.begin(), encKey.end());
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700135 BOOST_CHECK(safeBag2.getCertificate() == data);
Davide Pesavento765abc92021-12-27 00:44:04 -0500136 BOOST_CHECK_EQUAL_COLLECTIONS(safeBag2.getEncryptedKey().begin(), safeBag2.getEncryptedKey().end(),
137 encKey.begin(), encKey.end());
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700138}
139
140BOOST_AUTO_TEST_CASE(EncoderAndDecoder)
141{
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500142 Data data(Block{CERT});
Davide Pesavento765abc92021-12-27 00:44:04 -0500143 SafeBag safeBag(data, ENCRYPTED_KEY);
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700144
145 // wire encode
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500146 const auto& wireBlock = safeBag.wireEncode();
147 Block block(SAFE_BAG);
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700148
149 // check safe bag block
Junxiao Shi72c0c642018-04-20 15:41:09 +0000150 BOOST_CHECK_EQUAL(wireBlock, block);
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700151
152 // wire decode
153 SafeBag safeBag2;
154 safeBag2.wireDecode(wireBlock);
155
156 // check equal
Davide Pesavento765abc92021-12-27 00:44:04 -0500157 Buffer buffer1(safeBag2.getEncryptedKey().begin(), safeBag2.getEncryptedKey().end());
158 Buffer buffer2(ENCRYPTED_KEY, sizeof(ENCRYPTED_KEY));
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700159 BOOST_CHECK(buffer1 == buffer2);
160}
161
162BOOST_AUTO_TEST_SUITE_END() // TestSafeBag
163BOOST_AUTO_TEST_SUITE_END() // Security
164
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400165} // namespace ndn::tests