blob: 83230f683a9084e20947be2038b906c69e66ac19 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_KEY_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_KEY_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
11#include <vector>
Jeff Thompson25b4e612013-10-10 16:03:24 -070012#include "c/key-types.h"
Jeff Thompson2e6269c2013-08-22 10:36:00 -070013#include "name.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070014
Jeff Thompson25b4e612013-10-10 16:03:24 -070015struct ndn_KeyLocator;
16
Jeff Thompson5cae5e52013-07-10 19:41:20 -070017namespace ndn {
18
19class KeyLocator {
20public:
21 KeyLocator()
Jeff Thompson7329a132013-08-16 15:57:37 -070022 : type_((ndn_KeyLocatorType)-1), keyNameType_((ndn_KeyNameType)-1)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070023 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070024 }
25
26 /**
Jeff Thompsonf4585af2013-09-11 14:56:59 -070027 * Clear the keyData and set the type to none.
28 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070029 void
30 clear()
Jeff Thompsonf4585af2013-09-11 14:56:59 -070031 {
32 type_ = (ndn_KeyLocatorType)-1;
33 keyNameType_ = (ndn_KeyNameType)-1;
Jeff Thompson03616c92013-09-12 14:30:01 -070034 keyData_.reset();
Jeff Thompsonf4585af2013-09-11 14:56:59 -070035 }
36
37 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070038 * Set the keyLocatorStruct to point to the values in this key locator, without copying any memory.
39 * WARNING: The resulting pointers in keyLocatorStruct are invalid after a further use of this object which could reallocate memory.
40 * @param keyLocatorStruct a C ndn_KeyLocator struct where the name components array is already allocated.
41 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070042 void
43 get(struct ndn_KeyLocator& keyLocatorStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070044
45 /**
46 * Clear this key locator, and set the values by copying from the ndn_KeyLocator struct.
47 * @param keyLocatorStruct a C ndn_KeyLocator struct
48 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070049 void
50 set(const struct ndn_KeyLocator& keyLocatorStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070051
Jeff Thompson0050abe2013-09-17 12:50:25 -070052 ndn_KeyLocatorType
53 getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054
Jeff Thompson0050abe2013-09-17 12:50:25 -070055 const Blob&
56 getKeyData() const { return keyData_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070057
Jeff Thompson0050abe2013-09-17 12:50:25 -070058 const Name&
59 getKeyName() const { return keyName_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070060
Jeff Thompson0050abe2013-09-17 12:50:25 -070061 Name&
62 getKeyName() { return keyName_; }
63
64 ndn_KeyNameType
65 getKeyNameType() const { return keyNameType_; }
66
67 void
68 setType(ndn_KeyLocatorType type) { type_ = type; }
69
70 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -070071 setKeyData(const std::vector<uint8_t>& keyData) { keyData_ = keyData; }
Jeff Thompson0050abe2013-09-17 12:50:25 -070072
73 void
Jeff Thompson97223af2013-09-24 17:01:27 -070074 setKeyData(const uint8_t *keyData, size_t keyDataLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -070075 {
Jeff Thompson4c9c0452013-09-12 14:10:11 -070076 keyData_ = Blob(keyData, keyDataLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070077 }
Jeff Thompson63d02692013-08-16 12:09:07 -070078
79 /**
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070080 * Set keyData to point to an existing byte array. IMPORTANT: After calling this,
Jeff Thompson4c9c0452013-09-12 14:10:11 -070081 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
82 * @param keyData A pointer to a vector with the byte array. This takes another reference and does not copy the bytes.
Jeff Thompson63d02692013-08-16 12:09:07 -070083 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070084 void
Jeff Thompson10ad12a2013-09-24 16:19:11 -070085 setKeyData(const ptr_lib::shared_ptr<std::vector<uint8_t> > &keyData) { keyData_ = keyData; }
Jeff Thompson63d02692013-08-16 12:09:07 -070086
Jeff Thompson35a2bc12013-09-17 14:23:13 -070087 void setKeyName(const Name &keyName) { keyName_ = keyName; }
88
Jeff Thompson0050abe2013-09-17 12:50:25 -070089 void
90 setKeyNameType(ndn_KeyNameType keyNameType) { keyNameType_ = keyNameType; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070091
92private:
Jeff Thompson7329a132013-08-16 15:57:37 -070093 ndn_KeyLocatorType type_; /**< -1 for none */
Jeff Thompson4c9c0452013-09-12 14:10:11 -070094 Blob keyData_; /**< An array for the key data as follows:
Jeff Thompson7329a132013-08-16 15:57:37 -070095 * If type_ is ndn_KeyLocatorType_KEY, the key data.
96 * If type_ is ndn_KeyLocatorType_CERTIFICATE, the certificate data.
97 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST, the publisher public key digest.
98 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST, the publisher certificate digest.
99 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST, the publisher issuer key digest.
100 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST, the publisher issuer certificate digest.
101 */
102 Name keyName_; /**< The key name (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700103 ndn_KeyNameType keyNameType_; /**< The type of data for keyName_, -1 for none. (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700104};
105
106}
107
108#endif