Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | using namespace std; |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | IdentityCertificate::IdentityCertificate(const Data& data) |
| 17 | : Certificate(data) |
| 18 | { |
| 19 | if (!isCorrectName(data.getName())) |
| 20 | throw SecurityException("Wrong Identity Certificate Name!"); |
| 21 | } |
| 22 | |
Jeff Thompson | 6fe345d | 2013-10-15 18:05:41 -0700 | [diff] [blame] | 23 | IdentityCertificate::~IdentityCertificate() |
| 24 | { |
| 25 | } |
| 26 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 27 | bool |
| 28 | IdentityCertificate::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 | |
| 43 | Data& |
| 44 | IdentityCertificate::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 | |
| 53 | Name |
| 54 | IdentityCertificate::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 | |
| 66 | bool |
| 67 | IdentityCertificate::isIdentityCertificate(const Certificate& certificate) |
| 68 | { |
| 69 | return isCorrectName(certificate.getName()); |
| 70 | } |
| 71 | |
| 72 | } |