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"); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame^] | 62 | bool foundIdString = false; |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 63 | for (; i >= 0; i--) { |
| 64 | if (certificateName.get(i).toEscapedString() == idString) |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame^] | 65 | { |
| 66 | foundIdString = true; |
| 67 | break; |
| 68 | } |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 69 | } |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame^] | 70 | |
| 71 | if(!foundIdString) |
| 72 | throw Error("Incorrect identity certificate name " + certificateName.toUri()); |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 73 | |
| 74 | Name tmpName = certificateName.getSubName(0, i); |
| 75 | string keyString("KEY"); |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame^] | 76 | bool foundKeyString = false; |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 77 | for (i = 0; i < tmpName.size(); i++) { |
| 78 | if (tmpName.get(i).toEscapedString() == keyString) |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame^] | 79 | { |
| 80 | foundKeyString = true; |
| 81 | break; |
| 82 | } |
Jeff Thompson | 43a57b1 | 2013-10-22 16:25:38 -0700 | [diff] [blame] | 83 | } |
Yingdi Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame^] | 84 | |
| 85 | if(!foundKeyString) |
| 86 | throw Error("Incorrect identity certificate name " + certificateName.toUri()); |
Jeff Thompson | 173fd43 | 2013-10-12 18:16:41 -0700 | [diff] [blame] | 87 | |
Jeff Thompson | 7494261 | 2013-10-24 16:42:32 -0700 | [diff] [blame] | 88 | 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] | 89 | } |
| 90 | |
| 91 | } |