blob: a332f02c70bb3559b1d5d46943605ba96018ef3a [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 {
21public:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080022 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
23
Jeff Thompsonc0573432013-09-19 17:41:36 -070024 /**
25 * The default constructor.
26 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080027 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070028
29 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080030 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070031 * @param algorithm The algorithm of the public key.
32 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080033 *
34 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070035 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080036 PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize);
37
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080038 inline const Buffer&
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080039 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070040 {
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080041 return key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070042 }
43
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080044 inline void
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080045 set(const uint8_t *keyDerBuf, size_t keyDerSize)
46 {
47 Buffer buf(keyDerBuf, keyDerSize);
48 key_.swap(buf);
49 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070050
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080051 void
52 encode(CryptoPP::BufferedTransformation &out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070053
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080054 void
55 decode(CryptoPP::BufferedTransformation &in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070056
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080057 // /*
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 Afanasyevbf1a67a2014-01-05 23:36:13 -080064 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 Thompsonc0573432013-09-19 17:41:36 -070076private:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080077 Buffer key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070078};
79
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080080std::ostream &
81operator <<(std::ostream &os, const PublicKey &key);
82
Yingdi Yufc40d872014-02-18 12:56:04 -080083} // namespace ndn
Jeff Thompsonc0573432013-09-19 17:41:36 -070084
Yingdi Yufc40d872014-02-18 12:56:04 -080085#endif //NDN_SECURITY_PUBLIC_KEY_HPP