blob: eb342d6b60ff62a92d834852c26d9eef4b838a25 [file] [log] [blame]
Yingdi Yudbeb8e22013-10-14 09:36:31 -07001/* -*- 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 Yu2ac40fb2013-10-21 13:38:38 -070016#include "logging.h"
17
Yingdi Yudbeb8e22013-10-14 09:36:31 -070018using namespace std;
19using namespace ndn;
20using namespace ndn::security;
21
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070022INIT_LOGGER("ContactItem");
23
Yingdi Yudbeb8e22013-10-14 09:36:31 -070024ContactItem::ContactItem(const EndorseCertificate& selfEndorseCertificate,
Yingdi Yu71c01872013-11-03 16:22:05 -080025 bool isIntroducer,
Yingdi Yudbeb8e22013-10-14 09:36:31 -070026 const string& alias)
27 : m_selfEndorseCertificate(selfEndorseCertificate)
Yingdi Yu71c01872013-11-03 16:22:05 -080028 , m_isIntroducer(isIntroducer)
Yingdi Yudbeb8e22013-10-14 09:36:31 -070029{
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 Yu2ac40fb2013-10-21 13:38:38 -070033
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 Yudbeb8e22013-10-14 09:36:31 -070045
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070046 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 Yudbeb8e22013-10-14 09:36:31 -070058 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 Yuec5e72a2013-11-03 15:05:26 -080070ContactItem::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 Yu71c01872013-11-03 16:22:05 -080076 , m_isIntroducer(contactItem.m_isIntroducer)
77 , m_trustScope(contactItem.m_trustScope)
78 , m_trustScopeName(contactItem.m_trustScopeName)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080079{}
80
Yingdi Yu71c01872013-11-03 16:22:05 -080081bool
82ContactItem::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}