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