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 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 13 | #include <cryptopp/rsa.h> |
| 14 | #include <cryptopp/base64.h> |
| 15 | #include <cryptopp/files.h> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 16 | |
| 17 | using namespace std; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 18 | using namespace CryptoPP; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 19 | |
| 20 | namespace ndn { |
| 21 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 22 | static OID RSA_OID("1.2.840.113549.1.1.1"); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 24 | PublicKey::PublicKey() |
| 25 | { |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 26 | } |
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 | /** |
| 29 | * Create a new PublicKey with the given values. |
| 30 | * @param algorithm The algorithm of the public key. |
| 31 | * @param keyDer The blob of the PublicKeyInfo in terms of DER. |
| 32 | */ |
| 33 | PublicKey::PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize) |
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 | StringSource src(keyDerBuf, keyDerSize, true); |
| 36 | decode(src); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 39 | void |
| 40 | PublicKey::encode(CryptoPP::BufferedTransformation &out) const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 42 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 43 | // algorithm AlgorithmIdentifier |
| 44 | // keybits BIT STRING } |
| 45 | |
| 46 | out.Put(key_.buf(), key_.size()); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | PublicKey::decode(CryptoPP::BufferedTransformation &in) |
| 51 | { |
| 52 | // SubjectPublicKeyInfo ::= SEQUENCE { |
| 53 | // algorithm AlgorithmIdentifier |
| 54 | // keybits BIT STRING } |
| 55 | |
| 56 | try { |
| 57 | std::string out; |
| 58 | StringSink sink(out); |
| 59 | |
| 60 | //////////////////////// |
| 61 | // part 1: copy as is // |
| 62 | //////////////////////// |
| 63 | BERSequenceDecoder decoder(in); |
| 64 | { |
| 65 | assert (decoder.IsDefiniteLength()); |
| 66 | |
| 67 | DERSequenceEncoder encoder(sink); |
| 68 | decoder.TransferTo(encoder, decoder.RemainingLength()); |
| 69 | encoder.MessageEnd(); |
| 70 | } |
| 71 | decoder.MessageEnd(); |
| 72 | |
| 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 (" + algorithm.toString() + " requested"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | key_.assign(out.begin(), out.end()); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 91 | catch (CryptoPP::BERDecodeErr &err) { |
| 92 | throw Error("PublicKey decoding error"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Blob |
| 97 | // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 98 | // { |
| 99 | // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
| 100 | // uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 101 | // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
| 102 | |
| 103 | // return Blob(digest, sizeof(digest)); |
| 104 | // } |
| 105 | // else |
| 106 | // throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 107 | // } |
| 108 | |
| 109 | std::ostream & |
| 110 | operator <<(std::ostream &os, const PublicKey &key) |
| 111 | { |
| 112 | CryptoPP::StringSource(key.get().buf(), key.get().size(), true, |
| 113 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64)); |
| 114 | |
| 115 | return os; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } |