blob: 6cf449e410782679982f8f8506229d64257769fc [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;
29 keyData_.clear();
30 }
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 Thompson1656e6a2013-08-29 18:01:48 -070047 const std::vector<unsigned char>& 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 Thompson63d02692013-08-16 12:09:07 -070059 setVector(keyData_, keyData, keyDataLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070060 }
61
Jeff Thompson63d02692013-08-16 12:09:07 -070062 /**
63 * @deprecated Use getKeyData().
64 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070065 const std::vector<unsigned char>& getKeyOrCertificate() const { return getKeyData(); }
Jeff Thompson63d02692013-08-16 12:09:07 -070066
67 /**
68 * @deprecated Use setKeyData.
69 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070070 void setKeyOrCertificate(const std::vector<unsigned char>& keyData) { setKeyData(keyData); }
Jeff Thompson63d02692013-08-16 12:09:07 -070071
72 /**
73 * @deprecated Use setKeyData.
74 */
75 void setKeyOrCertificate(const unsigned char *keyData, unsigned int keyDataLength) { setKeyData(keyData, keyDataLength); }
76
Jeff Thompson7329a132013-08-16 15:57:37 -070077 void setKeyNameType(ndn_KeyNameType keyNameType) { keyNameType_ = keyNameType; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070078
79private:
Jeff Thompson7329a132013-08-16 15:57:37 -070080 ndn_KeyLocatorType type_; /**< -1 for none */
81 std::vector<unsigned char> keyData_; /**< A pointer to a pre-allocated buffer for the key data as follows:
82 * If type_ is ndn_KeyLocatorType_KEY, the key data.
83 * If type_ is ndn_KeyLocatorType_CERTIFICATE, the certificate data.
84 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST, the publisher public key digest.
85 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST, the publisher certificate digest.
86 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST, the publisher issuer key digest.
87 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST, the publisher issuer certificate digest.
88 */
89 Name keyName_; /**< The key name (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
Jeff Thompson1cf72e92013-08-23 20:38:39 -070090 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 -070091};
92
93}
94
95#endif