blob: f57f5a68372e31ea2b35c557e840473477559e96 [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
Jeff Thompson07996712013-10-17 17:31:40 -07009// 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 Thompson25b4e612013-10-10 16:03:24 -070012#include <ndn-cpp/security//security-exception.hpp>
Jeff Thompsonc0573432013-09-19 17:41:36 -070013#include "../../c/util/crypto.h"
Jeff Thompson07996712013-10-17 17:31:40 -070014#include "../../encoding/der/der.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070015#include <ndn-cpp/security/certificate/public-key.hpp>
Jeff Thompsonc0573432013-09-19 17:41:36 -070016
17using namespace std;
Jeff Thompsonc0573432013-09-19 17:41:36 -070018
19namespace ndn {
20
Jeff Thompsonce115762013-12-18 14:59:56 -080021ptr_lib::shared_ptr<der::DerNode>
Jeff Thompson07996712013-10-17 17:31:40 -070022PublicKey::toDer()
Jeff Thompsonc0573432013-09-19 17:41:36 -070023{
Jeff Thompson07996712013-10-17 17:31:40 -070024 ndnboost::iostreams::stream<ndnboost::iostreams::array_source> is((const char*)keyDer_.buf (), keyDer_.size ());
Jeff Thompsonc0573432013-09-19 17:41:36 -070025
Jeff Thompson07996712013-10-17 17:31:40 -070026 return der::DerNode::parse(reinterpret_cast<der::InputIterator&> (is));
Jeff Thompsonc0573432013-09-19 17:41:36 -070027}
Jeff Thompsonc0573432013-09-19 17:41:36 -070028
29static int RSA_OID[] = { 1, 2, 840, 113549, 1, 1, 1 };
30
Jeff Thompsonce115762013-12-18 14:59:56 -080031ptr_lib::shared_ptr<PublicKey>
Jeff Thompsonc0573432013-09-19 17:41:36 -070032PublicKey::fromDer(const Blob& keyDer)
33{
34 // Use a temporary pointer since d2i updates it.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070035 const uint8_t *derPointer = keyDer.buf();
Jeff Thompsonc0573432013-09-19 17:41:36 -070036 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
Jeff Thompsonce115762013-12-18 14:59:56 -080041 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 -070042}
43
44Blob
45PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
46{
47 if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
Jeff Thompson10ad12a2013-09-24 16:19:11 -070048 uint8_t digest[SHA256_DIGEST_LENGTH];
Jeff Thompsonc0573432013-09-19 17:41:36 -070049 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}