blob: 0717e1b5f3fbfacf35168ed8ef2b965450f18151 [file] [log] [blame]
Jeff Thompson5cae5e52013-07-10 19:41:20 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
6#ifndef NDN_KEY_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_KEY_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07008
9#include <vector>
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "c/key.h"
Jeff Thompson2e6269c2013-08-22 10:36:00 -070011#include "name.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070012
13namespace ndn {
14
15class KeyLocator {
16public:
17 KeyLocator()
Jeff Thompson7329a132013-08-16 15:57:37 -070018 : type_((ndn_KeyLocatorType)-1), keyNameType_((ndn_KeyNameType)-1)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070019 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020 }
21
22 /**
Jeff Thompsonf4585af2013-09-11 14:56:59 -070023 * Clear the keyData and set the type to none.
24 */
25 void clear()
26 {
27 type_ = (ndn_KeyLocatorType)-1;
28 keyNameType_ = (ndn_KeyNameType)-1;
Jeff Thompson4c9c0452013-09-12 14:10:11 -070029 keyData_ = Blob(0, 0);
Jeff Thompsonf4585af2013-09-11 14:56:59 -070030 }
31
32 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070033 * Set the keyLocatorStruct to point to the values in this key locator, without copying any memory.
34 * WARNING: The resulting pointers in keyLocatorStruct are invalid after a further use of this object which could reallocate memory.
35 * @param keyLocatorStruct a C ndn_KeyLocator struct where the name components array is already allocated.
36 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070037 void get(struct ndn_KeyLocator& keyLocatorStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070038
39 /**
40 * Clear this key locator, and set the values by copying from the ndn_KeyLocator struct.
41 * @param keyLocatorStruct a C ndn_KeyLocator struct
42 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070043 void set(const struct ndn_KeyLocator& keyLocatorStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070044
45 ndn_KeyLocatorType getType() const { return type_; }
46
Jeff Thompson4c9c0452013-09-12 14:10:11 -070047 const Blob& getKeyData() const { return keyData_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070048
Jeff Thompson1656e6a2013-08-29 18:01:48 -070049 const Name& getKeyName() const { return keyName_; }
50 Name& getKeyName() { return keyName_; }
Jeff Thompson7329a132013-08-16 15:57:37 -070051
52 ndn_KeyNameType getKeyNameType() const { return keyNameType_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070053
54 void setType(ndn_KeyLocatorType type) { type_ = type; }
55
Jeff Thompson1656e6a2013-08-29 18:01:48 -070056 void setKeyData(const std::vector<unsigned char>& keyData) { keyData_ = keyData; }
Jeff Thompson63d02692013-08-16 12:09:07 -070057 void setKeyData(const unsigned char *keyData, unsigned int keyDataLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -070058 {
Jeff Thompson4c9c0452013-09-12 14:10:11 -070059 keyData_ = Blob(keyData, keyDataLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070060 }
Jeff Thompson63d02692013-08-16 12:09:07 -070061
62 /**
Jeff Thompson4c9c0452013-09-12 14:10:11 -070063 * Set keyData to point to an existing byte array. IMPORTANT: After calling this constructor,
64 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
65 * @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 -070066 */
Jeff Thompson4c9c0452013-09-12 14:10:11 -070067 void setKeyData(const ptr_lib::shared_ptr<std::vector<unsigned char> > &keyData) { keyData_ = keyData; }
Jeff Thompson63d02692013-08-16 12:09:07 -070068
Jeff Thompson7329a132013-08-16 15:57:37 -070069 void setKeyNameType(ndn_KeyNameType keyNameType) { keyNameType_ = keyNameType; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070070
71private:
Jeff Thompson7329a132013-08-16 15:57:37 -070072 ndn_KeyLocatorType type_; /**< -1 for none */
Jeff Thompson4c9c0452013-09-12 14:10:11 -070073 Blob keyData_; /**< An array for the key data as follows:
Jeff Thompson7329a132013-08-16 15:57:37 -070074 * If type_ is ndn_KeyLocatorType_KEY, the key data.
75 * If type_ is ndn_KeyLocatorType_CERTIFICATE, the certificate data.
76 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST, the publisher public key digest.
77 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST, the publisher certificate digest.
78 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST, the publisher issuer key digest.
79 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST, the publisher issuer certificate digest.
80 */
81 Name keyName_; /**< The key name (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
Jeff Thompson1cf72e92013-08-23 20:38:39 -070082 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 -070083};
84
85}
86
87#endif