blob: 96c4441b759027e09a3d10c7ce44056fff63dc7e [file] [log] [blame]
Yingdi Yu41546342014-11-30 23:37:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu41546342014-11-30 23:37:53 -08004 *
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
24namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070025namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080026namespace v1 {
Yingdi Yu41546342014-11-30 23:37:53 -080027
28SecPublicInfo::SecPublicInfo(const std::string& location)
29 : m_location(location)
30{
31}
32
33SecPublicInfo::~SecPublicInfo()
34{
35}
36
37std::string
38SecPublicInfo::getPibLocator()
39{
Alexander Afanasyev07113802015-01-15 19:14:36 -080040 return this->getScheme() + ":" + m_location;
Yingdi Yu41546342014-11-30 23:37:53 -080041}
42
43void
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080044SecPublicInfo::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
Yingdi Yu41546342014-11-30 23:37:53 -080045{
46 addKey(keyName, publicKey);
47}
48
49void
50SecPublicInfo::setDefaultIdentity(const Name& identityName)
51{
52 setDefaultIdentityInternal(identityName);
53 refreshDefaultCertificate();
54}
55
56void
57SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
58{
59 setDefaultKeyNameForIdentityInternal(keyName);
60 refreshDefaultCertificate();
61}
62
63void
64SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
65{
66 setDefaultCertificateNameForKeyInternal(certificateName);
67 refreshDefaultCertificate();
68}
69
70Name
71SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
72{
73 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
74}
75
76Name
77SecPublicInfo::getDefaultCertificateName()
78{
79 if (m_defaultCertificate == nullptr)
80 refreshDefaultCertificate();
81
82 if (m_defaultCertificate == nullptr)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070083 BOOST_THROW_EXCEPTION(Error("No default certificate is set"));
Yingdi Yu41546342014-11-30 23:37:53 -080084
85 return m_defaultCertificate->getName();
86}
87
88Name
89SecPublicInfo::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 Mastorakis0d2ed2e2015-07-27 19:09:12 -0700103 BOOST_THROW_EXCEPTION(Error("Key name already exists: " + keyName.toUri()));
Yingdi Yu41546342014-11-30 23:37:53 -0800104
105 return keyName;
106}
107
108void
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800109SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
Yingdi Yu41546342014-11-30 23:37:53 -0800110{
111 addCertificate(certificate);
112 setDefaultCertificateNameForKeyInternal(certificate.getName());
113 refreshDefaultCertificate();
114}
115
116void
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800117SecPublicInfo::addCertificateAsIdentityDefault(const IdentityCertificate& certificate)
Yingdi Yu41546342014-11-30 23:37:53 -0800118{
119 addCertificate(certificate);
120 Name certName = certificate.getName();
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800121 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
Yingdi Yu41546342014-11-30 23:37:53 -0800122 setDefaultKeyNameForIdentityInternal(keyName);
123 setDefaultCertificateNameForKeyInternal(certName);
124 refreshDefaultCertificate();
125}
126
127void
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800128SecPublicInfo::addCertificateAsSystemDefault(const IdentityCertificate& certificate)
Yingdi Yu41546342014-11-30 23:37:53 -0800129{
130 addCertificate(certificate);
131 Name certName = certificate.getName();
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800132 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(certName);
Yingdi Yu41546342014-11-30 23:37:53 -0800133 setDefaultIdentityInternal(keyName.getPrefix(-1));
134 setDefaultKeyNameForIdentityInternal(keyName);
135 setDefaultCertificateNameForKeyInternal(certName);
136 refreshDefaultCertificate();
137}
138
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800139shared_ptr<IdentityCertificate>
Yingdi Yu41546342014-11-30 23:37:53 -0800140SecPublicInfo::defaultCertificate()
141{
142 return getDefaultCertificate();
143}
144
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800145shared_ptr<IdentityCertificate>
Yingdi Yu41546342014-11-30 23:37:53 -0800146SecPublicInfo::getDefaultCertificate()
147{
148 return m_defaultCertificate;
149}
150
151void
152SecPublicInfo::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 Afanasyev4c9a3d52017-01-03 17:45:19 -0800163} // namespace v1
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700164} // namespace security
Yingdi Yu41546342014-11-30 23:37:53 -0800165} // namespace ndn