blob: 8d6b7c510006c454dd38e2de8067aea1990445b0 [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
Yingdi Yufc40d872014-02-18 12:56:04 -08009#ifndef NDN_SECURITY_PUBLIC_KEY_HPP
10#define NDN_SECURITY_PUBLIC_KEY_HPP
Jeff Thompsonc0573432013-09-19 17:41:36 -070011
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080012#include "../common.hpp"
13
Yingdi Yu4f324632014-01-15 18:10:03 -080014#include "../encoding/oid.hpp"
15#include "../encoding/buffer.hpp"
16#include "security-common.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070017
18namespace ndn {
19
20class PublicKey {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070021public:
22 class Error : public std::runtime_error
23 {
24 public:
25 explicit
26 Error(const std::string& what)
27 : std::runtime_error(what)
28 {
29 }
30 };
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080031
Jeff Thompsonc0573432013-09-19 17:41:36 -070032 /**
33 * The default constructor.
34 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080035 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070036
37 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080038 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070039 * @param algorithm The algorithm of the public key.
40 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080041 *
42 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070043 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080044 PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize);
45
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080046 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080047 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070048 {
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080049 return key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070050 }
51
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080052 inline void
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080053 set(const uint8_t *keyDerBuf, size_t keyDerSize)
54 {
55 Buffer buf(keyDerBuf, keyDerSize);
56 key_.swap(buf);
57 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070058
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080059 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 encode(CryptoPP::BufferedTransformation& out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070061
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080062 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070063 decode(CryptoPP::BufferedTransformation& in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070064
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080065 // /*
66 // * Get the digest of the public key.
67 // * @param digestAlgorithm The digest algorithm. If omitted, use DIGEST_ALGORITHM_SHA256 by default.
68 // */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069 // Blob
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080070 // getDigest(DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) const;
71
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080072 inline bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073 operator ==(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080074 {
75 return key_ == key.key_;
76 }
77
78 inline bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079 operator !=(const PublicKey& key) const
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080080 {
81 return key_ != key.key_;
82 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070083
Jeff Thompsonc0573432013-09-19 17:41:36 -070084private:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080085 Buffer key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070086};
87
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088std::ostream&
89operator <<(std::ostream& os, const PublicKey& key);
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080090
Yingdi Yufc40d872014-02-18 12:56:04 -080091} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070092
Yingdi Yufc40d872014-02-18 12:56:04 -080093#endif //NDN_SECURITY_PUBLIC_KEY_HPP