blob: 8dbb35e6ff5be624523f75d3d20917ad9adb75ae [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/*
Davide Pesavento78ca8ae2022-05-01 01:37:05 -04003 * Copyright (c) 2013-2022 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/pib/key.hpp"
Junxiao Shi24c5a002018-12-12 04:47:15 +000023#include "ndn-cxx/security/pib/impl/key-impl.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
25namespace ndn {
26namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070027namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040029Key::Key() noexcept = default;
Yingdi Yucbe72b02015-11-25 17:35:37 -080030
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040031Key::Key(weak_ptr<detail::KeyImpl> impl) noexcept
Davide Pesaventof2cae612021-03-24 18:47:05 -040032 : m_impl(std::move(impl))
Yingdi Yub8f8b342015-04-27 11:06:42 -070033{
34}
35
Yingdi Yub8f8b342015-04-27 11:06:42 -070036const Name&
37Key::getName() const
38{
Yingdi Yucbe72b02015-11-25 17:35:37 -080039 return lock()->getName();
Yingdi Yub8f8b342015-04-27 11:06:42 -070040}
41
42const Name&
43Key::getIdentity() const
44{
Yingdi Yucbe72b02015-11-25 17:35:37 -080045 return lock()->getIdentity();
46}
Yingdi Yub8f8b342015-04-27 11:06:42 -070047
Yingdi Yucbe72b02015-11-25 17:35:37 -080048KeyType
49Key::getKeyType() const
50{
51 return lock()->getKeyType();
Yingdi Yub8f8b342015-04-27 11:06:42 -070052}
53
Davide Pesavento765abc92021-12-27 00:44:04 -050054span<const uint8_t>
Yingdi Yub8f8b342015-04-27 11:06:42 -070055Key::getPublicKey() const
56{
Yingdi Yucbe72b02015-11-25 17:35:37 -080057 return lock()->getPublicKey();
Yingdi Yub8f8b342015-04-27 11:06:42 -070058}
59
60void
Davide Pesaventof2cae612021-03-24 18:47:05 -040061Key::addCertificate(const Certificate& certificate) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070062{
Davide Pesaventof2cae612021-03-24 18:47:05 -040063 lock()->addCertificate(certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -070064}
65
66void
Yingdi Yufe4733a2015-10-22 14:24:12 -070067Key::removeCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070068{
Davide Pesaventof2cae612021-03-24 18:47:05 -040069 lock()->removeCertificate(certName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070070}
71
Davide Pesaventof2cae612021-03-24 18:47:05 -040072Certificate
Yingdi Yuc8209892015-06-19 17:47:56 -070073Key::getCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070074{
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 return lock()->getCertificate(certName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070076}
77
Yingdi Yuc8209892015-06-19 17:47:56 -070078const CertificateContainer&
79Key::getCertificates() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070080{
Yingdi Yucbe72b02015-11-25 17:35:37 -080081 return lock()->getCertificates();
Yingdi Yub8f8b342015-04-27 11:06:42 -070082}
83
Davide Pesaventof2cae612021-03-24 18:47:05 -040084const Certificate&
Yingdi Yufe4733a2015-10-22 14:24:12 -070085Key::setDefaultCertificate(const Name& certName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070086{
Yingdi Yucbe72b02015-11-25 17:35:37 -080087 return lock()->setDefaultCertificate(certName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070088}
89
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040090void
Davide Pesaventof2cae612021-03-24 18:47:05 -040091Key::setDefaultCertificate(const Certificate& certificate) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070092{
Yingdi Yucbe72b02015-11-25 17:35:37 -080093 return lock()->setDefaultCertificate(certificate);
Yingdi Yub8f8b342015-04-27 11:06:42 -070094}
95
Davide Pesaventof2cae612021-03-24 18:47:05 -040096const Certificate&
Yingdi Yuc8209892015-06-19 17:47:56 -070097Key::getDefaultCertificate() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070098{
Yingdi Yucbe72b02015-11-25 17:35:37 -080099 return lock()->getDefaultCertificate();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700100}
101
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400102Key::operator bool() const noexcept
Yingdi Yub8f8b342015-04-27 11:06:42 -0700103{
Davide Pesaventobdcedf42017-10-15 14:56:28 -0400104 return !m_impl.expired();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105}
106
Yingdi Yucbe72b02015-11-25 17:35:37 -0800107shared_ptr<detail::KeyImpl>
108Key::lock() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700109{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800110 auto impl = m_impl.lock();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111 if (impl == nullptr) {
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400112 NDN_THROW(std::domain_error("Invalid PIB key instance"));
Yingdi Yucbe72b02015-11-25 17:35:37 -0800113 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114 return impl;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700115}
116
Junxiao Shi5759be32017-10-15 00:00:52 +0000117bool
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400118Key::equals(const Key& other) const noexcept
Junxiao Shi5759be32017-10-15 00:00:52 +0000119{
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400120 return !this->m_impl.owner_before(other.m_impl) &&
121 !other.m_impl.owner_before(this->m_impl);
Junxiao Shi5759be32017-10-15 00:00:52 +0000122}
123
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700124} // namespace pib
125
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400126inline namespace v2 {
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700127
128Name
129constructKeyName(const Name& identity, const name::Component& keyId)
130{
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400131 return Name(identity)
132 .append(Certificate::KEY_COMPONENT)
133 .append(keyId);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700134}
135
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700136bool
137isValidKeyName(const Name& keyName)
138{
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400139 return keyName.size() >= Certificate::MIN_KEY_NAME_LENGTH &&
140 keyName.get(-Certificate::MIN_KEY_NAME_LENGTH) == Certificate::KEY_COMPONENT;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700141}
142
143Name
144extractIdentityFromKeyName(const Name& keyName)
145{
146 if (!isValidKeyName(keyName)) {
Davide Pesavento923ba442019-02-12 22:00:38 -0500147 NDN_THROW(std::invalid_argument("Key name `" + keyName.toUri() + "` "
148 "does not respect the naming conventions"));
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700149 }
150
151 return keyName.getPrefix(-Certificate::MIN_KEY_NAME_LENGTH); // trim everything after and including "KEY"
152}
153
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400154} // inline namespace v2
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155} // namespace security
156} // namespace ndn