blob: 7002d36181c2fb71ee559cc2244fbf7b053597c7 [file] [log] [blame]
Yingdi Yu41546342014-11-30 23:37:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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 {
Yingdi Yu41546342014-11-30 23:37:53 -080026
27SecPublicInfo::SecPublicInfo(const std::string& location)
28 : m_location(location)
29{
30}
31
32SecPublicInfo::~SecPublicInfo()
33{
34}
35
36std::string
37SecPublicInfo::getPibLocator()
38{
Alexander Afanasyev07113802015-01-15 19:14:36 -080039 return this->getScheme() + ":" + m_location;
Yingdi Yu41546342014-11-30 23:37:53 -080040}
41
42void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070043SecPublicInfo::addPublicKey(const Name& keyName, KeyType keyType, const v1::PublicKey& publicKey)
Yingdi Yu41546342014-11-30 23:37:53 -080044{
45 addKey(keyName, publicKey);
46}
47
48void
49SecPublicInfo::setDefaultIdentity(const Name& identityName)
50{
51 setDefaultIdentityInternal(identityName);
52 refreshDefaultCertificate();
53}
54
55void
56SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
57{
58 setDefaultKeyNameForIdentityInternal(keyName);
59 refreshDefaultCertificate();
60}
61
62void
63SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
64{
65 setDefaultCertificateNameForKeyInternal(certificateName);
66 refreshDefaultCertificate();
67}
68
69Name
70SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
71{
72 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
73}
74
75Name
76SecPublicInfo::getDefaultCertificateName()
77{
78 if (m_defaultCertificate == nullptr)
79 refreshDefaultCertificate();
80
81 if (m_defaultCertificate == nullptr)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070082 BOOST_THROW_EXCEPTION(Error("No default certificate is set"));
Yingdi Yu41546342014-11-30 23:37:53 -080083
84 return m_defaultCertificate->getName();
85}
86
87Name
88SecPublicInfo::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 Mastorakis0d2ed2e2015-07-27 19:09:12 -0700102 BOOST_THROW_EXCEPTION(Error("Key name already exists: " + keyName.toUri()));
Yingdi Yu41546342014-11-30 23:37:53 -0800103
104 return keyName;
105}
106
107void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700108SecPublicInfo::addCertificateAsKeyDefault(const v1::IdentityCertificate& certificate)
Yingdi Yu41546342014-11-30 23:37:53 -0800109{
110 addCertificate(certificate);
111 setDefaultCertificateNameForKeyInternal(certificate.getName());
112 refreshDefaultCertificate();
113}
114
115void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700116SecPublicInfo::addCertificateAsIdentityDefault(const v1::IdentityCertificate& certificate)
Yingdi Yu41546342014-11-30 23:37:53 -0800117{
118 addCertificate(certificate);
119 Name certName = certificate.getName();
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700120 Name keyName = v1::IdentityCertificate::certificateNameToPublicKeyName(certName);
Yingdi Yu41546342014-11-30 23:37:53 -0800121 setDefaultKeyNameForIdentityInternal(keyName);
122 setDefaultCertificateNameForKeyInternal(certName);
123 refreshDefaultCertificate();
124}
125
126void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700127SecPublicInfo::addCertificateAsSystemDefault(const v1::IdentityCertificate& certificate)
Yingdi Yu41546342014-11-30 23:37:53 -0800128{
129 addCertificate(certificate);
130 Name certName = certificate.getName();
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700131 Name keyName = v1::IdentityCertificate::certificateNameToPublicKeyName(certName);
Yingdi Yu41546342014-11-30 23:37:53 -0800132 setDefaultIdentityInternal(keyName.getPrefix(-1));
133 setDefaultKeyNameForIdentityInternal(keyName);
134 setDefaultCertificateNameForKeyInternal(certName);
135 refreshDefaultCertificate();
136}
137
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700138shared_ptr<v1::IdentityCertificate>
Yingdi Yu41546342014-11-30 23:37:53 -0800139SecPublicInfo::defaultCertificate()
140{
141 return getDefaultCertificate();
142}
143
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700144shared_ptr<v1::IdentityCertificate>
Yingdi Yu41546342014-11-30 23:37:53 -0800145SecPublicInfo::getDefaultCertificate()
146{
147 return m_defaultCertificate;
148}
149
150void
151SecPublicInfo::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 Afanasyev2fa59392016-07-29 17:24:23 -0700162} // namespace security
Yingdi Yu41546342014-11-30 23:37:53 -0800163} // namespace ndn