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 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 10 | #include "sec-tpm-osx.hpp" |
| 11 | |
| 12 | #include "security/public-key.hpp" |
| 13 | #include "util/logging.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame^] | 14 | #include "cryptopp.hpp" |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 15 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 16 | #include <pwd.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 21 | #include <CoreFoundation/CoreFoundation.h> |
| 22 | #include <Security/Security.h> |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 23 | #include <Security/SecRandom.h> |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 24 | #include <CoreServices/CoreServices.h> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 27 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 28 | INIT_LOGGER("ndn.SecTpmOsx"); |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 32 | class SecTpmOsx::Impl { |
| 33 | public: |
| 34 | Impl() |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 35 | : m_passwordSet(false) |
| 36 | , m_inTerminal(false) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 37 | {} |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 38 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 39 | /** |
| 40 | * @brief Convert NDN name of a key to internal name of the key. |
| 41 | * |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 42 | * @param keyName |
| 43 | * @param keyClass |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 44 | * @return the internal key name |
| 45 | */ |
| 46 | std::string |
| 47 | toInternalKeyName(const Name & keyName, KeyClass keyClass); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 48 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 49 | /** |
| 50 | * @brief Get key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 51 | * |
| 52 | * @param keyName |
| 53 | * @param keyClass |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 54 | * @returns pointer to the key |
| 55 | */ |
| 56 | SecKeychainItemRef |
| 57 | getKey(const Name & keyName, KeyClass keyClass); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 58 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 59 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 60 | * @brief Convert keyType to MAC OS symmetric key key type |
| 61 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 62 | * @param keyType |
| 63 | * @returns MAC OS key type |
| 64 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 65 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 66 | getSymKeyType(KeyType keyType); |
| 67 | |
| 68 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 69 | * @brief Convert keyType to MAC OS asymmetirc key type |
| 70 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 71 | * @param keyType |
| 72 | * @returns MAC OS key type |
| 73 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 74 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 75 | getAsymKeyType(KeyType keyType); |
| 76 | |
| 77 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 78 | * @brief Convert keyClass to MAC OS key class |
| 79 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 80 | * @param keyClass |
| 81 | * @returns MAC OS key class |
| 82 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 83 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 84 | getKeyClass(KeyClass keyClass); |
| 85 | |
| 86 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 87 | * @brief Convert digestAlgo to MAC OS algorithm id |
| 88 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 89 | * @param digestAlgo |
| 90 | * @returns MAC OS algorithm id |
| 91 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 92 | const CFStringRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 93 | getDigestAlgorithm(DigestAlgorithm digestAlgo); |
| 94 | |
| 95 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 96 | * @brief Get the digest size of the corresponding algorithm |
| 97 | * |
| 98 | * @param digestAlgo |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 99 | * @return digest size |
| 100 | */ |
| 101 | long |
| 102 | getDigestSize(DigestAlgorithm digestAlgo); |
| 103 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 104 | /////////////////////////////////////////////// |
| 105 | // everything here is public, including data // |
| 106 | /////////////////////////////////////////////// |
| 107 | public: |
| 108 | SecKeychainRef m_keyChainRef; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 109 | bool m_passwordSet; |
| 110 | string m_password; |
| 111 | bool m_inTerminal; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | |
| 115 | SecTpmOsx::SecTpmOsx() |
| 116 | : m_impl(new Impl) |
| 117 | { |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 118 | if(m_impl->m_inTerminal) |
| 119 | SecKeychainSetUserInteractionAllowed (false); |
| 120 | else |
| 121 | SecKeychainSetUserInteractionAllowed (true); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 122 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 123 | OSStatus res = SecKeychainCopyDefault(&m_impl->m_keyChainRef); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 124 | |
| 125 | if (res == errSecNoDefaultKeychain) //If no default key chain, create one. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 126 | throw Error("No default keychain, create one first!"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | SecTpmOsx::~SecTpmOsx(){ |
| 130 | //TODO: implement |
| 131 | } |
| 132 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 133 | void |
| 134 | SecTpmOsx::setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 135 | { |
| 136 | m_impl->m_passwordSet = true; |
| 137 | memset(const_cast<char*>(m_impl->m_password.c_str()), 0, m_impl->m_password.size()); |
| 138 | m_impl->m_password.clear(); |
| 139 | m_impl->m_password.append(reinterpret_cast<const char*>(password), passwordLength); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | SecTpmOsx::resetTpmPassword() |
| 144 | { |
| 145 | m_impl->m_passwordSet = false; |
| 146 | memset(const_cast<char*>(m_impl->m_password.c_str()), 0, m_impl->m_password.size()); |
| 147 | m_impl->m_password.clear(); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | SecTpmOsx::setInTerminal(bool inTerminal) |
| 152 | { |
| 153 | m_impl->m_inTerminal = inTerminal; |
| 154 | if(inTerminal) |
| 155 | SecKeychainSetUserInteractionAllowed (false); |
| 156 | else |
| 157 | SecKeychainSetUserInteractionAllowed (true); |
| 158 | } |
| 159 | |
| 160 | bool |
| 161 | SecTpmOsx::getInTerminal() |
| 162 | { |
| 163 | return m_impl->m_inTerminal; |
| 164 | } |
| 165 | |
| 166 | bool |
| 167 | SecTpmOsx::locked() |
| 168 | { |
| 169 | SecKeychainStatus keychainStatus; |
| 170 | |
| 171 | OSStatus res = SecKeychainGetStatus(m_impl->m_keyChainRef, &keychainStatus); |
| 172 | if(res != errSecSuccess) |
| 173 | return true; |
| 174 | else |
| 175 | return ((kSecUnlockStateStatus & keychainStatus) == 0); |
| 176 | } |
| 177 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 178 | bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 179 | SecTpmOsx::unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
| 180 | { |
| 181 | OSStatus res; |
| 182 | |
| 183 | // If the default key chain is already unlocked, return immediately. |
| 184 | if(!locked()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 185 | return true; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 186 | |
| 187 | // If the default key chain is locked, unlock the key chain. |
| 188 | if(usePassword) |
| 189 | { |
| 190 | // Use the supplied password. |
| 191 | res = SecKeychainUnlock(m_impl->m_keyChainRef, |
| 192 | passwordLength, |
| 193 | password, |
| 194 | true); |
| 195 | } |
| 196 | else if(m_impl->m_passwordSet) |
| 197 | { |
| 198 | // If no password supplied, then use the configured password if exists. |
| 199 | SecKeychainUnlock(m_impl->m_keyChainRef, |
| 200 | m_impl->m_password.size(), |
| 201 | m_impl->m_password.c_str(), |
| 202 | true); |
| 203 | } |
| 204 | else if(m_impl->m_inTerminal) |
| 205 | { |
| 206 | // If no configured password, get password from terminal if inTerminal set. |
| 207 | bool locked = true; |
| 208 | const char* fmt = "Password to unlock the default keychain: "; |
| 209 | int count = 0; |
| 210 | |
| 211 | while(locked) |
| 212 | { |
| 213 | if(count > 2) |
| 214 | break; |
| 215 | |
| 216 | char* getPassword = NULL; |
| 217 | getPassword = getpass(fmt); |
| 218 | count++; |
| 219 | |
| 220 | if (!getPassword) |
| 221 | continue; |
| 222 | |
| 223 | res = SecKeychainUnlock(m_impl->m_keyChainRef, |
| 224 | strlen(getPassword), |
| 225 | getPassword, |
| 226 | true); |
| 227 | |
| 228 | memset(getPassword, 0, strlen(getPassword)); |
| 229 | |
| 230 | if(res == errSecSuccess) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 231 | break; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | // If inTerminal is not set, get the password from GUI. |
| 237 | SecKeychainUnlock(m_impl->m_keyChainRef, 0, 0, false); |
| 238 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 239 | |
| 240 | return !locked(); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 243 | void |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 244 | SecTpmOsx::generateKeyPairInTpmInternal(const Name & keyName, KeyType keyType, int keySize, bool retry) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 245 | { |
| 246 | |
| 247 | if(doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)){ |
| 248 | _LOG_DEBUG("keyName has existed"); |
| 249 | throw Error("keyName has existed"); |
| 250 | } |
| 251 | |
| 252 | string keyNameUri = m_impl->toInternalKeyName(keyName, KEY_CLASS_PUBLIC); |
| 253 | |
| 254 | SecKeyRef publicKey, privateKey; |
| 255 | |
| 256 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 257 | keyNameUri.c_str(), |
| 258 | kCFStringEncodingUTF8); |
| 259 | |
| 260 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 261 | 3, |
| 262 | &kCFTypeDictionaryKeyCallBacks, |
| 263 | NULL); |
| 264 | |
| 265 | CFDictionaryAddValue(attrDict, kSecAttrKeyType, m_impl->getAsymKeyType(keyType)); |
| 266 | CFDictionaryAddValue(attrDict, kSecAttrKeySizeInBits, CFNumberCreate(NULL, kCFNumberIntType, &keySize)); |
| 267 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 268 | |
| 269 | OSStatus res = SecKeyGeneratePair((CFDictionaryRef)attrDict, &publicKey, &privateKey); |
| 270 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 271 | if (res == errSecSuccess) |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 272 | { |
| 273 | CFRelease(publicKey); |
| 274 | CFRelease(privateKey); |
| 275 | return; |
| 276 | } |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 277 | |
| 278 | if (res == errSecAuthFailed && !retry) |
| 279 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 280 | if(unlockTpm(0, 0, false)) |
| 281 | generateKeyPairInTpmInternal(keyName, keyType, keySize, true); |
| 282 | else |
| 283 | throw Error("Fail to unlock the keychain"); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 284 | } |
| 285 | else |
| 286 | { |
| 287 | _LOG_DEBUG("Fail to create a key pair: " << res); |
| 288 | throw Error("Fail to create a key pair"); |
| 289 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 293 | SecTpmOsx::deleteKeyPairInTpmInternal(const Name &keyName, bool retry) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 294 | { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 295 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 296 | keyName.toUri().c_str(), |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 297 | kCFStringEncodingUTF8); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 298 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 299 | CFMutableDictionaryRef searchDict = |
| 300 | CFDictionaryCreateMutable(NULL, 5, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 301 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 302 | CFDictionaryAddValue(searchDict, kSecClass, kSecClassKey); |
| 303 | CFDictionaryAddValue(searchDict, kSecAttrLabel, keyLabel); |
| 304 | CFDictionaryAddValue(searchDict, kSecMatchLimit, kSecMatchLimitAll); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 305 | OSStatus res = SecItemDelete(searchDict); |
| 306 | |
| 307 | if (res == errSecSuccess) |
| 308 | return; |
| 309 | |
| 310 | if (res == errSecAuthFailed && !retry) |
| 311 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 312 | if(unlockTpm(0, 0, false)) |
| 313 | deleteKeyPairInTpmInternal(keyName, true); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 314 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void |
| 318 | SecTpmOsx::generateSymmetricKeyInTpm(const Name & keyName, KeyType keyType, int keySize) |
| 319 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 320 | throw Error("SecTpmOsx::generateSymmetricKeyInTpm is not supported"); |
| 321 | // if(doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC)) |
| 322 | // throw Error("keyName has existed!"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 323 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 324 | // string keyNameUri = m_impl->toInternalKeyName(keyName, KEY_CLASS_SYMMETRIC); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 325 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 326 | // CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(kCFAllocatorDefault, |
| 327 | // 0, |
| 328 | // &kCFTypeDictionaryKeyCallBacks, |
| 329 | // &kCFTypeDictionaryValueCallBacks); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 330 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 331 | // CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 332 | // keyNameUri.c_str(), |
| 333 | // kCFStringEncodingUTF8); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 334 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 335 | // CFDictionaryAddValue(attrDict, kSecAttrKeyType, m_impl->getSymKeyType(keyType)); |
| 336 | // CFDictionaryAddValue(attrDict, kSecAttrKeySizeInBits, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &keySize)); |
| 337 | // CFDictionaryAddValue(attrDict, kSecAttrIsPermanent, kCFBooleanTrue); |
| 338 | // CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 339 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 340 | // CFErrorRef error = NULL; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 341 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 342 | // SecKeyRef symmetricKey = SecKeyGenerateSymmetric(attrDict, &error); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 343 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 344 | // if (error) |
| 345 | // throw Error("Fail to create a symmetric key"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 348 | shared_ptr<PublicKey> |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 349 | SecTpmOsx::getPublicKeyFromTpm(const Name & keyName) |
| 350 | { |
| 351 | _LOG_TRACE("OSXPrivateKeyStorage::getPublickey"); |
| 352 | |
| 353 | SecKeychainItemRef publicKey = m_impl->getKey(keyName, KEY_CLASS_PUBLIC); |
| 354 | |
| 355 | CFDataRef exportedKey; |
| 356 | |
| 357 | OSStatus res = SecItemExport(publicKey, |
| 358 | kSecFormatOpenSSL, |
| 359 | 0, |
| 360 | NULL, |
| 361 | &exportedKey); |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 362 | if (res != errSecSuccess) |
| 363 | { |
| 364 | throw Error("Cannot export requested public key from OSX Keychain"); |
| 365 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 366 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 367 | shared_ptr<PublicKey> key = make_shared<PublicKey>(CFDataGetBytePtr(exportedKey), CFDataGetLength(exportedKey)); |
| 368 | CFRelease(exportedKey); |
| 369 | return key; |
| 370 | } |
| 371 | |
| 372 | ConstBufferPtr |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 373 | SecTpmOsx::exportPrivateKeyPkcs1FromTpmInternal(const Name& keyName, bool retry) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 374 | { |
| 375 | using namespace CryptoPP; |
| 376 | |
| 377 | SecKeychainItemRef privateKey = m_impl->getKey(keyName, KEY_CLASS_PRIVATE); |
| 378 | CFDataRef exportedKey; |
| 379 | OSStatus res = SecItemExport(privateKey, |
| 380 | kSecFormatOpenSSL, |
| 381 | 0, |
| 382 | NULL, |
| 383 | &exportedKey); |
| 384 | |
| 385 | if(res != errSecSuccess) |
| 386 | { |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 387 | if(res == errSecAuthFailed && !retry) |
| 388 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 389 | if(unlockTpm(0, 0, false)) |
| 390 | return exportPrivateKeyPkcs1FromTpmInternal(keyName, true); |
| 391 | else |
| 392 | return shared_ptr<Buffer>(); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 393 | } |
| 394 | else |
| 395 | return shared_ptr<Buffer>(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | OBufferStream pkcs1Os; |
| 399 | FileSink sink(pkcs1Os); |
| 400 | |
| 401 | uint32_t version = 0; |
| 402 | OID algorithm("1.2.840.113549.1.1.1"); |
| 403 | SecByteBlock rawKeyBits; |
| 404 | // PrivateKeyInfo ::= SEQUENCE { |
| 405 | // version INTEGER, |
| 406 | // privateKeyAlgorithm SEQUENCE, |
| 407 | // privateKey OCTECT STRING} |
| 408 | DERSequenceEncoder privateKeyInfo(sink); |
| 409 | { |
| 410 | DEREncodeUnsigned<uint32_t>(privateKeyInfo, version, INTEGER); |
| 411 | DERSequenceEncoder privateKeyAlgorithm(privateKeyInfo); |
| 412 | { |
| 413 | algorithm.encode(privateKeyAlgorithm); |
| 414 | DEREncodeNull(privateKeyAlgorithm); |
| 415 | } |
| 416 | privateKeyAlgorithm.MessageEnd(); |
| 417 | DEREncodeOctetString(privateKeyInfo, CFDataGetBytePtr(exportedKey), CFDataGetLength(exportedKey)); |
| 418 | } |
| 419 | privateKeyInfo.MessageEnd(); |
| 420 | |
| 421 | CFRelease(exportedKey); |
| 422 | return pkcs1Os.buf(); |
| 423 | } |
| 424 | |
| 425 | bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 426 | SecTpmOsx::importPrivateKeyPkcs1IntoTpmInternal(const Name& keyName, const uint8_t* buf, size_t size, bool retry) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 427 | { |
| 428 | using namespace CryptoPP; |
| 429 | |
| 430 | StringSource privateKeySource(buf, size, true); |
| 431 | uint32_t tmpNum; |
| 432 | OID tmpOID; |
| 433 | SecByteBlock rawKeyBits; |
| 434 | // PrivateKeyInfo ::= SEQUENCE { |
| 435 | // INTEGER, |
| 436 | // SEQUENCE, |
| 437 | // OCTECT STRING} |
| 438 | BERSequenceDecoder privateKeyInfo(privateKeySource); |
| 439 | { |
| 440 | BERDecodeUnsigned<uint32_t>(privateKeyInfo, tmpNum, INTEGER); |
| 441 | BERSequenceDecoder sequenceDecoder(privateKeyInfo); |
| 442 | { |
| 443 | tmpOID.decode(sequenceDecoder); |
| 444 | BERDecodeNull(sequenceDecoder); |
| 445 | } |
| 446 | BERDecodeOctetString(privateKeyInfo, rawKeyBits); |
| 447 | } |
| 448 | privateKeyInfo.MessageEnd(); |
| 449 | |
| 450 | CFDataRef importedKey = CFDataCreateWithBytesNoCopy(NULL, |
| 451 | rawKeyBits.BytePtr(), |
| 452 | rawKeyBits.size(), |
| 453 | kCFAllocatorNull); |
| 454 | |
| 455 | SecExternalFormat externalFormat = kSecFormatOpenSSL; |
| 456 | SecExternalItemType externalType = kSecItemTypePrivateKey; |
| 457 | SecKeyImportExportParameters keyParams; |
| 458 | memset(&keyParams, 0, sizeof(keyParams)); |
| 459 | keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION; |
| 460 | keyParams.keyAttributes = CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_PERMANENT; |
| 461 | SecAccessRef access; |
| 462 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 463 | keyName.toUri().c_str(), |
| 464 | kCFStringEncodingUTF8); |
| 465 | SecAccessCreate(keyLabel, NULL, &access); |
| 466 | keyParams.accessRef = access; |
| 467 | CFArrayRef outItems; |
| 468 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame^] | 469 | #pragma clang diagnostic push |
| 470 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 471 | OSStatus res = SecKeychainItemImport (importedKey, |
| 472 | NULL, |
| 473 | &externalFormat, |
| 474 | &externalType, |
| 475 | 0, |
| 476 | &keyParams, |
| 477 | m_impl->m_keyChainRef, |
| 478 | &outItems); |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame^] | 479 | #pragma clang diagnostic pop |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 480 | |
| 481 | if(res != errSecSuccess) |
| 482 | { |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 483 | if(res == errSecAuthFailed && !retry) |
| 484 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 485 | if(unlockTpm(0, 0, false)) |
| 486 | return importPrivateKeyPkcs1IntoTpmInternal(keyName, buf, size, true); |
| 487 | else |
| 488 | return false; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | return false; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | SecKeychainItemRef privateKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems, 0); |
| 495 | SecKeychainAttribute attrs[1]; // maximum number of attributes |
| 496 | SecKeychainAttributeList attrList = { 0, attrs }; |
| 497 | string keyUri = keyName.toUri(); |
| 498 | { |
| 499 | attrs[attrList.count].tag = kSecKeyPrintName; |
| 500 | attrs[attrList.count].length = keyUri.size(); |
| 501 | attrs[attrList.count].data = (void *)keyUri.c_str(); |
| 502 | attrList.count++; |
| 503 | } |
| 504 | |
| 505 | res = SecKeychainItemModifyAttributesAndData(privateKey, |
| 506 | &attrList, |
| 507 | 0, |
| 508 | NULL); |
| 509 | |
| 510 | if(res != errSecSuccess) |
| 511 | { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | CFRelease(importedKey); |
| 516 | return true; |
| 517 | } |
| 518 | |
| 519 | bool |
| 520 | SecTpmOsx::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 521 | { |
| 522 | CFDataRef importedKey = CFDataCreateWithBytesNoCopy(NULL, |
| 523 | buf, |
| 524 | size, |
| 525 | kCFAllocatorNull); |
| 526 | |
| 527 | SecExternalFormat externalFormat = kSecFormatOpenSSL; |
| 528 | SecExternalItemType externalType = kSecItemTypePublicKey; |
| 529 | CFArrayRef outItems; |
| 530 | |
| 531 | OSStatus res = SecItemImport (importedKey, |
| 532 | NULL, |
| 533 | &externalFormat, |
| 534 | &externalType, |
| 535 | 0, |
| 536 | NULL, |
| 537 | m_impl->m_keyChainRef, |
| 538 | &outItems); |
| 539 | |
| 540 | if(res != errSecSuccess) |
| 541 | return false; |
| 542 | |
| 543 | SecKeychainItemRef publicKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems, 0); |
| 544 | SecKeychainAttribute attrs[1]; // maximum number of attributes |
| 545 | SecKeychainAttributeList attrList = { 0, attrs }; |
| 546 | string keyUri = keyName.toUri(); |
| 547 | { |
| 548 | attrs[attrList.count].tag = kSecKeyPrintName; |
| 549 | attrs[attrList.count].length = keyUri.size(); |
| 550 | attrs[attrList.count].data = (void *)keyUri.c_str(); |
| 551 | attrList.count++; |
| 552 | } |
| 553 | |
| 554 | res = SecKeychainItemModifyAttributesAndData(publicKey, |
| 555 | &attrList, |
| 556 | 0, |
| 557 | NULL); |
| 558 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 559 | if(res != errSecSuccess) |
| 560 | return false; |
| 561 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 562 | CFRelease(importedKey); |
| 563 | return true; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | Block |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 567 | SecTpmOsx::signInTpmInternal(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm, bool retry) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 568 | { |
| 569 | _LOG_TRACE("OSXPrivateKeyStorage::Sign"); |
| 570 | |
| 571 | CFDataRef dataRef = CFDataCreateWithBytesNoCopy(NULL, |
| 572 | data, |
| 573 | dataLength, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 574 | kCFAllocatorNull); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 575 | |
| 576 | SecKeyRef privateKey = (SecKeyRef)m_impl->getKey(keyName, KEY_CLASS_PRIVATE); |
| 577 | |
| 578 | CFErrorRef error; |
| 579 | SecTransformRef signer = SecSignTransformCreate((SecKeyRef)privateKey, &error); |
| 580 | if (error) throw Error("Fail to create signer"); |
| 581 | |
| 582 | // Set input |
| 583 | Boolean set_res = SecTransformSetAttribute(signer, |
| 584 | kSecTransformInputAttributeName, |
| 585 | dataRef, |
| 586 | &error); |
| 587 | if (error) throw Error("Fail to configure input of signer"); |
| 588 | |
| 589 | // Enable use of padding |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 590 | SecTransformSetAttribute(signer, |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 591 | kSecPaddingKey, |
| 592 | kSecPaddingPKCS1Key, |
| 593 | &error); |
| 594 | if (error) throw Error("Fail to configure digest algorithm of signer"); |
| 595 | |
| 596 | // Set padding type |
| 597 | set_res = SecTransformSetAttribute(signer, |
| 598 | kSecDigestTypeAttribute, |
| 599 | m_impl->getDigestAlgorithm(digestAlgorithm), |
| 600 | &error); |
| 601 | if (error) throw Error("Fail to configure digest algorithm of signer"); |
| 602 | |
| 603 | // Set padding attribute |
| 604 | long digestSize = m_impl->getDigestSize(digestAlgorithm); |
| 605 | set_res = SecTransformSetAttribute(signer, |
| 606 | kSecDigestLengthAttribute, |
| 607 | CFNumberCreate(NULL, kCFNumberLongType, &digestSize), |
| 608 | &error); |
| 609 | if (error) throw Error("Fail to configure digest size of signer"); |
| 610 | |
| 611 | // Actually sign |
| 612 | CFDataRef signature = (CFDataRef) SecTransformExecute(signer, &error); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 613 | if (error) |
| 614 | { |
| 615 | if(!retry) |
| 616 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 617 | if(unlockTpm(0, 0, false)) |
| 618 | return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, true); |
| 619 | else |
| 620 | throw Error("Fail to unlock the keychain"); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 621 | } |
| 622 | else |
| 623 | { |
| 624 | CFShow(error); |
| 625 | throw Error("Fail to sign data"); |
| 626 | } |
| 627 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 628 | |
| 629 | if (!signature) throw Error("Signature is NULL!\n"); |
| 630 | |
| 631 | return Block(Tlv::SignatureValue, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 632 | make_shared<Buffer>(CFDataGetBytePtr(signature), CFDataGetLength(signature))); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 636 | SecTpmOsx::decryptInTpm(const uint8_t* data, size_t dataLength, const Name & keyName, bool sym) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 637 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 638 | throw Error("SecTpmOsx::decryptInTpm is not supported"); |
| 639 | // _LOG_TRACE("OSXPrivateKeyStorage::Decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 640 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 641 | // KeyClass keyClass; |
| 642 | // if(sym) |
| 643 | // keyClass = KEY_CLASS_SYMMETRIC; |
| 644 | // else |
| 645 | // keyClass = KEY_CLASS_PRIVATE; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 646 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 647 | // CFDataRef dataRef = CFDataCreate(NULL, |
| 648 | // reinterpret_cast<const unsigned char*>(data), |
| 649 | // dataLength |
| 650 | // ); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 651 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 652 | // // _LOG_DEBUG("CreateData"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 653 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 654 | // SecKeyRef decryptKey = (SecKeyRef)m_impl->getKey(keyName, keyClass); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 655 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 656 | // // _LOG_DEBUG("GetKey"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 657 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 658 | // CFErrorRef error; |
| 659 | // SecTransformRef decrypt = SecDecryptTransformCreate(decryptKey, &error); |
| 660 | // if (error) throw Error("Fail to create decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 661 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 662 | // Boolean set_res = SecTransformSetAttribute(decrypt, |
| 663 | // kSecTransformInputAttributeName, |
| 664 | // dataRef, |
| 665 | // &error); |
| 666 | // if (error) throw Error("Fail to configure decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 667 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 668 | // CFDataRef output = (CFDataRef) SecTransformExecute(decrypt, &error); |
| 669 | // if (error) |
| 670 | // { |
| 671 | // CFShow(error); |
| 672 | // throw Error("Fail to decrypt data"); |
| 673 | // } |
| 674 | // if (!output) throw Error("Output is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 675 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 676 | // return make_shared<Buffer>(CFDataGetBytePtr(output), CFDataGetLength(output)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 679 | void |
| 680 | SecTpmOsx::addAppToACL(const Name & keyName, KeyClass keyClass, const string & appPath, AclType acl) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 681 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 682 | if(keyClass == KEY_CLASS_PRIVATE && acl == ACL_TYPE_PRIVATE) |
| 683 | { |
| 684 | SecKeychainItemRef privateKey = m_impl->getKey(keyName, keyClass); |
| 685 | |
| 686 | SecAccessRef accRef; |
| 687 | OSStatus acc_res = SecKeychainItemCopyAccess(privateKey, &accRef); |
| 688 | |
| 689 | CFArrayRef signACL = SecAccessCopyMatchingACLList(accRef, |
| 690 | kSecACLAuthorizationSign); |
| 691 | |
| 692 | SecACLRef aclRef = (SecACLRef) CFArrayGetValueAtIndex(signACL, 0); |
| 693 | |
| 694 | CFArrayRef appList; |
| 695 | CFStringRef description; |
| 696 | SecKeychainPromptSelector promptSelector; |
| 697 | OSStatus acl_res = SecACLCopyContents(aclRef, |
| 698 | &appList, |
| 699 | &description, |
| 700 | &promptSelector); |
| 701 | |
| 702 | CFMutableArrayRef newAppList = CFArrayCreateMutableCopy(NULL, |
| 703 | 0, |
| 704 | appList); |
| 705 | |
| 706 | SecTrustedApplicationRef trustedApp; |
| 707 | acl_res = SecTrustedApplicationCreateFromPath(appPath.c_str(), |
| 708 | &trustedApp); |
| 709 | |
| 710 | CFArrayAppendValue(newAppList, trustedApp); |
| 711 | |
| 712 | acl_res = SecACLSetContents(aclRef, |
| 713 | newAppList, |
| 714 | description, |
| 715 | promptSelector); |
| 716 | |
| 717 | acc_res = SecKeychainItemSetAccess(privateKey, accRef); |
| 718 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 721 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 722 | SecTpmOsx::encryptInTpm(const uint8_t* data, size_t dataLength, const Name & keyName, bool sym) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 723 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 724 | throw Error("SecTpmOsx::encryptInTpm is not supported"); |
| 725 | // _LOG_TRACE("OSXPrivateKeyStorage::Encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 726 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 727 | // KeyClass keyClass; |
| 728 | // if(sym) |
| 729 | // keyClass = KEY_CLASS_SYMMETRIC; |
| 730 | // else |
| 731 | // keyClass = KEY_CLASS_PUBLIC; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 732 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 733 | // CFDataRef dataRef = CFDataCreate(NULL, |
| 734 | // reinterpret_cast<const unsigned char*>(data), |
| 735 | // dataLength |
| 736 | // ); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 737 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 738 | // SecKeyRef encryptKey = (SecKeyRef)m_impl->getKey(keyName, keyClass); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 739 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 740 | // CFErrorRef error; |
| 741 | // SecTransformRef encrypt = SecEncryptTransformCreate(encryptKey, &error); |
| 742 | // if (error) throw Error("Fail to create encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 743 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 744 | // Boolean set_res = SecTransformSetAttribute(encrypt, |
| 745 | // kSecTransformInputAttributeName, |
| 746 | // dataRef, |
| 747 | // &error); |
| 748 | // if (error) throw Error("Fail to configure encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 749 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 750 | // CFDataRef output = (CFDataRef) SecTransformExecute(encrypt, &error); |
| 751 | // if (error) throw Error("Fail to encrypt data"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 752 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 753 | // if (!output) throw Error("Output is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 754 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 755 | // return make_shared<Buffer> (CFDataGetBytePtr(output), CFDataGetLength(output)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | bool |
| 759 | SecTpmOsx::doesKeyExistInTpm(const Name & keyName, KeyClass keyClass) |
| 760 | { |
| 761 | _LOG_TRACE("OSXPrivateKeyStorage::doesKeyExist"); |
| 762 | |
| 763 | string keyNameUri = m_impl->toInternalKeyName(keyName, keyClass); |
| 764 | |
| 765 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 766 | keyNameUri.c_str(), |
| 767 | kCFStringEncodingUTF8); |
| 768 | |
| 769 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 770 | 4, |
| 771 | &kCFTypeDictionaryKeyCallBacks, |
| 772 | NULL); |
| 773 | |
| 774 | CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 775 | // CFDictionaryAddValue(attrDict, kSecAttrKeyClass, m_impl->getKeyClass(keyClass)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 776 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 777 | CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue); |
| 778 | |
| 779 | SecKeychainItemRef itemRef; |
| 780 | OSStatus res = SecItemCopyMatching((CFDictionaryRef)attrDict, (CFTypeRef*)&itemRef); |
| 781 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 782 | if(res == errSecSuccess) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 783 | return true; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 784 | else |
| 785 | return false; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 786 | |
| 787 | } |
| 788 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 789 | bool |
| 790 | SecTpmOsx::generateRandomBlock(uint8_t* res, size_t size) |
| 791 | { |
| 792 | return (SecRandomCopyBytes(kSecRandomDefault, size, res) == 0); |
| 793 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 794 | |
| 795 | //////////////////////////////// |
| 796 | // OSXPrivateKeyStorage::Impl // |
| 797 | //////////////////////////////// |
| 798 | |
| 799 | SecKeychainItemRef |
| 800 | SecTpmOsx::Impl::getKey(const Name & keyName, KeyClass keyClass) |
| 801 | { |
| 802 | string keyNameUri = toInternalKeyName(keyName, keyClass); |
| 803 | |
| 804 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 805 | keyNameUri.c_str(), |
| 806 | kCFStringEncodingUTF8); |
| 807 | |
| 808 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 809 | 5, |
| 810 | &kCFTypeDictionaryKeyCallBacks, |
| 811 | NULL); |
| 812 | |
| 813 | CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey); |
| 814 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 815 | CFDictionaryAddValue(attrDict, kSecAttrKeyClass, getKeyClass(keyClass)); |
| 816 | CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue); |
| 817 | |
| 818 | SecKeychainItemRef keyItem; |
| 819 | |
| 820 | OSStatus res = SecItemCopyMatching((CFDictionaryRef) attrDict, (CFTypeRef*)&keyItem); |
| 821 | |
| 822 | if(res != errSecSuccess){ |
| 823 | _LOG_DEBUG("Fail to find the key!"); |
| 824 | return NULL; |
| 825 | } |
| 826 | else |
| 827 | return keyItem; |
| 828 | } |
| 829 | |
| 830 | string |
| 831 | SecTpmOsx::Impl::toInternalKeyName(const Name & keyName, KeyClass keyClass) |
| 832 | { |
| 833 | string keyUri = keyName.toUri(); |
| 834 | |
| 835 | if(KEY_CLASS_SYMMETRIC == keyClass) |
| 836 | return keyUri + "/symmetric"; |
| 837 | else |
| 838 | return keyUri; |
| 839 | } |
| 840 | |
| 841 | const CFTypeRef |
| 842 | SecTpmOsx::Impl::getAsymKeyType(KeyType keyType) |
| 843 | { |
| 844 | switch(keyType){ |
| 845 | case KEY_TYPE_RSA: |
| 846 | return kSecAttrKeyTypeRSA; |
| 847 | default: |
| 848 | _LOG_DEBUG("Unrecognized key type!") |
| 849 | return NULL; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | const CFTypeRef |
| 854 | SecTpmOsx::Impl::getSymKeyType(KeyType keyType) |
| 855 | { |
| 856 | switch(keyType){ |
| 857 | case KEY_TYPE_AES: |
| 858 | return kSecAttrKeyTypeAES; |
| 859 | default: |
| 860 | _LOG_DEBUG("Unrecognized key type!") |
| 861 | return NULL; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | const CFTypeRef |
| 866 | SecTpmOsx::Impl::getKeyClass(KeyClass keyClass) |
| 867 | { |
| 868 | switch(keyClass){ |
| 869 | case KEY_CLASS_PRIVATE: |
| 870 | return kSecAttrKeyClassPrivate; |
| 871 | case KEY_CLASS_PUBLIC: |
| 872 | return kSecAttrKeyClassPublic; |
| 873 | case KEY_CLASS_SYMMETRIC: |
| 874 | return kSecAttrKeyClassSymmetric; |
| 875 | default: |
| 876 | _LOG_DEBUG("Unrecognized key class!"); |
| 877 | return NULL; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | const CFStringRef |
| 882 | SecTpmOsx::Impl::getDigestAlgorithm(DigestAlgorithm digestAlgo) |
| 883 | { |
| 884 | switch(digestAlgo){ |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 885 | // case DIGEST_MD2: |
| 886 | // return kSecDigestMD2; |
| 887 | // case DIGEST_MD5: |
| 888 | // return kSecDigestMD5; |
| 889 | // case DIGEST_SHA1: |
| 890 | // return kSecDigestSHA1; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 891 | case DIGEST_ALGORITHM_SHA256: |
| 892 | return kSecDigestSHA2; |
| 893 | default: |
| 894 | _LOG_DEBUG("Unrecognized digest algorithm!"); |
| 895 | return NULL; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 896 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 897 | } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 898 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 899 | long |
| 900 | SecTpmOsx::Impl::getDigestSize(DigestAlgorithm digestAlgo) |
| 901 | { |
| 902 | switch(digestAlgo){ |
| 903 | case DIGEST_ALGORITHM_SHA256: |
| 904 | return 256; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 905 | // case DIGEST_SHA1: |
| 906 | // case DIGEST_MD2: |
| 907 | // case DIGEST_MD5: |
| 908 | // return 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 909 | default: |
| 910 | _LOG_DEBUG("Unrecognized digest algorithm! Unknown digest size"); |
| 911 | return -1; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 912 | } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 913 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 914 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 915 | } // namespace ndn |