blob: b4e6daf76cb880c9f8f8735c463538bbf0912bb2 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 Yu6ee2d362015-07-16 21:48:05 -070062Key::addCertificate(const v2::Certificate& certificate)
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
68Key::removeCertificate(const Name& certName)
69{
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 Yub8f8b342015-04-27 11:06:42 -070086Key::setDefaultCertificate(const Name& certName)
87{
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&
92Key::setDefaultCertificate(const v2::Certificate& certificate)
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{
105 return !(this->operator!());
106}
107
108bool
109Key::operator!() const
110{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111 return m_impl.expired();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700112}
113
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114shared_ptr<detail::KeyImpl>
115Key::lock() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800117 auto impl = m_impl.lock();
118
119 if (impl == nullptr) {
120 BOOST_THROW_EXCEPTION(std::domain_error("Invalid key instance"));
121 }
122
123 return impl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700124}
125
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700126} // namespace pib
127
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700128namespace v2 {
129
130Name
131constructKeyName(const Name& identity, const name::Component& keyId)
132{
133 Name keyName = identity;
134 keyName
135 .append(Certificate::KEY_COMPONENT)
136 .append(keyId);
137 return keyName;
138}
139
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700140bool
141isValidKeyName(const Name& keyName)
142{
143 return (keyName.size() > Certificate::MIN_KEY_NAME_LENGTH &&
144 keyName.get(-Certificate::MIN_KEY_NAME_LENGTH) == Certificate::KEY_COMPONENT);
145}
146
147Name
148extractIdentityFromKeyName(const Name& keyName)
149{
150 if (!isValidKeyName(keyName)) {
151 BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` "
152 "does not follow the naming conventions"));
153 }
154
155 return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
156}
157
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700158} // namespace v2
159
Yingdi Yub8f8b342015-04-27 11:06:42 -0700160} // namespace security
161} // namespace ndn