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 | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 31 | PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 33 | StringSource src(keyDerBuf, keyDerSize, true); |
| 34 | decode(src); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 37 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | PublicKey::encode(CryptoPP::BufferedTransformation& out) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 40 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 41 | // algorithm AlgorithmIdentifier |
| 42 | // keybits BIT STRING } |
| 43 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 44 | out.Put(m_key.buf(), m_key.size()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | PublicKey::decode(CryptoPP::BufferedTransformation& in) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 49 | { |
| 50 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 51 | // algorithm AlgorithmIdentifier |
| 52 | // keybits BIT STRING } |
| 53 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 54 | try |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 55 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 56 | std::string out; |
| 57 | StringSink sink(out); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 58 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 59 | //////////////////////// |
| 60 | // part 1: copy as is // |
| 61 | //////////////////////// |
| 62 | BERSequenceDecoder decoder(in); |
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 | assert(decoder.IsDefiniteLength()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 65 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 66 | DERSequenceEncoder encoder(sink); |
| 67 | decoder.TransferTo(encoder, decoder.RemainingLength()); |
| 68 | encoder.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 | decoder.MessageEnd(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 71 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 72 | //////////////////////// |
| 73 | // part 2: check if the key is RSA (since it is the only supported for now) |
| 74 | //////////////////////// |
| 75 | StringSource checkedSource(out, true); |
| 76 | BERSequenceDecoder subjectPublicKeyInfo(checkedSource); |
| 77 | { |
| 78 | BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo); |
| 79 | { |
| 80 | OID algorithm; |
| 81 | algorithm.decode(algorithmInfo); |
| 82 | |
| 83 | if (algorithm != RSA_OID) |
| 84 | throw Error("Only RSA public keys are supported for now (" + |
| 85 | algorithm.toString() + " requested)"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | m_key.assign(out.begin(), out.end()); |
| 90 | } |
| 91 | catch (CryptoPP::BERDecodeErr& err) |
| 92 | { |
| 93 | throw Error("PublicKey decoding error"); |
| 94 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Blob |
| 98 | // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 99 | // { |
| 100 | // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
| 101 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 102 | // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 104 | // return Blob(digest, sizeof(digest)); |
| 105 | // } |
| 106 | // else |
| 107 | // throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 108 | // } |
| 109 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 110 | std::ostream& |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 111 | operator<<(std::ostream& os, const PublicKey& key) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 112 | { |
| 113 | CryptoPP::StringSource(key.get().buf(), key.get().size(), true, |
| 114 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64)); |
| 115 | |
| 116 | return os; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 119 | } // namespace ndn |