NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 
34  : m_type(KEY_TYPE_NULL)
35 {
36 }
37 
38 PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize)
39  : m_type(KEY_TYPE_NULL)
40 {
41  CryptoPP::StringSource src(keyDerBuf, keyDerSize, true);
42  decode(src);
43 }
44 
45 const Block&
47 {
48  if (m_key.empty())
49  BOOST_THROW_EXCEPTION(Error("Public key is empty"));
50 
51  if (m_digest.hasWire())
52  return m_digest;
53  else {
54  m_digest = Block(tlv::KeyDigest, crypto::sha256(m_key.buf(), m_key.size()));
55  m_digest.encode();
56  return m_digest;
57  }
58 }
59 
60 
61 void
62 PublicKey::encode(CryptoPP::BufferedTransformation& out) const
63 {
64  // SubjectPublicKeyInfo ::= SEQUENCE {
65  // algorithm AlgorithmIdentifier
66  // keybits BIT STRING }
67 
68  out.Put(m_key.buf(), m_key.size());
69 }
70 
71 void
72 PublicKey::decode(CryptoPP::BufferedTransformation& in)
73 {
74  // SubjectPublicKeyInfo ::= SEQUENCE {
75  // algorithm AlgorithmIdentifier
76  // keybits BIT STRING }
77 
78  using namespace CryptoPP;
79  try
80  {
81  std::string out;
82  StringSink sink(out);
83 
85  // part 1: copy as is //
87  BERSequenceDecoder decoder(in);
88  {
89  assert(decoder.IsDefiniteLength());
90 
91  DERSequenceEncoder encoder(sink);
92  decoder.TransferTo(encoder, decoder.RemainingLength());
93  encoder.MessageEnd();
94  }
95  decoder.MessageEnd();
96 
98  // part 2: check if the key is RSA (since it is the only supported for now)
100  StringSource checkedSource(out, true);
101  BERSequenceDecoder subjectPublicKeyInfo(checkedSource);
102  {
103  BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo);
104  {
105  OID algorithm;
106  algorithm.decode(algorithmInfo);
107 
108  if (algorithm == oid::RSA)
109  m_type = KEY_TYPE_RSA;
110  else if (algorithm == oid::ECDSA)
111  m_type = KEY_TYPE_ECDSA;
112  else
113  BOOST_THROW_EXCEPTION(Error("Only RSA/ECDSA public keys are supported for now (" +
114  algorithm.toString() + " requested)"));
115  }
116  }
117 
118  m_key.assign(out.begin(), out.end());
119  }
120  catch (CryptoPP::BERDecodeErr& err)
121  {
122  m_type = KEY_TYPE_NULL;
123  BOOST_THROW_EXCEPTION(Error("PublicKey decoding error"));
124  }
125 
126  m_digest.reset();
127 }
128 
129 // Blob
130 // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
131 // {
132 // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
133 // uint8_t digest[SHA256_DIGEST_LENGTH];
134 // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
135 
136 // return Blob(digest, sizeof(digest));
137 // }
138 // else
139 // throw UnrecognizedDigestAlgorithmException("Wrong format!");
140 // }
141 
142 std::ostream&
143 operator<<(std::ostream& os, const PublicKey& key)
144 {
145  CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
146  new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
147 
148  return os;
149 }
150 
151 } // namespace ndn
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.
Copyright (c) 2013-2014 Regents of the University of California.
Definition: oid.hpp:29
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:320
PublicKey()
The default constructor.
Definition: public-key.cpp:33
void encode(CryptoPP::BufferedTransformation &out) const
Definition: public-key.cpp:62
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void decode(CryptoPP::BufferedTransformation &in)
Definition: oid.cpp:147
uint8_t * buf()
Definition: buffer.hpp:87
const Block & computeDigest() const
Definition: public-key.cpp:46
void decode(CryptoPP::BufferedTransformation &in)
Definition: public-key.cpp:72
Definition: oid.hpp:35
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:355
const Buffer & get() const
Definition: public-key.hpp:70
std::string toString() const
Definition: oid.cpp:70
ConstBufferPtr sha256(const uint8_t *data, size_t dataLength)
Compute the sha-256 digest of data.
Definition: crypto.cpp:51
const OID ECDSA("1.2.840.10045.2.1")
Definition: oid.hpp:102
const OID RSA("1.2.840.113549.1.1.1")
Definition: oid.hpp:101