Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -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 | */ |
| 23 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 24 | #include "ndn-cxx/security/certificate.hpp" |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 27 | #include "tests/unit/clock-fixture.hpp" |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 28 | |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 33 | inline namespace v2 { |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 34 | namespace tests { |
| 35 | |
| 36 | using namespace ndn::tests; |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Security) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(TestCertificate, ClockFixture) |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 40 | |
| 41 | const uint8_t PUBLIC_KEY[] = { |
| 42 | 0x30, 0x81, 0x9d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, |
| 43 | 0x01, 0x05, 0x00, 0x03, 0x81, 0x8b, 0x00, 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9e, |
| 44 | 0x06, 0x3e, 0x47, 0x85, 0xb2, 0x34, 0x37, 0xaa, 0x85, 0x47, 0xac, 0x03, 0x24, 0x83, 0xb5, |
| 45 | 0x9c, 0xa8, 0x05, 0x3a, 0x24, 0x1e, 0xeb, 0x89, 0x01, 0xbb, 0xe9, 0x9b, 0xb2, 0xc3, 0x22, |
| 46 | 0xac, 0x68, 0xe3, 0xf0, 0x6c, 0x02, 0xce, 0x68, 0xa6, 0xc4, 0xd0, 0xa7, 0x06, 0x90, 0x9c, |
| 47 | 0xaa, 0x1b, 0x08, 0x1d, 0x8b, 0x43, 0x9a, 0x33, 0x67, 0x44, 0x6d, 0x21, 0xa3, 0x1b, 0x88, |
| 48 | 0x9a, 0x97, 0x5e, 0x59, 0xc4, 0x15, 0x0b, 0xd9, 0x2c, 0xbd, 0x51, 0x07, 0x61, 0x82, 0xad, |
| 49 | 0xc1, 0xb8, 0xd7, 0xbf, 0x9b, 0xcf, 0x7d, 0x24, 0xc2, 0x63, 0xf3, 0x97, 0x17, 0xeb, 0xfe, |
| 50 | 0x62, 0x25, 0xba, 0x5b, 0x4d, 0x8a, 0xc2, 0x7a, 0xbd, 0x43, 0x8a, 0x8f, 0xb8, 0xf2, 0xf1, |
| 51 | 0xc5, 0x6a, 0x30, 0xd3, 0x50, 0x8c, 0xc8, 0x9a, 0xdf, 0xef, 0xed, 0x35, 0xe7, 0x7a, 0x62, |
| 52 | 0xea, 0x76, 0x7c, 0xbb, 0x08, 0x26, 0xc7, 0x02, 0x01, 0x11 |
| 53 | }; |
| 54 | |
| 55 | const uint8_t SIG_INFO[] = { |
| 56 | 0x16, 0x55, 0x1B, 0x01, 0x01, 0x1C, 0x26, 0x07, 0x24, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x05, |
| 57 | 0x73, 0x69, 0x74, 0x65, 0x31, 0x08, 0x11, 0x6B, 0x73, 0x6B, 0x2D, 0x32, 0x35, 0x31, 0x36, 0x34, |
| 58 | 0x32, 0x35, 0x33, 0x37, 0x37, 0x30, 0x39, 0x34, 0x08, 0x03, 0x4B, 0x45, 0x59, 0xFD, 0x00, 0xFD, |
| 59 | 0x26, 0xFD, 0x00, 0xFE, 0x0F, 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x31, 0x34, 0x54, 0x32, 0x32, |
| 60 | 0x33, 0x37, 0x33, 0x39, 0xFD, 0x00, 0xFF, 0x0F, 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x31, 0x38, |
| 61 | 0x54, 0x32, 0x32, 0x33, 0x37, 0x33, 0x38 |
| 62 | }; |
| 63 | |
| 64 | const uint8_t SIG_VALUE[] = { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 65 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, 0xe3, 0xb9, 0xea, |
| 66 | 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, |
| 67 | 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, |
| 68 | 0x4d, 0x10, 0x1d, 0xdc, 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, |
| 69 | 0xcf, 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, 0x64, 0xd6, |
| 70 | 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, 0x55, 0xf6, 0x1c, 0x19, 0x0b, |
| 71 | 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, |
| 72 | 0x6d, 0xdb, 0x56, 0xac, 0xa3, 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1, |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | const uint8_t CERT[] = { |
| 76 | 0x06, 0xFD, 0x01, 0xBB, // Data |
| 77 | 0x07, 0x33, // Name /ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B |
| 78 | 0x08, 0x03, 0x6E, 0x64, 0x6E, |
| 79 | 0x08, 0x05, 0x73, 0x69, 0x74, 0x65, 0x31, |
| 80 | 0x08, 0x03, 0x4B, 0x45, 0x59, |
| 81 | 0x08, 0x11, |
| 82 | 0x6B, 0x73, 0x6B, 0x2D, 0x31, 0x34, 0x31, 0x36, 0x34, 0x32, 0x35, 0x33, 0x37, 0x37, 0x30, 0x39, |
| 83 | 0x34, |
| 84 | 0x08, 0x04, 0x30, 0x31, 0x32, 0x33, |
| 85 | 0x08, 0x07, 0xFD, 0x00, 0x00, 0x01, 0x49, 0xC9, 0x8B, |
| 86 | 0x14, 0x09, // MetaInfo |
| 87 | 0x18, 0x01, 0x02, // ContentType = Key |
| 88 | 0x19, 0x04, 0x00, 0x36, 0xEE, 0x80, // FreshnessPeriod = 3600000 ms |
| 89 | 0x15, 0xA0, // Content |
| 90 | 0x30, 0x81, 0x9D, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, |
| 91 | 0x05, 0x00, 0x03, 0x81, 0x8B, 0x00, 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9E, 0x06, 0x3E, |
| 92 | 0x47, 0x85, 0xB2, 0x34, 0x37, 0xAA, 0x85, 0x47, 0xAC, 0x03, 0x24, 0x83, 0xB5, 0x9C, 0xA8, 0x05, |
| 93 | 0x3A, 0x24, 0x1E, 0xEB, 0x89, 0x01, 0xBB, 0xE9, 0x9B, 0xB2, 0xC3, 0x22, 0xAC, 0x68, 0xE3, 0xF0, |
| 94 | 0x6C, 0x02, 0xCE, 0x68, 0xA6, 0xC4, 0xD0, 0xA7, 0x06, 0x90, 0x9C, 0xAA, 0x1B, 0x08, 0x1D, 0x8B, |
| 95 | 0x43, 0x9A, 0x33, 0x67, 0x44, 0x6D, 0x21, 0xA3, 0x1B, 0x88, 0x9A, 0x97, 0x5E, 0x59, 0xC4, 0x15, |
| 96 | 0x0B, 0xD9, 0x2C, 0xBD, 0x51, 0x07, 0x61, 0x82, 0xAD, 0xC1, 0xB8, 0xD7, 0xBF, 0x9B, 0xCF, 0x7D, |
| 97 | 0x24, 0xC2, 0x63, 0xF3, 0x97, 0x17, 0xEB, 0xFE, 0x62, 0x25, 0xBA, 0x5B, 0x4D, 0x8A, 0xC2, 0x7A, |
| 98 | 0xBD, 0x43, 0x8A, 0x8F, 0xB8, 0xF2, 0xF1, 0xC5, 0x6A, 0x30, 0xD3, 0x50, 0x8C, 0xC8, 0x9A, 0xDF, |
| 99 | 0xEF, 0xED, 0x35, 0xE7, 0x7A, 0x62, 0xEA, 0x76, 0x7C, 0xBB, 0x08, 0x26, 0xC7, 0x02, 0x01, 0x11, |
| 100 | 0x16, 0x55, // SignatureInfo |
| 101 | 0x1B, 0x01, 0x01, // SignatureType |
| 102 | 0x1C, 0x26, // KeyLocator: /ndn/site1/KEY/ksk-2516425377094 |
| 103 | 0x07, 0x24, |
| 104 | 0x08, 0x03, 0x6E, 0x64, 0x6E, |
| 105 | 0x08, 0x05, 0x73, 0x69, 0x74, 0x65, 0x31, |
| 106 | 0x08, 0x03, 0x4B, 0x45, 0x59, |
| 107 | 0x08, 0x11, |
| 108 | 0x6B, 0x73, 0x6B, 0x2D, 0x32, 0x35, 0x31, 0x36, 0x34, 0x32, 0x35, 0x33, 0x37, 0x37, 0x30, 0x39, |
| 109 | 0x34, |
| 110 | 0xFD, 0x00, 0xFD, 0x26, // ValidityPeriod: (20150814T223739, 20150818T223738) |
| 111 | 0xFD, 0x00, 0xFE, 0x0F, |
| 112 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x31, 0x34, 0x54, 0x32, 0x32, 0x33, 0x37, 0x33, 0x39, |
| 113 | 0xFD, 0x00, 0xFF, 0x0F, |
| 114 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x31, 0x38, 0x54, 0x32, 0x32, 0x33, 0x37, 0x33, 0x38, |
| 115 | 0x17, 0x80, // SignatureValue |
| 116 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 117 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 118 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 119 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 120 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 121 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 122 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 123 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF |
| 124 | }; |
| 125 | |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 126 | static void |
| 127 | generateFakeSignature(Data& data) |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 128 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 129 | SignatureInfo signatureInfo(Block{SIG_INFO}); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 130 | signatureInfo.setKeyLocator(KeyLocator(Name("/ndn/site1/KEY/ksk-2516425377094"))); |
| 131 | signatureInfo.setValidityPeriod(ValidityPeriod(time::fromIsoString("20141111T050000"), |
| 132 | time::fromIsoString("20141111T060000"))); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 133 | |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 134 | data.setSignatureInfo(signatureInfo); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 135 | data.setSignatureValue(make_shared<Buffer>(SIG_VALUE, sizeof(SIG_VALUE))); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(Construction) |
| 139 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 140 | Block block(CERT); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 141 | Certificate certificate(block); |
| 142 | |
| 143 | BOOST_CHECK_EQUAL(certificate.getName(), "/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"); |
| 144 | BOOST_CHECK_EQUAL(certificate.getKeyName(), "/ndn/site1/KEY/ksk-1416425377094"); |
| 145 | BOOST_CHECK_EQUAL(certificate.getIdentity(), "/ndn/site1"); |
| 146 | BOOST_CHECK_EQUAL(certificate.getIssuerId(), name::Component("0123")); |
| 147 | BOOST_CHECK_EQUAL(certificate.getKeyId(), name::Component("ksk-1416425377094")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 148 | BOOST_REQUIRE_EQUAL(certificate.getKeyLocator().has_value(), true); |
| 149 | BOOST_CHECK_EQUAL(certificate.getKeyLocator()->getName(), "/ndn/site1/KEY/ksk-2516425377094"); |
| 150 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(certificate.getValidityPeriod()), |
| 151 | "(20150814T223739, 20150818T223738)"); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 152 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 153 | BOOST_CHECK_THROW(certificate.getExtension(12345), ndn::Data::Error); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 154 | BOOST_CHECK_NO_THROW(certificate.getPublicKey()); |
| 155 | |
| 156 | Data data(block); |
| 157 | Certificate certificate2(std::move(data)); |
| 158 | BOOST_CHECK_EQUAL(certificate, certificate2); |
| 159 | } |
| 160 | |
| 161 | BOOST_AUTO_TEST_CASE(Setters) |
| 162 | { |
| 163 | Certificate certificate; |
| 164 | certificate.setName("/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 165 | certificate.setFreshnessPeriod(1_h); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 166 | certificate.setContent(PUBLIC_KEY); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 167 | generateFakeSignature(certificate); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 168 | |
| 169 | BOOST_CHECK_EQUAL(certificate.getName(), "/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"); |
| 170 | BOOST_CHECK_EQUAL(certificate.getKeyName(), "/ndn/site1/KEY/ksk-1416425377094"); |
| 171 | BOOST_CHECK_EQUAL(certificate.getIdentity(), "/ndn/site1"); |
| 172 | BOOST_CHECK_EQUAL(certificate.getIssuerId(), name::Component("0123")); |
| 173 | BOOST_CHECK_EQUAL(certificate.getKeyId(), name::Component("ksk-1416425377094")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 174 | BOOST_REQUIRE_EQUAL(certificate.getKeyLocator().has_value(), true); |
| 175 | BOOST_CHECK_EQUAL(certificate.getKeyLocator()->getName(), "/ndn/site1/KEY/ksk-2516425377094"); |
| 176 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(certificate.getValidityPeriod()), |
| 177 | "(20141111T050000, 20141111T060000)"); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 178 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 179 | BOOST_CHECK_THROW(certificate.getExtension(12345), ndn::Data::Error); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 180 | BOOST_CHECK_NO_THROW(certificate.getPublicKey()); |
| 181 | } |
| 182 | |
| 183 | BOOST_AUTO_TEST_CASE(ValidityPeriodChecking) |
| 184 | { |
| 185 | Certificate certificate; |
| 186 | certificate.setName("/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 187 | certificate.setFreshnessPeriod(1_h); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 188 | certificate.setContent(PUBLIC_KEY); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 189 | generateFakeSignature(certificate); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 190 | |
| 191 | BOOST_CHECK_EQUAL(certificate.isValid(), true); |
| 192 | BOOST_CHECK_EQUAL(certificate.isValid(time::fromIsoString("20141111T045959")), false); |
| 193 | BOOST_CHECK_EQUAL(certificate.isValid(time::fromIsoString("20141111T060001")), false); |
| 194 | } |
| 195 | |
| 196 | // This fixture prepares a well-formed certificate. A test case then modifies one of the |
| 197 | // fields, and verifies the Certificate class correctly identifies the certificate as |
| 198 | // malformed. |
| 199 | class InvalidCertFixture |
| 200 | { |
| 201 | public: |
| 202 | InvalidCertFixture() |
| 203 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 204 | Certificate certBase(Block{CERT}); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 205 | BOOST_CHECK_NO_THROW((Certificate(certBase))); |
| 206 | |
| 207 | m_certBase = Data(certBase); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 208 | generateFakeSignature(m_certBase); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 209 | |
| 210 | BOOST_CHECK_NO_THROW((Certificate(m_certBase))); |
| 211 | } |
| 212 | |
| 213 | public: |
| 214 | Data m_certBase; |
| 215 | }; |
| 216 | |
| 217 | BOOST_FIXTURE_TEST_CASE(InvalidName, InvalidCertFixture) |
| 218 | { |
| 219 | Data data(m_certBase); |
| 220 | data.setName("/ndn/site1/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 221 | generateFakeSignature(data); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 222 | |
| 223 | BOOST_CHECK_THROW((Certificate(data)), Certificate::Error); |
| 224 | BOOST_CHECK_THROW((Certificate(std::move(data))), Certificate::Error); |
| 225 | } |
| 226 | |
| 227 | BOOST_FIXTURE_TEST_CASE(InvalidType, InvalidCertFixture) |
| 228 | { |
| 229 | Data data(m_certBase); |
| 230 | data.setContentType(tlv::ContentType_Blob); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 231 | generateFakeSignature(data); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 232 | |
| 233 | BOOST_CHECK_THROW((Certificate(data)), Certificate::Error); |
| 234 | BOOST_CHECK_THROW((Certificate(std::move(data))), Certificate::Error); |
| 235 | } |
| 236 | |
| 237 | BOOST_FIXTURE_TEST_CASE(EmptyContent, InvalidCertFixture) |
| 238 | { |
| 239 | Data data(m_certBase); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 240 | data.setContent(span<uint8_t>{}); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 241 | generateFakeSignature(data); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 242 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 243 | BOOST_CHECK_THROW(Certificate{data}, Certificate::Error); |
| 244 | BOOST_CHECK_THROW(Certificate{std::move(data)}, Certificate::Error); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 245 | |
| 246 | Certificate cert(m_certBase); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 247 | cert.setContent(span<uint8_t>{}); |
Eric Newberry | a3c8bd1 | 2020-05-15 17:27:07 -0700 | [diff] [blame] | 248 | generateFakeSignature(cert); |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 249 | BOOST_CHECK_THROW(cert.getPublicKey(), Certificate::Error); |
| 250 | } |
| 251 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 252 | BOOST_AUTO_TEST_CASE(PrintCertificateInfo) |
| 253 | { |
| 254 | const std::string expectedCertificateInfo = std::string(R"INFO( |
| 255 | Certificate name: |
| 256 | /ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B |
| 257 | Validity: |
| 258 | NotBefore: 20150814T223739 |
| 259 | NotAfter: 20150818T223738 |
| 260 | Public key bits: |
| 261 | MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCeBj5HhbI0N6qFR6wDJIO1nKgF |
| 262 | OiQe64kBu+mbssMirGjj8GwCzmimxNCnBpCcqhsIHYtDmjNnRG0hoxuImpdeWcQV |
| 263 | C9ksvVEHYYKtwbjXv5vPfSTCY/OXF+v+YiW6W02Kwnq9Q4qPuPLxxWow01CMyJrf |
| 264 | 7+0153pi6nZ8uwgmxwIBEQ== |
| 265 | Signature Information: |
| 266 | Signature Type: SignatureSha256WithRsa |
| 267 | Key Locator: Name=/ndn/site1/KEY/ksk-2516425377094 |
| 268 | )INFO").substr(1); |
| 269 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 270 | Certificate certificate(Block{CERT}); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(certificate), expectedCertificateInfo); |
| 272 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 273 | // TODO: Check output formats of other certificates |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 276 | BOOST_AUTO_TEST_SUITE_END() // TestCertificate |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 277 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 278 | |
| 279 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 280 | } // inline namespace v2 |
Zhiyi Zhang | f4bb5c7 | 2015-08-19 19:02:51 -0700 | [diff] [blame] | 281 | } // namespace security |
| 282 | } // namespace ndn |