blob: 1fac1ec293237ef8d8d6f89da0ae231e1e6d3fe8 [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
Jeff Thompson31b5c2f2013-10-12 15:13:16 -070012#include "../../encoding/oid.hpp"
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080013#include "../../encoding/buffer.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070014#include "../security-common.hpp"
15
16namespace ndn {
17
18class PublicKey {
19public:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080020 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
21
Jeff Thompsonc0573432013-09-19 17:41:36 -070022 /**
23 * The default constructor.
24 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080025 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070026
27 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080028 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070029 * @param algorithm The algorithm of the public key.
30 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080031 *
32 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070033 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080034 PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize);
35
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080036 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080037 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070038 {
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080039 return key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070040 }
41
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080042 inline void
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080043 set(const uint8_t *keyDerBuf, size_t keyDerSize)
44 {
45 Buffer buf(keyDerBuf, keyDerSize);
46 key_.swap(buf);
47 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070048
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080049 void
50 encode(CryptoPP::BufferedTransformation &out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070051
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080052 void
53 decode(CryptoPP::BufferedTransformation &in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070054
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080055 // /*
56 // * Get the digest of the public key.
57 // * @param digestAlgorithm The digest algorithm. If omitted, use DIGEST_ALGORITHM_SHA256 by default.
58 // */
59 // Blob
60 // getDigest(DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) const;
61
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080062 inline bool
63 operator ==(const PublicKey &key) const
64 {
65 return key_ == key.key_;
66 }
67
68 inline bool
69 operator !=(const PublicKey &key) const
70 {
71 return key_ != key.key_;
72 }
73
Jeff Thompsonc0573432013-09-19 17:41:36 -070074private:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080075 Buffer key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070076};
77
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080078std::ostream &
79operator <<(std::ostream &os, const PublicKey &key);
80
Jeff Thompsonc0573432013-09-19 17:41:36 -070081}
82
83#endif