blob: c14b32802e84871f71d87d7127884426c6a72a28 [file] [log] [blame]
Yingdi Yu53b8a9c2013-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 Yu79c25a22013-10-21 13:38:38 -070016#include "logging.h"
17
Yingdi Yu53b8a9c2013-10-14 09:36:31 -070018using namespace std;
19using namespace ndn;
20using namespace ndn::security;
21
Yingdi Yu79c25a22013-10-21 13:38:38 -070022INIT_LOGGER("ContactItem");
23
Yingdi Yu53b8a9c2013-10-14 09:36:31 -070024ContactItem::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 Yu79c25a22013-10-21 13:38:38 -070031
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 Yu53b8a9c2013-10-14 09:36:31 -070043
Yingdi Yu79c25a22013-10-21 13:38:38 -070044 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 Yu53b8a9c2013-10-14 09:36:31 -070056 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