Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -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 "endorse-certificate.h" |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 12 | #include "endorse-extension.pb.h" |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 13 | #include <boost/iostreams/stream.hpp> |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 14 | #include "logging.h" |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | using namespace ndn; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 20 | INIT_LOGGER("EndorseCertificate"); |
| 21 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 22 | namespace chronos{ |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 24 | const OID EndorseCertificate::PROFILE_EXT_OID("1.3.6.1.5.32.2.1"); |
| 25 | const OID EndorseCertificate::ENDORSE_EXT_OID("1.3.6.1.5.32.2.2"); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 26 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 27 | Chronos::EndorseExtensionMsg& |
| 28 | operator << (Chronos::EndorseExtensionMsg& endorseExtension, const vector<string>& endorseList) |
| 29 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 30 | vector<string>::const_iterator it = endorseList.begin(); |
| 31 | for(; it != endorseList.end(); it++) |
| 32 | endorseExtension.add_endorseentry()->set_name(*it); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 33 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 34 | return endorseExtension; |
| 35 | } |
| 36 | |
| 37 | Chronos::EndorseExtensionMsg& |
| 38 | operator >> (Chronos::EndorseExtensionMsg& endorseExtension, vector<string>& endorseList) |
| 39 | { |
| 40 | for(int i = 0; i < endorseExtension.endorseentry_size(); i ++) |
| 41 | endorseList.push_back(endorseExtension.endorseentry(i).name()); |
| 42 | |
| 43 | return endorseExtension; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 46 | EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 47 | const Profile& profile, |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 48 | const vector<string>& endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 49 | : Certificate() |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 50 | , m_profile(profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 51 | , m_endorseList(endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 52 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 53 | m_keyName = IdentityCertificate::certificateNameToPublicKeyName(kskCertificate.getName()); |
| 54 | m_signer = m_keyName; |
| 55 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 56 | Name dataName = m_keyName; |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 57 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 58 | setName(dataName); |
| 59 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 60 | setNotBefore(kskCertificate.getNotBefore()); |
| 61 | setNotAfter(kskCertificate.getNotAfter()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 62 | addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 63 | setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 64 | |
| 65 | OBufferStream profileStream; |
| 66 | m_profile.encode(profileStream); |
| 67 | addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf())); |
| 68 | |
| 69 | OBufferStream endorseStream; |
| 70 | Chronos::EndorseExtensionMsg endorseExtension; |
| 71 | endorseExtension << m_endorseList; |
| 72 | endorseExtension.SerializeToOstream(&endorseStream); |
| 73 | addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf())); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 74 | |
| 75 | encode(); |
| 76 | } |
| 77 | |
| 78 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 79 | const Name& signer, |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 80 | const vector<string>& endorseList) |
| 81 | : Certificate() |
| 82 | , m_keyName(endorseCertificate.m_keyName) |
| 83 | , m_signer(signer) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 84 | , m_profile(endorseCertificate.m_profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 85 | , m_endorseList(endorseList) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 86 | { |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 87 | Name dataName = m_keyName; |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 88 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 89 | setName(dataName); |
| 90 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 91 | setNotBefore(endorseCertificate.getNotBefore()); |
| 92 | setNotAfter(endorseCertificate.getNotAfter()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 93 | addSubjectDescription(CertificateSubjectDescription("2.5.4.41", m_keyName.toUri())); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 94 | setPublicKeyInfo(endorseCertificate.getPublicKeyInfo()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 95 | |
| 96 | OBufferStream profileStream; |
| 97 | m_profile.encode(profileStream); |
| 98 | addExtension(CertificateExtension(PROFILE_EXT_OID, true, *profileStream.buf())); |
| 99 | |
| 100 | OBufferStream endorseStream; |
| 101 | Chronos::EndorseExtensionMsg endorseExtension; |
| 102 | endorseExtension << m_endorseList; |
| 103 | endorseExtension.SerializeToOstream(&endorseStream); |
| 104 | addExtension(CertificateExtension(ENDORSE_EXT_OID, true, *endorseStream.buf())); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 105 | |
| 106 | encode(); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 110 | : Certificate(endorseCertificate) |
| 111 | , m_keyName(endorseCertificate.m_keyName) |
| 112 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 113 | , m_profile(endorseCertificate.m_profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 114 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 115 | {} |
| 116 | |
| 117 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 118 | : Certificate(data) |
| 119 | { |
| 120 | const Name& dataName = data.getName(); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 121 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 122 | if(dataName.size() < 3 || dataName.get(-3).toEscapedString() != "PROFILE-CERT") |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 123 | throw Error("No PROFILE-CERT component in data name!"); |
| 124 | |
| 125 | m_keyName = dataName.getPrefix(-3); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 126 | m_signer.wireDecode(dataName.get(-2).blockFromValue()); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 127 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 128 | ExtensionList::iterator it = extensionList_.begin(); |
| 129 | for(; it != extensionList_.end(); it++) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 130 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 131 | if(PROFILE_EXT_OID == it->getOid()) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 132 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 133 | boost::iostreams::stream<boost::iostreams::array_source> is |
| 134 | (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size()); |
| 135 | m_profile.decode(is); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 136 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 137 | if(ENDORSE_EXT_OID == it->getOid()) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 138 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 139 | Chronos::EndorseExtensionMsg endorseExtension; |
| 140 | |
| 141 | boost::iostreams::stream<boost::iostreams::array_source> is |
| 142 | (reinterpret_cast<const char*>(it->getValue().buf()), it->getValue().size()); |
| 143 | endorseExtension.ParseFromIstream(&is); |
| 144 | |
| 145 | endorseExtension >> m_endorseList; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 146 | } |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 149 | |
| 150 | }//chronos |