blob: b8222c236501e12d6b93fb2ed8c839b48e1c885e [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"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070021
Yingdi Yu4f324632014-01-15 18:10:03 -080022#include "../encoding/buffer.hpp"
23#include "security-common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070024
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025namespace CryptoPP {
26class BufferedTransformation;
27}
28
Jeff Thompsonc0573432013-09-19 17:41:36 -070029namespace ndn {
30
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070031class PublicKey
32{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033public:
34 class Error : public std::runtime_error
35 {
36 public:
37 explicit
38 Error(const std::string& what)
39 : std::runtime_error(what)
40 {
41 }
42 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080043
Jeff Thompsonc0573432013-09-19 17:41:36 -070044 /**
45 * The default constructor.
46 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080047 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070048
49 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070050 * @brief Create a new PublicKey from @param keyDerBuf in DER buffer
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080051 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -070052 * @param keyDerBuf The pointer to the first byte of buffer containing DER of public key
53 * @param keyDerSize Size of the buffer
54 *
55 * @throws PublicKey::Error If DER in buffer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070056 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070057 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080058
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080059 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080060 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070061 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070062 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070063 }
64
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080065 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070066 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080067 {
68 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070069 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080070 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070071
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080072 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070074
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080075 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070077
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080078 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070079 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080080 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070081 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080082 }
83
84 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070085 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080086 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070087 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080088 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089
Jeff Thompsonc0573432013-09-19 17:41:36 -070090private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070091 Buffer m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070092};
93
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070094std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070095operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080096
Yingdi Yufc40d872014-02-18 12:56:04 -080097} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070098
Yingdi Yufc40d872014-02-18 12:56:04 -080099#endif //NDN_SECURITY_PUBLIC_KEY_HPP