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