blob: 243bccc4aa51da9ab4107211aa6e923d3dca7caf [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"
32#include "security-common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070033
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070034namespace CryptoPP {
35class BufferedTransformation;
36}
37
Jeff Thompsonc0573432013-09-19 17:41:36 -070038namespace ndn {
39
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070040class PublicKey
41{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070042public:
43 class Error : public std::runtime_error
44 {
45 public:
46 explicit
47 Error(const std::string& what)
48 : std::runtime_error(what)
49 {
50 }
51 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080052
Jeff Thompsonc0573432013-09-19 17:41:36 -070053 /**
54 * The default constructor.
55 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080056 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070057
58 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070059 * @brief Create a new PublicKey from @param keyDerBuf in DER buffer
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080060 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070061 * @param keyDerBuf The pointer to the first byte of buffer containing DER of public key
62 * @param keyDerSize Size of the buffer
63 *
64 * @throws PublicKey::Error If DER in buffer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070065 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070066 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080067
Yingdi Yu2620b1c2014-06-12 15:32:57 -070068 const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080069 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070070 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070071 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070072 }
73
Yingdi Yu2620b1c2014-06-12 15:32:57 -070074 void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070075 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080076 {
77 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070078 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080079 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070080
Yingdi Yu2620b1c2014-06-12 15:32:57 -070081 KeyType
82 getKeyType() const
83 {
84 return m_type;
85 }
86
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080087 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070089
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080090 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070092
Yingdi Yu2620b1c2014-06-12 15:32:57 -070093 bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070094 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080095 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070096 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080097 }
98
Yingdi Yu2620b1c2014-06-12 15:32:57 -070099 bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700100 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800101 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700102 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -0800103 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104
Jeff Thompsonc0573432013-09-19 17:41:36 -0700105private:
Yingdi Yu2620b1c2014-06-12 15:32:57 -0700106 KeyType m_type;
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700107 Buffer m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -0700108};
109
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700110std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700111operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -0800112
Yingdi Yufc40d872014-02-18 12:56:04 -0800113} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -0700114
Yingdi Yufc40d872014-02-18 12:56:04 -0800115#endif //NDN_SECURITY_PUBLIC_KEY_HPP