blob: 4a8f16ade9dcb25f508b31913034d79a2c0a8b90 [file] [log] [blame]
Jeff Thompsonc0573432013-09-19 17:41:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_PUBLIC_KEY_HPP
10#define NDN_SECURITY_PUBLIC_KEY_HPP
Jeff Thompsonc0573432013-09-19 17:41:36 -070011
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../common.hpp"
Yingdi Yu4f324632014-01-15 18:10:03 -080013#include "../encoding/oid.hpp"
14#include "../encoding/buffer.hpp"
15#include "security-common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070016
17namespace ndn {
18
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070019class PublicKey
20{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070021public:
22 class Error : public std::runtime_error
23 {
24 public:
25 explicit
26 Error(const std::string& what)
27 : std::runtime_error(what)
28 {
29 }
30 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080031
Jeff Thompsonc0573432013-09-19 17:41:36 -070032 /**
33 * The default constructor.
34 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080035 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070036
37 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080038 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070039 * @param algorithm The algorithm of the public key.
40 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080041 *
42 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070043 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070044 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080045
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080046 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080047 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070048 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070049 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070050 }
51
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080052 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070053 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080054 {
55 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070056 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080057 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070058
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080059 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070061
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080062 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070064
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080065 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070066 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080067 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070068 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080069 }
70
71 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070072 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080073 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070074 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080075 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076
Jeff Thompsonc0573432013-09-19 17:41:36 -070077private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070078 Buffer m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070079};
80
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070081std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070082operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080083
Yingdi Yufc40d872014-02-18 12:56:04 -080084} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070085
Yingdi Yufc40d872014-02-18 12:56:04 -080086#endif //NDN_SECURITY_PUBLIC_KEY_HPP