blob: 6d44811f2fb74554c9bdb62856ec041a42027a64 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu28fd32f2014-01-28 19:03:03 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu28fd32f2014-01-28 19:03:03 -080020 */
21
Yingdi Yuf56c68f2014-04-24 21:50:13 -070022#include "security/sec-public-info-sqlite3.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080023#include "security/key-chain.hpp"
Yingdi Yu40b53092014-06-17 17:10:02 -070024#include "security/cryptopp.hpp"
25#include "encoding/buffer-stream.hpp"
Yingdi Yu9a335352014-01-31 11:57:46 -080026#include "util/time.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080027
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028#include "boost-test.hpp"
29
Yingdi Yu28fd32f2014-01-28 19:03:03 -080030using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080031namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080032
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070033BOOST_AUTO_TEST_SUITE(SecurityTestSecPublicInfoSqlite3)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080034
Yingdi Yu40b53092014-06-17 17:10:02 -070035const std::string RSA_DER("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuFoDcNtffwbfFix64fw0\
36hI2tKMkFrc6Ex7yw0YLMK9vGE8lXOyBl/qXabow6RCz+GldmFN6E2Qhm1+AX3Zm5\
37sj3H53/HPtzMefvMQ9X7U+lK8eNMWawpRzvBh4/36VrK/awlkNIVIQ9aXj6q6BVe\
38zL+zWT/WYemLq/8A1/hHWiwCtfOH1xQhGqWHJzeSgwIgOOrzxTbRaCjhAb1u2TeV\
39yx/I9H/DV+AqSHCaYbB92HDcDN0kqwSnUf5H1+osE9MR5DLBLhXdSiULSgxT3Or/\
40y2QgsgUK59WrjhlVMPEiHHRs15NZJbL1uQFXjgScdEarohcY3dilqotineFZCeN8\
41DwIDAQAB");
42const std::string ECDSA_DER("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENZpqkPJDj8uhSpffOiCbvSYMLsGB\
431Eo/WU6mrexjGvduQXjqwon/eSHFI6EgHZk8L9KfiV5XVtVsk2g5wIpJVg==");
44
Yingdi Yuf56c68f2014-04-24 21:50:13 -070045BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080046{
Yingdi Yuf56c68f2014-04-24 21:50:13 -070047 BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
48 KeyChain keyChain("sqlite3", "file");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080049
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050 Name identity("/TestSecPublicInfoSqlite3/Delete");
51 identity.appendVersion();
52
Alexander Afanasyevb46f9702014-02-06 18:58:55 -080053 Name certName1;
54 BOOST_REQUIRE_NO_THROW(certName1 = keyChain.createIdentity(identity));
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080055
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070056 Name keyName1 = IdentityCertificate::certificateNameToPublicKeyName(certName1);
Yingdi Yu9a335352014-01-31 11:57:46 -080057 Name keyName2;
Yingdi Yuf56c68f2014-04-24 21:50:13 -070058 BOOST_REQUIRE_NO_THROW(keyName2 = keyChain.generateRsaKeyPairAsDefault(identity));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070059
Yingdi Yu2e57a582014-02-20 23:34:43 -080060 shared_ptr<IdentityCertificate> cert2;
61 BOOST_REQUIRE_NO_THROW(cert2 = keyChain.selfSign(keyName2));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080062 Name certName2 = cert2->getName();
Yingdi Yu2e57a582014-02-20 23:34:43 -080063 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert2));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070064
Yingdi Yu9a335352014-01-31 11:57:46 -080065 Name keyName3;
Yingdi Yuf56c68f2014-04-24 21:50:13 -070066 BOOST_REQUIRE_NO_THROW(keyName3 = keyChain.generateRsaKeyPairAsDefault(identity));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070067
Yingdi Yu2e57a582014-02-20 23:34:43 -080068 shared_ptr<IdentityCertificate> cert3;
69 BOOST_REQUIRE_NO_THROW(cert3 = keyChain.selfSign(keyName3));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080070 Name certName3 = cert3->getName();
Yingdi Yu2e57a582014-02-20 23:34:43 -080071 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert3));
72 shared_ptr<IdentityCertificate> cert4;
73 BOOST_REQUIRE_NO_THROW(cert4 = keyChain.selfSign(keyName3));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080074 Name certName4 = cert4->getName();
Yingdi Yu2e57a582014-02-20 23:34:43 -080075 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert4));
76 shared_ptr<IdentityCertificate> cert5;
77 BOOST_REQUIRE_NO_THROW(cert5 = keyChain.selfSign(keyName3));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080078 Name certName5 = cert5->getName();
Yingdi Yu2e57a582014-02-20 23:34:43 -080079 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert5));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080080
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080081 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), true);
82 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), true);
83 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), true);
84 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), true);
85 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName1), true);
86 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName2), true);
87 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), true);
88 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), true);
89 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName5), true);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080090
Yingdi Yuf8fc8de2014-02-25 15:45:39 -080091 BOOST_REQUIRE_NO_THROW(keyChain.deleteCertificate(certName5));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080092 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName5), false);
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080093 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), true);
94 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), true);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080095 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), true);
96
Yingdi Yuf8fc8de2014-02-25 15:45:39 -080097 BOOST_REQUIRE_NO_THROW(keyChain.deleteKey(keyName3));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080098 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), false);
99 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), false);
100 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), false);
101 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), true);
102 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), true);
103 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), true);
104
Yingdi Yuf8fc8de2014-02-25 15:45:39 -0800105 BOOST_REQUIRE_NO_THROW(keyChain.deleteIdentity(identity));
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800106 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName2), false);
107 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), false);
108 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName1), false);
109 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), false);
110 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false);
111}
112
Yingdi Yu40b53092014-06-17 17:10:02 -0700113BOOST_AUTO_TEST_CASE(KeyTypeRsa)
114{
115 using namespace CryptoPP;
116
117 OBufferStream os;
118 StringSource ss(reinterpret_cast<const uint8_t*>(RSA_DER.c_str()), RSA_DER.size(),
119 true, new Base64Decoder(new FileSink(os)));
120
121 shared_ptr<PublicKey> rsaKey;
122 BOOST_REQUIRE_NO_THROW(rsaKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
123 os.buf()->size())));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700124 Name rsaKeyName("/TestSecPublicInfoSqlite3/KeyType/RSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700125 SecPublicInfoSqlite3 pib;
126 pib.addPublicKey(rsaKeyName, rsaKey->getKeyType(), *rsaKey);
127
128 BOOST_CHECK_EQUAL(KEY_TYPE_RSA, pib.getPublicKeyType(rsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700129
130 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/RSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700131}
132
133BOOST_AUTO_TEST_CASE(KeyTypeEcdsa)
134{
135 using namespace CryptoPP;
136
137 OBufferStream os;
138 StringSource ss(reinterpret_cast<const uint8_t*>(ECDSA_DER.c_str()), ECDSA_DER.size(),
139 true, new Base64Decoder(new FileSink(os)));
140
141 shared_ptr<PublicKey> ecdsaKey;
142 BOOST_REQUIRE_NO_THROW(ecdsaKey = shared_ptr<PublicKey>(new PublicKey(os.buf()->buf(),
143 os.buf()->size())));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700144 Name ecdsaKeyName("/TestSecPublicInfoSqlite3/KeyType/ECDSA/ksk-123");
Yingdi Yu40b53092014-06-17 17:10:02 -0700145 SecPublicInfoSqlite3 pib;
146 pib.addPublicKey(ecdsaKeyName, ecdsaKey->getKeyType(), *ecdsaKey);
147
148 BOOST_CHECK_EQUAL(KEY_TYPE_ECDSA, pib.getPublicKeyType(ecdsaKeyName));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700149 pib.deleteIdentityInfo(Name("/TestSecPublicInfoSqlite3/KeyType/ECDSA"));
Yingdi Yu40b53092014-06-17 17:10:02 -0700150}
151
152BOOST_AUTO_TEST_CASE(KeyTypeNonExist)
153{
154 Name nullKeyName("/TestSecPublicInfoSqlite3/KeyType/Null");
155 SecPublicInfoSqlite3 pib;
156
157 BOOST_CHECK_EQUAL(KEY_TYPE_NULL, pib.getPublicKeyType(nullKeyName));
158
159}
160
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800161BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800162
163} // namespace ndn