Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [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 "sec-public-info.hpp" |
| 23 | |
| 24 | namespace ndn { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 25 | namespace security { |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 26 | namespace v1 { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 27 | |
| 28 | SecPublicInfo::SecPublicInfo(const std::string& location) |
| 29 | : m_location(location) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | SecPublicInfo::~SecPublicInfo() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | std::string |
| 38 | SecPublicInfo::getPibLocator() |
| 39 | { |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 40 | return this->getScheme() + ":" + m_location; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 44 | SecPublicInfo::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 45 | { |
| 46 | addKey(keyName, publicKey); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | SecPublicInfo::setDefaultIdentity(const Name& identityName) |
| 51 | { |
| 52 | setDefaultIdentityInternal(identityName); |
| 53 | refreshDefaultCertificate(); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName) |
| 58 | { |
| 59 | setDefaultKeyNameForIdentityInternal(keyName); |
| 60 | refreshDefaultCertificate(); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName) |
| 65 | { |
| 66 | setDefaultCertificateNameForKeyInternal(certificateName); |
| 67 | refreshDefaultCertificate(); |
| 68 | } |
| 69 | |
| 70 | Name |
| 71 | SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName) |
| 72 | { |
| 73 | return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName)); |
| 74 | } |
| 75 | |
| 76 | Name |
| 77 | SecPublicInfo::getDefaultCertificateName() |
| 78 | { |
| 79 | if (m_defaultCertificate == nullptr) |
| 80 | refreshDefaultCertificate(); |
| 81 | |
| 82 | if (m_defaultCertificate == nullptr) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 83 | BOOST_THROW_EXCEPTION(Error("No default certificate is set")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 84 | |
| 85 | return m_defaultCertificate->getName(); |
| 86 | } |
| 87 | |
| 88 | Name |
| 89 | SecPublicInfo::getNewKeyName(const Name& identityName, bool useKsk) |
| 90 | { |
| 91 | std::ostringstream oss; |
| 92 | |
| 93 | if (useKsk) |
| 94 | oss << "ksk-"; |
| 95 | else |
| 96 | oss << "dsk-"; |
| 97 | |
| 98 | oss << time::toUnixTimestamp(time::system_clock::now()).count(); |
| 99 | |
| 100 | Name keyName = Name(identityName).append(oss.str()); |
| 101 | |
| 102 | if (doesPublicKeyExist(keyName)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 103 | BOOST_THROW_EXCEPTION(Error("Key name already exists: " + keyName.toUri())); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 104 | |
| 105 | return keyName; |
| 106 | } |
| 107 | |
| 108 | void |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 109 | SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 110 | { |
| 111 | addCertificate(certificate); |
| 112 | setDefaultCertificateNameForKeyInternal(certificate.getName()); |
| 113 | refreshDefaultCertificate(); |
| 114 | } |
| 115 | |
| 116 | void |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 117 | SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 118 | { |
| 119 | addCertificate(certificate); |
| 120 | Name certName = certificate.getName(); |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 121 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 122 | setDefaultKeyNameForIdentityInternal(keyName); |
| 123 | setDefaultCertificateNameForKeyInternal(certName); |
| 124 | refreshDefaultCertificate(); |
| 125 | } |
| 126 | |
| 127 | void |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 128 | SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 129 | { |
| 130 | addCertificate(certificate); |
| 131 | Name certName = certificate.getName(); |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 132 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 133 | setDefaultIdentityInternal(keyName.getPrefix(-1)); |
| 134 | setDefaultKeyNameForIdentityInternal(keyName); |
| 135 | setDefaultCertificateNameForKeyInternal(certName); |
| 136 | refreshDefaultCertificate(); |
| 137 | } |
| 138 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 139 | shared_ptr<IdentityCertificate> |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 140 | SecPublicInfo::defaultCertificate() |
| 141 | { |
| 142 | return getDefaultCertificate(); |
| 143 | } |
| 144 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 145 | shared_ptr<IdentityCertificate> |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 146 | SecPublicInfo::getDefaultCertificate() |
| 147 | { |
| 148 | return m_defaultCertificate; |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | SecPublicInfo::refreshDefaultCertificate() |
| 153 | { |
| 154 | try { |
| 155 | Name certName = getDefaultCertificateNameForIdentity(getDefaultIdentity()); |
| 156 | m_defaultCertificate = getCertificate(certName); |
| 157 | } |
| 158 | catch (SecPublicInfo::Error&) { |
| 159 | m_defaultCertificate.reset(); |
| 160 | } |
| 161 | } |
| 162 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 163 | } // namespace v1 |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 164 | } // namespace security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 165 | } // namespace ndn |