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> |
| 10 | |
| 11 | #if NDN_CPP_USE_SYSTEM_BOOST |
| 12 | #include <boost/iostreams/stream.hpp> |
| 13 | #include <boost/iostreams/device/array.hpp> |
| 14 | namespace ndnboost = boost; |
| 15 | #else |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 16 | // We can use ndnboost::iostreams because this is internal and will not conflict with the application if it uses boost::iostreams. |
| 17 | #include <ndnboost/iostreams/stream.hpp> |
| 18 | #include <ndnboost/iostreams/device/array.hpp> |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 19 | #endif |
| 20 | |
| 21 | #include <ndn-cpp/security/security-exception.hpp> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 22 | #include "../../c/util/crypto.h" |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 23 | #include "../../encoding/der/der.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 24 | #include <ndn-cpp/security/certificate/public-key.hpp> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 30 | ptr_lib::shared_ptr<der::DerNode> |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 31 | PublicKey::toDer() |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 33 | ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)keyDer_.buf (), keyDer_.size ()); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 35 | return der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is)); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 36 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 37 | |
| 38 | static int RSA_OID[] = { 1, 2, 840, 113549, 1, 1, 1 }; |
| 39 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 40 | ptr_lib::shared_ptr<PublicKey> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 41 | PublicKey::fromDer(const Blob& keyDer) |
| 42 | { |
| 43 | // Use a temporary pointer since d2i updates it. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 44 | const uint8_t *derPointer = keyDer.buf(); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 45 | RSA *publicKey = d2i_RSA_PUBKEY(NULL, &derPointer, keyDer.size()); |
| 46 | if (!publicKey) |
| 47 | throw UnrecognizedKeyFormatException("Error decoding public key DER"); |
| 48 | RSA_free(publicKey); |
| 49 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 50 | return ptr_lib::shared_ptr<PublicKey>(new PublicKey(OID(vector<int>(RSA_OID, RSA_OID + sizeof(RSA_OID))), keyDer)); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Blob |
| 54 | PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 55 | { |
| 56 | if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 57 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 58 | ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
| 59 | |
| 60 | return Blob(digest, sizeof(digest)); |
| 61 | } |
| 62 | else |
| 63 | throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 64 | } |
| 65 | |
| 66 | } |