blob: 8215905d02911a84b3610d8ce144563f4b20faab [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 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070026 void
27 clear()
Jeff Thompsonf4585af2013-09-11 14:56:59 -070028 {
29 type_ = (ndn_KeyLocatorType)-1;
30 keyNameType_ = (ndn_KeyNameType)-1;
Jeff Thompson03616c92013-09-12 14:30:01 -070031 keyData_.reset();
Jeff Thompsonf4585af2013-09-11 14:56:59 -070032 }
33
34 /**
Jeff Thompson5cae5e52013-07-10 19:41:20 -070035 * Set the keyLocatorStruct to point to the values in this key locator, without copying any memory.
36 * WARNING: The resulting pointers in keyLocatorStruct are invalid after a further use of this object which could reallocate memory.
37 * @param keyLocatorStruct a C ndn_KeyLocator struct where the name components array is already allocated.
38 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070039 void
40 get(struct ndn_KeyLocator& keyLocatorStruct) const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070041
42 /**
43 * Clear this key locator, and set the values by copying from the ndn_KeyLocator struct.
44 * @param keyLocatorStruct a C ndn_KeyLocator struct
45 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070046 void
47 set(const struct ndn_KeyLocator& keyLocatorStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -070048
Jeff Thompson0050abe2013-09-17 12:50:25 -070049 ndn_KeyLocatorType
50 getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070051
Jeff Thompson0050abe2013-09-17 12:50:25 -070052 const Blob&
53 getKeyData() const { return keyData_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070054
Jeff Thompson0050abe2013-09-17 12:50:25 -070055 const Name&
56 getKeyName() const { return keyName_; }
Jeff Thompson46bd45f2013-08-08 16:46:41 -070057
Jeff Thompson0050abe2013-09-17 12:50:25 -070058 Name&
59 getKeyName() { return keyName_; }
60
61 ndn_KeyNameType
62 getKeyNameType() const { return keyNameType_; }
63
64 void
65 setType(ndn_KeyLocatorType type) { type_ = type; }
66
67 void
68 setKeyData(const std::vector<unsigned char>& keyData) { keyData_ = keyData; }
69
70 void
71 setKeyData(const unsigned char *keyData, unsigned int keyDataLength)
Jeff Thompson46bd45f2013-08-08 16:46:41 -070072 {
Jeff Thompson4c9c0452013-09-12 14:10:11 -070073 keyData_ = Blob(keyData, keyDataLength);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070074 }
Jeff Thompson63d02692013-08-16 12:09:07 -070075
76 /**
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070077 * Set keyData to point to an existing byte array. IMPORTANT: After calling this,
Jeff Thompson4c9c0452013-09-12 14:10:11 -070078 * if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.
79 * @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 -070080 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070081 void
82 setKeyData(const ptr_lib::shared_ptr<std::vector<unsigned char> > &keyData) { keyData_ = keyData; }
Jeff Thompson63d02692013-08-16 12:09:07 -070083
Jeff Thompson0050abe2013-09-17 12:50:25 -070084 void
85 setKeyNameType(ndn_KeyNameType keyNameType) { keyNameType_ = keyNameType; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070086
87private:
Jeff Thompson7329a132013-08-16 15:57:37 -070088 ndn_KeyLocatorType type_; /**< -1 for none */
Jeff Thompson4c9c0452013-09-12 14:10:11 -070089 Blob keyData_; /**< An array for the key data as follows:
Jeff Thompson7329a132013-08-16 15:57:37 -070090 * If type_ is ndn_KeyLocatorType_KEY, the key data.
91 * If type_ is ndn_KeyLocatorType_CERTIFICATE, the certificate data.
92 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST, the publisher public key digest.
93 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST, the publisher certificate digest.
94 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST, the publisher issuer key digest.
95 * If type_ is ndn_KeyLocatorType_KEYNAME and keyNameType_ is ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST, the publisher issuer certificate digest.
96 */
97 Name keyName_; /**< The key name (only used if type_ is ndn_KeyLocatorType_KEYNAME.) */
Jeff Thompson1cf72e92013-08-23 20:38:39 -070098 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 -070099};
100
101}
102
103#endif