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