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 | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 9 | #include <ndn-cpp/common.hpp> |
Alexander Afanasyev | 6be1a6a | 2014-01-06 00:08:14 -0800 | [diff] [blame] | 10 | #include <ndn-cpp/security/certificate/public-key.hpp> |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 12 | #if NDN_CPP_USE_SYSTEM_BOOST |
| 13 | #include <boost/iostreams/stream.hpp> |
| 14 | #include <boost/iostreams/device/array.hpp> |
| 15 | namespace ndnboost = boost; |
| 16 | #else |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 17 | // We can use ndnboost::iostreams because this is internal and will not conflict with the application if it uses boost::iostreams. |
| 18 | #include <ndnboost/iostreams/stream.hpp> |
| 19 | #include <ndnboost/iostreams/device/array.hpp> |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 20 | #endif |
| 21 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 22 | #include <cryptopp/rsa.h> |
| 23 | #include <cryptopp/base64.h> |
| 24 | #include <cryptopp/files.h> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 27 | using namespace CryptoPP; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 31 | static OID RSA_OID("1.2.840.113549.1.1.1"); |
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 | PublicKey::PublicKey() |
| 34 | { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 35 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Create a new PublicKey with the given values. |
| 39 | * @param algorithm The algorithm of the public key. |
| 40 | * @param keyDer The blob of the PublicKeyInfo in terms of DER. |
| 41 | */ |
| 42 | PublicKey::PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize) |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 44 | StringSource src(keyDerBuf, keyDerSize, true); |
| 45 | decode(src); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 48 | void |
| 49 | PublicKey::encode(CryptoPP::BufferedTransformation &out) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 50 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 51 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 52 | // algorithm AlgorithmIdentifier |
| 53 | // keybits BIT STRING } |
| 54 | |
| 55 | out.Put(key_.buf(), key_.size()); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | PublicKey::decode(CryptoPP::BufferedTransformation &in) |
| 60 | { |
| 61 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 62 | // algorithm AlgorithmIdentifier |
| 63 | // keybits BIT STRING } |
| 64 | |
| 65 | try { |
| 66 | std::string out; |
| 67 | StringSink sink(out); |
| 68 | |
| 69 | //////////////////////// |
| 70 | // part 1: copy as is // |
| 71 | //////////////////////// |
| 72 | BERSequenceDecoder decoder(in); |
| 73 | { |
| 74 | assert (decoder.IsDefiniteLength()); |
| 75 | |
| 76 | DERSequenceEncoder encoder(sink); |
| 77 | decoder.TransferTo(encoder, decoder.RemainingLength()); |
| 78 | encoder.MessageEnd(); |
| 79 | } |
| 80 | decoder.MessageEnd(); |
| 81 | |
| 82 | //////////////////////// |
| 83 | // part 2: check if the key is RSA (since it is the only supported for now) |
| 84 | //////////////////////// |
| 85 | StringSource checkedSource(out, true); |
| 86 | BERSequenceDecoder subjectPublicKeyInfo(checkedSource); |
| 87 | { |
| 88 | BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo); |
| 89 | { |
| 90 | OID algorithm; |
| 91 | algorithm.decode(algorithmInfo); |
| 92 | |
| 93 | if (algorithm != RSA_OID) |
| 94 | throw Error("Only RSA public keys are supported for now (" + algorithm.toString() + " requested"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | key_.assign(out.begin(), out.end()); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 99 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 100 | catch (CryptoPP::BERDecodeErr &err) { |
| 101 | throw Error("PublicKey decoding error"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Blob |
| 106 | // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 107 | // { |
| 108 | // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
| 109 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 110 | // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
| 111 | |
| 112 | // return Blob(digest, sizeof(digest)); |
| 113 | // } |
| 114 | // else |
| 115 | // throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 116 | // } |
| 117 | |
| 118 | std::ostream & |
| 119 | operator <<(std::ostream &os, const PublicKey &key) |
| 120 | { |
| 121 | CryptoPP::StringSource(key.get().buf(), key.get().size(), true, |
| 122 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64)); |
| 123 | |
| 124 | return os; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } |