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