blob: a328112c4e805ad999856856f551b6655129a1f8 [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!");
21}
22
Jeff Thompson6fe345d2013-10-15 18:05:41 -070023IdentityCertificate::~IdentityCertificate()
24{
25}
26
Jeff Thompson173fd432013-10-12 18:16:41 -070027bool
28IdentityCertificate::isCorrectName(const Name& name)
29{
30 int i = name.size() - 1;
31
32 for (; i >= 0; i--) {
33 if(name.get(i).toEscapedString() == string("ID-CERT"))
34 break;
35 }
36
37 if (i < 0)
38 return false;
39
40 return true;
41}
42
43Data&
44IdentityCertificate::setName(const Name& name)
45{
46 if (!isCorrectName(name))
47 throw SecurityException("Wrong Identity Certificate Name!");
48
49 Data::setName(name);
50 return *this;
51}
52
53Name
54IdentityCertificate::getPublicKeyName() const
55{
56 const Name& certificateName = getName();
57 int i = certificateName.size() - 1;
58
59 for (; i >= 0; i--)
60 if(certificateName.get(i).toEscapedString() == string("ID-CERT"))
61 break;
62
63 return certificateName.getSubName(0, i);
64}
65
66bool
67IdentityCertificate::isIdentityCertificate(const Certificate& certificate)
68{
69 return isCorrectName(certificate.getName());
70}
71
72}