Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
| 27 | #include "../encoding/oid.hpp" |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 28 | #include "../util/crypto.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 { |
| 32 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 33 | PublicKey::PublicKey() |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 34 | : m_type(KEY_TYPE_NULL) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 35 | { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 36 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize) |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 39 | : m_type(KEY_TYPE_NULL) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 40 | { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 41 | CryptoPP::StringSource src(keyDerBuf, keyDerSize, true); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 42 | decode(src); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 45 | const Block& |
| 46 | PublicKey::computeDigest() const |
| 47 | { |
| 48 | if (m_key.empty()) |
| 49 | throw Error("Public key is empty"); |
| 50 | |
| 51 | if (m_digest.hasWire()) |
| 52 | return m_digest; |
| 53 | else { |
| 54 | m_digest = Block(tlv::KeyDigest, crypto::sha256(m_key.buf(), m_key.size())); |
| 55 | m_digest.encode(); |
| 56 | return m_digest; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 61 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | PublicKey::encode(CryptoPP::BufferedTransformation& out) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 63 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 64 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 65 | // algorithm AlgorithmIdentifier |
| 66 | // keybits BIT STRING } |
| 67 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 68 | out.Put(m_key.buf(), m_key.size()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 72 | PublicKey::decode(CryptoPP::BufferedTransformation& in) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 73 | { |
| 74 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 75 | // algorithm AlgorithmIdentifier |
| 76 | // keybits BIT STRING } |
| 77 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 78 | using namespace CryptoPP; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 79 | try |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 80 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 81 | std::string out; |
| 82 | StringSink sink(out); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 83 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 84 | //////////////////////// |
| 85 | // part 1: copy as is // |
| 86 | //////////////////////// |
| 87 | BERSequenceDecoder decoder(in); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 88 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 89 | assert(decoder.IsDefiniteLength()); |
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 | DERSequenceEncoder encoder(sink); |
| 92 | decoder.TransferTo(encoder, decoder.RemainingLength()); |
| 93 | encoder.MessageEnd(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 94 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 95 | decoder.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 | //////////////////////// |
| 98 | // part 2: check if the key is RSA (since it is the only supported for now) |
| 99 | //////////////////////// |
| 100 | StringSource checkedSource(out, true); |
| 101 | BERSequenceDecoder subjectPublicKeyInfo(checkedSource); |
| 102 | { |
| 103 | BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo); |
| 104 | { |
| 105 | OID algorithm; |
| 106 | algorithm.decode(algorithmInfo); |
| 107 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 108 | if (algorithm == oid::RSA) |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 109 | m_type = KEY_TYPE_RSA; |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 110 | else if (algorithm == oid::ECDSA) |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 111 | m_type = KEY_TYPE_ECDSA; |
| 112 | else |
| 113 | throw Error("Only RSA/ECDSA public keys are supported for now (" + |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 114 | algorithm.toString() + " requested)"); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | m_key.assign(out.begin(), out.end()); |
| 119 | } |
| 120 | catch (CryptoPP::BERDecodeErr& err) |
| 121 | { |
Yingdi Yu | 2620b1c | 2014-06-12 15:32:57 -0700 | [diff] [blame] | 122 | m_type = KEY_TYPE_NULL; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 123 | throw Error("PublicKey decoding error"); |
| 124 | } |
Yingdi Yu | e36322a | 2014-11-04 14:16:54 -0800 | [diff] [blame] | 125 | |
| 126 | m_digest.reset(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Blob |
| 130 | // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 131 | // { |
| 132 | // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
| 133 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 134 | // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 136 | // return Blob(digest, sizeof(digest)); |
| 137 | // } |
| 138 | // else |
| 139 | // throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 140 | // } |
| 141 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 142 | std::ostream& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 143 | operator<<(std::ostream& os, const PublicKey& key) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 144 | { |
| 145 | CryptoPP::StringSource(key.get().buf(), key.get().size(), true, |
| 146 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64)); |
| 147 | |
| 148 | return os; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 151 | } // namespace ndn |