NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
public-key.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #include "public-key.hpp"
26 
27 #include "../../encoding/oid.hpp"
28 #include "../../util/crypto.hpp"
29 #include "cryptopp.hpp"
30 
31 namespace ndn {
32 namespace security {
33 namespace v1 {
34 
36  : m_type(KeyType::NONE)
37 {
38 }
39 
40 PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize)
41  : m_type(KeyType::NONE)
42 {
43  CryptoPP::StringSource src(keyDerBuf, keyDerSize, true);
44  decode(src);
45 }
46 
47 const Block&
49 {
50  if (m_key.empty())
51  BOOST_THROW_EXCEPTION(Error("Public key is empty"));
52 
53  if (m_digest.hasWire())
54  return m_digest;
55  else {
56  m_digest = Block(tlv::KeyDigest, crypto::computeSha256Digest(m_key.buf(), m_key.size()));
57  m_digest.encode();
58  return m_digest;
59  }
60 }
61 
62 
63 void
64 PublicKey::encode(CryptoPP::BufferedTransformation& out) const
65 {
66  // SubjectPublicKeyInfo ::= SEQUENCE {
67  // algorithm AlgorithmIdentifier
68  // keybits BIT STRING }
69 
70  out.Put(m_key.buf(), m_key.size());
71 }
72 
73 void
74 PublicKey::decode(CryptoPP::BufferedTransformation& in)
75 {
76  // SubjectPublicKeyInfo ::= SEQUENCE {
77  // algorithm AlgorithmIdentifier
78  // keybits BIT STRING }
79 
80  using namespace CryptoPP;
81  try
82  {
83  std::string out;
84  StringSink sink(out);
85 
87  // part 1: copy as is //
89  BERSequenceDecoder decoder(in);
90  {
91  assert(decoder.IsDefiniteLength());
92 
93  DERSequenceEncoder encoder(sink);
94  decoder.TransferTo(encoder, decoder.RemainingLength());
95  encoder.MessageEnd();
96  }
97  decoder.MessageEnd();
98 
100  // part 2: check if the key is RSA (since it is the only supported for now)
102  StringSource checkedSource(out, true);
103  BERSequenceDecoder subjectPublicKeyInfo(checkedSource);
104  {
105  BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo);
106  {
107  Oid algorithm;
108  algorithm.decode(algorithmInfo);
109 
110  if (algorithm == oid::RSA)
111  m_type = KeyType::RSA;
112  else if (algorithm == oid::ECDSA)
113  m_type = KeyType::EC;
114  else
115  BOOST_THROW_EXCEPTION(Error("Only RSA/ECDSA public keys are supported for now (" +
116  algorithm.toString() + " requested)"));
117  }
118  }
119 
120  m_key.assign(out.begin(), out.end());
121  }
122  catch (CryptoPP::BERDecodeErr& err)
123  {
124  m_type = KeyType::NONE;
125  BOOST_THROW_EXCEPTION(Error("PublicKey decoding error"));
126  }
127 
128  m_digest.reset();
129 }
130 
131 // Blob
132 // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
133 // {
134 // if (digestAlgorithm == DigestAlgorithm::SHA256) {
135 // uint8_t digest[SHA256_DIGEST_LENGTH];
136 // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
137 
138 // return Blob(digest, sizeof(digest));
139 // }
140 // else
141 // throw UnrecognizedDigestAlgorithmException("Wrong format!");
142 // }
143 
144 std::ostream&
145 operator<<(std::ostream& os, const PublicKey& key)
146 {
147  CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
148  new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
149 
150  return os;
151 }
152 
153 } // namespace v1
154 } // namespace security
155 } // namespace ndn
void decode(CryptoPP::BufferedTransformation &in)
Definition: oid.cpp:135
const Oid ECDSA("1.2.840.10045.2.1")
Definition: oid.hpp:102
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
const Buffer & get() const
Definition: public-key.hpp:72
Copyright (c) 2013-2016 Regents of the University of California.
Definition: oid.hpp:29
std::ostream & operator<<(std::ostream &os, const Certificate &cert)
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
PublicKey()
The default constructor.
Definition: public-key.cpp:35
uint8_t * buf()
Definition: buffer.hpp:87
const Oid RSA("1.2.840.113549.1.1.1")
Definition: oid.hpp:101
std::string toString() const
Definition: oid.cpp:58
Definition: oid.hpp:35
const Block & computeDigest() const
Definition: public-key.cpp:48
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:355
ConstBufferPtr computeSha256Digest(const uint8_t *data, size_t dataLength)
Compute the sha-256 digest of data.
Definition: crypto.cpp:31
void decode(CryptoPP::BufferedTransformation &in)
Definition: public-key.cpp:74
void encode(CryptoPP::BufferedTransformation &out) const
Definition: public-key.cpp:64