blob: ed0687a17e73a6640075be83585fe023ee1880a0 [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
9#ifndef NDN_PUBLIC_KEY_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -070010#define NDN_PUBLIC_KEY_HPP
Jeff Thompsonc0573432013-09-19 17:41:36 -070011
Alexander Afanasyev54467af2014-01-06 15:45:32 -080012#include <stdexcept>
Jeff Thompson31b5c2f2013-10-12 15:13:16 -070013#include "../../encoding/oid.hpp"
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080014#include "../../encoding/buffer.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070015#include "../security-common.hpp"
16
17namespace ndn {
18
19class PublicKey {
20public:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080021 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
22
Jeff Thompsonc0573432013-09-19 17:41:36 -070023 /**
24 * The default constructor.
25 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080026 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070027
28 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080029 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070030 * @param algorithm The algorithm of the public key.
31 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080032 *
33 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070034 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080035 PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize);
36
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080037 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080038 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070039 {
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080040 return key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070041 }
42
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080043 inline void
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080044 set(const uint8_t *keyDerBuf, size_t keyDerSize)
45 {
46 Buffer buf(keyDerBuf, keyDerSize);
47 key_.swap(buf);
48 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070049
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080050 void
51 encode(CryptoPP::BufferedTransformation &out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070052
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080053 void
54 decode(CryptoPP::BufferedTransformation &in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070055
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080056 // /*
57 // * Get the digest of the public key.
58 // * @param digestAlgorithm The digest algorithm. If omitted, use DIGEST_ALGORITHM_SHA256 by default.
59 // */
60 // Blob
61 // getDigest(DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) const;
62
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080063 inline bool
64 operator ==(const PublicKey &key) const
65 {
66 return key_ == key.key_;
67 }
68
69 inline bool
70 operator !=(const PublicKey &key) const
71 {
72 return key_ != key.key_;
73 }
74
Jeff Thompsonc0573432013-09-19 17:41:36 -070075private:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080076 Buffer key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070077};
78
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080079std::ostream &
80operator <<(std::ostream &os, const PublicKey &key);
81
Jeff Thompsonc0573432013-09-19 17:41:36 -070082}
83
84#endif