Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
| 13 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 16 | #include "common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 17 | #include "public-key.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 18 | #include "cryptopp.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 19 | |
| 20 | using namespace std; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 21 | using namespace CryptoPP; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 22 | |
| 23 | namespace ndn { |
| 24 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 25 | static OID RSA_OID("1.2.840.113549.1.1.1"); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 27 | PublicKey::PublicKey() |
| 28 | { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 29 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Create a new PublicKey with the given values. |
| 33 | * @param algorithm The algorithm of the public key. |
| 34 | * @param keyDer The blob of the PublicKeyInfo in terms of DER. |
| 35 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 37 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 38 | StringSource src(keyDerBuf, keyDerSize, true); |
| 39 | decode(src); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 42 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | PublicKey::encode(CryptoPP::BufferedTransformation& out) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 45 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 46 | // algorithm AlgorithmIdentifier |
| 47 | // keybits BIT STRING } |
| 48 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 49 | out.Put(m_key.buf(), m_key.size()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | PublicKey::decode(CryptoPP::BufferedTransformation& in) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 54 | { |
| 55 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 56 | // algorithm AlgorithmIdentifier |
| 57 | // keybits BIT STRING } |
| 58 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 59 | try |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 60 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 61 | std::string out; |
| 62 | StringSink sink(out); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 63 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 64 | //////////////////////// |
| 65 | // part 1: copy as is // |
| 66 | //////////////////////// |
| 67 | BERSequenceDecoder decoder(in); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 68 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 69 | assert(decoder.IsDefiniteLength()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 70 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 71 | DERSequenceEncoder encoder(sink); |
| 72 | decoder.TransferTo(encoder, decoder.RemainingLength()); |
| 73 | encoder.MessageEnd(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 74 | } |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 75 | decoder.MessageEnd(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 76 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 77 | //////////////////////// |
| 78 | // part 2: check if the key is RSA (since it is the only supported for now) |
| 79 | //////////////////////// |
| 80 | StringSource checkedSource(out, true); |
| 81 | BERSequenceDecoder subjectPublicKeyInfo(checkedSource); |
| 82 | { |
| 83 | BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo); |
| 84 | { |
| 85 | OID algorithm; |
| 86 | algorithm.decode(algorithmInfo); |
| 87 | |
| 88 | if (algorithm != RSA_OID) |
| 89 | throw Error("Only RSA public keys are supported for now (" + |
| 90 | algorithm.toString() + " requested)"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | m_key.assign(out.begin(), out.end()); |
| 95 | } |
| 96 | catch (CryptoPP::BERDecodeErr& err) |
| 97 | { |
| 98 | throw Error("PublicKey decoding error"); |
| 99 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Blob |
| 103 | // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 104 | // { |
| 105 | // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
| 106 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 107 | // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 109 | // return Blob(digest, sizeof(digest)); |
| 110 | // } |
| 111 | // else |
| 112 | // throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 113 | // } |
| 114 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 115 | std::ostream& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 116 | operator<<(std::ostream& os, const PublicKey& key) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 117 | { |
| 118 | CryptoPP::StringSource(key.get().buf(), key.get().size(), true, |
| 119 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64)); |
| 120 | |
| 121 | return os; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 124 | } // namespace ndn |