blob: 43e122358b7983007ca206bccb22a33228a8bde8 [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
9#ifndef NDN_PUBLIC_KEY_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -070010#define NDN_PUBLIC_KEY_HPP
Jeff Thompsonc0573432013-09-19 17:41:36 -070011
Jeff Thompson31b5c2f2013-10-12 15:13:16 -070012#include "../../encoding/oid.hpp"
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080013#include "../../encoding/buffer.hpp"
Jeff Thompsonc0573432013-09-19 17:41:36 -070014#include "../security-common.hpp"
15
16namespace ndn {
17
18class PublicKey {
19public:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080020 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
21
Jeff Thompsonc0573432013-09-19 17:41:36 -070022 /**
23 * The default constructor.
24 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080025 PublicKey();
Jeff Thompsonc0573432013-09-19 17:41:36 -070026
27 /**
Jeff Thompson10a8c382013-12-13 11:53:43 -080028 * Create a new PublicKey with the given values.
Jeff Thompsonc0573432013-09-19 17:41:36 -070029 * @param algorithm The algorithm of the public key.
30 * @param keyDer The blob of the PublicKeyInfo in terms of DER.
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080031 *
32 * @throws PublicKey::Error If algorithm is not supported or keyDer cannot be decoded
Jeff Thompsonc0573432013-09-19 17:41:36 -070033 */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080034 PublicKey(const uint8_t *keyDerBuf, size_t keyDerSize);
35
36 const Buffer&
37 get() const
Jeff Thompsonc0573432013-09-19 17:41:36 -070038 {
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080039 return key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070040 }
41
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080042 void
43 set(const uint8_t *keyDerBuf, size_t keyDerSize)
44 {
45 Buffer buf(keyDerBuf, keyDerSize);
46 key_.swap(buf);
47 }
Jeff Thompsonc0573432013-09-19 17:41:36 -070048
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080049 void
50 encode(CryptoPP::BufferedTransformation &out) const;
Jeff Thompsonc0573432013-09-19 17:41:36 -070051
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080052 void
53 decode(CryptoPP::BufferedTransformation &in);
Jeff Thompsonc0573432013-09-19 17:41:36 -070054
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080055 // /*
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
Jeff Thompsonc0573432013-09-19 17:41:36 -070062private:
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080063 Buffer key_;
Jeff Thompsonc0573432013-09-19 17:41:36 -070064};
65
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080066std::ostream &
67operator <<(std::ostream &os, const PublicKey &key);
68
Jeff Thompsonc0573432013-09-19 17:41:36 -070069}
70
71#endif