blob: 685fa293ca663c4bd094c59087ec2e71f2346f28 [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"
Jeff Thompson173fd432013-10-12 18:16:41 -070027
Jeff Thompson173fd432013-10-12 18:16:41 -070028namespace ndn {
29
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070030using std::string;
31
Jeff Thompson173fd432013-10-12 18:16:41 -070032bool
33IdentityCertificate::isCorrectName(const Name& name)
34{
Jeff Thompson43a57b12013-10-22 16:25:38 -070035 string idString("ID-CERT");
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070036 int i = name.size() - 1;
Jeff Thompson173fd432013-10-12 18:16:41 -070037 for (; i >= 0; i--) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070038 if (name.get(i).toUri() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070039 break;
40 }
41
42 if (i < 0)
43 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044
Jeff Thompson43a57b12013-10-22 16:25:38 -070045 string keyString("KEY");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 size_t keyIndex = 0;
47 for (; keyIndex < name.size(); keyIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070048 if (name.get(keyIndex).toUri() == keyString)
Jeff Thompson43a57b12013-10-22 16:25:38 -070049 break;
50 }
51
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 if (keyIndex >= name.size())
Jeff Thompson43a57b12013-10-22 16:25:38 -070053 return false;
54
Jeff Thompson173fd432013-10-12 18:16:41 -070055 return true;
56}
57
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080058void
Jeff Thompson43a57b12013-10-22 16:25:38 -070059IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070060{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080061 if (!isCorrectName(getName()))
62 throw Error("Wrong Identity Certificate Name!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 m_publicKeyName = certificateNameToPublicKeyName(getName());
Jeff Thompson74942612013-10-24 16:42:32 -070065}
Jeff Thompson173fd432013-10-12 18:16:41 -070066
Jeff Thompson74942612013-10-24 16:42:32 -070067bool
68IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080069{
70 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -070071}
72
73Name
74IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
75{
Jeff Thompson43a57b12013-10-22 16:25:38 -070076 string idString("ID-CERT");
Yingdi Yu88663af2014-01-15 15:21:38 -080077 bool foundIdString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070078 size_t idCertComponentIndex = certificateName.size() - 1;
79 for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070080 if (certificateName.get(idCertComponentIndex).toUri() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -080081 {
82 foundIdString = true;
83 break;
84 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070085 }
Yingdi Yu88663af2014-01-15 15:21:38 -080086
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070087 if (!foundIdString)
Yingdi Yu88663af2014-01-15 15:21:38 -080088 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070090 Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
Jeff Thompson43a57b12013-10-22 16:25:38 -070091 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -080092 bool foundKeyString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070093 size_t keyComponentIndex = 0;
94 for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070095 if (tmpName.get(keyComponentIndex).toUri() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080096 {
97 foundKeyString = true;
98 break;
99 }
Jeff Thompson43a57b12013-10-22 16:25:38 -0700100 }
Yingdi Yu88663af2014-01-15 15:21:38 -0800101
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102 if (!foundKeyString)
Yingdi Yu88663af2014-01-15 15:21:38 -0800103 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700104
105 return tmpName
106 .getSubName(0, keyComponentIndex)
107 .append(tmpName.getSubName(keyComponentIndex + 1,
108 tmpName.size() - keyComponentIndex - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -0700109}
110
Yingdi Yufc40d872014-02-18 12:56:04 -0800111} // namespace ndn