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> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 12 | #include "endorse-certificate.hpp" |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 13 | #include <boost/iostreams/stream.hpp> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 14 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 15 | #include "endorse-extension.hpp" |
| 16 | #include <list> |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 17 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 18 | namespace chronochat { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 19 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 20 | using std::vector; |
| 21 | using std::string; |
| 22 | |
| 23 | using ndn::PublicKey; |
| 24 | using ndn::IdentityCertificate; |
| 25 | using ndn::CertificateSubjectDescription; |
| 26 | using ndn::CertificateExtension; |
| 27 | using ndn::OID; |
| 28 | using ndn::OBufferStream; |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 30 | const OID EndorseCertificate::PROFILE_EXT_OID("1.3.6.1.5.32.2.1"); |
| 31 | 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] | 32 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 33 | const vector<string> EndorseCertificate::DEFAULT_ENDORSE_LIST; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 34 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 35 | EndorseExtension& |
| 36 | operator<<(EndorseExtension& endorseExtension, const vector<string>& endorseList) |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 37 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 38 | for (const auto& entry : endorseList) |
| 39 | endorseExtension.addEntry(entry); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 40 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 41 | return endorseExtension; |
| 42 | } |
| 43 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 44 | EndorseExtension& |
| 45 | operator>>(EndorseExtension& endorseExtension, vector<string>& endorseList) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 46 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 47 | const std::list<string>& endorseEntries = endorseExtension.getEntries(); |
| 48 | for (const auto& entry: endorseEntries) |
| 49 | endorseList.push_back(entry); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 50 | |
| 51 | return endorseExtension; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 54 | EndorseCertificate::EndorseCertificate(const IdentityCertificate& kskCertificate, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 55 | const Profile& profile, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 56 | const vector<string>& endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 57 | : Certificate() |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 58 | , m_profile(profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 59 | , m_endorseList(endorseList) |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 60 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 61 | m_keyName = IdentityCertificate::certificateNameToPublicKeyName(kskCertificate.getName()); |
| 62 | m_signer = m_keyName; |
| 63 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 64 | Name dataName = m_keyName; |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 65 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 66 | setName(dataName); |
| 67 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 68 | setNotBefore(kskCertificate.getNotBefore()); |
| 69 | setNotAfter(kskCertificate.getNotAfter()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 70 | addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri())); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 71 | setPublicKeyInfo(kskCertificate.getPublicKeyInfo()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 72 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 73 | Block profileWire = m_profile.wireEncode(); |
| 74 | addExtension(CertificateExtension(PROFILE_EXT_OID, true, ndn::Buffer(profileWire.wire(), |
| 75 | profileWire.size()))); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 76 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 77 | EndorseExtension endorseExtension; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 78 | endorseExtension << m_endorseList; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 79 | Block endorseWire = endorseExtension.wireEncode(); |
| 80 | addExtension(CertificateExtension(ENDORSE_EXT_OID, true, ndn::Buffer(endorseWire.wire(), |
| 81 | endorseWire.size()))); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 82 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 83 | encode(); |
| 84 | } |
| 85 | |
| 86 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate, |
| 87 | const Name& signer, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 88 | const vector<string>& endorseList) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 89 | : Certificate() |
| 90 | , m_keyName(endorseCertificate.m_keyName) |
| 91 | , m_signer(signer) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 92 | , m_profile(endorseCertificate.m_profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 93 | , m_endorseList(endorseList) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 94 | { |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 95 | Name dataName = m_keyName; |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 96 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 97 | setName(dataName); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 98 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 99 | setNotBefore(endorseCertificate.getNotBefore()); |
| 100 | setNotAfter(endorseCertificate.getNotAfter()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 101 | addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri())); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 102 | setPublicKeyInfo(endorseCertificate.getPublicKeyInfo()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 103 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 104 | Block profileWire = m_profile.wireEncode(); |
| 105 | addExtension(CertificateExtension(PROFILE_EXT_OID, true, ndn::Buffer(profileWire.wire(), |
| 106 | profileWire.size()))); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 107 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 108 | EndorseExtension endorseExtension; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 109 | endorseExtension << m_endorseList; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 110 | Block endorseWire = endorseExtension.wireEncode(); |
| 111 | addExtension(CertificateExtension(ENDORSE_EXT_OID, true, ndn::Buffer(endorseWire.wire(), |
| 112 | endorseWire.size()))); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 113 | |
| 114 | encode(); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 117 | EndorseCertificate::EndorseCertificate(const Name& keyName, |
| 118 | const PublicKey& key, |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 119 | const time::system_clock::TimePoint& notBefore, |
| 120 | const time::system_clock::TimePoint& notAfter, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 121 | const Name& signer, |
| 122 | const Profile& profile, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 123 | const vector<string>& endorseList) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 124 | : Certificate() |
| 125 | , m_keyName(keyName) |
| 126 | , m_signer(signer) |
| 127 | , m_profile(profile) |
| 128 | , m_endorseList(endorseList) |
| 129 | { |
| 130 | Name dataName = m_keyName; |
| 131 | dataName.append("PROFILE-CERT").append(m_signer.wireEncode()).appendVersion(); |
| 132 | setName(dataName); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 133 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | setNotBefore(notBefore); |
| 135 | setNotAfter(notAfter); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 136 | addSubjectDescription(CertificateSubjectDescription(OID("2.5.4.41"), m_keyName.toUri())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 137 | setPublicKeyInfo(key); |
| 138 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 139 | Block profileWire = m_profile.wireEncode(); |
| 140 | addExtension(CertificateExtension(PROFILE_EXT_OID, true, ndn::Buffer(profileWire.wire(), |
| 141 | profileWire.size()))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 142 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 143 | EndorseExtension endorseExtension; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 144 | endorseExtension << m_endorseList; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 145 | Block endorseWire = endorseExtension.wireEncode(); |
| 146 | addExtension(CertificateExtension(ENDORSE_EXT_OID, true, ndn::Buffer(endorseWire.wire(), |
| 147 | endorseWire.size()))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 148 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 149 | encode(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 152 | EndorseCertificate::EndorseCertificate(const EndorseCertificate& endorseCertificate) |
| 153 | : Certificate(endorseCertificate) |
| 154 | , m_keyName(endorseCertificate.m_keyName) |
| 155 | , m_signer(endorseCertificate.m_signer) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 156 | , m_profile(endorseCertificate.m_profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 157 | , m_endorseList(endorseCertificate.m_endorseList) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 158 | { |
| 159 | } |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 160 | |
| 161 | EndorseCertificate::EndorseCertificate(const Data& data) |
| 162 | : Certificate(data) |
| 163 | { |
| 164 | const Name& dataName = data.getName(); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 165 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 166 | if(dataName.size() < 3 || dataName.get(-3).toUri() != "PROFILE-CERT") |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 167 | throw Error("No PROFILE-CERT component in data name!"); |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 168 | |
| 169 | m_keyName = dataName.getPrefix(-3); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 170 | m_signer.wireDecode(dataName.get(-2).blockFromValue()); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 171 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 172 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 173 | for (const auto& entry : m_extensionList) { |
| 174 | if (PROFILE_EXT_OID == entry.getOid()) { |
| 175 | m_profile.wireDecode(Block(entry.getValue().buf(), entry.getValue().size())); |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 176 | } |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 177 | if (ENDORSE_EXT_OID == entry.getOid()) { |
| 178 | EndorseExtension endorseExtension; |
| 179 | endorseExtension.wireDecode(Block(entry.getValue().buf(), entry.getValue().size())); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 180 | |
| 181 | endorseExtension >> m_endorseList; |
| 182 | } |
| 183 | } |
Yingdi Yu | c972c45 | 2013-10-11 11:02:09 -0700 | [diff] [blame] | 184 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 185 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 186 | } // namespace chronochat |