blob: b3c544a161b44e4b4d889e3d6c1e7b97434cbe69 [file] [log] [blame]
Jeff Thompson173fd432013-10-12 18:16:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Jeff Thompson173fd432013-10-12 18:16:41 -070013 */
14
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080015#include "common.hpp"
16
17#include "identity-certificate.hpp"
Jeff Thompson173fd432013-10-12 18:16:41 -070018
19using namespace std;
20
21namespace ndn {
22
Jeff Thompson173fd432013-10-12 18:16:41 -070023bool
24IdentityCertificate::isCorrectName(const Name& name)
25{
Jeff Thompson43a57b12013-10-22 16:25:38 -070026 string idString("ID-CERT");
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070027 int i = name.size() - 1;
Jeff Thompson173fd432013-10-12 18:16:41 -070028 for (; i >= 0; i--) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070029 if (name.get(i).toUri() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070030 break;
31 }
32
33 if (i < 0)
34 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035
Jeff Thompson43a57b12013-10-22 16:25:38 -070036 string keyString("KEY");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037 size_t keyIndex = 0;
38 for (; keyIndex < name.size(); keyIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070039 if (name.get(keyIndex).toUri() == keyString)
Jeff Thompson43a57b12013-10-22 16:25:38 -070040 break;
41 }
42
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 if (keyIndex >= name.size())
Jeff Thompson43a57b12013-10-22 16:25:38 -070044 return false;
45
Jeff Thompson173fd432013-10-12 18:16:41 -070046 return true;
47}
48
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080049void
Jeff Thompson43a57b12013-10-22 16:25:38 -070050IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070051{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080052 if (!isCorrectName(getName()))
53 throw Error("Wrong Identity Certificate Name!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070054
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070055 m_publicKeyName = certificateNameToPublicKeyName(getName());
Jeff Thompson74942612013-10-24 16:42:32 -070056}
Jeff Thompson173fd432013-10-12 18:16:41 -070057
Jeff Thompson74942612013-10-24 16:42:32 -070058bool
59IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080060{
61 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -070062}
63
64Name
65IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
66{
Jeff Thompson43a57b12013-10-22 16:25:38 -070067 string idString("ID-CERT");
Yingdi Yu88663af2014-01-15 15:21:38 -080068 bool foundIdString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070069 size_t idCertComponentIndex = certificateName.size() - 1;
70 for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070071 if (certificateName.get(idCertComponentIndex).toUri() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -080072 {
73 foundIdString = true;
74 break;
75 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070076 }
Yingdi Yu88663af2014-01-15 15:21:38 -080077
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070078 if (!foundIdString)
Yingdi Yu88663af2014-01-15 15:21:38 -080079 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070081 Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
Jeff Thompson43a57b12013-10-22 16:25:38 -070082 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -080083 bool foundKeyString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070084 size_t keyComponentIndex = 0;
85 for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
Alexander Afanasyev9c578182014-05-14 17:28:28 -070086 if (tmpName.get(keyComponentIndex).toUri() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080087 {
88 foundKeyString = true;
89 break;
90 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070091 }
Yingdi Yu88663af2014-01-15 15:21:38 -080092
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 if (!foundKeyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080094 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070095
96 return tmpName
97 .getSubName(0, keyComponentIndex)
98 .append(tmpName.getSubName(keyComponentIndex + 1,
99 tmpName.size() - keyComponentIndex - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -0700100}
101
Yingdi Yufc40d872014-02-18 12:56:04 -0800102} // namespace ndn