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 | { |
| 70 | const Name& certificateName = getName(); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 71 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 72 | int i = certificateName.size() - 1; |
| 73 | string idString("ID-CERT"); |
| 74 | for (; i >= 0; i--) { |
| 75 | if (certificateName.get(i).toEscapedString() == idString) |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | Name tmpName = certificateName.getSubName(0, i); |
| 80 | string keyString("KEY"); |
| 81 | for (i = 0; i < tmpName.size(); i++) { |
| 82 | if (tmpName.get(i).toEscapedString() == keyString) |
| 83 | break; |
| 84 | } |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 85 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 86 | publicKeyName_ = 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] | 87 | } |
| 88 | |
| 89 | bool |
| 90 | IdentityCertificate::isIdentityCertificate(const Certificate& certificate) |
| 91 | { |
| 92 | return isCorrectName(certificate.getName()); |
| 93 | } |
| 94 | |
| 95 | } |