blob: 5cb570a402709bb35768e910ee6684d754d40490 [file] [log] [blame]
Jeff Thompson173fd432013-10-12 18:16:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009#include "common.hpp"
10
11#include "identity-certificate.hpp"
Jeff Thompson173fd432013-10-12 18:16:41 -070012
13using namespace std;
14
15namespace ndn {
16
Jeff Thompson173fd432013-10-12 18:16:41 -070017bool
18IdentityCertificate::isCorrectName(const Name& name)
19{
Jeff Thompson43a57b12013-10-22 16:25:38 -070020 string idString("ID-CERT");
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070021 int i = name.size() - 1;
Jeff Thompson173fd432013-10-12 18:16:41 -070022 for (; i >= 0; i--) {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023 if (name.get(i).toEscapedString() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070024 break;
25 }
26
27 if (i < 0)
28 return false;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029
Jeff Thompson43a57b12013-10-22 16:25:38 -070030 string keyString("KEY");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 size_t keyIndex = 0;
32 for (; keyIndex < name.size(); keyIndex++) {
33 if (name.get(keyIndex).toEscapedString() == keyString)
Jeff Thompson43a57b12013-10-22 16:25:38 -070034 break;
35 }
36
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070037 if (keyIndex >= name.size())
Jeff Thompson43a57b12013-10-22 16:25:38 -070038 return false;
39
Jeff Thompson173fd432013-10-12 18:16:41 -070040 return true;
41}
42
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080043void
Jeff Thompson43a57b12013-10-22 16:25:38 -070044IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070045{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080046 if (!isCorrectName(getName()))
47 throw Error("Wrong Identity Certificate Name!");
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070049 m_publicKeyName = certificateNameToPublicKeyName(getName());
Jeff Thompson74942612013-10-24 16:42:32 -070050}
Jeff Thompson173fd432013-10-12 18:16:41 -070051
Jeff Thompson74942612013-10-24 16:42:32 -070052bool
53IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080054{
55 return dynamic_cast<const IdentityCertificate*>(&certificate);
Jeff Thompson74942612013-10-24 16:42:32 -070056}
57
58Name
59IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
60{
Jeff Thompson43a57b12013-10-22 16:25:38 -070061 string idString("ID-CERT");
Yingdi Yu88663af2014-01-15 15:21:38 -080062 bool foundIdString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070063 size_t idCertComponentIndex = certificateName.size() - 1;
64 for (; idCertComponentIndex + 1 > 0; --idCertComponentIndex) {
65 if (certificateName.get(idCertComponentIndex).toEscapedString() == idString)
Yingdi Yu88663af2014-01-15 15:21:38 -080066 {
67 foundIdString = true;
68 break;
69 }
Jeff Thompson43a57b12013-10-22 16:25:38 -070070 }
Yingdi Yu88663af2014-01-15 15:21:38 -080071
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072 if (!foundIdString)
Yingdi Yu88663af2014-01-15 15:21:38 -080073 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070074
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070075 Name tmpName = certificateName.getSubName(0, idCertComponentIndex);
Jeff Thompson43a57b12013-10-22 16:25:38 -070076 string keyString("KEY");
Yingdi Yu88663af2014-01-15 15:21:38 -080077 bool foundKeyString = false;
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070078 size_t keyComponentIndex = 0;
79 for (; keyComponentIndex < tmpName.size(); keyComponentIndex++) {
80 if (tmpName.get(keyComponentIndex).toEscapedString() == keyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080081 {
82 foundKeyString = 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 (!foundKeyString)
Yingdi Yu88663af2014-01-15 15:21:38 -080088 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070089
90 return tmpName
91 .getSubName(0, keyComponentIndex)
92 .append(tmpName.getSubName(keyComponentIndex + 1,
93 tmpName.size() - keyComponentIndex - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070094}
95
Yingdi Yufc40d872014-02-18 12:56:04 -080096} // namespace ndn