blob: fc41a94b97f097f68671bb6473c643ba3505eae6 [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -07001/* -*- 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 Afanasyev8e96e582013-11-19 12:07:04 -08009#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>
14namespace ndnboost = boost;
15#else
Jeff Thompson07996712013-10-17 17:31:40 -070016// 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 Afanasyev8e96e582013-11-19 12:07:04 -080019#endif
20
21#include <ndn-cpp/security/security-exception.hpp>
Jeff Thompsonc0573432013-09-19 17:41:36 -070022#include "../../c/util/crypto.h"
Jeff Thompson07996712013-10-17 17:31:40 -070023#include "../../encoding/der/der.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070024#include <ndn-cpp/security/certificate/public-key.hpp>
Jeff Thompsonc0573432013-09-19 17:41:36 -070025
26using namespace std;
Jeff Thompsonc0573432013-09-19 17:41:36 -070027
28namespace ndn {
29
Jeff Thompsonce115762013-12-18 14:59:56 -080030ptr_lib::shared_ptr<der::DerNode>
Jeff Thompson07996712013-10-17 17:31:40 -070031PublicKey::toDer()
Jeff Thompsonc0573432013-09-19 17:41:36 -070032{
Jeff Thompson07996712013-10-17 17:31:40 -070033 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)keyDer_.buf (), keyDer_.size ());
Jeff Thompsonc0573432013-09-19 17:41:36 -070034
Jeff Thompson07996712013-10-17 17:31:40 -070035 return der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is));
Jeff Thompsonc0573432013-09-19 17:41:36 -070036}
Jeff Thompsonc0573432013-09-19 17:41:36 -070037
38static int RSA_OID[] = { 1, 2, 840, 113549, 1, 1, 1 };
39
Jeff Thompsonce115762013-12-18 14:59:56 -080040ptr_lib::shared_ptr<PublicKey>
Jeff Thompsonc0573432013-09-19 17:41:36 -070041PublicKey::fromDer(const Blob& keyDer)
42{
43 // Use a temporary pointer since d2i updates it.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070044 const uint8_t *derPointer = keyDer.buf();
Jeff Thompsonc0573432013-09-19 17:41:36 -070045 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 Thompsonce115762013-12-18 14:59:56 -080050 return ptr_lib::shared_ptr<PublicKey>(new PublicKey(OID(vector<int>(RSA_OID, RSA_OID + sizeof(RSA_OID))), keyDer));
Jeff Thompsonc0573432013-09-19 17:41:36 -070051}
52
53Blob
54PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
55{
56 if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
Jeff Thompson10ad12a2013-09-24 16:19:11 -070057 uint8_t digest[SHA256_DIGEST_LENGTH];
Jeff Thompsonc0573432013-09-19 17:41:36 -070058 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}