All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
public-key.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
9 #include "common.hpp"
10 #include "public-key.hpp"
11 #include "cryptopp.hpp"
12 
13 using namespace std;
14 using namespace CryptoPP;
15 
16 namespace ndn {
17 
18 static OID RSA_OID("1.2.840.113549.1.1.1");
19 
20 PublicKey::PublicKey()
21 {
22 }
23 
29 PublicKey::PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize)
30 {
31  StringSource src(keyDerBuf, keyDerSize, true);
32  decode(src);
33 }
34 
35 void
36 PublicKey::encode(CryptoPP::BufferedTransformation& out) const
37 {
38  // SubjectPublicKeyInfo ::= SEQUENCE {
39  // algorithm AlgorithmIdentifier
40  // keybits BIT STRING }
41 
42  out.Put(m_key.buf(), m_key.size());
43 }
44 
45 void
46 PublicKey::decode(CryptoPP::BufferedTransformation& in)
47 {
48  // SubjectPublicKeyInfo ::= SEQUENCE {
49  // algorithm AlgorithmIdentifier
50  // keybits BIT STRING }
51 
52  try
53  {
54  std::string out;
55  StringSink sink(out);
56 
58  // part 1: copy as is //
60  BERSequenceDecoder decoder(in);
61  {
62  assert(decoder.IsDefiniteLength());
63 
64  DERSequenceEncoder encoder(sink);
65  decoder.TransferTo(encoder, decoder.RemainingLength());
66  encoder.MessageEnd();
67  }
68  decoder.MessageEnd();
69 
71  // part 2: check if the key is RSA (since it is the only supported for now)
73  StringSource checkedSource(out, true);
74  BERSequenceDecoder subjectPublicKeyInfo(checkedSource);
75  {
76  BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo);
77  {
78  OID algorithm;
79  algorithm.decode(algorithmInfo);
80 
81  if (algorithm != RSA_OID)
82  throw Error("Only RSA public keys are supported for now (" +
83  algorithm.toString() + " requested)");
84  }
85  }
86 
87  m_key.assign(out.begin(), out.end());
88  }
89  catch (CryptoPP::BERDecodeErr& err)
90  {
91  throw Error("PublicKey decoding error");
92  }
93 }
94 
95 // Blob
96 // PublicKey::getDigest(DigestAlgorithm digestAlgorithm) const
97 // {
98 // if (digestAlgorithm == DIGEST_ALGORITHM_SHA256) {
99 // uint8_t digest[SHA256_DIGEST_LENGTH];
100 // ndn_digestSha256(keyDer_.buf(), keyDer_.size(), digest);
101 
102 // return Blob(digest, sizeof(digest));
103 // }
104 // else
105 // throw UnrecognizedDigestAlgorithmException("Wrong format!");
106 // }
107 
108 std::ostream&
109 operator<<(std::ostream& os, const PublicKey& key)
110 {
111  CryptoPP::StringSource(key.get().buf(), key.get().size(), true,
112  new CryptoPP::Base64Encoder(new CryptoPP::FileSink(os), true, 64));
113 
114  return os;
115 }
116 
117 } // namespace ndn
std::string toString() const
Definition: oid.cpp:50
static OID RSA_OID("1.2.840.113549.1.1.1")
void decode(CryptoPP::BufferedTransformation &in)
Definition: oid.cpp:124
uint8_t * buf()
Get pointer to the first byte of the buffer (alternative version)
Definition: buffer.hpp:86
Definition: oid.hpp:20
const Buffer & get() const
Definition: public-key.hpp:47
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.hpp:523