blob: 6d9cc4c80fc320eccb5531e22c99c7e3b334c162 [file] [log] [blame]
Yingdi Yu41546342014-11-30 23:37:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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 {
25
26SecPublicInfo::SecPublicInfo(const std::string& location)
27 : m_location(location)
28{
29}
30
31SecPublicInfo::~SecPublicInfo()
32{
33}
34
35std::string
36SecPublicInfo::getPibLocator()
37{
Alexander Afanasyev07113802015-01-15 19:14:36 -080038 return this->getScheme() + ":" + m_location;
Yingdi Yu41546342014-11-30 23:37:53 -080039}
40
41void
42SecPublicInfo::addPublicKey(const Name& keyName, KeyType keyType, const PublicKey& publicKey)
43{
44 addKey(keyName, publicKey);
45}
46
47void
48SecPublicInfo::setDefaultIdentity(const Name& identityName)
49{
50 setDefaultIdentityInternal(identityName);
51 refreshDefaultCertificate();
52}
53
54void
55SecPublicInfo::setDefaultKeyNameForIdentity(const Name& keyName)
56{
57 setDefaultKeyNameForIdentityInternal(keyName);
58 refreshDefaultCertificate();
59}
60
61void
62SecPublicInfo::setDefaultCertificateNameForKey(const Name& certificateName)
63{
64 setDefaultCertificateNameForKeyInternal(certificateName);
65 refreshDefaultCertificate();
66}
67
68Name
69SecPublicInfo::getDefaultCertificateNameForIdentity(const Name& identityName)
70{
71 return getDefaultCertificateNameForKey(getDefaultKeyNameForIdentity(identityName));
72}
73
74Name
75SecPublicInfo::getDefaultCertificateName()
76{
77 if (m_defaultCertificate == nullptr)
78 refreshDefaultCertificate();
79
80 if (m_defaultCertificate == nullptr)
81 throw Error("No default certificate is set");
82
83 return m_defaultCertificate->getName();
84}
85
86Name
87SecPublicInfo::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))
101 throw Error("Key name already exists: " + keyName.toUri());
102
103 return keyName;
104}
105
106void
107SecPublicInfo::addCertificateAsKeyDefault(const IdentityCertificate& certificate)
108{
109 addCertificate(certificate);
110 setDefaultCertificateNameForKeyInternal(certificate.getName());
111 refreshDefaultCertificate();
112}
113
114void
115SecPublicInfo::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
125void
126SecPublicInfo::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
137shared_ptr<IdentityCertificate>
138SecPublicInfo::defaultCertificate()
139{
140 return getDefaultCertificate();
141}
142
143shared_ptr<IdentityCertificate>
144SecPublicInfo::getDefaultCertificate()
145{
146 return m_defaultCertificate;
147}
148
149void
150SecPublicInfo::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