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