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 | |
| 9 | #include "../security-exception.hpp" |
| 10 | #include "../../c/util/crypto.h" |
| 11 | #include "public-key.hpp" |
| 12 | |
| 13 | using namespace std; |
| 14 | using namespace ndn::ptr_lib; |
| 15 | |
| 16 | namespace ndn { |
| 17 | |
| 18 | #if 0 |
| 19 | Ptr<der::DerNode> |
| 20 | PublicKey::toDER() |
| 21 | { |
| 22 | boost::iostreams::stream |
| 23 | <boost::iostreams::array_source> is (m_key.buf (), m_key.size ()); |
| 24 | |
| 25 | return der::DerNode::parse(reinterpret_cast<InputIterator &> (is)); |
| 26 | } |
| 27 | #endif |
| 28 | |
| 29 | static int RSA_OID[] = { 1, 2, 840, 113549, 1, 1, 1 }; |
| 30 | |
| 31 | shared_ptr<PublicKey> |
| 32 | PublicKey::fromDer(const Blob& keyDer) |
| 33 | { |
| 34 | // Use a temporary pointer since d2i updates it. |
| 35 | const unsigned char *derPointer = keyDer.buf(); |
| 36 | RSA *publicKey = d2i_RSA_PUBKEY(NULL, &derPointer, keyDer.size()); |
| 37 | if (!publicKey) |
| 38 | throw UnrecognizedKeyFormatException("Error decoding public key DER"); |
| 39 | RSA_free(publicKey); |
| 40 | |
| 41 | return shared_ptr<PublicKey>(new PublicKey(OID(vector<int>(RSA_OID, RSA_OID + sizeof(RSA_OID))), keyDer)); |
| 42 | } |
| 43 | |
| 44 | Blob |
| 45 | PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const |
| 46 | { |
| 47 | if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) { |
| 48 | unsigned char digest[SHA256_DIGEST_LENGTH]; |
| 49 | ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest); |
| 50 | |
| 51 | return Blob(digest, sizeof(digest)); |
| 52 | } |
| 53 | else |
| 54 | throw UnrecognizedDigestAlgorithmException("Wrong format!"); |
| 55 | } |
| 56 | |
| 57 | } |