blob: 53df40c64e7a7f7ae02403a762d8ae63cf9c287a [file] [log] [blame]
Yingdi Yu2620b1c2014-06-12 15:32:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu99b2a002015-08-12 12:47:44 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Yingdi Yu2620b1c2014-06-12 15:32:57 -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
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070022#include "security/v1/public-key.hpp"
23#include "security/v1/cryptopp.hpp"
Yingdi Yu2620b1c2014-06-12 15:32:57 -070024#include "encoding/buffer-stream.hpp"
25
26#include "boost-test.hpp"
27
Yingdi Yu2620b1c2014-06-12 15:32:57 -070028namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070029namespace security {
30namespace v1 {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031namespace tests {
Yingdi Yu2620b1c2014-06-12 15:32:57 -070032
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070033BOOST_AUTO_TEST_SUITE(Security)
34BOOST_AUTO_TEST_SUITE(V1)
35BOOST_AUTO_TEST_SUITE(TestPublicKey)
Yingdi Yu2620b1c2014-06-12 15:32:57 -070036
37const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
38hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
39sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
40zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
41yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
42y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
43DwIDAQAB");
Yingdi Yue36322a2014-11-04 14:16:54 -080044
45const uint8_t RSA_DER_KEY_DIGEST[] = {
46 0x1d, 0x20,
47 0x58, 0x72, 0x4c, 0xf7, 0x36, 0x3d, 0xee, 0x4a,
48 0x5c, 0x5b, 0x39, 0x44, 0x2d, 0xf6, 0x1a, 0x24,
49 0xda, 0x13, 0xac, 0xab, 0x70, 0xf7, 0x74, 0x40,
50 0x5a, 0x44, 0xfe, 0xc0, 0xc9, 0x26, 0x58, 0x74
51};
52
Yingdi Yu2620b1c2014-06-12 15:32:57 -070053const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
541Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
55
Yingdi Yue36322a2014-11-04 14:16:54 -080056const uint8_t ECDSA_DER_KEY_DIGEST[] = {
57 0x1d, 0x20,
58 0xaf, 0x82, 0x3f, 0xfc, 0xdc, 0x85, 0xb2, 0xa4,
59 0xc8, 0xf5, 0x3b, 0x1a, 0xf8, 0xec, 0x4a, 0x55,
60 0x97, 0x55, 0x19, 0x3f, 0x54, 0xdd, 0xd0, 0xfd,
61 0xb5, 0x9d, 0x80, 0x65, 0x80, 0x6b, 0x4b, 0x63
62};
63
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070064BOOST_AUTO_TEST_CASE(Rsa)
Yingdi Yu2620b1c2014-06-12 15:32:57 -070065{
66 using namespace CryptoPP;
67
68 OBufferStream os;
69 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
70 true, new Base64Decoder(new FileSink(os)));
71
72 shared_ptr<PublicKey> publicKey;
73 BOOST_REQUIRE_NO_THROW(publicKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
74 os.buf()->size())));
75
Yingdi Yu99b2a002015-08-12 12:47:44 -070076 BOOST_CHECK_EQUAL(publicKey->getKeyType(), KeyType::RSA);
Yingdi Yue36322a2014-11-04 14:16:54 -080077
78 Block digestBlock(RSA_DER_KEY_DIGEST, sizeof(RSA_DER_KEY_DIGEST));
79 const Block& digest = publicKey->computeDigest();
80 BOOST_CHECK_EQUAL_COLLECTIONS(digestBlock.wire(),
81 digestBlock.wire() + digestBlock.size(),
82 digest.wire(),
83 digest.wire() + digest.size());
Yingdi Yu2620b1c2014-06-12 15:32:57 -070084}
85
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070086BOOST_AUTO_TEST_CASE(Ecdsa)
Yingdi Yu2620b1c2014-06-12 15:32:57 -070087{
88 using namespace CryptoPP;
89
90 OBufferStream os;
91 StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(),
92 true, new Base64Decoder(new FileSink(os)));
93
94 shared_ptr<PublicKey> publicKey;
95 BOOST_REQUIRE_NO_THROW(publicKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
96 os.buf()->size())));
97
Yingdi Yu99b2a002015-08-12 12:47:44 -070098 BOOST_CHECK_EQUAL(publicKey->getKeyType(), KeyType::EC);
Yingdi Yue36322a2014-11-04 14:16:54 -080099
100 Block digestBlock(ECDSA_DER_KEY_DIGEST, sizeof(ECDSA_DER_KEY_DIGEST));
101 const Block& digest = publicKey->computeDigest();
102 BOOST_CHECK_EQUAL_COLLECTIONS(digestBlock.wire(),
103 digestBlock.wire() + digestBlock.size(),
104 digest.wire(),
105 digest.wire() + digest.size());
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700106}
107
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700108BOOST_AUTO_TEST_SUITE_END() // TestPublicKey
109BOOST_AUTO_TEST_SUITE_END() // V1
110BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700111
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800112} // namespace tests
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700113} // namespace v1
114} // namespace security
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700115} // namespace ndn