Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 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 | |
| 22 | #include "key-impl.hpp" |
| 23 | #include "../pib-impl.hpp" |
| 24 | #include "../pib.hpp" |
| 25 | #include "../../transform/public-key.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
| 29 | namespace pib { |
| 30 | namespace detail { |
| 31 | |
| 32 | KeyImpl::KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> impl) |
| 33 | : m_identity(v2::extractIdentityFromKeyName(keyName)) |
| 34 | , m_keyName(keyName) |
| 35 | , m_key(key, keyLen) |
| 36 | , m_isDefaultCertificateLoaded(false) |
| 37 | , m_certificates(keyName, impl) |
| 38 | , m_impl(impl) |
| 39 | { |
| 40 | BOOST_ASSERT(impl != nullptr); |
| 41 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 42 | transform::PublicKey publicKey; |
| 43 | try { |
| 44 | publicKey.loadPkcs8(key, keyLen); |
| 45 | } |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 46 | catch (const transform::PublicKey::Error&) { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 47 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid key bits")); |
| 48 | } |
| 49 | m_keyType = publicKey.getKeyType(); |
| 50 | |
| 51 | m_impl->addKey(m_identity, m_keyName, key, keyLen); |
| 52 | } |
| 53 | |
| 54 | KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> impl) |
| 55 | : m_identity(v2::extractIdentityFromKeyName(keyName)) |
| 56 | , m_keyName(keyName) |
| 57 | , m_isDefaultCertificateLoaded(false) |
| 58 | , m_certificates(keyName, impl) |
| 59 | , m_impl(impl) |
| 60 | { |
| 61 | BOOST_ASSERT(impl != nullptr); |
| 62 | |
| 63 | m_key = m_impl->getKeyBits(m_keyName); |
| 64 | |
| 65 | transform::PublicKey key; |
| 66 | key.loadPkcs8(m_key.buf(), m_key.size()); |
| 67 | m_keyType = key.getKeyType(); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | KeyImpl::addCertificate(const v2::Certificate& certificate) |
| 72 | { |
| 73 | BOOST_ASSERT(m_certificates.isConsistent()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 74 | m_certificates.add(certificate); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | KeyImpl::removeCertificate(const Name& certName) |
| 79 | { |
| 80 | BOOST_ASSERT(m_certificates.isConsistent()); |
| 81 | |
| 82 | if (m_isDefaultCertificateLoaded && m_defaultCertificate.getName() == certName) |
| 83 | m_isDefaultCertificateLoaded = false; |
| 84 | |
| 85 | m_certificates.remove(certName); |
| 86 | } |
| 87 | |
| 88 | v2::Certificate |
| 89 | KeyImpl::getCertificate(const Name& certName) const |
| 90 | { |
| 91 | BOOST_ASSERT(m_certificates.isConsistent()); |
| 92 | |
| 93 | return m_certificates.get(certName); |
| 94 | } |
| 95 | |
| 96 | const CertificateContainer& |
| 97 | KeyImpl::getCertificates() const |
| 98 | { |
| 99 | BOOST_ASSERT(m_certificates.isConsistent()); |
| 100 | |
| 101 | return m_certificates; |
| 102 | } |
| 103 | |
| 104 | const v2::Certificate& |
| 105 | KeyImpl::setDefaultCertificate(const Name& certName) |
| 106 | { |
| 107 | BOOST_ASSERT(m_certificates.isConsistent()); |
| 108 | |
| 109 | m_defaultCertificate = m_certificates.get(certName); |
| 110 | m_impl->setDefaultCertificateOfKey(m_keyName, certName); |
| 111 | m_isDefaultCertificateLoaded = true; |
| 112 | return m_defaultCertificate; |
| 113 | } |
| 114 | |
| 115 | const v2::Certificate& |
| 116 | KeyImpl::setDefaultCertificate(const v2::Certificate& certificate) |
| 117 | { |
| 118 | addCertificate(certificate); |
| 119 | return setDefaultCertificate(certificate.getName()); |
| 120 | } |
| 121 | |
| 122 | const v2::Certificate& |
| 123 | KeyImpl::getDefaultCertificate() const |
| 124 | { |
| 125 | BOOST_ASSERT(m_certificates.isConsistent()); |
| 126 | |
| 127 | if (!m_isDefaultCertificateLoaded) { |
| 128 | m_defaultCertificate = m_impl->getDefaultCertificateOfKey(m_keyName); |
| 129 | m_isDefaultCertificateLoaded = true; |
| 130 | } |
| 131 | |
| 132 | BOOST_ASSERT(m_impl->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode()); |
| 133 | |
| 134 | return m_defaultCertificate; |
| 135 | } |
| 136 | |
| 137 | } // namespace detail |
| 138 | } // namespace pib |
| 139 | } // namespace security |
| 140 | } // namespace ndn |