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