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 | |
| 23 | bool |
| 24 | IdentityCertificate::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 | |
| 39 | Data& |
| 40 | IdentityCertificate::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 | |
| 49 | Name |
| 50 | IdentityCertificate::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 | |
| 62 | bool |
| 63 | IdentityCertificate::isIdentityCertificate(const Certificate& certificate) |
| 64 | { |
| 65 | return isCorrectName(certificate.getName()); |
| 66 | } |
| 67 | |
| 68 | } |