blob: 7e4bca78770a219c5551b7e061789562226a5316 [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 Thompsona4aa4ce2013-11-21 10:56:24 -080025IdentityCertificate::IdentityCertificate(const IdentityCertificate& identityCertificate)
26 : Certificate(identityCertificate)
27 , publicKeyName_(identityCertificate.publicKeyName_)
28{
29}
30
Jeff Thompson6fe345d2013-10-15 18:05:41 -070031IdentityCertificate::~IdentityCertificate()
32{
33}
34
Jeff Thompson173fd432013-10-12 18:16:41 -070035bool
36IdentityCertificate::isCorrectName(const Name& name)
37{
38 int i = name.size() - 1;
39
Jeff Thompson43a57b12013-10-22 16:25:38 -070040 string idString("ID-CERT");
Jeff Thompson173fd432013-10-12 18:16:41 -070041 for (; i >= 0; i--) {
Jeff Thompson43a57b12013-10-22 16:25:38 -070042 if(name.get(i).toEscapedString() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070043 break;
44 }
45
46 if (i < 0)
47 return false;
48
Jeff Thompson43a57b12013-10-22 16:25:38 -070049 int keyIdx = 0;
50 string keyString("KEY");
51 for (; keyIdx < name.size(); keyIdx++) {
52 if(name.get(keyIdx).toEscapedString() == keyString)
53 break;
54 }
55
56 if (keyIdx >= name.size())
57 return false;
58
Jeff Thompson173fd432013-10-12 18:16:41 -070059 return true;
60}
61
62Data&
63IdentityCertificate::setName(const Name& name)
64{
65 if (!isCorrectName(name))
66 throw SecurityException("Wrong Identity Certificate Name!");
67
68 Data::setName(name);
Jeff Thompson43a57b12013-10-22 16:25:38 -070069 setPublicKeyName();
Jeff Thompson173fd432013-10-12 18:16:41 -070070 return *this;
71}
72
Jeff Thompson43a57b12013-10-22 16:25:38 -070073void
74IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070075{
Jeff Thompson74942612013-10-24 16:42:32 -070076 publicKeyName_ = certificateNameToPublicKeyName(getName());
77}
Jeff Thompson173fd432013-10-12 18:16:41 -070078
Jeff Thompson74942612013-10-24 16:42:32 -070079bool
80IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
81{
82 return isCorrectName(certificate.getName());
83}
84
85Name
86IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
87{
Jeff Thompson43a57b12013-10-22 16:25:38 -070088 int i = certificateName.size() - 1;
89 string idString("ID-CERT");
90 for (; i >= 0; i--) {
91 if (certificateName.get(i).toEscapedString() == idString)
92 break;
93 }
94
95 Name tmpName = certificateName.getSubName(0, i);
96 string keyString("KEY");
97 for (i = 0; i < tmpName.size(); i++) {
98 if (tmpName.get(i).toEscapedString() == keyString)
99 break;
100 }
Jeff Thompson173fd432013-10-12 18:16:41 -0700101
Jeff Thompson74942612013-10-24 16:42:32 -0700102 return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -0700103}
104
105}