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 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 15 | bool |
| 16 | IdentityCertificate::isCorrectName(const Name& name) |
| 17 | { |
| 18 | int i = name.size() - 1; |
| 19 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 20 | string idString("ID-CERT"); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 21 | for (; i >= 0; i--) { |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 22 | if(name.get(i).toEscapedString() == idString) |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 23 | break; |
| 24 | } |
| 25 | |
| 26 | if (i < 0) |
| 27 | return false; |
| 28 | |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 29 | int keyIdx = 0; |
| 30 | string keyString("KEY"); |
| 31 | for (; keyIdx < name.size(); keyIdx++) { |
| 32 | if(name.get(keyIdx).toEscapedString() == keyString) |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | if (keyIdx >= name.size()) |
| 37 | return false; |
| 38 | |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
Alexander Afanasyev | 64a3d81 | 2014-01-05 23:35:05 -0800 | [diff] [blame] | 42 | void |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 43 | IdentityCertificate::setPublicKeyName() |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 45 | if (!isCorrectName(getName())) |
| 46 | throw Error("Wrong Identity Certificate Name!"); |
| 47 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 48 | publicKeyName_ = certificateNameToPublicKeyName(getName()); |
| 49 | } |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 50 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 51 | bool |
| 52 | IdentityCertificate::isIdentityCertificate(const Certificate& certificate) |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 53 | { |
| 54 | return dynamic_cast<const IdentityCertificate*>(&certificate); |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | Name |
| 58 | IdentityCertificate::certificateNameToPublicKeyName(const Name& certificateName) |
| 59 | { |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 60 | int i = certificateName.size() - 1; |
| 61 | string idString("ID-CERT"); |
| 62 | for (; i >= 0; i--) { |
| 63 | if (certificateName.get(i).toEscapedString() == idString) |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | Name tmpName = certificateName.getSubName(0, i); |
| 68 | string keyString("KEY"); |
| 69 | for (i = 0; i < tmpName.size(); i++) { |
| 70 | if (tmpName.get(i).toEscapedString() == keyString) |
| 71 | break; |
| 72 | } |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 74 | 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] | 75 | } |
| 76 | |
| 77 | } |