Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2022 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 | |||||
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/pib/key.hpp" |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 23 | #include "ndn-cxx/security/pib/impl/key-impl.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
25 | namespace ndn { | ||||
26 | namespace security { | ||||
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 27 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 29 | Key::Key() noexcept = default; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 30 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 31 | Key::Key(weak_ptr<detail::KeyImpl> impl) noexcept |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 32 | : m_impl(std::move(impl)) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 33 | { |
34 | } | ||||
35 | |||||
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 36 | const Name& |
37 | Key::getName() const | ||||
38 | { | ||||
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 39 | return lock()->getName(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 40 | } |
41 | |||||
42 | const Name& | ||||
43 | Key::getIdentity() const | ||||
44 | { | ||||
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 45 | return lock()->getIdentity(); |
46 | } | ||||
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 48 | KeyType |
49 | Key::getKeyType() const | ||||
50 | { | ||||
51 | return lock()->getKeyType(); | ||||
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 52 | } |
53 | |||||
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 54 | span<const uint8_t> |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 55 | Key::getPublicKey() const |
56 | { | ||||
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 57 | return lock()->getPublicKey(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 58 | } |
59 | |||||
60 | void | ||||
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 61 | Key::addCertificate(const Certificate& certificate) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 62 | { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 63 | lock()->addCertificate(certificate); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 64 | } |
65 | |||||
66 | void | ||||
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 67 | Key::removeCertificate(const Name& certName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 68 | { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 69 | lock()->removeCertificate(certName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 70 | } |
71 | |||||
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 72 | Certificate |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 73 | Key::getCertificate(const Name& certName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 74 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 75 | return lock()->getCertificate(certName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 76 | } |
77 | |||||
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 78 | const CertificateContainer& |
79 | Key::getCertificates() const | ||||
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 80 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 81 | return lock()->getCertificates(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 82 | } |
83 | |||||
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 84 | const Certificate& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 85 | Key::setDefaultCertificate(const Name& certName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 86 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 87 | return lock()->setDefaultCertificate(certName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 88 | } |
89 | |||||
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 90 | void |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 91 | Key::setDefaultCertificate(const Certificate& certificate) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 92 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 93 | return lock()->setDefaultCertificate(certificate); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 94 | } |
95 | |||||
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 96 | const Certificate& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 97 | Key::getDefaultCertificate() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 98 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 99 | return lock()->getDefaultCertificate(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 100 | } |
101 | |||||
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 102 | Key::operator bool() const noexcept |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 103 | { |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame] | 104 | return !m_impl.expired(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 105 | } |
106 | |||||
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 107 | shared_ptr<detail::KeyImpl> |
108 | Key::lock() const | ||||
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 109 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 110 | auto impl = m_impl.lock(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 111 | if (impl == nullptr) { |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 112 | NDN_THROW(std::domain_error("Invalid PIB key instance")); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 113 | } |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 114 | return impl; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 115 | } |
116 | |||||
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 117 | bool |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 118 | Key::equals(const Key& other) const noexcept |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 119 | { |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 120 | return !this->m_impl.owner_before(other.m_impl) && |
121 | !other.m_impl.owner_before(this->m_impl); | ||||
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 122 | } |
123 | |||||
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 124 | } // namespace pib |
125 | |||||
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 126 | inline namespace v2 { |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 127 | |
128 | Name | ||||
129 | constructKeyName(const Name& identity, const name::Component& keyId) | ||||
130 | { | ||||
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 131 | return Name(identity) |
132 | .append(Certificate::KEY_COMPONENT) | ||||
133 | .append(keyId); | ||||
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 134 | } |
135 | |||||
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 136 | bool |
137 | isValidKeyName(const Name& keyName) | ||||
138 | { | ||||
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame^] | 139 | return keyName.size() >= Certificate::MIN_KEY_NAME_LENGTH && |
140 | keyName.get(-Certificate::MIN_KEY_NAME_LENGTH) == Certificate::KEY_COMPONENT; | ||||
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 141 | } |
142 | |||||
143 | Name | ||||
144 | extractIdentityFromKeyName(const Name& keyName) | ||||
145 | { | ||||
146 | if (!isValidKeyName(keyName)) { | ||||
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 147 | NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` " |
148 | "does not respect the naming conventions")); | ||||
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 149 | } |
150 | |||||
151 | return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY" | ||||
152 | } | ||||
153 | |||||
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 154 | } // inline namespace v2 |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 155 | } // namespace security |
156 | } // namespace ndn |