Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 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.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 23 | #include "detail/key-impl.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 24 | #include "../v2/certificate.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 28 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 30 | Key::Key() = default; |
| 31 | |
| 32 | Key::Key(weak_ptr<detail::KeyImpl> impl) |
| 33 | : m_impl(impl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 37 | const Name& |
| 38 | Key::getName() const |
| 39 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 40 | return lock()->getName(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | const Name& |
| 44 | Key::getIdentity() const |
| 45 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 46 | return lock()->getIdentity(); |
| 47 | } |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 49 | KeyType |
| 50 | Key::getKeyType() const |
| 51 | { |
| 52 | return lock()->getKeyType(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 55 | const Buffer& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 56 | Key::getPublicKey() const |
| 57 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 58 | return lock()->getPublicKey(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 62 | Key::addCertificate(const v2::Certificate& certificate) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 63 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 64 | return lock()->addCertificate(certificate); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | Key::removeCertificate(const Name& certName) |
| 69 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 70 | return lock()->removeCertificate(certName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 73 | v2::Certificate |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 74 | Key::getCertificate(const Name& certName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 75 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 76 | return lock()->getCertificate(certName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 79 | const CertificateContainer& |
| 80 | Key::getCertificates() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 81 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 82 | return lock()->getCertificates(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 85 | const v2::Certificate& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 86 | Key::setDefaultCertificate(const Name& certName) |
| 87 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 88 | return lock()->setDefaultCertificate(certName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 91 | const v2::Certificate& |
| 92 | Key::setDefaultCertificate(const v2::Certificate& certificate) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 93 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 94 | return lock()->setDefaultCertificate(certificate); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 97 | const v2::Certificate& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 98 | Key::getDefaultCertificate() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 99 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 100 | return lock()->getDefaultCertificate(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Key::operator bool() const |
| 104 | { |
| 105 | return !(this->operator!()); |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | Key::operator!() const |
| 110 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 111 | return m_impl.expired(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 114 | shared_ptr<detail::KeyImpl> |
| 115 | Key::lock() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 116 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 117 | 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 126 | } // namespace pib |
| 127 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 128 | namespace v2 { |
| 129 | |
| 130 | Name |
| 131 | constructKeyName(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 Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 140 | bool |
| 141 | isValidKeyName(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 | |
| 147 | Name |
| 148 | extractIdentityFromKeyName(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 Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 158 | } // namespace v2 |
| 159 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 160 | } // namespace security |
| 161 | } // namespace ndn |