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 | |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 8 | #include <ndn-cpp-dev/ndn-cpp-config.h> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 9 | |
| 10 | #include <fstream> |
| 11 | #include <sstream> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 12 | |
Yingdi Yu | 4f32463 | 2014-01-15 18:10:03 -0800 | [diff] [blame] | 13 | #include "../util/logging.hpp" |
Alexander Afanasyev | 6be1a6a | 2014-01-06 00:08:14 -0800 | [diff] [blame] | 14 | |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 15 | #include <ndn-cpp-dev/security/sec-tpm-osx.hpp> |
| 16 | #include <ndn-cpp-dev/security/public-key.hpp> |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 17 | |
| 18 | #include <CoreFoundation/CoreFoundation.h> |
| 19 | #include <Security/Security.h> |
| 20 | #include <CoreServices/CoreServices.h> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 21 | |
| 22 | using namespace std; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 24 | INIT_LOGGER("SecTpmOsx"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn |
| 27 | { |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 28 | class SecTpmOsx::Impl { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 29 | public: |
| 30 | Impl(const std::string &keychainName) |
Yingdi Yu | bdfa624 | 2013-12-24 11:49:13 +0800 | [diff] [blame] | 31 | : keyChainName_ ("" == keychainName ? "login.keychain" : keychainName) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * convert NDN name of a key to internal name of the key |
| 37 | * @param keyName the NDN name of the key |
| 38 | * @param keyClass the class of the key |
| 39 | * @return the internal key name |
| 40 | */ |
| 41 | std::string |
| 42 | toInternalKeyName(const Name & keyName, KeyClass keyClass); |
| 43 | |
| 44 | /** |
| 45 | * Get key |
| 46 | * @param keyName the name of the key |
| 47 | * @param keyClass the class of the key |
| 48 | * @returns pointer to the key |
| 49 | */ |
| 50 | SecKeychainItemRef |
| 51 | getKey(const Name & keyName, KeyClass keyClass); |
| 52 | |
| 53 | /** |
| 54 | * convert keyType to MAC OS symmetric key key type |
| 55 | * @param keyType |
| 56 | * @returns MAC OS key type |
| 57 | */ |
| 58 | const CFTypeRef |
| 59 | getSymKeyType(KeyType keyType); |
| 60 | |
| 61 | /** |
| 62 | * convert keyType to MAC OS asymmetirc key type |
| 63 | * @param keyType |
| 64 | * @returns MAC OS key type |
| 65 | */ |
| 66 | const CFTypeRef |
| 67 | getAsymKeyType(KeyType keyType); |
| 68 | |
| 69 | /** |
| 70 | * convert keyClass to MAC OS key class |
| 71 | * @param keyClass |
| 72 | * @returns MAC OS key class |
| 73 | */ |
| 74 | const CFTypeRef |
| 75 | getKeyClass(KeyClass keyClass); |
| 76 | |
| 77 | /** |
| 78 | * convert digestAlgo to MAC OS algorithm id |
| 79 | * @param digestAlgo |
| 80 | * @returns MAC OS algorithm id |
| 81 | */ |
| 82 | const CFStringRef |
| 83 | getDigestAlgorithm(DigestAlgorithm digestAlgo); |
| 84 | |
| 85 | /** |
| 86 | * get the digest size of the corresponding algorithm |
| 87 | * @param digestAlgo the digest algorithm |
| 88 | * @return digest size |
| 89 | */ |
| 90 | long |
| 91 | getDigestSize(DigestAlgorithm digestAlgo); |
| 92 | |
| 93 | /////////////////////////////////////////////// |
| 94 | // everything here is public, including data // |
| 95 | /////////////////////////////////////////////// |
| 96 | public: |
| 97 | const std::string keyChainName_; |
| 98 | SecKeychainRef keyChainRef_; |
| 99 | SecKeychainRef originalDefaultKeyChain_; |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 104 | SecTpmOsx::SecTpmOsx(const string & keychainName) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 105 | : impl_(new Impl(keychainName)) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 106 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 107 | OSStatus res = SecKeychainCreate(impl_->keyChainName_.c_str(), //Keychain path |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 108 | 0, //Keychain password length |
| 109 | NULL, //Keychain password |
| 110 | true, //User prompt |
| 111 | NULL, //Initial access of Keychain |
| 112 | &impl_->keyChainRef_); //Keychain reference |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 113 | |
| 114 | if (res == errSecDuplicateKeychain) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 115 | res = SecKeychainOpen(impl_->keyChainName_.c_str(), |
| 116 | &impl_->keyChainRef_); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 117 | |
| 118 | if (res != errSecSuccess){ |
| 119 | _LOG_DEBUG("Fail to initialize keychain ref: " << res); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 120 | throw Error("Fail to initialize keychain ref"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 123 | res = SecKeychainCopyDefault(&impl_->originalDefaultKeyChain_); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 125 | res = SecKeychainSetDefault(impl_->keyChainRef_); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 126 | if (res != errSecSuccess){ |
| 127 | _LOG_DEBUG("Fail to set default keychain: " << res); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 128 | throw Error("Fail to set default keychain"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 132 | SecTpmOsx::~SecTpmOsx(){ |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 133 | //TODO: implement |
| 134 | } |
| 135 | |
| 136 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 137 | SecTpmOsx::generateKeyPairInTpm(const Name & keyName, KeyType keyType, int keySize) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 138 | { |
| 139 | |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 140 | if(doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)){ |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 141 | _LOG_DEBUG("keyName has existed"); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 142 | throw Error("keyName has existed"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 145 | string keyNameUri = impl_->toInternalKeyName(keyName, KEY_CLASS_PUBLIC); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 146 | |
| 147 | SecKeyRef publicKey, privateKey; |
| 148 | |
| 149 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
Jeff Thompson | 556cc30 | 2013-10-22 17:40:07 -0700 | [diff] [blame] | 150 | keyNameUri.c_str(), |
| 151 | kCFStringEncodingUTF8); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 152 | |
| 153 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 154 | 3, |
| 155 | &kCFTypeDictionaryKeyCallBacks, |
| 156 | NULL); |
| 157 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 158 | CFDictionaryAddValue(attrDict, kSecAttrKeyType, impl_->getAsymKeyType(keyType)); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 159 | CFDictionaryAddValue(attrDict, kSecAttrKeySizeInBits, CFNumberCreate(NULL, kCFNumberIntType, &keySize)); |
| 160 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 161 | |
| 162 | OSStatus res = SecKeyGeneratePair((CFDictionaryRef)attrDict, &publicKey, &privateKey); |
| 163 | |
| 164 | CFRelease(publicKey); |
| 165 | CFRelease(privateKey); |
| 166 | |
| 167 | if (res != errSecSuccess){ |
| 168 | _LOG_DEBUG("Fail to create a key pair: " << res); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 169 | throw Error("Fail to create a key pair"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 174 | SecTpmOsx::generateSymmetricKeyInTpm(const Name & keyName, KeyType keyType, int keySize) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 175 | { |
| 176 | |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 177 | if(doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC)) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 178 | throw Error("keyName has existed!"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 180 | string keyNameUri = impl_->toInternalKeyName(keyName, KEY_CLASS_SYMMETRIC); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 181 | |
| 182 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(kCFAllocatorDefault, |
| 183 | 0, |
| 184 | &kCFTypeDictionaryKeyCallBacks, |
| 185 | &kCFTypeDictionaryValueCallBacks); |
| 186 | |
| 187 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
Jeff Thompson | 556cc30 | 2013-10-22 17:40:07 -0700 | [diff] [blame] | 188 | keyNameUri.c_str(), |
| 189 | kCFStringEncodingUTF8); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 191 | CFDictionaryAddValue(attrDict, kSecAttrKeyType, impl_->getSymKeyType(keyType)); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 192 | CFDictionaryAddValue(attrDict, kSecAttrKeySizeInBits, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &keySize)); |
| 193 | CFDictionaryAddValue(attrDict, kSecAttrIsPermanent, kCFBooleanTrue); |
| 194 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 195 | |
| 196 | CFErrorRef error = NULL; |
| 197 | |
| 198 | SecKeyRef symmetricKey = SecKeyGenerateSymmetric(attrDict, &error); |
| 199 | |
| 200 | if (error) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 201 | throw Error("Fail to create a symmetric key"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 204 | ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 205 | SecTpmOsx::getPublicKeyFromTpm(const Name & keyName) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 206 | { |
| 207 | _LOG_TRACE("OSXPrivateKeyStorage::getPublickey"); |
| 208 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 209 | SecKeychainItemRef publicKey = impl_->getKey(keyName, KEY_CLASS_PUBLIC); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 210 | |
| 211 | CFDataRef exportedKey; |
| 212 | |
| 213 | OSStatus res = SecItemExport(publicKey, |
| 214 | kSecFormatOpenSSL, |
| 215 | 0, |
| 216 | NULL, |
| 217 | &exportedKey); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 218 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 219 | return ptr_lib::make_shared<PublicKey>(CFDataGetBytePtr(exportedKey), CFDataGetLength(exportedKey)); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 222 | Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 223 | SecTpmOsx::signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 224 | { |
| 225 | _LOG_TRACE("OSXPrivateKeyStorage::Sign"); |
| 226 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 227 | CFDataRef dataRef = CFDataCreateWithBytesNoCopy(NULL, |
| 228 | data, |
| 229 | dataLength, |
| 230 | kCFAllocatorNull |
| 231 | ); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 232 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 233 | SecKeyRef privateKey = (SecKeyRef)impl_->getKey(keyName, KEY_CLASS_PRIVATE); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 234 | |
| 235 | CFErrorRef error; |
| 236 | SecTransformRef signer = SecSignTransformCreate((SecKeyRef)privateKey, &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 237 | if (error) throw Error("Fail to create signer"); |
| 238 | |
| 239 | // Set input |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 240 | Boolean set_res = SecTransformSetAttribute(signer, |
| 241 | kSecTransformInputAttributeName, |
| 242 | dataRef, |
| 243 | &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 244 | if (error) throw Error("Fail to configure input of signer"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 245 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 246 | // Enable use of padding |
| 247 | SecTransformSetAttribute( |
| 248 | signer, |
| 249 | kSecPaddingKey, |
| 250 | kSecPaddingPKCS1Key, |
| 251 | &error); |
| 252 | if (error) throw Error("Fail to configure digest algorithm of signer"); |
| 253 | |
| 254 | // Set padding type |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 255 | set_res = SecTransformSetAttribute(signer, |
| 256 | kSecDigestTypeAttribute, |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 257 | impl_->getDigestAlgorithm(digestAlgorithm), |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 258 | &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 259 | if (error) throw Error("Fail to configure digest algorithm of signer"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 260 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 261 | // Set padding attribute |
| 262 | long digestSize = impl_->getDigestSize(digestAlgorithm); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 263 | set_res = SecTransformSetAttribute(signer, |
| 264 | kSecDigestLengthAttribute, |
| 265 | CFNumberCreate(NULL, kCFNumberLongType, &digestSize), |
| 266 | &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 267 | if (error) throw Error("Fail to configure digest size of signer"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 268 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 269 | // Actually sign |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 270 | CFDataRef signature = (CFDataRef) SecTransformExecute(signer, &error); |
| 271 | if (error) { |
| 272 | CFShow(error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 273 | throw Error("Fail to sign data"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 276 | if (!signature) throw Error("Signature is NULL!\n"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 277 | |
Alexander Afanasyev | bd5ba40 | 2014-01-05 22:41:09 -0800 | [diff] [blame] | 278 | return Block(Tlv::SignatureValue, |
| 279 | ptr_lib::make_shared<Buffer>(CFDataGetBytePtr(signature), CFDataGetLength(signature))); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 282 | ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 283 | SecTpmOsx::decryptInTpm(const Name & keyName, const uint8_t* data, size_t dataLength, bool sym) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 284 | { |
| 285 | _LOG_TRACE("OSXPrivateKeyStorage::Decrypt"); |
| 286 | |
| 287 | KeyClass keyClass; |
| 288 | if(sym) |
| 289 | keyClass = KEY_CLASS_SYMMETRIC; |
| 290 | else |
| 291 | keyClass = KEY_CLASS_PRIVATE; |
| 292 | |
| 293 | CFDataRef dataRef = CFDataCreate(NULL, |
| 294 | reinterpret_cast<const unsigned char*>(data), |
| 295 | dataLength |
| 296 | ); |
| 297 | |
| 298 | // _LOG_DEBUG("CreateData"); |
| 299 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 300 | SecKeyRef decryptKey = (SecKeyRef)impl_->getKey(keyName, keyClass); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 301 | |
| 302 | // _LOG_DEBUG("GetKey"); |
| 303 | |
| 304 | CFErrorRef error; |
| 305 | SecTransformRef decrypt = SecDecryptTransformCreate(decryptKey, &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 306 | if (error) throw Error("Fail to create decrypt"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 307 | |
| 308 | Boolean set_res = SecTransformSetAttribute(decrypt, |
| 309 | kSecTransformInputAttributeName, |
| 310 | dataRef, |
| 311 | &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 312 | if (error) throw Error("Fail to configure decrypt"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 313 | |
| 314 | CFDataRef output = (CFDataRef) SecTransformExecute(decrypt, &error); |
| 315 | if (error) |
| 316 | { |
| 317 | CFShow(error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 318 | throw Error("Fail to decrypt data"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 319 | } |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 320 | if (!output) throw Error("Output is NULL!\n"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 321 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 322 | return ptr_lib::make_shared<Buffer>(CFDataGetBytePtr(output), CFDataGetLength(output)); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 325 | bool |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 326 | SecTpmOsx::setACL(const Name & keyName, KeyClass keyClass, int acl, const string & appPath) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 327 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 328 | SecKeychainItemRef privateKey = impl_->getKey(keyName, keyClass); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 329 | |
| 330 | SecAccessRef accRef; |
| 331 | OSStatus acc_res = SecKeychainItemCopyAccess(privateKey, &accRef); |
| 332 | |
| 333 | CFArrayRef signACL = SecAccessCopyMatchingACLList(accRef, |
| 334 | kSecACLAuthorizationSign); |
| 335 | |
| 336 | SecACLRef aclRef = (SecACLRef) CFArrayGetValueAtIndex(signACL, 0); |
| 337 | |
| 338 | CFArrayRef appList; |
| 339 | CFStringRef description; |
| 340 | SecKeychainPromptSelector promptSelector; |
| 341 | OSStatus acl_res = SecACLCopyContents(aclRef, |
| 342 | &appList, |
| 343 | &description, |
| 344 | &promptSelector); |
| 345 | |
| 346 | CFMutableArrayRef newAppList = CFArrayCreateMutableCopy(NULL, |
| 347 | 0, |
| 348 | appList); |
| 349 | |
| 350 | SecTrustedApplicationRef trustedApp; |
| 351 | acl_res = SecTrustedApplicationCreateFromPath(appPath.c_str(), |
| 352 | &trustedApp); |
| 353 | |
| 354 | CFArrayAppendValue(newAppList, trustedApp); |
| 355 | |
| 356 | |
| 357 | CFArrayRef authList = SecACLCopyAuthorizations(aclRef); |
| 358 | |
| 359 | acl_res = SecACLRemove(aclRef); |
| 360 | |
| 361 | SecACLRef newACL; |
| 362 | acl_res = SecACLCreateWithSimpleContents(accRef, |
| 363 | newAppList, |
| 364 | description, |
| 365 | promptSelector, |
| 366 | &newACL); |
| 367 | |
| 368 | acl_res = SecACLUpdateAuthorizations(newACL, authList); |
| 369 | |
| 370 | acc_res = SecKeychainItemSetAccess(privateKey, accRef); |
| 371 | |
| 372 | return true; |
| 373 | } |
| 374 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 375 | // bool |
| 376 | // OSXPrivateKeyStorage::verifyData(const Name & keyName, const Blob & pData, const Blob & pSig, DigestAlgorithm digestAlgo) |
| 377 | // { |
| 378 | // _LOG_TRACE("OSXPrivateKeyStorage::Verify"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 379 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 380 | // CFDataRef dataRef = CFDataCreate(NULL, |
| 381 | // reinterpret_cast<const unsigned char*>(pData.buf()), |
| 382 | // pData.size()); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 383 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 384 | // CFDataRef sigRef = CFDataCreate(NULL, |
| 385 | // reinterpret_cast<const unsigned char*>(pSig.buf()), |
| 386 | // pSig.size()); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 387 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 388 | // SecKeyRef publicKey = (SecKeyRef)impl_->getKey(keyName, KEY_CLASS_PUBLIC); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 389 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 390 | // CFErrorRef error; |
| 391 | // SecTransformRef verifier = SecVerifyTransformCreate(publicKey, sigRef, &error); |
| 392 | // if (error) throw Error("Fail to create verifier"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 393 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 394 | // Boolean set_res = SecTransformSetAttribute(verifier, |
| 395 | // kSecTransformInputAttributeName, |
| 396 | // dataRef, |
| 397 | // &error); |
| 398 | // if (error) throw Error("Fail to configure input of verifier"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 399 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 400 | // set_res = SecTransformSetAttribute(verifier, |
| 401 | // kSecDigestTypeAttribute, |
| 402 | // impl_->getDigestAlgorithm(digestAlgo), |
| 403 | // &error); |
| 404 | // if (error) throw Error("Fail to configure digest algorithm of verifier"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 405 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 406 | // long digestSize = impl_->getDigestSize(digestAlgo); |
| 407 | // set_res = SecTransformSetAttribute(verifier, |
| 408 | // kSecDigestLengthAttribute, |
| 409 | // CFNumberCreate(NULL, kCFNumberLongType, &digestSize), |
| 410 | // &error); |
| 411 | // if (error) throw Error("Fail to configure digest size of verifier"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 412 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 413 | // CFBooleanRef result = (CFBooleanRef) SecTransformExecute(verifier, &error); |
| 414 | // if (error) throw Error("Fail to verify data"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 415 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 416 | // if (result == kCFBooleanTrue) |
| 417 | // return true; |
| 418 | // else |
| 419 | // return false; |
| 420 | // } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 421 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 422 | ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 423 | SecTpmOsx::encryptInTpm(const Name & keyName, const uint8_t* data, size_t dataLength, bool sym) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 424 | { |
| 425 | _LOG_TRACE("OSXPrivateKeyStorage::Encrypt"); |
| 426 | |
| 427 | KeyClass keyClass; |
| 428 | if(sym) |
| 429 | keyClass = KEY_CLASS_SYMMETRIC; |
| 430 | else |
| 431 | keyClass = KEY_CLASS_PUBLIC; |
| 432 | |
| 433 | CFDataRef dataRef = CFDataCreate(NULL, |
| 434 | reinterpret_cast<const unsigned char*>(data), |
| 435 | dataLength |
| 436 | ); |
| 437 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 438 | SecKeyRef encryptKey = (SecKeyRef)impl_->getKey(keyName, keyClass); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 439 | |
| 440 | CFErrorRef error; |
| 441 | SecTransformRef encrypt = SecEncryptTransformCreate(encryptKey, &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 442 | if (error) throw Error("Fail to create encrypt"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 443 | |
| 444 | Boolean set_res = SecTransformSetAttribute(encrypt, |
| 445 | kSecTransformInputAttributeName, |
| 446 | dataRef, |
| 447 | &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 448 | if (error) throw Error("Fail to configure encrypt"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 449 | |
| 450 | CFDataRef output = (CFDataRef) SecTransformExecute(encrypt, &error); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 451 | if (error) throw Error("Fail to encrypt data"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 452 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 453 | if (!output) throw Error("Output is NULL!\n"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 454 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 455 | return ptr_lib::make_shared<Buffer> (CFDataGetBytePtr(output), CFDataGetLength(output)); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 458 | bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 459 | SecTpmOsx::doesKeyExistInTpm(const Name & keyName, KeyClass keyClass) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 460 | { |
| 461 | _LOG_TRACE("OSXPrivateKeyStorage::doesKeyExist"); |
| 462 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 463 | string keyNameUri = impl_->toInternalKeyName(keyName, keyClass); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 464 | |
| 465 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
Jeff Thompson | 556cc30 | 2013-10-22 17:40:07 -0700 | [diff] [blame] | 466 | keyNameUri.c_str(), |
| 467 | kCFStringEncodingUTF8); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 468 | |
| 469 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 470 | 3, |
| 471 | &kCFTypeDictionaryKeyCallBacks, |
| 472 | NULL); |
| 473 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 474 | CFDictionaryAddValue(attrDict, kSecAttrKeyClass, impl_->getKeyClass(keyClass)); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 475 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 476 | CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue); |
| 477 | |
| 478 | SecKeychainItemRef itemRef; |
| 479 | OSStatus res = SecItemCopyMatching((CFDictionaryRef)attrDict, (CFTypeRef*)&itemRef); |
| 480 | |
| 481 | if(res == errSecItemNotFound) |
| 482 | return true; |
| 483 | else |
| 484 | return false; |
| 485 | |
| 486 | } |
| 487 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 488 | |
| 489 | //////////////////////////////// |
| 490 | // OSXPrivateKeyStorage::Impl // |
| 491 | //////////////////////////////// |
| 492 | |
| 493 | SecKeychainItemRef |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 494 | SecTpmOsx::Impl::getKey(const Name & keyName, KeyClass keyClass) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 495 | { |
| 496 | string keyNameUri = toInternalKeyName(keyName, keyClass); |
| 497 | |
Jeff Thompson | 556cc30 | 2013-10-22 17:40:07 -0700 | [diff] [blame] | 498 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 499 | keyNameUri.c_str(), |
| 500 | kCFStringEncodingUTF8); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 501 | |
| 502 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 503 | 5, |
| 504 | &kCFTypeDictionaryKeyCallBacks, |
| 505 | NULL); |
| 506 | |
| 507 | CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey); |
| 508 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 509 | CFDictionaryAddValue(attrDict, kSecAttrKeyClass, getKeyClass(keyClass)); |
| 510 | CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue); |
| 511 | |
| 512 | SecKeychainItemRef keyItem; |
| 513 | |
| 514 | OSStatus res = SecItemCopyMatching((CFDictionaryRef) attrDict, (CFTypeRef*)&keyItem); |
| 515 | |
| 516 | if(res != errSecSuccess){ |
| 517 | _LOG_DEBUG("Fail to find the key!"); |
| 518 | return NULL; |
| 519 | } |
| 520 | else |
| 521 | return keyItem; |
| 522 | } |
| 523 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 524 | string |
| 525 | SecTpmOsx::Impl::toInternalKeyName(const Name & keyName, KeyClass keyClass) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 526 | { |
| 527 | string keyUri = keyName.toUri(); |
| 528 | |
| 529 | if(KEY_CLASS_SYMMETRIC == keyClass) |
| 530 | return keyUri + "/symmetric"; |
| 531 | else |
| 532 | return keyUri; |
| 533 | } |
| 534 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 535 | const CFTypeRef |
| 536 | SecTpmOsx::Impl::getAsymKeyType(KeyType keyType) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 537 | { |
| 538 | switch(keyType){ |
| 539 | case KEY_TYPE_RSA: |
| 540 | return kSecAttrKeyTypeRSA; |
| 541 | default: |
| 542 | _LOG_DEBUG("Unrecognized key type!") |
| 543 | return NULL; |
| 544 | } |
| 545 | } |
| 546 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 547 | const CFTypeRef |
| 548 | SecTpmOsx::Impl::getSymKeyType(KeyType keyType) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 549 | { |
| 550 | switch(keyType){ |
| 551 | case KEY_TYPE_AES: |
| 552 | return kSecAttrKeyTypeAES; |
| 553 | default: |
| 554 | _LOG_DEBUG("Unrecognized key type!") |
| 555 | return NULL; |
| 556 | } |
| 557 | } |
| 558 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 559 | const CFTypeRef |
| 560 | SecTpmOsx::Impl::getKeyClass(KeyClass keyClass) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 561 | { |
| 562 | switch(keyClass){ |
| 563 | case KEY_CLASS_PRIVATE: |
| 564 | return kSecAttrKeyClassPrivate; |
| 565 | case KEY_CLASS_PUBLIC: |
| 566 | return kSecAttrKeyClassPublic; |
| 567 | case KEY_CLASS_SYMMETRIC: |
| 568 | return kSecAttrKeyClassSymmetric; |
| 569 | default: |
| 570 | _LOG_DEBUG("Unrecognized key class!"); |
| 571 | return NULL; |
| 572 | } |
| 573 | } |
| 574 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 575 | const CFStringRef |
| 576 | SecTpmOsx::Impl::getDigestAlgorithm(DigestAlgorithm digestAlgo) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 577 | { |
| 578 | switch(digestAlgo){ |
| 579 | // case DIGEST_MD2: |
| 580 | // return kSecDigestMD2; |
| 581 | // case DIGEST_MD5: |
| 582 | // return kSecDigestMD5; |
| 583 | // case DIGEST_SHA1: |
| 584 | // return kSecDigestSHA1; |
| 585 | case DIGEST_ALGORITHM_SHA256: |
| 586 | return kSecDigestSHA2; |
| 587 | default: |
| 588 | _LOG_DEBUG("Unrecognized digest algorithm!"); |
| 589 | return NULL; |
| 590 | } |
| 591 | } |
| 592 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 593 | long |
| 594 | SecTpmOsx::Impl::getDigestSize(DigestAlgorithm digestAlgo) |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 595 | { |
| 596 | switch(digestAlgo){ |
| 597 | case DIGEST_ALGORITHM_SHA256: |
| 598 | return 256; |
| 599 | // case DIGEST_SHA1: |
| 600 | // case DIGEST_MD2: |
| 601 | // case DIGEST_MD5: |
| 602 | // return 0; |
| 603 | default: |
| 604 | _LOG_DEBUG("Unrecognized digest algorithm! Unknown digest size"); |
| 605 | return -1; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | } |