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