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