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