blob: 1ed567c57f1dbf76fa4831bd55edc68b9f4a8adc [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 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080046 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070047 * @param algorithm The algorithm of the public key.
48 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080049 *
50 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070051 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070052 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080053
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080054 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080055 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070056 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070057 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070058 }
59
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080060 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070061 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080062 {
63 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070064 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080065 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070066
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080067 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070068 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070069
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080070 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070072
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080073 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070074 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080075 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070076 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080077 }
78
79 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070080 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080081 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070082 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080083 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070084
Jeff Thompsonc0573432013-09-19 17:41:36 -070085private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070086 Buffer m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070087};
88
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070090operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080091
Yingdi Yufc40d872014-02-18 12:56:04 -080092} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070093
Yingdi Yufc40d872014-02-18 12:56:04 -080094#endif //NDN_SECURITY_PUBLIC_KEY_HPP