blob: 7e6d50d58b67df4056f50568b7a3817b68027ed2 [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
Jeff Thompson173fd432013-10-12 18:16:41 -07009#include <ndn-cpp/security/certificate/identity-certificate.hpp>
10
11using namespace std;
12
13namespace ndn {
14
15IdentityCertificate::IdentityCertificate(const Data& data)
16 : Certificate(data)
17{
18 if (!isCorrectName(data.getName()))
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080019 throw Error("Wrong Identity Certificate Name!");
Jeff Thompson43a57b12013-10-22 16:25:38 -070020
21 setPublicKeyName();
Jeff Thompson173fd432013-10-12 18:16:41 -070022}
23
Jeff Thompson6fe345d2013-10-15 18:05:41 -070024IdentityCertificate::~IdentityCertificate()
25{
26}
27
Jeff Thompson173fd432013-10-12 18:16:41 -070028bool
29IdentityCertificate::isCorrectName(const Name& name)
30{
31 int i = name.size() - 1;
32
Jeff Thompson43a57b12013-10-22 16:25:38 -070033 string idString("ID-CERT");
Jeff Thompson173fd432013-10-12 18:16:41 -070034 for (; i >= 0; i--) {
Jeff Thompson43a57b12013-10-22 16:25:38 -070035 if(name.get(i).toEscapedString() == idString)
Jeff Thompson173fd432013-10-12 18:16:41 -070036 break;
37 }
38
39 if (i < 0)
40 return false;
41
Jeff Thompson43a57b12013-10-22 16:25:38 -070042 int keyIdx = 0;
43 string keyString("KEY");
44 for (; keyIdx < name.size(); keyIdx++) {
45 if(name.get(keyIdx).toEscapedString() == keyString)
46 break;
47 }
48
49 if (keyIdx >= name.size())
50 return false;
51
Jeff Thompson173fd432013-10-12 18:16:41 -070052 return true;
53}
54
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080055void
Jeff Thompson173fd432013-10-12 18:16:41 -070056IdentityCertificate::setName(const Name& name)
57{
58 if (!isCorrectName(name))
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080059 throw Error("Wrong Identity Certificate Name!");
Jeff Thompson173fd432013-10-12 18:16:41 -070060
61 Data::setName(name);
Jeff Thompson43a57b12013-10-22 16:25:38 -070062 setPublicKeyName();
Jeff Thompson173fd432013-10-12 18:16:41 -070063}
64
Jeff Thompson43a57b12013-10-22 16:25:38 -070065void
66IdentityCertificate::setPublicKeyName()
Jeff Thompson173fd432013-10-12 18:16:41 -070067{
Jeff Thompson74942612013-10-24 16:42:32 -070068 publicKeyName_ = certificateNameToPublicKeyName(getName());
69}
Jeff Thompson173fd432013-10-12 18:16:41 -070070
Jeff Thompson74942612013-10-24 16:42:32 -070071bool
72IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
73{
74 return isCorrectName(certificate.getName());
75}
76
77Name
78IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName)
79{
Jeff Thompson43a57b12013-10-22 16:25:38 -070080 int i = certificateName.size() - 1;
81 string idString("ID-CERT");
82 for (; i >= 0; i--) {
83 if (certificateName.get(i).toEscapedString() == idString)
84 break;
85 }
86
87 Name tmpName = certificateName.getSubName(0, i);
88 string keyString("KEY");
89 for (i = 0; i < tmpName.size(); i++) {
90 if (tmpName.get(i).toEscapedString() == keyString)
91 break;
92 }
Jeff Thompson173fd432013-10-12 18:16:41 -070093
Jeff Thompson74942612013-10-24 16:42:32 -070094 return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1));
Jeff Thompson173fd432013-10-12 18:16:41 -070095}
96
97}