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