blob: 96fda5859071787c34923551f6f9353e2d808786 [file] [log] [blame]
Yingdi Yu2620b1c2014-06-12 15:32:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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
22#include "security/public-key.hpp"
23#include "security/cryptopp.hpp"
24#include "encoding/buffer-stream.hpp"
25
26#include "boost-test.hpp"
27
Yingdi Yu2620b1c2014-06-12 15:32:57 -070028namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Yingdi Yu2620b1c2014-06-12 15:32:57 -070030
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031BOOST_AUTO_TEST_SUITE(SecurityPublicKey)
Yingdi Yu2620b1c2014-06-12 15:32:57 -070032
33const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
34hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
35sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
36zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
37yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
38y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
39DwIDAQAB");
Yingdi Yue36322a2014-11-04 14:16:54 -080040
41const uint8_t RSA_DER_KEY_DIGEST[] = {
42 0x1d, 0x20,
43 0x58, 0x72, 0x4c, 0xf7, 0x36, 0x3d, 0xee, 0x4a,
44 0x5c, 0x5b, 0x39, 0x44, 0x2d, 0xf6, 0x1a, 0x24,
45 0xda, 0x13, 0xac, 0xab, 0x70, 0xf7, 0x74, 0x40,
46 0x5a, 0x44, 0xfe, 0xc0, 0xc9, 0x26, 0x58, 0x74
47};
48
Yingdi Yu2620b1c2014-06-12 15:32:57 -070049const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
501Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
51
Yingdi Yue36322a2014-11-04 14:16:54 -080052const uint8_t ECDSA_DER_KEY_DIGEST[] = {
53 0x1d, 0x20,
54 0xaf, 0x82, 0x3f, 0xfc, 0xdc, 0x85, 0xb2, 0xa4,
55 0xc8, 0xf5, 0x3b, 0x1a, 0xf8, 0xec, 0x4a, 0x55,
56 0x97, 0x55, 0x19, 0x3f, 0x54, 0xdd, 0xd0, 0xfd,
57 0xb5, 0x9d, 0x80, 0x65, 0x80, 0x6b, 0x4b, 0x63
58};
59
Yingdi Yu2620b1c2014-06-12 15:32:57 -070060BOOST_AUTO_TEST_CASE(RSA)
61{
62 using namespace CryptoPP;
63
64 OBufferStream os;
65 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
66 true, new Base64Decoder(new FileSink(os)));
67
68 shared_ptr<PublicKey> publicKey;
69 BOOST_REQUIRE_NO_THROW(publicKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
70 os.buf()->size())));
71
72 BOOST_CHECK_EQUAL(publicKey->getKeyType(), KEY_TYPE_RSA);
Yingdi Yue36322a2014-11-04 14:16:54 -080073
74 Block digestBlock(RSA_DER_KEY_DIGEST, sizeof(RSA_DER_KEY_DIGEST));
75 const Block& digest = publicKey->computeDigest();
76 BOOST_CHECK_EQUAL_COLLECTIONS(digestBlock.wire(),
77 digestBlock.wire() + digestBlock.size(),
78 digest.wire(),
79 digest.wire() + digest.size());
Yingdi Yu2620b1c2014-06-12 15:32:57 -070080}
81
82BOOST_AUTO_TEST_CASE(ECDSA)
83{
84 using namespace CryptoPP;
85
86 OBufferStream os;
87 StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(),
88 true, new Base64Decoder(new FileSink(os)));
89
90 shared_ptr<PublicKey> publicKey;
91 BOOST_REQUIRE_NO_THROW(publicKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
92 os.buf()->size())));
93
94 BOOST_CHECK_EQUAL(publicKey->getKeyType(), KEY_TYPE_ECDSA);
Yingdi Yue36322a2014-11-04 14:16:54 -080095
96 Block digestBlock(ECDSA_DER_KEY_DIGEST, sizeof(ECDSA_DER_KEY_DIGEST));
97 const Block& digest = publicKey->computeDigest();
98 BOOST_CHECK_EQUAL_COLLECTIONS(digestBlock.wire(),
99 digestBlock.wire() + digestBlock.size(),
100 digest.wire(),
101 digest.wire() + digest.size());
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700102}
103
Yingdi Yue36322a2014-11-04 14:16:54 -0800104
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700105BOOST_AUTO_TEST_SUITE_END()
106
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800107} // namespace tests
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700108} // namespace ndn