Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 1 | /* -*- 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 Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 10 | #define NDN_PUBLIC_KEY_HPP |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | 54467af | 2014-01-06 15:45:32 -0800 | [diff] [blame] | 12 | #include <stdexcept> |
Jeff Thompson | 31b5c2f | 2013-10-12 15:13:16 -0700 | [diff] [blame] | 13 | #include "../../encoding/oid.hpp" |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 14 | #include "../../encoding/buffer.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 15 | #include "../security-common.hpp" |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | class PublicKey { |
| 20 | public: |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 21 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 22 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 23 | /** |
| 24 | * The default constructor. |
| 25 | */ |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 26 | PublicKey(); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
Jeff Thompson | 10a8c38 | 2013-12-13 11:53:43 -0800 | [diff] [blame] | 29 | * Create a new PublicKey with the given values. |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 30 | * @param algorithm The algorithm of the public key. |
| 31 | * @param keyDer The blob of the PublicKeyInfo in terms of DER. |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 32 | * |
| 33 | * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 34 | */ |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 35 | PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize); |
| 36 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 37 | inline const Buffer& |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 38 | get() const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 40 | return key_; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 43 | inline void |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 44 | set(const uint8_t *keyDerBuf, size_t keyDerSize) |
| 45 | { |
| 46 | Buffer buf(keyDerBuf, keyDerSize); |
| 47 | key_.swap(buf); |
| 48 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 50 | void |
| 51 | encode(CryptoPP::BufferedTransformation &out) const; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 53 | void |
| 54 | decode(CryptoPP::BufferedTransformation &in); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 56 | // /* |
| 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 Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 63 | 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 Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 75 | private: |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 76 | Buffer key_; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 79 | std::ostream & |
| 80 | operator <<(std::ostream &os, const PublicKey &key); |
| 81 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | #endif |