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