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