Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_OSX_PRIVATEKEY_STORAGE_H |
| 9 | #define NDN_OSX_PRIVATEKEY_STORAGE_H |
| 10 | |
Jeff Thompson | f21e224 | 2013-10-09 19:01:49 -0700 | [diff] [blame] | 11 | // Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_OSX_SECURITY 1. |
Jeff Thompson | 6e22904 | 2013-10-10 11:09:49 -0700 | [diff] [blame] | 12 | #include <ndn-cpp/ndn-cpp-config.h> |
Jeff Thompson | f21e224 | 2013-10-09 19:01:49 -0700 | [diff] [blame] | 13 | #if NDN_CPP_HAVE_OSX_SECURITY |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 14 | |
| 15 | #include "../../common.hpp" |
| 16 | #include "private-key-storage.hpp" |
| 17 | |
| 18 | #include <CoreFoundation/CoreFoundation.h> |
| 19 | #include <Security/Security.h> |
| 20 | #include <CoreServices/CoreServices.h> |
| 21 | |
| 22 | namespace ndn |
| 23 | { |
| 24 | |
| 25 | class OSXPrivateKeyStorage : public PrivateKeyStorage { |
| 26 | public: |
| 27 | /** |
| 28 | * constructor of OSXPrivateKeyStorage |
| 29 | * @param keychainName the name of keychain |
| 30 | */ |
| 31 | OSXPrivateKeyStorage(const std::string & keychainName = ""); |
| 32 | |
| 33 | /** |
| 34 | * destructor of OSXPrivateKeyStore |
| 35 | */ |
| 36 | virtual |
| 37 | ~OSXPrivateKeyStorage(); |
| 38 | |
| 39 | /** |
| 40 | * Generate a pair of asymmetric keys. |
| 41 | * @param keyName The name of the key pair. |
| 42 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 43 | * @param keySize The size of the key pair. |
| 44 | */ |
| 45 | virtual void |
| 46 | generateKeyPair(const Name& keyName, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048); |
| 47 | |
| 48 | /** |
| 49 | * Get the public key |
| 50 | * @param keyName The name of public key. |
| 51 | * @return The public key. |
| 52 | */ |
| 53 | virtual ptr_lib::shared_ptr<PublicKey> |
| 54 | getPublicKey(const Name& keyName); |
| 55 | |
| 56 | /** |
| 57 | * Fetch the private key for keyName and sign the data, returning a signature Blob. |
| 58 | * @param data Pointer to the input byte array. |
| 59 | * @param dataLength The length of data. |
| 60 | * @param keyName The name of the signing key. |
| 61 | * @param digestAlgorithm the digest algorithm. |
| 62 | * @return The signature, or a null pointer if signing fails. |
| 63 | */ |
| 64 | virtual Blob |
| 65 | sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256); |
| 66 | |
| 67 | /** |
| 68 | * Decrypt data. |
| 69 | * @param keyName The name of the decrypting key. |
| 70 | * @param data The byte to be decrypted. |
| 71 | * @param dataLength the length of data. |
| 72 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 73 | * @return The decrypted data. |
| 74 | */ |
| 75 | virtual Blob |
| 76 | decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false); |
| 77 | |
| 78 | /** |
| 79 | * Encrypt data. |
| 80 | * @param keyName The name of the encrypting key. |
| 81 | * @param data The byte to be encrypted. |
| 82 | * @param dataLength the length of data. |
| 83 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 84 | * @return The encrypted data. |
| 85 | */ |
| 86 | virtual Blob |
| 87 | encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false); |
| 88 | |
| 89 | /** |
| 90 | * Generate a symmetric key. |
| 91 | * @param keyName The name of the key. |
| 92 | * @param keyType The type of the key, e.g. KEY_TYPE_AES. |
| 93 | * @param keySize The size of the key. |
| 94 | */ |
| 95 | virtual void |
| 96 | generateKey(const Name& keyName, KeyType keyType = KEY_TYPE_AES, int keySize = 256); |
| 97 | |
| 98 | /** |
| 99 | * Check if a particular key exists. |
| 100 | * @param keyName The name of the key. |
| 101 | * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC. |
| 102 | * @return True if the key exists, otherwise false. |
| 103 | */ |
| 104 | virtual bool |
| 105 | doesKeyExist(const Name& keyName, KeyClass keyClass); |
| 106 | |
| 107 | /** |
| 108 | * configure ACL of a particular key |
| 109 | * @param keyName the name of key |
| 110 | * @param keyClass the class of key, e.g. Private Key |
| 111 | * @param acl the new acl of the key |
| 112 | * @param appPath the absolute path to the application |
| 113 | * @returns true if setting succeeds |
| 114 | */ |
| 115 | bool |
| 116 | setACL(const Name & keyName, KeyClass keyClass, int acl, const std::string & appPath); |
| 117 | |
| 118 | /** |
| 119 | * verify data (test only) |
| 120 | * @param keyName the name of key |
| 121 | * @param pData the data to be verified |
| 122 | * @param pSig the signature associated with the data |
| 123 | * @param digestAlgo digest algorithm |
| 124 | * @return true if signature can be verified, otherwise false |
| 125 | */ |
| 126 | bool |
| 127 | verifyData(const Name & keyName, const Blob & pData, const Blob & pSig, DigestAlgorithm digestAlgo = DIGEST_ALGORITHM_SHA256); |
| 128 | |
| 129 | private: |
| 130 | /** |
| 131 | * convert NDN name of a key to internal name of the key |
| 132 | * @param keyName the NDN name of the key |
| 133 | * @param keyClass the class of the key |
| 134 | * @return the internal key name |
| 135 | */ |
| 136 | std::string |
| 137 | toInternalKeyName(const Name & keyName, KeyClass keyClass); |
| 138 | |
| 139 | /** |
| 140 | * Get key |
| 141 | * @param keyName the name of the key |
| 142 | * @param keyClass the class of the key |
| 143 | * @returns pointer to the key |
| 144 | */ |
| 145 | SecKeychainItemRef |
| 146 | getKey(const Name & keyName, KeyClass keyClass); |
| 147 | |
| 148 | /** |
| 149 | * convert keyType to MAC OS symmetric key key type |
| 150 | * @param keyType |
| 151 | * @returns MAC OS key type |
| 152 | */ |
| 153 | const CFTypeRef |
| 154 | getSymKeyType(KeyType keyType); |
| 155 | |
| 156 | /** |
| 157 | * convert keyType to MAC OS asymmetirc key type |
| 158 | * @param keyType |
| 159 | * @returns MAC OS key type |
| 160 | */ |
| 161 | const CFTypeRef |
| 162 | getAsymKeyType(KeyType keyType); |
| 163 | |
| 164 | /** |
| 165 | * convert keyClass to MAC OS key class |
| 166 | * @param keyClass |
| 167 | * @returns MAC OS key class |
| 168 | */ |
| 169 | const CFTypeRef |
| 170 | getKeyClass(KeyClass keyClass); |
| 171 | |
| 172 | /** |
| 173 | * convert digestAlgo to MAC OS algorithm id |
| 174 | * @param digestAlgo |
| 175 | * @returns MAC OS algorithm id |
| 176 | */ |
| 177 | const CFStringRef |
| 178 | getDigestAlgorithm(DigestAlgorithm digestAlgo); |
| 179 | |
| 180 | /** |
| 181 | * convert format to MAC OS key format |
| 182 | * @param format |
| 183 | * @returns MAC OS keyformat |
| 184 | */ |
| 185 | SecExternalFormat |
| 186 | getFormat(KeyFormat format); |
| 187 | |
| 188 | /** |
| 189 | * get the digest size of the corresponding algorithm |
| 190 | * @param digestAlgo the digest algorithm |
| 191 | * @return digest size |
| 192 | */ |
| 193 | long |
| 194 | getDigestSize(DigestAlgorithm digestAlgo); |
| 195 | |
| 196 | const std::string keyChainName_; |
| 197 | SecKeychainRef keyChainRef_; |
| 198 | SecKeychainRef originalDefaultKeyChain_; |
| 199 | }; |
| 200 | |
| 201 | } |
| 202 | |
Jeff Thompson | c564781 | 2013-10-11 18:06:38 -0700 | [diff] [blame] | 203 | #endif // NDN_CPP_HAVE_OSX_SECURITY |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 204 | |
| 205 | #endif |