blob: 7774755fcf99569438cbb2e3311851a719b0d770 [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
19class PublicKey {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070020public:
21 class Error : public std::runtime_error
22 {
23 public:
24 explicit
25 Error(const std::string& what)
26 : std::runtime_error(what)
27 {
28 }
29 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080030
Jeff Thompsonc0573432013-09-19 17:41:36 -070031 /**
32 * The default constructor.
33 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080034 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070035
36 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080037 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070038 * @param algorithm The algorithm of the public key.
39 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080040 *
41 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070042 */
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070043 PublicKey(const uint8_t* keyDerBuf, size_t keyDerSize);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080044
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080045 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080046 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070047 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070048 return m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070049 }
50
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080051 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070052 set(const uint8_t* keyDerBuf, size_t keyDerSize)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080053 {
54 Buffer buf(keyDerBuf, keyDerSize);
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070055 m_key.swap(buf);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080056 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070057
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080058 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070059 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070060
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080061 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070063
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080064 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070065 operator==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080066 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070067 return m_key == key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080068 }
69
70 inline bool
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070071 operator!=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080072 {
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070073 return m_key != key.m_key;
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080074 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070075
Jeff Thompsonc0573432013-09-19 17:41:36 -070076private:
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070077 Buffer m_key;
Jeff Thompsonc0573432013-09-19 17:41:36 -070078};
79
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080std::ostream&
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070081operator<<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080082
Yingdi Yufc40d872014-02-18 12:56:04 -080083} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070084
Yingdi Yufc40d872014-02-18 12:56:04 -080085#endif //NDN_SECURITY_PUBLIC_KEY_HPP