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