blob: 2e2ea790856377cd736f3def099a28ac2822ef18 [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
9#include <ndn-cpp/security/security-exception.hpp>
10#include <ndn-cpp/security/certificate/identity-certificate.hpp>
11
12using namespace std;
13
14namespace ndn {
15
16IdentityCertificate::IdentityCertificate(const Data& data)
17 : Certificate(data)
18{
19 if (!isCorrectName(data.getName()))
20 throw SecurityException("Wrong Identity Certificate Name!");
Jeff Thompson43a57b12013-10-22 16:25:38 -070021
22 setPublicKeyName();
Jeff Thompson173fd432013-10-12 18:16:41 -070023}
24
Jeff Thompson6fe345d2013-10-15 18:05:41 -070025IdentityCertificate::~IdentityCertificate()
26{
27}
28
Jeff Thompson173fd432013-10-12 18:16:41 -070029bool
30IdentityCertificate::isCorrectName(const Name& name)
31{
32 int i = name.size() - 1;
33
Jeff Thompson43a57b12013-10-22 16:25:38 -070034 string idString("ID-CERT");
Jeff Thompson173fd432013-10-12 18:16:41 -070035 for (; i >= 0; i--) {
Jeff Thompson43a57b12013-10-22 16:25:38 -070036 if(name.get(i).toEscapedString() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070037 break;
38 }
39
40 if (i < 0)
41 return false;
42
Jeff Thompson43a57b12013-10-22 16:25:38 -070043 int keyIdx = 0;
44 string keyString("KEY");
45 for (; keyIdx < name.size(); keyIdx++) {
46 if(name.get(keyIdx).toEscapedString() == keyString)
47 break;
48 }
49
50 if (keyIdx >= name.size())
51 return false;
52
Jeff Thompson173fd432013-10-12 18:16:41 -070053 return true;
54}
55
56Data&
57IdentityCertificate::setName(const Name& name)
58{
59 if (!isCorrectName(name))
60 throw SecurityException("Wrong Identity Certificate Name!");
61
62 Data::setName(name);
Jeff Thompson43a57b12013-10-22 16:25:38 -070063 setPublicKeyName();
Jeff Thompson173fd432013-10-12 18:16:41 -070064 return *this;
65}
66
Jeff Thompson43a57b12013-10-22 16:25:38 -070067void
68IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070069{
70 const Name& certificateName = getName();
Jeff Thompson173fd432013-10-12 18:16:41 -070071
Jeff Thompson43a57b12013-10-22 16:25:38 -070072 int i = certificateName.size() - 1;
73 string idString("ID-CERT");
74 for (; i >= 0; i--) {
75 if (certificateName.get(i).toEscapedString() == idString)
76 break;
77 }
78
79 Name tmpName = certificateName.getSubName(0, i);
80 string keyString("KEY");
81 for (i = 0; i < tmpName.size(); i++) {
82 if (tmpName.get(i).toEscapedString() == keyString)
83 break;
84 }
Jeff Thompson173fd432013-10-12 18:16:41 -070085
Jeff Thompson43a57b12013-10-22 16:25:38 -070086 publicKeyName_ = tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070087}
88
89bool
90IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
91{
92 return isCorrectName(certificate.getName());
93}
94
95}