blob: 95f6c1b6a9bcee5717fa132621036cbb8e1d402e [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{
Jeff Thompson74942612013-10-24 16:42:32 -070070 publicKeyName_ = certificateNameToPublicKeyName(getName());
71}
Jeff Thompson173fd432013-10-12 18:16:41 -070072
Jeff Thompson74942612013-10-24 16:42:32 -070073bool
74IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
75{
76 return isCorrectName(certificate.getName());
77}
78
79Name
80IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
81{
Jeff Thompson43a57b12013-10-22 16:25:38 -070082 int i = certificateName.size() - 1;
83 string idString("ID-CERT");
84 for (; i >= 0; i--) {
85 if (certificateName.get(i).toEscapedString() == idString)
86 break;
87 }
88
89 Name tmpName = certificateName.getSubName(0, i);
90 string keyString("KEY");
91 for (i = 0; i < tmpName.size(); i++) {
92 if (tmpName.get(i).toEscapedString() == keyString)
93 break;
94 }
Jeff Thompson173fd432013-10-12 18:16:41 -070095
Jeff Thompson74942612013-10-24 16:42:32 -070096 return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070097}
98
99}