Yingdi Yu | dbeb8e2 | 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 | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 16 | #include "logging.h" |
| 17 | |
Yingdi Yu | dbeb8e2 | 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 | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 22 | INIT_LOGGER("ContactItem"); |
| 23 | |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 24 | ContactItem::ContactItem(const EndorseCertificate& selfEndorseCertificate, |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame^] | 25 | bool isIntroducer, |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 26 | const string& alias) |
| 27 | : m_selfEndorseCertificate(selfEndorseCertificate) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame^] | 28 | , m_isIntroducer(isIntroducer) |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 29 | { |
| 30 | Name endorsedkeyName = selfEndorseCertificate.getPublicKeyName(); |
| 31 | Ptr<const signature::Sha256WithRsa> endorseSig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa>(selfEndorseCertificate.getSignature()); |
| 32 | const Name& signingKeyName = endorseSig->getKeyLocator().getKeyName(); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 33 | |
| 34 | int i = 0; |
| 35 | int j = -1; |
| 36 | string keyString("KEY"); |
| 37 | string idString("ID-CERT"); |
| 38 | for(; i < signingKeyName.size(); i++) |
| 39 | { |
| 40 | if(keyString == signingKeyName.get(i).toUri()) |
| 41 | j = i; |
| 42 | if(idString == signingKeyName.get(i).toUri()) |
| 43 | break; |
| 44 | } |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 45 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 46 | if(i >= signingKeyName.size() || j < 0) |
| 47 | throw LnException("Wrong name!"); |
| 48 | |
| 49 | Name subName = signingKeyName.getSubName(0, j); |
| 50 | subName.append(signingKeyName.getSubName(j+1, i-j-1)); |
| 51 | |
| 52 | |
| 53 | |
| 54 | // _LOG_DEBUG("endorsedkeyName " << endorsedkeyName.toUri()); |
| 55 | // _LOG_DEBUG("subKeyName " << subName.toUri()); |
| 56 | |
| 57 | if(endorsedkeyName != subName) |
Yingdi Yu | dbeb8e2 | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 58 | throw LnException("not a self-claimed"); |
| 59 | |
| 60 | m_namespace = endorsedkeyName.getSubName(0, endorsedkeyName.size() - 1); |
| 61 | m_alias = alias.empty() ? m_namespace.toUri() : alias; |
| 62 | |
| 63 | Ptr<ProfileData> profileData = selfEndorseCertificate.getProfileData(); |
| 64 | Ptr<const Blob> nameBlob = profileData->getProfile().getProfileEntry("name"); |
| 65 | m_name = string(nameBlob->buf(), nameBlob->size()); |
| 66 | Ptr<const Blob> institutionBlob = profileData->getProfile().getProfileEntry("institution"); |
| 67 | m_institution = string(institutionBlob->buf(), institutionBlob->size()); |
| 68 | } |
| 69 | |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 70 | ContactItem::ContactItem(const ContactItem& contactItem) |
| 71 | : m_selfEndorseCertificate(contactItem.m_selfEndorseCertificate) |
| 72 | , m_namespace(contactItem.m_namespace) |
| 73 | , m_alias(contactItem.m_alias) |
| 74 | , m_name(contactItem.m_name) |
| 75 | , m_institution(contactItem.m_institution) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame^] | 76 | , m_isIntroducer(contactItem.m_isIntroducer) |
| 77 | , m_trustScope(contactItem.m_trustScope) |
| 78 | , m_trustScopeName(contactItem.m_trustScopeName) |
Yingdi Yu | ec5e72a | 2013-11-03 15:05:26 -0800 | [diff] [blame] | 79 | {} |
| 80 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame^] | 81 | bool |
| 82 | ContactItem::canBeTrustedFor(const Name& name) |
| 83 | { |
| 84 | vector<Ptr<Regex> >::iterator it = m_trustScope.begin(); |
| 85 | |
| 86 | for(; it != m_trustScope.end(); it++) |
| 87 | if((*it)->match(name)) |
| 88 | return true; |
| 89 | return false; |
| 90 | } |