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!"); |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 21 | |
| 22 | setPublicKeyName(); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Jeff Thompson | a4aa4ce | 2013-11-21 10:56:24 -0800 | [diff] [blame] | 25 | IdentityCertificate::IdentityCertificate(const IdentityCertificate& identityCertificate) |
| 26 | : Certificate(identityCertificate) |
| 27 | , publicKeyName_(identityCertificate.publicKeyName_) |
| 28 | { |
| 29 | } |
| 30 | |
Jeff Thompson | 6fe345d | 2013-10-15 18:05:41 -0700 | [diff] [blame] | 31 | IdentityCertificate::~IdentityCertificate() |
| 32 | { |
| 33 | } |
| 34 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 35 | bool |
| 36 | IdentityCertificate::isCorrectName(const Name& name) |
| 37 | { |
| 38 | int i = name.size() - 1; |
| 39 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 40 | string idString("ID-CERT"); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 41 | for (; i >= 0; i--) { |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 42 | if(name.get(i).toEscapedString() == idString) |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 43 | break; |
| 44 | } |
| 45 | |
| 46 | if (i < 0) |
| 47 | return false; |
| 48 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 49 | int keyIdx = 0; |
| 50 | string keyString("KEY"); |
| 51 | for (; keyIdx < name.size(); keyIdx++) { |
| 52 | if(name.get(keyIdx).toEscapedString() == keyString) |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | if (keyIdx >= name.size()) |
| 57 | return false; |
| 58 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
| 62 | Data& |
| 63 | IdentityCertificate::setName(const Name& name) |
| 64 | { |
| 65 | if (!isCorrectName(name)) |
| 66 | throw SecurityException("Wrong Identity Certificate Name!"); |
| 67 | |
| 68 | Data::setName(name); |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 69 | setPublicKeyName(); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 70 | return *this; |
| 71 | } |
| 72 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 73 | void |
| 74 | IdentityCertificate::setPublicKeyName() |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 75 | { |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 76 | publicKeyName_ = certificateNameToPublicKeyName(getName()); |
| 77 | } |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 79 | bool |
| 80 | IdentityCertificate::isIdentityCertificate(const Certificate& certificate) |
| 81 | { |
| 82 | return isCorrectName(certificate.getName()); |
| 83 | } |
| 84 | |
| 85 | Name |
| 86 | IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName) |
| 87 | { |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 88 | int i = certificateName.size() - 1; |
| 89 | string idString("ID-CERT"); |
| 90 | for (; i >= 0; i--) { |
| 91 | if (certificateName.get(i).toEscapedString() == idString) |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | Name tmpName = certificateName.getSubName(0, i); |
| 96 | string keyString("KEY"); |
| 97 | for (i = 0; i < tmpName.size(); i++) { |
| 98 | if (tmpName.get(i).toEscapedString() == keyString) |
| 99 | break; |
| 100 | } |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 101 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 102 | return tmpName.getSubName(0, i).append(tmpName.getSubName(i + 1, tmpName.size() - i - 1)); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } |