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