blob: efb7df5330612cb7084131bb0fbd27d7bdad6301 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -04002/*
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -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 "key.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080023#include "detail/key-impl.hpp"
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070024#include "../v2/certificate.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
26namespace ndn {
27namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070028namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
Yingdi Yucbe72b02015-11-25 17:35:37 -080030Key::Key() = default;
31
32Key::Key(weak_ptr<detail::KeyImpl> impl)
33 : m_impl(impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070034{
35}
36
Yingdi Yub8f8b342015-04-27 11:06:42 -070037const Name&
38Key::getName() const
39{
Yingdi Yucbe72b02015-11-25 17:35:37 -080040 return lock()->getName();
Yingdi Yub8f8b342015-04-27 11:06:42 -070041}
42
43const Name&
44Key::getIdentity() const
45{
Yingdi Yucbe72b02015-11-25 17:35:37 -080046 return lock()->getIdentity();
47}
Yingdi Yub8f8b342015-04-27 11:06:42 -070048
Yingdi Yucbe72b02015-11-25 17:35:37 -080049KeyType
50Key::getKeyType() const
51{
52 return lock()->getKeyType();
Yingdi Yub8f8b342015-04-27 11:06:42 -070053}
54
Yingdi Yu6ee2d362015-07-16 21:48:05 -070055const Buffer&
Yingdi Yub8f8b342015-04-27 11:06:42 -070056Key::getPublicKey() const
57{
Yingdi Yucbe72b02015-11-25 17:35:37 -080058 return lock()->getPublicKey();
Yingdi Yub8f8b342015-04-27 11:06:42 -070059}
60
61void
Yingdi Yufe4733a2015-10-22 14:24:12 -070062Key::addCertificate(const v2::Certificate& certificate) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070063{
Yingdi Yucbe72b02015-11-25 17:35:37 -080064 return lock()->addCertificate(certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -070065}
66
67void
Yingdi Yufe4733a2015-10-22 14:24:12 -070068Key::removeCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070069{
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 return lock()->removeCertificate(certName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070071}
72
Yingdi Yu6ee2d362015-07-16 21:48:05 -070073v2::Certificate
Yingdi Yuc8209892015-06-19 17:47:56 -070074Key::getCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070075{
Yingdi Yucbe72b02015-11-25 17:35:37 -080076 return lock()->getCertificate(certName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070077}
78
Yingdi Yuc8209892015-06-19 17:47:56 -070079const CertificateContainer&
80Key::getCertificates() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070081{
Yingdi Yucbe72b02015-11-25 17:35:37 -080082 return lock()->getCertificates();
Yingdi Yub8f8b342015-04-27 11:06:42 -070083}
84
Yingdi Yu6ee2d362015-07-16 21:48:05 -070085const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -070086Key::setDefaultCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070087{
Yingdi Yucbe72b02015-11-25 17:35:37 -080088 return lock()->setDefaultCertificate(certName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070089}
90
Yingdi Yu6ee2d362015-07-16 21:48:05 -070091const v2::Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -070092Key::setDefaultCertificate(const v2::Certificate& certificate) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070093{
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 return lock()->setDefaultCertificate(certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -070095}
96
Yingdi Yu6ee2d362015-07-16 21:48:05 -070097const v2::Certificate&
Yingdi Yuc8209892015-06-19 17:47:56 -070098Key::getDefaultCertificate() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070099{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100 return lock()->getDefaultCertificate();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700101}
102
103Key::operator bool() const
104{
Davide Pesaventobdcedf42017-10-15 14:56:28 -0400105 return !m_impl.expired();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700106}
107
Yingdi Yucbe72b02015-11-25 17:35:37 -0800108shared_ptr<detail::KeyImpl>
109Key::lock() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700110{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111 auto impl = m_impl.lock();
112
113 if (impl == nullptr) {
114 BOOST_THROW_EXCEPTION(std::domain_error("Invalid key instance"));
115 }
116
117 return impl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700118}
119
Junxiao Shi5759be32017-10-15 00:00:52 +0000120bool
121operator!=(const Key& lhs, const Key& rhs)
122{
123 return lhs.m_impl.owner_before(rhs.m_impl) || rhs.m_impl.owner_before(lhs.m_impl);
124}
125
126std::ostream&
127operator<<(std::ostream& os, const Key& key)
128{
129 if (key) {
130 os << key.getName();
131 }
132 else {
133 os << "(empty)";
134 }
135 return os;
136}
137
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700138} // namespace pib
139
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700140namespace v2 {
141
142Name
143constructKeyName(const Name& identity, const name::Component& keyId)
144{
145 Name keyName = identity;
146 keyName
147 .append(Certificate::KEY_COMPONENT)
148 .append(keyId);
149 return keyName;
150}
151
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700152bool
153isValidKeyName(const Name& keyName)
154{
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -0400155 return (keyName.size() >= Certificate::MIN_KEY_NAME_LENGTH &&
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700156 keyName.get(-Certificate::MIN_KEY_NAME_LENGTH) == Certificate::KEY_COMPONENT);
157}
158
159Name
160extractIdentityFromKeyName(const Name& keyName)
161{
162 if (!isValidKeyName(keyName)) {
163 BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` "
164 "does not follow the naming conventions"));
165 }
166
167 return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
168}
169
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700170} // namespace v2
171
Yingdi Yub8f8b342015-04-27 11:06:42 -0700172} // namespace security
173} // namespace ndn