blob: ea8a946878cbc6c423bd490c453f4bd96d6f17f8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson173fd432013-10-12 18:16:41 -07002/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Jeff Thompson173fd432013-10-12 18:16:41 -070020 */
21
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022#include "identity-certificate.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070023#include "../../util/concepts.hpp"
Jeff Thompson173fd432013-10-12 18:16:41 -070024
Jeff Thompson173fd432013-10-12 18:16:41 -070025namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070026namespace security {
27namespace v1 {
Jeff Thompson173fd432013-10-12 18:16:41 -070028
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070029using std::string;
30
Yingdi Yu80979ba2014-11-25 14:38:36 -080031BOOST_CONCEPT_ASSERT((WireEncodable<IdentityCertificate>));
32BOOST_CONCEPT_ASSERT((WireDecodable<IdentityCertificate>));
33static_assert(std::is_base_of<Certificate::Error, IdentityCertificate::Error>::value,
34 "IdentityCertificate::Error must inherit from Certificate::Error");
35
36IdentityCertificate::IdentityCertificate()
37{
Junxiao Shi8ca43252015-06-11 21:29:43 -070038 this->setFreshnessPeriod(time::hours(1));
Yingdi Yu80979ba2014-11-25 14:38:36 -080039}
40
41IdentityCertificate::IdentityCertificate(const Data& data)
42 : Certificate(data)
43{
44 setPublicKeyName();
45}
46
47IdentityCertificate::IdentityCertificate(const Block& block)
48 : Certificate(block)
49{
50 setPublicKeyName();
51}
52
53void
54IdentityCertificate::wireDecode(const Block& wire)
55{
56 Certificate::wireDecode(wire);
57 setPublicKeyName();
58}
59
60void
61IdentityCertificate::setName(const Name& name)
62{
63 Certificate::setName(name);
64 setPublicKeyName();
65}
66
Jeff Thompson173fd432013-10-12 18:16:41 -070067bool
68IdentityCertificate::isCorrectName(const Name& name)
69{
Jeff Thompson43a57b12013-10-22 16:25:38 -070070 string idString("ID-CERT");
Davide Pesavento8f5cbdc2015-09-13 00:59:28 +020071 ssize_t i = name.size() - 1;
Jeff Thompson173fd432013-10-12 18:16:41 -070072 for (; i >= 0; i--) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070073 if (name.get(i).toUri() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070074 break;
75 }
76
77 if (i < 0)
78 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079
Jeff Thompson43a57b12013-10-22 16:25:38 -070080 string keyString("KEY");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081 size_t keyIndex = 0;
82 for (; keyIndex < name.size(); keyIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070083 if (name.get(keyIndex).toUri() == keyString)
Jeff Thompson43a57b12013-10-22 16:25:38 -070084 break;
85 }
86
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 if (keyIndex >= name.size())
Jeff Thompson43a57b12013-10-22 16:25:38 -070088 return false;
89
Jeff Thompson173fd432013-10-12 18:16:41 -070090 return true;
91}
92
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080093void
Jeff Thompson43a57b12013-10-22 16:25:38 -070094IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070095{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080096 if (!isCorrectName(getName()))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070097 BOOST_THROW_EXCEPTION(Error("Wrong Identity Certificate Name"));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070099 m_publicKeyName = certificateNameToPublicKeyName(getName());
Jeff Thompson74942612013-10-24 16:42:32 -0700100}
Jeff Thompson173fd432013-10-12 18:16:41 -0700101
Jeff Thompson74942612013-10-24 16:42:32 -0700102bool
103IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800104{
105 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -0700106}
107
108Name
109IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
110{
Jeff Thompson43a57b12013-10-22 16:25:38 -0700111 string idString("ID-CERT");
Yingdi Yu88663af2014-01-15 15:21:38 -0800112 bool foundIdString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700113 size_t idCertComponentIndex = certificateName.size() - 1;
114 for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700115 if (certificateName.get(idCertComponentIndex).toUri() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800116 {
117 foundIdString = true;
118 break;
119 }
Jeff Thompson43a57b12013-10-22 16:25:38 -0700120 }
Yingdi Yu88663af2014-01-15 15:21:38 -0800121
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 if (!foundIdString)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700123 BOOST_THROW_EXCEPTION(Error("Incorrect identity certificate name " + certificateName.toUri()));
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700125 Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
Jeff Thompson43a57b12013-10-22 16:25:38 -0700126 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -0800127 bool foundKeyString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700128 size_t keyComponentIndex = 0;
129 for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700130 if (tmpName.get(keyComponentIndex).toUri() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800131 {
132 foundKeyString = true;
133 break;
134 }
Jeff Thompson43a57b12013-10-22 16:25:38 -0700135 }
Yingdi Yu88663af2014-01-15 15:21:38 -0800136
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700137 if (!foundKeyString)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700138 BOOST_THROW_EXCEPTION(Error("Incorrect identity certificate name " + certificateName.toUri()));
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700139
140 return tmpName
141 .getSubName(0, keyComponentIndex)
142 .append(tmpName.getSubName(keyComponentIndex + 1,
143 tmpName.size() - keyComponentIndex - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -0700144}
145
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700146} // namespace v1
147} // namespace security
Yingdi Yufc40d872014-02-18 12:56:04 -0800148} // namespace ndn