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