blob: e939978dfbbffefda71517580176265c8fd1d5d8 [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
13 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
14 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonc0573432013-09-19 17:41:36 -070015 */
16
Yingdi Yufc40d872014-02-18 12:56:04 -080017#ifndef NDN_SECURITY_PUBLIC_KEY_HPP
18#define NDN_SECURITY_PUBLIC_KEY_HPP
Jeff Thompsonc0573432013-09-19 17:41:36 -070019
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080020#include "../common.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080021#include "../encoding/oid.hpp"
22#include "../encoding/buffer.hpp"
23#include "security-common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070024
25namespace ndn {
26
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070027class PublicKey
28{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029public:
30 class Error : public std::runtime_error
31 {
32 public:
33 explicit
34 Error(const std::string& what)
35 : std::runtime_error(what)
36 {
37 }
38 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080039
Jeff Thompsonc0573432013-09-19 17:41:36 -070040 /**
41 * The default constructor.
42 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080043 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070044
45 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070046 * @brief Create a new PublicKey from @param keyDerBuf in DER buffer
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080047 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070048 * @param keyDerBuf The pointer to the first byte of buffer containing DER of public key
49 * @param keyDerSize Size of the buffer
50 *
51 * @throws PublicKey::Error If DER in buffer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070052 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070053 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080054
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080055 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080056 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070057 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070058 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070059 }
60
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080061 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070062 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080063 {
64 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070065 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080066 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070067
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080068 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070070
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080071 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070072 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070073
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080074 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070075 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080076 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070077 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080078 }
79
80 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070081 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080082 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070083 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080084 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070085
Jeff Thompsonc0573432013-09-19 17:41:36 -070086private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070087 Buffer m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070088};
89
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070090std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070091operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080092
Yingdi Yufc40d872014-02-18 12:56:04 -080093} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070094
Yingdi Yufc40d872014-02-18 12:56:04 -080095#endif //NDN_SECURITY_PUBLIC_KEY_HPP