blob: 6b67535c44341e3e8030f4bad06ec43d6a5fbbea [file] [log] [blame]
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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 * @author Jeff Thompson <jefft0@remap.ucla.edu>
24 */
25
26#ifndef NDN_SECURITY_V1_PUBLIC_KEY_HPP
27#define NDN_SECURITY_V1_PUBLIC_KEY_HPP
28
29#include "../../common.hpp"
30
31#include "../../encoding/buffer.hpp"
32#include "../../encoding/block.hpp"
33#include "../security-common.hpp"
34
35namespace CryptoPP {
36class BufferedTransformation;
37} // namespace CryptoPP
38
39namespace ndn {
40namespace security {
41namespace v1 {
42
43class PublicKey
44{
45public:
46 class Error : public std::runtime_error
47 {
48 public:
49 explicit
50 Error(const std::string& what)
51 : std::runtime_error(what)
52 {
53 }
54 };
55
56 /**
57 * The default constructor.
58 */
59 PublicKey();
60
61 /**
62 * @brief Create a new PublicKey from @p keyDerBuf in DER buffer
63 *
64 * @param keyDerBuf The pointer to the first byte of buffer containing DER of public key
65 * @param keyDerSize Size of the buffer
66 *
67 * @throws PublicKey::Error If DER in buffer cannot be decoded
68 */
69 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
70
71 const Buffer&
72 get() const
73 {
74 return m_key;
75 }
76
77 void
78 set(const uint8_t* keyDerBuf, size_t keyDerSize)
79 {
80 Buffer buf(keyDerBuf, keyDerSize);
81 m_key.swap(buf);
82 }
83
84 KeyType
85 getKeyType() const
86 {
87 return m_type;
88 }
89
90 /**
91 * @return a KeyDigest block that matches this public key
92 */
93 const Block&
94 computeDigest() const;
95
96 void
97 encode(CryptoPP::BufferedTransformation& out) const;
98
99 void
100 decode(CryptoPP::BufferedTransformation& in);
101
102 bool
103 operator==(const PublicKey& key) const
104 {
105 return m_key == key.m_key;
106 }
107
108 bool
109 operator!=(const PublicKey& key) const
110 {
111 return m_key != key.m_key;
112 }
113
114private:
115 KeyType m_type;
116 Buffer m_key;
117 mutable Block m_digest;
118};
119
120std::ostream&
121operator<<(std::ostream& os, const PublicKey& key);
122
123} // namespace v1
124} // namespace security
125
126#ifdef NDN_CXX_KEEP_SECURITY_V1_ALIASES
127/// @deprecated When needed, use explicit namespace
128using security::v1::PublicKey;
129#endif // NDN_CXX_KEEP_SECURITY_V1_ALIASES
130
131} // namespace ndn
132
133#endif // NDN_SECURITY_V1_PUBLIC_KEY_HPP