blob: 35a0de61308fb6c9ab4b5c2865aff3bcea9ce5d7 [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
32KeyImpl::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
42 if (m_impl->hasKey(m_keyName)) {
43 BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing key " + m_keyName.toUri()));
44 }
45
46 transform::PublicKey publicKey;
47 try {
48 publicKey.loadPkcs8(key, keyLen);
49 }
50 catch (transform::PublicKey::Error&) {
51 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid key bits"));
52 }
53 m_keyType = publicKey.getKeyType();
54
55 m_impl->addKey(m_identity, m_keyName, key, keyLen);
56}
57
58KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> impl)
59 : m_identity(v2::extractIdentityFromKeyName(keyName))
60 , m_keyName(keyName)
61 , m_isDefaultCertificateLoaded(false)
62 , m_certificates(keyName, impl)
63 , m_impl(impl)
64{
65 BOOST_ASSERT(impl != nullptr);
66
67 m_key = m_impl->getKeyBits(m_keyName);
68
69 transform::PublicKey key;
70 key.loadPkcs8(m_key.buf(), m_key.size());
71 m_keyType = key.getKeyType();
72}
73
74void
75KeyImpl::addCertificate(const v2::Certificate& certificate)
76{
77 BOOST_ASSERT(m_certificates.isConsistent());
78
79 if (m_certificates.find(certificate.getName()) != m_certificates.end()) {
80 BOOST_THROW_EXCEPTION(Pib::Error("Cannot overwrite existing certificate " + certificate.getName().toUri()));
81 }
82
83 m_certificates.add(certificate);
84}
85
86void
87KeyImpl::removeCertificate(const Name& certName)
88{
89 BOOST_ASSERT(m_certificates.isConsistent());
90
91 if (m_isDefaultCertificateLoaded && m_defaultCertificate.getName() == certName)
92 m_isDefaultCertificateLoaded = false;
93
94 m_certificates.remove(certName);
95}
96
97v2::Certificate
98KeyImpl::getCertificate(const Name& certName) const
99{
100 BOOST_ASSERT(m_certificates.isConsistent());
101
102 return m_certificates.get(certName);
103}
104
105const CertificateContainer&
106KeyImpl::getCertificates() const
107{
108 BOOST_ASSERT(m_certificates.isConsistent());
109
110 return m_certificates;
111}
112
113const v2::Certificate&
114KeyImpl::setDefaultCertificate(const Name& certName)
115{
116 BOOST_ASSERT(m_certificates.isConsistent());
117
118 m_defaultCertificate = m_certificates.get(certName);
119 m_impl->setDefaultCertificateOfKey(m_keyName, certName);
120 m_isDefaultCertificateLoaded = true;
121 return m_defaultCertificate;
122}
123
124const v2::Certificate&
125KeyImpl::setDefaultCertificate(const v2::Certificate& certificate)
126{
127 addCertificate(certificate);
128 return setDefaultCertificate(certificate.getName());
129}
130
131const v2::Certificate&
132KeyImpl::getDefaultCertificate() const
133{
134 BOOST_ASSERT(m_certificates.isConsistent());
135
136 if (!m_isDefaultCertificateLoaded) {
137 m_defaultCertificate = m_impl->getDefaultCertificateOfKey(m_keyName);
138 m_isDefaultCertificateLoaded = true;
139 }
140
141 BOOST_ASSERT(m_impl->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode());
142
143 return m_defaultCertificate;
144}
145
146} // namespace detail
147} // namespace pib
148} // namespace security
149} // namespace ndn