blob: 7ec06f933da134574ecd2fc4714d2b27ae2cf86f [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson173fd432013-10-12 18:16:41 -070022 */
23
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "common.hpp"
25
26#include "identity-certificate.hpp"
Yingdi Yu80979ba2014-11-25 14:38:36 -080027#include "../util/concepts.hpp"
Jeff Thompson173fd432013-10-12 18:16:41 -070028
Jeff Thompson173fd432013-10-12 18:16:41 -070029namespace ndn {
30
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070031using std::string;
32
Yingdi Yu80979ba2014-11-25 14:38:36 -080033BOOST_CONCEPT_ASSERT((WireEncodable<IdentityCertificate>));
34BOOST_CONCEPT_ASSERT((WireDecodable<IdentityCertificate>));
35static_assert(std::is_base_of<Certificate::Error, IdentityCertificate::Error>::value,
36 "IdentityCertificate::Error must inherit from Certificate::Error");
37
38IdentityCertificate::IdentityCertificate()
39{
40}
41
42IdentityCertificate::IdentityCertificate(const Data& data)
43 : Certificate(data)
44{
45 setPublicKeyName();
46}
47
48IdentityCertificate::IdentityCertificate(const Block& block)
49 : Certificate(block)
50{
51 setPublicKeyName();
52}
53
54void
55IdentityCertificate::wireDecode(const Block& wire)
56{
57 Certificate::wireDecode(wire);
58 setPublicKeyName();
59}
60
61void
62IdentityCertificate::setName(const Name& name)
63{
64 Certificate::setName(name);
65 setPublicKeyName();
66}
67
Jeff Thompson173fd432013-10-12 18:16:41 -070068bool
69IdentityCertificate::isCorrectName(const Name& name)
70{
Jeff Thompson43a57b12013-10-22 16:25:38 -070071 string idString("ID-CERT");
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070072 int i = name.size() - 1;
Jeff Thompson173fd432013-10-12 18:16:41 -070073 for (; i >= 0; i--) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070074 if (name.get(i).toUri() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070075 break;
76 }
77
78 if (i < 0)
79 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080
Jeff Thompson43a57b12013-10-22 16:25:38 -070081 string keyString("KEY");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082 size_t keyIndex = 0;
83 for (; keyIndex < name.size(); keyIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070084 if (name.get(keyIndex).toUri() == keyString)
Jeff Thompson43a57b12013-10-22 16:25:38 -070085 break;
86 }
87
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088 if (keyIndex >= name.size())
Jeff Thompson43a57b12013-10-22 16:25:38 -070089 return false;
90
Jeff Thompson173fd432013-10-12 18:16:41 -070091 return true;
92}
93
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080094void
Jeff Thompson43a57b12013-10-22 16:25:38 -070095IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070096{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080097 if (!isCorrectName(getName()))
98 throw Error("Wrong Identity Certificate Name!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700100 m_publicKeyName = certificateNameToPublicKeyName(getName());
Jeff Thompson74942612013-10-24 16:42:32 -0700101}
Jeff Thompson173fd432013-10-12 18:16:41 -0700102
Jeff Thompson74942612013-10-24 16:42:32 -0700103bool
104IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800105{
106 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -0700107}
108
109Name
110IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
111{
Jeff Thompson43a57b12013-10-22 16:25:38 -0700112 string idString("ID-CERT");
Yingdi Yu88663af2014-01-15 15:21:38 -0800113 bool foundIdString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700114 size_t idCertComponentIndex = certificateName.size() - 1;
115 for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700116 if (certificateName.get(idCertComponentIndex).toUri() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800117 {
118 foundIdString = true;
119 break;
120 }
Jeff Thompson43a57b12013-10-22 16:25:38 -0700121 }
Yingdi Yu88663af2014-01-15 15:21:38 -0800122
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 if (!foundIdString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800124 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700126 Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
Jeff Thompson43a57b12013-10-22 16:25:38 -0700127 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -0800128 bool foundKeyString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700129 size_t keyComponentIndex = 0;
130 for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700131 if (tmpName.get(keyComponentIndex).toUri() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800132 {
133 foundKeyString = true;
134 break;
135 }
Jeff Thompson43a57b12013-10-22 16:25:38 -0700136 }
Yingdi Yu88663af2014-01-15 15:21:38 -0800137
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138 if (!foundKeyString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800139 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700140
141 return tmpName
142 .getSubName(0, keyComponentIndex)
143 .append(tmpName.getSubName(keyComponentIndex + 1,
144 tmpName.size() - keyComponentIndex - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -0700145}
146
Yingdi Yufc40d872014-02-18 12:56:04 -0800147} // namespace ndn