blob: c68d895ee8729c69e650c4d0b6c9facceb86d281 [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_KEY_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_KEY_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07009
10#include <vector>
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "c/key.h"
Jeff Thompson2e6269c2013-08-22 10:36:00 -070012#include "name.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070013
14namespace ndn {
15
16class KeyLocator {
17public:
18 KeyLocator()
Jeff Thompson7329a132013-08-16 15:57:37 -070019 : type_((ndn_KeyLocatorType)-1), keyNameType_((ndn_KeyNameType)-1)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070021 }
22
23 /**
Jeff Thompsonf4585af2013-09-11 14:56:59 -070024 * Clear the keyData and set the type to none.
25 */
26 void clear()
27 {
28 type_ = (ndn_KeyLocatorType)-1;
29 keyNameType_ = (ndn_KeyNameType)-1;
Jeff Thompson03616c92013-09-12 14:30:01 -070030 keyData_.reset();
Jeff Thompsonf4585af2013-09-11 14:56:59 -070031 }
32
33 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070034 * Set the keyLocatorStruct to point to the values in this key locator, without copying any memory.
35 * WARNING: The resulting pointers in keyLocatorStruct are invalid after a further use of this object which could reallocate memory.
36 * @param keyLocatorStruct a C ndn_KeyLocator struct where the name components array is already allocated.
37 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070038 void get(struct ndn_KeyLocator& keyLocatorStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070039
40 /**
41 * Clear this key locator, and set the values by copying from the ndn_KeyLocator struct.
42 * @param keyLocatorStruct a C ndn_KeyLocator struct
43 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070044 void set(const struct ndn_KeyLocator& keyLocatorStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070045
46 ndn_KeyLocatorType getType() const { return type_; }
47
Jeff Thompson4c9c0452013-09-12 14:10:11 -070048 const Blob& getKeyData() const { return keyData_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070049
Jeff Thompson1656e6a2013-08-29 18:01:48 -070050 const Name& getKeyName() const { return keyName_; }
51 Name& getKeyName() { return keyName_; }
Jeff Thompson7329a132013-08-16 15:57:37 -070052
53 ndn_KeyNameType getKeyNameType() const { return keyNameType_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070054
55 void setType(ndn_KeyLocatorType type) { type_ = type; }
56
Jeff Thompson1656e6a2013-08-29 18:01:48 -070057 void setKeyData(const std::vector<unsigned char>& keyData) { keyData_ = keyData; }
Jeff Thompson63d02692013-08-16 12:09:07 -070058 void setKeyData(const unsigned char *keyData, unsigned int keyDataLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -070059 {
Jeff Thompson4c9c0452013-09-12 14:10:11 -070060 keyData_ = Blob(keyData, keyDataLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070061 }
Jeff Thompson63d02692013-08-16 12:09:07 -070062
63 /**
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070064 * Set keyData to point to an existing byte array. IMPORTANT: After calling this,
Jeff Thompson4c9c0452013-09-12 14:10:11 -070065 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
66 * @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 -070067 */
Jeff Thompson4c9c0452013-09-12 14:10:11 -070068 void setKeyData(const ptr_lib::shared_ptr<std::vector<unsigned char> > &keyData) { keyData_ = keyData; }
Jeff Thompson63d02692013-08-16 12:09:07 -070069
Jeff Thompson7329a132013-08-16 15:57:37 -070070 void setKeyNameType(ndn_KeyNameType keyNameType) { keyNameType_ = keyNameType; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070071
72private:
Jeff Thompson7329a132013-08-16 15:57:37 -070073 ndn_KeyLocatorType type_; /**< -1 for none */
Jeff Thompson4c9c0452013-09-12 14:10:11 -070074 Blob keyData_; /**< An array for the key data as follows:
Jeff Thompson7329a132013-08-16 15:57:37 -070075 * If type_ is ndn_KeyLocatorType_KEY, the key data.
76 * If type_ is ndn_KeyLocatorType_CERTIFICATE, the certificate data.
77 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST, the publisher public key digest.
78 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST, the publisher certificate digest.
79 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST, the publisher issuer key digest.
80 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST, the publisher issuer certificate digest.
81 */
82 Name keyName_; /**< The key name (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
Jeff Thompson1cf72e92013-08-23 20:38:39 -070083 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 -070084};
85
86}
87
88#endif