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