blob: e739717f56375eee60085e411b9f147bc7c9b559 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonc0573432013-09-19 17:41:36 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
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>
Jeff Thompsonc0573432013-09-19 17:41:36 -070024 */
25
Yingdi Yufc40d872014-02-18 12:56:04 -080026#ifndef NDN_SECURITY_PUBLIC_KEY_HPP
27#define NDN_SECURITY_PUBLIC_KEY_HPP
Jeff Thompsonc0573432013-09-19 17:41:36 -070028
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080029#include "../common.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030
Yingdi Yu4f324632014-01-15 18:10:03 -080031#include "../encoding/buffer.hpp"
Yingdi Yue36322a2014-11-04 14:16:54 -080032#include "../encoding/block.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080033#include "security-common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070034
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070035namespace CryptoPP {
36class BufferedTransformation;
37}
38
Jeff Thompsonc0573432013-09-19 17:41:36 -070039namespace ndn {
40
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070041class PublicKey
42{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043public:
44 class Error : public std::runtime_error
45 {
46 public:
47 explicit
48 Error(const std::string& what)
49 : std::runtime_error(what)
50 {
51 }
52 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080053
Jeff Thompsonc0573432013-09-19 17:41:36 -070054 /**
55 * The default constructor.
56 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080057 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070058
59 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070060 * @brief Create a new PublicKey from @param keyDerBuf in DER buffer
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080061 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070062 * @param keyDerBuf The pointer to the first byte of buffer containing DER of public key
63 * @param keyDerSize Size of the buffer
64 *
65 * @throws PublicKey::Error If DER in buffer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070066 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070067 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080068
Yingdi Yu2620b1c2014-06-12 15:32:57 -070069 const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080070 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070071 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070072 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070073 }
74
Yingdi Yu2620b1c2014-06-12 15:32:57 -070075 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070076 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080077 {
78 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070079 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080080 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070081
Yingdi Yu2620b1c2014-06-12 15:32:57 -070082 KeyType
83 getKeyType() const
84 {
85 return m_type;
86 }
87
Yingdi Yue36322a2014-11-04 14:16:54 -080088 /**
89 * @return a KeyDigest block that matches this public key
90 */
91 const Block&
92 computeDigest() const;
93
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080094 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070095 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070096
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080097 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070099
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700100 bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700101 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800102 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700103 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800104 }
105
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700106 bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800108 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700109 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800110 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700111
Jeff Thompsonc0573432013-09-19 17:41:36 -0700112private:
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700113 KeyType m_type;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700114 Buffer m_key;
Yingdi Yue36322a2014-11-04 14:16:54 -0800115 mutable Block m_digest;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700116};
117
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700119operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800120
Yingdi Yufc40d872014-02-18 12:56:04 -0800121} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -0700122
Yingdi Yufc40d872014-02-18 12:56:04 -0800123#endif //NDN_SECURITY_PUBLIC_KEY_HPP