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 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 9 | #ifndef NDN_SECURITY_PUBLIC_KEY_HPP |
| 10 | #define NDN_SECURITY_PUBLIC_KEY_HPP |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 12 | #include "../common.hpp" |
| 13 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 14 | #include "../encoding/oid.hpp" |
| 15 | #include "../encoding/buffer.hpp" |
| 16 | #include "security-common.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 17 | |
| 18 | namespace ndn { |
| 19 | |
| 20 | class PublicKey { |
| 21 | public: |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 22 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 23 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 24 | /** |
| 25 | * The default constructor. |
| 26 | */ |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 27 | PublicKey(); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Jeff Thompson | 10a8c38 | 2013-12-13 11:53:43 -0800 | [diff] [blame] | 30 | * Create a new PublicKey with the given values. |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 31 | * @param algorithm The algorithm of the public key. |
| 32 | * @param keyDer The blob of the PublicKeyInfo in terms of DER. |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 33 | * |
| 34 | * @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] | 35 | */ |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 36 | PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize); |
| 37 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 38 | inline const Buffer& |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 39 | get() const |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 40 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 41 | return key_; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 44 | inline void |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 45 | set(const uint8_t *keyDerBuf, size_t keyDerSize) |
| 46 | { |
| 47 | Buffer buf(keyDerBuf, keyDerSize); |
| 48 | key_.swap(buf); |
| 49 | } |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 51 | void |
| 52 | encode(CryptoPP::BufferedTransformation &out) const; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 54 | void |
| 55 | decode(CryptoPP::BufferedTransformation &in); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 57 | // /* |
| 58 | // * Get the digest of the public key. |
| 59 | // * @param digestAlgorithm The digest algorithm. If omitted, use DIGEST_ALGORITHM_SHA256 by default. |
| 60 | // */ |
| 61 | // Blob |
| 62 | // getDigest(DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) const; |
| 63 | |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 64 | inline bool |
| 65 | operator ==(const PublicKey &key) const |
| 66 | { |
| 67 | return key_ == key.key_; |
| 68 | } |
| 69 | |
| 70 | inline bool |
| 71 | operator !=(const PublicKey &key) const |
| 72 | { |
| 73 | return key_ != key.key_; |
| 74 | } |
| 75 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 76 | private: |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 77 | Buffer key_; |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 80 | std::ostream & |
| 81 | operator <<(std::ostream &os, const PublicKey &key); |
| 82 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 83 | } // namespace ndn |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 84 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame^] | 85 | #endif //NDN_SECURITY_PUBLIC_KEY_HPP |