blob: e88bdacc34b00ab30fac16acbd39d80740561448 [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- 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
27namespace ndn {
28namespace security {
29namespace pib {
30namespace detail {
31
Davide Pesavento50f66752017-05-15 20:57:12 -040032KeyImpl::KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl)
Yingdi Yucbe72b02015-11-25 17:35:37 -080033 : m_identity(v2::extractIdentityFromKeyName(keyName))
34 , m_keyName(keyName)
35 , m_key(key, keyLen)
Davide Pesavento50f66752017-05-15 20:57:12 -040036 , m_pib(std::move(pibImpl))
37 , m_certificates(keyName, m_pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080038 , m_isDefaultCertificateLoaded(false)
Yingdi Yucbe72b02015-11-25 17:35:37 -080039{
Davide Pesavento50f66752017-05-15 20:57:12 -040040 BOOST_ASSERT(m_pib != nullptr);
Yingdi Yucbe72b02015-11-25 17:35:37 -080041
Yingdi Yucbe72b02015-11-25 17:35:37 -080042 transform::PublicKey publicKey;
43 try {
44 publicKey.loadPkcs8(key, keyLen);
45 }
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -080046 catch (const transform::PublicKey::Error&) {
Yingdi Yucbe72b02015-11-25 17:35:37 -080047 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid key bits"));
48 }
49 m_keyType = publicKey.getKeyType();
50
Davide Pesavento50f66752017-05-15 20:57:12 -040051 m_pib->addKey(m_identity, m_keyName, key, keyLen);
Yingdi Yucbe72b02015-11-25 17:35:37 -080052}
53
Davide Pesavento50f66752017-05-15 20:57:12 -040054KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl)
Yingdi Yucbe72b02015-11-25 17:35:37 -080055 : m_identity(v2::extractIdentityFromKeyName(keyName))
56 , m_keyName(keyName)
Davide Pesavento50f66752017-05-15 20:57:12 -040057 , m_pib(std::move(pibImpl))
58 , m_certificates(keyName, m_pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080059 , m_isDefaultCertificateLoaded(false)
Yingdi Yucbe72b02015-11-25 17:35:37 -080060{
Davide Pesavento50f66752017-05-15 20:57:12 -040061 BOOST_ASSERT(m_pib != nullptr);
Yingdi Yucbe72b02015-11-25 17:35:37 -080062
Davide Pesavento50f66752017-05-15 20:57:12 -040063 m_key = m_pib->getKeyBits(m_keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -080064
65 transform::PublicKey key;
66 key.loadPkcs8(m_key.buf(), m_key.size());
67 m_keyType = key.getKeyType();
68}
69
70void
71KeyImpl::addCertificate(const v2::Certificate& certificate)
72{
73 BOOST_ASSERT(m_certificates.isConsistent());
Yingdi Yucbe72b02015-11-25 17:35:37 -080074 m_certificates.add(certificate);
75}
76
77void
78KeyImpl::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
88v2::Certificate
89KeyImpl::getCertificate(const Name& certName) const
90{
91 BOOST_ASSERT(m_certificates.isConsistent());
Yingdi Yucbe72b02015-11-25 17:35:37 -080092 return m_certificates.get(certName);
93}
94
95const CertificateContainer&
96KeyImpl::getCertificates() const
97{
98 BOOST_ASSERT(m_certificates.isConsistent());
Yingdi Yucbe72b02015-11-25 17:35:37 -080099 return m_certificates;
100}
101
102const v2::Certificate&
103KeyImpl::setDefaultCertificate(const Name& certName)
104{
105 BOOST_ASSERT(m_certificates.isConsistent());
106
107 m_defaultCertificate = m_certificates.get(certName);
Davide Pesavento50f66752017-05-15 20:57:12 -0400108 m_pib->setDefaultCertificateOfKey(m_keyName, certName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800109 m_isDefaultCertificateLoaded = true;
110 return m_defaultCertificate;
111}
112
113const v2::Certificate&
114KeyImpl::setDefaultCertificate(const v2::Certificate& certificate)
115{
116 addCertificate(certificate);
117 return setDefaultCertificate(certificate.getName());
118}
119
120const v2::Certificate&
121KeyImpl::getDefaultCertificate() const
122{
123 BOOST_ASSERT(m_certificates.isConsistent());
124
125 if (!m_isDefaultCertificateLoaded) {
Davide Pesavento50f66752017-05-15 20:57:12 -0400126 m_defaultCertificate = m_pib->getDefaultCertificateOfKey(m_keyName);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800127 m_isDefaultCertificateLoaded = true;
128 }
Davide Pesavento50f66752017-05-15 20:57:12 -0400129 BOOST_ASSERT(m_pib->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130
131 return m_defaultCertificate;
132}
133
134} // namespace detail
135} // namespace pib
136} // namespace security
137} // namespace ndn