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