Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame^] | 2 | /* |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 25 | #include "public-key.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 27 | #include "../../encoding/oid.hpp" |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame^] | 28 | #include "../../util/digest.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 29 | #include "cryptopp.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 30 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 31 | namespace ndn { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 32 | namespace security { |
| 33 | namespace v1 { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 35 | PublicKey::PublicKey() |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 36 | : m_type(KeyType::NONE) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 37 | { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 38 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 41 | : m_type(KeyType::NONE) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 42 | { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 43 | CryptoPP::StringSource src(keyDerBuf, keyDerSize, true); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 44 | decode(src); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 47 | const Block& |
| 48 | PublicKey::computeDigest() const |
| 49 | { |
| 50 | if (m_key.empty()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 51 | BOOST_THROW_EXCEPTION(Error("Public key is empty")); |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 52 | |
| 53 | if (m_digest.hasWire()) |
| 54 | return m_digest; |
| 55 | else { |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame^] | 56 | m_digest = Block(tlv::KeyDigest, util::Sha256::computeDigest(m_key.buf(), m_key.size())); |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 57 | m_digest.encode(); |
| 58 | return m_digest; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 63 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 64 | PublicKey::encode(CryptoPP::BufferedTransformation& out) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 65 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 66 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 67 | // algorithm AlgorithmIdentifier |
| 68 | // keybits BIT STRING } |
| 69 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 70 | out.Put(m_key.buf(), m_key.size()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 74 | PublicKey::decode(CryptoPP::BufferedTransformation& in) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 75 | { |
| 76 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 77 | // algorithm AlgorithmIdentifier |
| 78 | // keybits BIT STRING } |
| 79 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 80 | using namespace CryptoPP; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 81 | try |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 82 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 83 | std::string out; |
| 84 | StringSink sink(out); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 85 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 86 | //////////////////////// |
| 87 | // part 1: copy as is // |
| 88 | //////////////////////// |
| 89 | BERSequenceDecoder decoder(in); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 90 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 91 | assert(decoder.IsDefiniteLength()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 92 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 93 | DERSequenceEncoder encoder(sink); |
| 94 | decoder.TransferTo(encoder, decoder.RemainingLength()); |
| 95 | encoder.MessageEnd(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 96 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 97 | decoder.MessageEnd(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 98 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 99 | //////////////////////// |
| 100 | // part 2: check if the key is RSA (since it is the only supported for now) |
| 101 | //////////////////////// |
| 102 | StringSource checkedSource(out, true); |
| 103 | BERSequenceDecoder subjectPublicKeyInfo(checkedSource); |
| 104 | { |
| 105 | BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo); |
| 106 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 107 | Oid algorithm; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 108 | algorithm.decode(algorithmInfo); |
| 109 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 110 | if (algorithm == oid::RSA) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 111 | m_type = KeyType::RSA; |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 112 | else if (algorithm == oid::ECDSA) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 113 | m_type = KeyType::EC; |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 114 | else |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(Error("Only RSA/EC public keys are supported for now (" + |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 116 | algorithm.toString() + " requested)")); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | m_key.assign(out.begin(), out.end()); |
| 121 | } |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame^] | 122 | catch (const CryptoPP::BERDecodeErr& err) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 123 | { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 124 | m_type = KeyType::NONE; |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 125 | BOOST_THROW_EXCEPTION(Error("PublicKey decoding error")); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 126 | } |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 127 | |
| 128 | m_digest.reset(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Blob |
| 132 | // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 133 | // { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 134 | // if (digestAlgorithm == DigestAlgorithm::SHA256) { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 135 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 136 | // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 138 | // return Blob(digest, sizeof(digest)); |
| 139 | // } |
| 140 | // else |
| 141 | // throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 142 | // } |
| 143 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 144 | std::ostream& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 145 | operator<<(std::ostream& os, const PublicKey& key) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 146 | { |
| 147 | CryptoPP::StringSource(key.get().buf(), key.get().size(), true, |
| 148 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64)); |
| 149 | |
| 150 | return os; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 153 | } // namespace v1 |
| 154 | } // namespace security |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 155 | } // namespace ndn |