blob: b4d5cb5d1514d98e8ab8a487ebb1af4571653e1d [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--) {
Jeff Thompson43a57b12013-10-22 16:25:38 -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;
29
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070030 size_t keyIdx = 0;
Jeff Thompson43a57b12013-10-22 16:25:38 -070031 string keyString("KEY");
32 for (; keyIdx < name.size(); keyIdx++) {
33 if(name.get(keyIdx).toEscapedString() == keyString)
34 break;
35 }
36
37 if (keyIdx >= name.size())
38 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!");
48
Jeff Thompson74942612013-10-24 16:42:32 -070049 publicKeyName_ = certificateNameToPublicKeyName(getName());
50}
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
72 if(!foundIdString)
73 throw Error("Incorrect identity certificate name " + certificateName.toUri());
Jeff Thompson43a57b12013-10-22 16:25:38 -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
87 if(!foundKeyString)
88 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