Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -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 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "contact-item.h" |
| 12 | #include "exception.h" |
| 13 | |
| 14 | #include <ndn.cxx/fields/signature-sha256-with-rsa.h> |
| 15 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 16 | #include "logging.h" |
| 17 | |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 18 | using namespace std; |
| 19 | using namespace ndn; |
| 20 | using namespace ndn::security; |
| 21 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 22 | INIT_LOGGER("ContactItem"); |
| 23 | |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 24 | ContactItem::ContactItem(const EndorseCertificate& selfEndorseCertificate, |
| 25 | const string& alias) |
| 26 | : m_selfEndorseCertificate(selfEndorseCertificate) |
| 27 | { |
| 28 | Name endorsedkeyName = selfEndorseCertificate.getPublicKeyName(); |
| 29 | Ptr<const signature::Sha256WithRsa> endorseSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(selfEndorseCertificate.getSignature()); |
| 30 | const Name& signingKeyName = endorseSig->getKeyLocator().getKeyName(); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 31 | |
| 32 | int i = 0; |
| 33 | int j = -1; |
| 34 | string keyString("KEY"); |
| 35 | string idString("ID-CERT"); |
| 36 | for(; i < signingKeyName.size(); i++) |
| 37 | { |
| 38 | if(keyString == signingKeyName.get(i).toUri()) |
| 39 | j = i; |
| 40 | if(idString == signingKeyName.get(i).toUri()) |
| 41 | break; |
| 42 | } |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 43 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 44 | if(i >= signingKeyName.size() || j < 0) |
| 45 | throw LnException("Wrong name!"); |
| 46 | |
| 47 | Name subName = signingKeyName.getSubName(0, j); |
| 48 | subName.append(signingKeyName.getSubName(j+1, i-j-1)); |
| 49 | |
| 50 | |
| 51 | |
| 52 | // _LOG_DEBUG("endorsedkeyName " << endorsedkeyName.toUri()); |
| 53 | // _LOG_DEBUG("subKeyName " << subName.toUri()); |
| 54 | |
| 55 | if(endorsedkeyName != subName) |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 56 | throw LnException("not a self-claimed"); |
| 57 | |
| 58 | m_namespace = endorsedkeyName.getSubName(0, endorsedkeyName.size() - 1); |
| 59 | m_alias = alias.empty() ? m_namespace.toUri() : alias; |
| 60 | |
| 61 | Ptr<ProfileData> profileData = selfEndorseCertificate.getProfileData(); |
| 62 | Ptr<const Blob> nameBlob = profileData->getProfile().getProfileEntry("name"); |
| 63 | m_name = string(nameBlob->buf(), nameBlob->size()); |
| 64 | Ptr<const Blob> institutionBlob = profileData->getProfile().getProfileEntry("institution"); |
| 65 | m_institution = string(institutionBlob->buf(), institutionBlob->size()); |
| 66 | } |
| 67 | |