v1/public-key.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2017 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  *
21  * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
22  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
23  */
24 
25 #include "public-key.hpp"
26 
27 #include "../../encoding/oid.hpp"
28 #include "../../util/sha256.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, util::Sha256::computeDigest(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/EC public keys are supported for now (" +
116  algorithm.toString() + " requested)"));
117  }
118  }
119 
120  m_key.assign(out.begin(), out.end());
121  }
122  catch (const 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:96
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:75
const Buffer & get() const
const Block & computeDigest() const
Copyright (c) 2013-2017 Regents of the University of California.
Definition: oid.hpp:29
std::ostream & operator<<(std::ostream &os, const Certificate &cert)
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
void encode(CryptoPP::BufferedTransformation &out) const
PublicKey()
The default constructor.
uint8_t * buf()
Definition: buffer.hpp:87
const Oid RSA("1.2.840.113549.1.1.1")
Definition: oid.hpp:95
Definition: oid.hpp:35
void encode()
Encode sub elements into TLV-VALUE.
Definition: block.cpp:367
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:255
ConstBufferPtr computeDigest()
Finalize and return the digest based on all previously supplied inputs.
Definition: sha256.cpp:63
void decode(CryptoPP::BufferedTransformation &in)
std::string toString() const
Definition: oid.cpp:58