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 | b1db7c6 | 2014-04-03 14:57:25 -0700 | [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 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 46 | std::string |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 47 | toInternalKeyName(const Name & keyName, KeyClass keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [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 | * |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 52 | * @param keyName |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 53 | * @param keyClass |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 54 | * @returns pointer to the key |
| 55 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 56 | SecKeychainItemRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 57 | getKey(const Name & keyName, KeyClass keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [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 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 65 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 66 | getSymKeyType(KeyType keyType); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 67 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 74 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 75 | getAsymKeyType(KeyType keyType); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 76 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 83 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 84 | getKeyClass(KeyClass keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 85 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 92 | const CFStringRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 93 | getDigestAlgorithm(DigestAlgorithm digestAlgo); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 94 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 101 | long |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 124 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 181 | OSStatus res; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 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; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 210 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 211 | while(locked) |
| 212 | { |
| 213 | if(count > 2) |
| 214 | break; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 215 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 216 | char* getPassword = NULL; |
| 217 | getPassword = getpass(fmt); |
| 218 | count++; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 219 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 220 | if (!getPassword) |
| 221 | continue; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 222 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 223 | res = SecKeychainUnlock(m_impl->m_keyChainRef, |
| 224 | strlen(getPassword), |
| 225 | getPassword, |
| 226 | true); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 227 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 228 | memset(getPassword, 0, strlen(getPassword)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 229 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [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) |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 245 | { |
| 246 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 256 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 257 | keyNameUri.c_str(), |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 258 | kCFStringEncodingUTF8); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 259 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 277 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 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 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 295 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 299 | CFMutableDictionaryRef searchDict = |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 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; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 309 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 317 | void |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 331 | // CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 332 | // keyNameUri.c_str(), |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 344 | // if (error) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 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 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 419 | privateKeyInfo.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 420 | |
| 421 | CFRelease(exportedKey); |
| 422 | return pkcs1Os.buf(); |
| 423 | } |
| 424 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 425 | #ifdef __GNUC__ |
| 426 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 427 | #pragma GCC diagnostic push |
| 428 | #endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 429 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 430 | #endif // __GNUC__ |
| 431 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 432 | bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 433 | 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] | 434 | { |
| 435 | using namespace CryptoPP; |
| 436 | |
| 437 | StringSource privateKeySource(buf, size, true); |
| 438 | uint32_t tmpNum; |
| 439 | OID tmpOID; |
| 440 | SecByteBlock rawKeyBits; |
| 441 | // PrivateKeyInfo ::= SEQUENCE { |
| 442 | // INTEGER, |
| 443 | // SEQUENCE, |
| 444 | // OCTECT STRING} |
| 445 | BERSequenceDecoder privateKeyInfo(privateKeySource); |
| 446 | { |
| 447 | BERDecodeUnsigned<uint32_t>(privateKeyInfo, tmpNum, INTEGER); |
| 448 | BERSequenceDecoder sequenceDecoder(privateKeyInfo); |
| 449 | { |
| 450 | tmpOID.decode(sequenceDecoder); |
| 451 | BERDecodeNull(sequenceDecoder); |
| 452 | } |
| 453 | BERDecodeOctetString(privateKeyInfo, rawKeyBits); |
| 454 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 455 | privateKeyInfo.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 456 | |
| 457 | CFDataRef importedKey = CFDataCreateWithBytesNoCopy(NULL, |
| 458 | rawKeyBits.BytePtr(), |
| 459 | rawKeyBits.size(), |
| 460 | kCFAllocatorNull); |
| 461 | |
| 462 | SecExternalFormat externalFormat = kSecFormatOpenSSL; |
| 463 | SecExternalItemType externalType = kSecItemTypePrivateKey; |
| 464 | SecKeyImportExportParameters keyParams; |
| 465 | memset(&keyParams, 0, sizeof(keyParams)); |
| 466 | keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION; |
| 467 | keyParams.keyAttributes = CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_PERMANENT; |
| 468 | SecAccessRef access; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 469 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 470 | keyName.toUri().c_str(), |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 471 | kCFStringEncodingUTF8); |
| 472 | SecAccessCreate(keyLabel, NULL, &access); |
| 473 | keyParams.accessRef = access; |
| 474 | CFArrayRef outItems; |
| 475 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 476 | #ifdef __clang__ |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 477 | #pragma clang diagnostic push |
| 478 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 479 | #endif // __clang__ |
| 480 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 481 | OSStatus res = SecKeychainItemImport (importedKey, |
| 482 | NULL, |
| 483 | &externalFormat, |
| 484 | &externalType, |
| 485 | 0, |
| 486 | &keyParams, |
| 487 | m_impl->m_keyChainRef, |
| 488 | &outItems); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 489 | |
| 490 | #ifdef __clang__ |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 491 | #pragma clang diagnostic pop |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 492 | #endif // __clang__ |
| 493 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 494 | if(res != errSecSuccess) |
| 495 | { |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 496 | if(res == errSecAuthFailed && !retry) |
| 497 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 498 | if(unlockTpm(0, 0, false)) |
| 499 | return importPrivateKeyPkcs1IntoTpmInternal(keyName, buf, size, true); |
| 500 | else |
| 501 | return false; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 502 | } |
| 503 | else |
| 504 | return false; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | SecKeychainItemRef privateKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems, 0); |
| 508 | SecKeychainAttribute attrs[1]; // maximum number of attributes |
| 509 | SecKeychainAttributeList attrList = { 0, attrs }; |
| 510 | string keyUri = keyName.toUri(); |
| 511 | { |
| 512 | attrs[attrList.count].tag = kSecKeyPrintName; |
| 513 | attrs[attrList.count].length = keyUri.size(); |
| 514 | attrs[attrList.count].data = (void *)keyUri.c_str(); |
| 515 | attrList.count++; |
| 516 | } |
| 517 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 518 | res = SecKeychainItemModifyAttributesAndData(privateKey, |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 519 | &attrList, |
| 520 | 0, |
| 521 | NULL); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 522 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 523 | if(res != errSecSuccess) |
| 524 | { |
| 525 | return false; |
| 526 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 527 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 528 | CFRelease(importedKey); |
| 529 | return true; |
| 530 | } |
| 531 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 532 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 533 | #pragma GCC diagnostic pop |
| 534 | #endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 535 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 536 | bool |
| 537 | SecTpmOsx::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 538 | { |
| 539 | CFDataRef importedKey = CFDataCreateWithBytesNoCopy(NULL, |
| 540 | buf, |
| 541 | size, |
| 542 | kCFAllocatorNull); |
| 543 | |
| 544 | SecExternalFormat externalFormat = kSecFormatOpenSSL; |
| 545 | SecExternalItemType externalType = kSecItemTypePublicKey; |
| 546 | CFArrayRef outItems; |
| 547 | |
| 548 | OSStatus res = SecItemImport (importedKey, |
| 549 | NULL, |
| 550 | &externalFormat, |
| 551 | &externalType, |
| 552 | 0, |
| 553 | NULL, |
| 554 | m_impl->m_keyChainRef, |
| 555 | &outItems); |
| 556 | |
| 557 | if(res != errSecSuccess) |
| 558 | return false; |
| 559 | |
| 560 | SecKeychainItemRef publicKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems, 0); |
| 561 | SecKeychainAttribute attrs[1]; // maximum number of attributes |
| 562 | SecKeychainAttributeList attrList = { 0, attrs }; |
| 563 | string keyUri = keyName.toUri(); |
| 564 | { |
| 565 | attrs[attrList.count].tag = kSecKeyPrintName; |
| 566 | attrs[attrList.count].length = keyUri.size(); |
| 567 | attrs[attrList.count].data = (void *)keyUri.c_str(); |
| 568 | attrList.count++; |
| 569 | } |
| 570 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 571 | res = SecKeychainItemModifyAttributesAndData(publicKey, |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 572 | &attrList, |
| 573 | 0, |
| 574 | NULL); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 575 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 576 | if(res != errSecSuccess) |
| 577 | return false; |
| 578 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 579 | CFRelease(importedKey); |
| 580 | return true; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | Block |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 584 | 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] | 585 | { |
| 586 | _LOG_TRACE("OSXPrivateKeyStorage::Sign"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 587 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 588 | CFDataRef dataRef = CFDataCreateWithBytesNoCopy(NULL, |
| 589 | data, |
| 590 | dataLength, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 591 | kCFAllocatorNull); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 592 | |
| 593 | SecKeyRef privateKey = (SecKeyRef)m_impl->getKey(keyName, KEY_CLASS_PRIVATE); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 594 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 595 | CFErrorRef error; |
| 596 | SecTransformRef signer = SecSignTransformCreate((SecKeyRef)privateKey, &error); |
| 597 | if (error) throw Error("Fail to create signer"); |
| 598 | |
| 599 | // Set input |
| 600 | Boolean set_res = SecTransformSetAttribute(signer, |
| 601 | kSecTransformInputAttributeName, |
| 602 | dataRef, |
| 603 | &error); |
| 604 | if (error) throw Error("Fail to configure input of signer"); |
| 605 | |
| 606 | // Enable use of padding |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 607 | SecTransformSetAttribute(signer, |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 608 | kSecPaddingKey, |
| 609 | kSecPaddingPKCS1Key, |
| 610 | &error); |
| 611 | if (error) throw Error("Fail to configure digest algorithm of signer"); |
| 612 | |
| 613 | // Set padding type |
| 614 | set_res = SecTransformSetAttribute(signer, |
| 615 | kSecDigestTypeAttribute, |
| 616 | m_impl->getDigestAlgorithm(digestAlgorithm), |
| 617 | &error); |
| 618 | if (error) throw Error("Fail to configure digest algorithm of signer"); |
| 619 | |
| 620 | // Set padding attribute |
| 621 | long digestSize = m_impl->getDigestSize(digestAlgorithm); |
| 622 | set_res = SecTransformSetAttribute(signer, |
| 623 | kSecDigestLengthAttribute, |
| 624 | CFNumberCreate(NULL, kCFNumberLongType, &digestSize), |
| 625 | &error); |
| 626 | if (error) throw Error("Fail to configure digest size of signer"); |
| 627 | |
| 628 | // Actually sign |
| 629 | CFDataRef signature = (CFDataRef) SecTransformExecute(signer, &error); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 630 | if (error) |
| 631 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 632 | if(!retry) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 633 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 634 | if(unlockTpm(0, 0, false)) |
| 635 | return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, true); |
| 636 | else |
| 637 | throw Error("Fail to unlock the keychain"); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 638 | } |
| 639 | else |
| 640 | { |
| 641 | CFShow(error); |
| 642 | throw Error("Fail to sign data"); |
| 643 | } |
| 644 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 645 | |
| 646 | if (!signature) throw Error("Signature is NULL!\n"); |
| 647 | |
| 648 | return Block(Tlv::SignatureValue, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 649 | make_shared<Buffer>(CFDataGetBytePtr(signature), CFDataGetLength(signature))); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 653 | 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] | 654 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 655 | throw Error("SecTpmOsx::decryptInTpm is not supported"); |
| 656 | // _LOG_TRACE("OSXPrivateKeyStorage::Decrypt"); |
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 | // KeyClass keyClass; |
| 659 | // if(sym) |
| 660 | // keyClass = KEY_CLASS_SYMMETRIC; |
| 661 | // else |
| 662 | // keyClass = KEY_CLASS_PRIVATE; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 663 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 664 | // CFDataRef dataRef = CFDataCreate(NULL, |
| 665 | // reinterpret_cast<const unsigned char*>(data), |
| 666 | // dataLength |
| 667 | // ); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 668 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 669 | // // _LOG_DEBUG("CreateData"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 670 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 671 | // SecKeyRef decryptKey = (SecKeyRef)m_impl->getKey(keyName, keyClass); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 672 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 673 | // // _LOG_DEBUG("GetKey"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 674 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 675 | // CFErrorRef error; |
| 676 | // SecTransformRef decrypt = SecDecryptTransformCreate(decryptKey, &error); |
| 677 | // if (error) throw Error("Fail to create decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 678 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 679 | // Boolean set_res = SecTransformSetAttribute(decrypt, |
| 680 | // kSecTransformInputAttributeName, |
| 681 | // dataRef, |
| 682 | // &error); |
| 683 | // if (error) throw Error("Fail to configure decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 684 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 685 | // CFDataRef output = (CFDataRef) SecTransformExecute(decrypt, &error); |
| 686 | // if (error) |
| 687 | // { |
| 688 | // CFShow(error); |
| 689 | // throw Error("Fail to decrypt data"); |
| 690 | // } |
| 691 | // if (!output) throw Error("Output is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 692 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 693 | // return make_shared<Buffer>(CFDataGetBytePtr(output), CFDataGetLength(output)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 694 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 695 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 696 | void |
| 697 | SecTpmOsx::addAppToACL(const Name & keyName, KeyClass keyClass, const string & appPath, AclType acl) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 698 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 699 | if(keyClass == KEY_CLASS_PRIVATE && acl == ACL_TYPE_PRIVATE) |
| 700 | { |
| 701 | SecKeychainItemRef privateKey = m_impl->getKey(keyName, keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 702 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 703 | SecAccessRef accRef; |
| 704 | OSStatus acc_res = SecKeychainItemCopyAccess(privateKey, &accRef); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 705 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 706 | CFArrayRef signACL = SecAccessCopyMatchingACLList(accRef, |
| 707 | kSecACLAuthorizationSign); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 708 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 709 | SecACLRef aclRef = (SecACLRef) CFArrayGetValueAtIndex(signACL, 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 710 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 711 | CFArrayRef appList; |
| 712 | CFStringRef description; |
| 713 | SecKeychainPromptSelector promptSelector; |
| 714 | OSStatus acl_res = SecACLCopyContents(aclRef, |
| 715 | &appList, |
| 716 | &description, |
| 717 | &promptSelector); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 718 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 719 | CFMutableArrayRef newAppList = CFArrayCreateMutableCopy(NULL, |
| 720 | 0, |
| 721 | appList); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 722 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 723 | SecTrustedApplicationRef trustedApp; |
| 724 | acl_res = SecTrustedApplicationCreateFromPath(appPath.c_str(), |
| 725 | &trustedApp); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 726 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 727 | CFArrayAppendValue(newAppList, trustedApp); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 728 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 729 | acl_res = SecACLSetContents(aclRef, |
| 730 | newAppList, |
| 731 | description, |
| 732 | promptSelector); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 733 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 734 | acc_res = SecKeychainItemSetAccess(privateKey, accRef); |
| 735 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 738 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 739 | 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] | 740 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 741 | throw Error("SecTpmOsx::encryptInTpm is not supported"); |
| 742 | // _LOG_TRACE("OSXPrivateKeyStorage::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 | // KeyClass keyClass; |
| 745 | // if(sym) |
| 746 | // keyClass = KEY_CLASS_SYMMETRIC; |
| 747 | // else |
| 748 | // keyClass = KEY_CLASS_PUBLIC; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 749 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 750 | // CFDataRef dataRef = CFDataCreate(NULL, |
| 751 | // reinterpret_cast<const unsigned char*>(data), |
| 752 | // dataLength |
| 753 | // ); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 754 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 755 | // SecKeyRef encryptKey = (SecKeyRef)m_impl->getKey(keyName, keyClass); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 756 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 757 | // CFErrorRef error; |
| 758 | // SecTransformRef encrypt = SecEncryptTransformCreate(encryptKey, &error); |
| 759 | // if (error) throw Error("Fail to create encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 760 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 761 | // Boolean set_res = SecTransformSetAttribute(encrypt, |
| 762 | // kSecTransformInputAttributeName, |
| 763 | // dataRef, |
| 764 | // &error); |
| 765 | // if (error) throw Error("Fail to configure encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 766 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 767 | // CFDataRef output = (CFDataRef) SecTransformExecute(encrypt, &error); |
| 768 | // if (error) throw Error("Fail to encrypt data"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 769 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 770 | // if (!output) throw Error("Output is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 771 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 772 | // return make_shared<Buffer> (CFDataGetBytePtr(output), CFDataGetLength(output)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | bool |
| 776 | SecTpmOsx::doesKeyExistInTpm(const Name & keyName, KeyClass keyClass) |
| 777 | { |
| 778 | _LOG_TRACE("OSXPrivateKeyStorage::doesKeyExist"); |
| 779 | |
| 780 | string keyNameUri = m_impl->toInternalKeyName(keyName, keyClass); |
| 781 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 782 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 783 | keyNameUri.c_str(), |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 784 | kCFStringEncodingUTF8); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 785 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 786 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 787 | 4, |
| 788 | &kCFTypeDictionaryKeyCallBacks, |
| 789 | NULL); |
| 790 | |
| 791 | CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 792 | // CFDictionaryAddValue(attrDict, kSecAttrKeyClass, m_impl->getKeyClass(keyClass)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 793 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 794 | CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 795 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 796 | SecKeychainItemRef itemRef; |
| 797 | OSStatus res = SecItemCopyMatching((CFDictionaryRef)attrDict, (CFTypeRef*)&itemRef); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 798 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 799 | if(res == errSecSuccess) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 800 | return true; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 801 | else |
| 802 | return false; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 803 | |
| 804 | } |
| 805 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 806 | bool |
| 807 | SecTpmOsx::generateRandomBlock(uint8_t* res, size_t size) |
| 808 | { |
| 809 | return (SecRandomCopyBytes(kSecRandomDefault, size, res) == 0); |
| 810 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 811 | |
| 812 | //////////////////////////////// |
| 813 | // OSXPrivateKeyStorage::Impl // |
| 814 | //////////////////////////////// |
| 815 | |
| 816 | SecKeychainItemRef |
| 817 | SecTpmOsx::Impl::getKey(const Name & keyName, KeyClass keyClass) |
| 818 | { |
| 819 | string keyNameUri = toInternalKeyName(keyName, keyClass); |
| 820 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 821 | CFStringRef keyLabel = CFStringCreateWithCString(NULL, |
| 822 | keyNameUri.c_str(), |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 823 | kCFStringEncodingUTF8); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 824 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 825 | CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, |
| 826 | 5, |
| 827 | &kCFTypeDictionaryKeyCallBacks, |
| 828 | NULL); |
| 829 | |
| 830 | CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey); |
| 831 | CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel); |
| 832 | CFDictionaryAddValue(attrDict, kSecAttrKeyClass, getKeyClass(keyClass)); |
| 833 | CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 834 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 835 | SecKeychainItemRef keyItem; |
| 836 | |
| 837 | OSStatus res = SecItemCopyMatching((CFDictionaryRef) attrDict, (CFTypeRef*)&keyItem); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 838 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 839 | if(res != errSecSuccess){ |
| 840 | _LOG_DEBUG("Fail to find the key!"); |
| 841 | return NULL; |
| 842 | } |
| 843 | else |
| 844 | return keyItem; |
| 845 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 846 | |
| 847 | string |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 848 | SecTpmOsx::Impl::toInternalKeyName(const Name & keyName, KeyClass keyClass) |
| 849 | { |
| 850 | string keyUri = keyName.toUri(); |
| 851 | |
| 852 | if(KEY_CLASS_SYMMETRIC == keyClass) |
| 853 | return keyUri + "/symmetric"; |
| 854 | else |
| 855 | return keyUri; |
| 856 | } |
| 857 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 858 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 859 | SecTpmOsx::Impl::getAsymKeyType(KeyType keyType) |
| 860 | { |
| 861 | switch(keyType){ |
| 862 | case KEY_TYPE_RSA: |
| 863 | return kSecAttrKeyTypeRSA; |
| 864 | default: |
| 865 | _LOG_DEBUG("Unrecognized key type!") |
| 866 | return NULL; |
| 867 | } |
| 868 | } |
| 869 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 870 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 871 | SecTpmOsx::Impl::getSymKeyType(KeyType keyType) |
| 872 | { |
| 873 | switch(keyType){ |
| 874 | case KEY_TYPE_AES: |
| 875 | return kSecAttrKeyTypeAES; |
| 876 | default: |
| 877 | _LOG_DEBUG("Unrecognized key type!") |
| 878 | return NULL; |
| 879 | } |
| 880 | } |
| 881 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 882 | const CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 883 | SecTpmOsx::Impl::getKeyClass(KeyClass keyClass) |
| 884 | { |
| 885 | switch(keyClass){ |
| 886 | case KEY_CLASS_PRIVATE: |
| 887 | return kSecAttrKeyClassPrivate; |
| 888 | case KEY_CLASS_PUBLIC: |
| 889 | return kSecAttrKeyClassPublic; |
| 890 | case KEY_CLASS_SYMMETRIC: |
| 891 | return kSecAttrKeyClassSymmetric; |
| 892 | default: |
| 893 | _LOG_DEBUG("Unrecognized key class!"); |
| 894 | return NULL; |
| 895 | } |
| 896 | } |
| 897 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 898 | const CFStringRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 899 | SecTpmOsx::Impl::getDigestAlgorithm(DigestAlgorithm digestAlgo) |
| 900 | { |
| 901 | switch(digestAlgo){ |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 902 | // case DIGEST_MD2: |
| 903 | // return kSecDigestMD2; |
| 904 | // case DIGEST_MD5: |
| 905 | // return kSecDigestMD5; |
| 906 | // case DIGEST_SHA1: |
| 907 | // return kSecDigestSHA1; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 908 | case DIGEST_ALGORITHM_SHA256: |
| 909 | return kSecDigestSHA2; |
| 910 | default: |
| 911 | _LOG_DEBUG("Unrecognized digest algorithm!"); |
| 912 | return NULL; |
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 | } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 915 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 916 | long |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 917 | SecTpmOsx::Impl::getDigestSize(DigestAlgorithm digestAlgo) |
| 918 | { |
| 919 | switch(digestAlgo){ |
| 920 | case DIGEST_ALGORITHM_SHA256: |
| 921 | return 256; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 922 | // case DIGEST_SHA1: |
| 923 | // case DIGEST_MD2: |
| 924 | // case DIGEST_MD5: |
| 925 | // return 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 926 | default: |
| 927 | _LOG_DEBUG("Unrecognized digest algorithm! Unknown digest size"); |
| 928 | return -1; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 929 | } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 930 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 931 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 932 | } // namespace ndn |