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