blob: c3cc7c23a71bf2f2b01e8fcb5685aa0348d87df0 [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 Pesavento93101df2021-06-15 18:33:09 -04003 * Copyright (c) 2013-2021 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
28namespace ndn {
29namespace security {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(Security)
33BOOST_AUTO_TEST_SUITE(TestSafeBag)
34
35const 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 Pesavento765abc92021-12-27 00:44:04 -050075const uint8_t ENCRYPTED_KEY[] = {
Zhiyi Zhang3e62a832015-07-20 18:36:31 -070076 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
77};
78
79const 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 Pesavento93101df2021-06-15 18:33:09 -0400118 0x81, 0x08, // EncryptedKey
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700119 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
120};
121
122BOOST_AUTO_TEST_CASE(Constructor)
123{
Davide Pesavento765abc92021-12-27 00:44:04 -0500124 Data data(Block(CERT, sizeof(CERT)));
125 SafeBag safeBag1(data, ENCRYPTED_KEY);
126 SafeBag safeBag2(Block(SAFE_BAG, sizeof(SAFE_BAG)));
127 auto encKey = make_span(ENCRYPTED_KEY);
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700128
129 BOOST_CHECK(safeBag1.getCertificate() == data);
Davide Pesavento765abc92021-12-27 00:44:04 -0500130 BOOST_CHECK_EQUAL_COLLECTIONS(safeBag1.getEncryptedKey().begin(), safeBag1.getEncryptedKey().end(),
131 encKey.begin(), encKey.end());
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700132 BOOST_CHECK(safeBag2.getCertificate() == data);
Davide Pesavento765abc92021-12-27 00:44:04 -0500133 BOOST_CHECK_EQUAL_COLLECTIONS(safeBag2.getEncryptedKey().begin(), safeBag2.getEncryptedKey().end(),
134 encKey.begin(), encKey.end());
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700135}
136
137BOOST_AUTO_TEST_CASE(EncoderAndDecoder)
138{
139 Block dataBlock(CERT, sizeof(CERT));
140 Data data(dataBlock);
Davide Pesavento765abc92021-12-27 00:44:04 -0500141 SafeBag safeBag(data, ENCRYPTED_KEY);
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700142
143 // wire encode
144 Block wireBlock = safeBag.wireEncode();
145 Block block(SAFE_BAG, sizeof(SAFE_BAG));
146
147 // check safe bag block
Junxiao Shi72c0c642018-04-20 15:41:09 +0000148 BOOST_CHECK_EQUAL(wireBlock, block);
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700149
150 // wire decode
151 SafeBag safeBag2;
152 safeBag2.wireDecode(wireBlock);
153
154 // check equal
Davide Pesavento765abc92021-12-27 00:44:04 -0500155 Buffer buffer1(safeBag2.getEncryptedKey().begin(), safeBag2.getEncryptedKey().end());
156 Buffer buffer2(ENCRYPTED_KEY, sizeof(ENCRYPTED_KEY));
Zhiyi Zhang3e62a832015-07-20 18:36:31 -0700157 BOOST_CHECK(buffer1 == buffer2);
158}
159
160BOOST_AUTO_TEST_SUITE_END() // TestSafeBag
161BOOST_AUTO_TEST_SUITE_END() // Security
162
163} // namespace tests
164} // namespace security
165} // namespace ndn