Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 24 | #include "sec-tpm-osx.hpp" |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 25 | #include "public-key.hpp" |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include "../encoding/oid.hpp" |
| 28 | #include "../encoding/buffer-stream.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 29 | #include "cryptopp.hpp" |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 31 | #include <pwd.h> |
| 32 | #include <unistd.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 36 | #include <boost/lexical_cast.hpp> |
| 37 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 38 | #include <CoreFoundation/CoreFoundation.h> |
| 39 | #include <Security/Security.h> |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 40 | #include <Security/SecRandom.h> |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 41 | #include <CoreServices/CoreServices.h> |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 59d67a5 | 2014-04-03 16:09:31 -0700 | [diff] [blame] | 43 | #include <Security/SecDigestTransform.h> |
| 44 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 45 | namespace ndn { |
| 46 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 47 | using std::string; |
| 48 | |
Alexander Afanasyev | 0711380 | 2015-01-15 19:14:36 -0800 | [diff] [blame] | 49 | const std::string SecTpmOsx::SCHEME("tpm-osxkeychain"); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 51 | /** |
| 52 | * @brief Helper class to wrap CoreFoundation object pointers |
| 53 | * |
| 54 | * The class is similar in spirit to shared_ptr, but uses CoreFoundation |
| 55 | * mechanisms to retain/release object. |
| 56 | * |
| 57 | * Original implementation by Christopher Hunt and it was borrowed from |
| 58 | * http://www.cocoabuilder.com/archive/cocoa/130776-auto-cfrelease-and.html |
| 59 | */ |
| 60 | template<class T> |
| 61 | class CFReleaser |
| 62 | { |
| 63 | public: |
| 64 | ////////////////////////////// |
| 65 | // Construction/destruction // |
| 66 | |
| 67 | CFReleaser() |
| 68 | : m_typeRef(0) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | CFReleaser(const T& typeRef) |
| 73 | : m_typeRef(typeRef) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | CFReleaser(const CFReleaser& inReleaser) |
| 78 | : m_typeRef(0) |
| 79 | { |
| 80 | retain(inReleaser.m_typeRef); |
| 81 | } |
| 82 | |
| 83 | CFReleaser& |
| 84 | operator=(const T& typeRef) |
| 85 | { |
| 86 | if (typeRef != m_typeRef) { |
| 87 | release(); |
| 88 | m_typeRef = typeRef; |
| 89 | } |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | CFReleaser& |
| 94 | operator=(const CFReleaser& inReleaser) |
| 95 | { |
| 96 | retain(inReleaser.m_typeRef); |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | ~CFReleaser() |
| 101 | { |
| 102 | release(); |
| 103 | } |
| 104 | |
| 105 | //////////// |
| 106 | // Access // |
| 107 | |
| 108 | // operator const T&() const |
| 109 | // { |
| 110 | // return m_typeRef; |
| 111 | // } |
| 112 | |
| 113 | // operator T&() |
| 114 | // { |
| 115 | // return m_typeRef; |
| 116 | // } |
| 117 | |
| 118 | const T& |
| 119 | get() const |
| 120 | { |
| 121 | return m_typeRef; |
| 122 | } |
| 123 | |
| 124 | T& |
| 125 | get() |
| 126 | { |
| 127 | return m_typeRef; |
| 128 | } |
| 129 | |
| 130 | /////////////////// |
| 131 | // Miscellaneous // |
| 132 | |
| 133 | void |
| 134 | retain(const T& typeRef) |
| 135 | { |
| 136 | if (typeRef != 0) { |
| 137 | CFRetain(typeRef); |
| 138 | } |
| 139 | release(); |
| 140 | m_typeRef = typeRef; |
| 141 | } |
| 142 | |
| 143 | void release() |
| 144 | { |
| 145 | if (m_typeRef != 0) { |
| 146 | CFRelease(m_typeRef); |
| 147 | m_typeRef = 0; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | private: |
| 152 | T m_typeRef; |
| 153 | }; |
| 154 | |
| 155 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 156 | class SecTpmOsx::Impl |
| 157 | { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 158 | public: |
| 159 | Impl() |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 160 | : m_passwordSet(false) |
| 161 | , m_inTerminal(false) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 162 | { |
| 163 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 164 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 165 | /** |
| 166 | * @brief Convert NDN name of a key to internal name of the key. |
| 167 | * |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 168 | * @param keyName |
| 169 | * @param keyClass |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 170 | * @return the internal key name |
| 171 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 172 | std::string |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 173 | toInternalKeyName(const Name& keyName, KeyClass keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 174 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 175 | /** |
| 176 | * @brief Get key. |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 177 | * |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 178 | * @param keyName |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 179 | * @param keyClass |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 180 | * @returns pointer to the key |
| 181 | */ |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 182 | CFReleaser<SecKeychainItemRef> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 183 | getKey(const Name& keyName, KeyClass keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 184 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 185 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 186 | * @brief Convert keyType to MAC OS symmetric key key type |
| 187 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 188 | * @param keyType |
| 189 | * @returns MAC OS key type |
| 190 | */ |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 191 | CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 192 | getSymKeyType(KeyType keyType); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 193 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 194 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 195 | * @brief Convert keyType to MAC OS asymmetirc key type |
| 196 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 197 | * @param keyType |
| 198 | * @returns MAC OS key type |
| 199 | */ |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 200 | CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 201 | getAsymKeyType(KeyType keyType); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 202 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 203 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 204 | * @brief Convert keyClass to MAC OS key class |
| 205 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 206 | * @param keyClass |
| 207 | * @returns MAC OS key class |
| 208 | */ |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 209 | CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 210 | getKeyClass(KeyClass keyClass); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 211 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 212 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 213 | * @brief Convert digestAlgo to MAC OS algorithm id |
| 214 | * |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 215 | * @param digestAlgo |
| 216 | * @returns MAC OS algorithm id |
| 217 | */ |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 218 | CFStringRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 219 | getDigestAlgorithm(DigestAlgorithm digestAlgo); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 220 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 221 | /** |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 222 | * @brief Get the digest size of the corresponding algorithm |
| 223 | * |
| 224 | * @param digestAlgo |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 225 | * @return digest size |
| 226 | */ |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 227 | long |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 228 | getDigestSize(DigestAlgorithm digestAlgo); |
| 229 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 230 | /////////////////////////////////////////////// |
| 231 | // everything here is public, including data // |
| 232 | /////////////////////////////////////////////// |
| 233 | public: |
| 234 | SecKeychainRef m_keyChainRef; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 235 | bool m_passwordSet; |
| 236 | string m_password; |
| 237 | bool m_inTerminal; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 240 | SecTpmOsx::SecTpmOsx(const std::string& location) |
| 241 | : SecTpm(location) |
| 242 | , m_impl(new Impl) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 243 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 244 | // TODO: add location support |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 245 | if (m_impl->m_inTerminal) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 246 | SecKeychainSetUserInteractionAllowed(false); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 247 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 248 | SecKeychainSetUserInteractionAllowed(true); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 249 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 250 | OSStatus res = SecKeychainCopyDefault(&m_impl->m_keyChainRef); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 251 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 252 | if (res == errSecNoDefaultKeychain) //If no default key chain, create one. |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 253 | throw Error("No default keychain, create one first!"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 256 | SecTpmOsx::~SecTpmOsx() |
| 257 | { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 260 | void |
| 261 | SecTpmOsx::setTpmPassword(const uint8_t* password, size_t passwordLength) |
| 262 | { |
| 263 | m_impl->m_passwordSet = true; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 264 | std::fill(m_impl->m_password.begin(), m_impl->m_password.end(), 0); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 265 | m_impl->m_password.clear(); |
| 266 | m_impl->m_password.append(reinterpret_cast<const char*>(password), passwordLength); |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | SecTpmOsx::resetTpmPassword() |
| 271 | { |
| 272 | m_impl->m_passwordSet = false; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 273 | std::fill(m_impl->m_password.begin(), m_impl->m_password.end(), 0); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 274 | m_impl->m_password.clear(); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | SecTpmOsx::setInTerminal(bool inTerminal) |
| 279 | { |
| 280 | m_impl->m_inTerminal = inTerminal; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 281 | if (inTerminal) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 282 | SecKeychainSetUserInteractionAllowed(false); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 283 | else |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 284 | SecKeychainSetUserInteractionAllowed(true); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 288 | SecTpmOsx::getInTerminal() const |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 289 | { |
| 290 | return m_impl->m_inTerminal; |
| 291 | } |
| 292 | |
| 293 | bool |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 294 | SecTpmOsx::isLocked() |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 295 | { |
| 296 | SecKeychainStatus keychainStatus; |
| 297 | |
| 298 | OSStatus res = SecKeychainGetStatus(m_impl->m_keyChainRef, &keychainStatus); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 299 | if (res != errSecSuccess) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 300 | return true; |
| 301 | else |
| 302 | return ((kSecUnlockStateStatus & keychainStatus) == 0); |
| 303 | } |
| 304 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 305 | bool |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 306 | SecTpmOsx::unlockTpm(const char* password, size_t passwordLength, bool usePassword) |
| 307 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 308 | OSStatus res; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 309 | |
| 310 | // If the default key chain is already unlocked, return immediately. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 311 | if (!isLocked()) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 312 | return true; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 313 | |
| 314 | // If the default key chain is locked, unlock the key chain. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 315 | if (usePassword) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 316 | { |
| 317 | // Use the supplied password. |
| 318 | res = SecKeychainUnlock(m_impl->m_keyChainRef, |
| 319 | passwordLength, |
| 320 | password, |
| 321 | true); |
| 322 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 323 | else if (m_impl->m_passwordSet) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 324 | { |
| 325 | // If no password supplied, then use the configured password if exists. |
| 326 | SecKeychainUnlock(m_impl->m_keyChainRef, |
| 327 | m_impl->m_password.size(), |
| 328 | m_impl->m_password.c_str(), |
| 329 | true); |
| 330 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 331 | else if (m_impl->m_inTerminal) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 332 | { |
| 333 | // If no configured password, get password from terminal if inTerminal set. |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 334 | bool isLocked = true; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 335 | const char* fmt = "Password to unlock the default keychain: "; |
| 336 | int count = 0; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 337 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 338 | while (isLocked) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 339 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 340 | if (count > 2) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 341 | break; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 342 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 343 | char* getPassword = 0; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 344 | getPassword = getpass(fmt); |
| 345 | count++; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 346 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 347 | if (!getPassword) |
| 348 | continue; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 349 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 350 | res = SecKeychainUnlock(m_impl->m_keyChainRef, |
| 351 | strlen(getPassword), |
| 352 | getPassword, |
| 353 | true); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 354 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 355 | memset(getPassword, 0, strlen(getPassword)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 356 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 357 | if (res == errSecSuccess) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 358 | break; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | // If inTerminal is not set, get the password from GUI. |
| 364 | SecKeychainUnlock(m_impl->m_keyChainRef, 0, 0, false); |
| 365 | } |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 366 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 367 | return !isLocked(); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 370 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 371 | SecTpmOsx::generateKeyPairInTpmInternal(const Name& keyName, |
| 372 | const KeyParams& params, |
| 373 | bool needRetry) |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 374 | { |
| 375 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 376 | if (doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC)) |
| 377 | { |
| 378 | throw Error("keyName has existed"); |
| 379 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 380 | |
| 381 | string keyNameUri = m_impl->toInternalKeyName(keyName, KEY_CLASS_PUBLIC); |
| 382 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 383 | CFReleaser<CFStringRef> keyLabel = |
| 384 | CFStringCreateWithCString(0, |
| 385 | keyNameUri.c_str(), |
| 386 | kCFStringEncodingUTF8); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 387 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 388 | CFReleaser<CFMutableDictionaryRef> attrDict = |
| 389 | CFDictionaryCreateMutable(0, |
| 390 | 3, |
| 391 | &kCFTypeDictionaryKeyCallBacks, |
| 392 | 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 393 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 394 | KeyType keyType = params.getKeyType(); |
| 395 | uint32_t keySize; |
| 396 | switch (keyType) |
| 397 | { |
| 398 | case KEY_TYPE_RSA: |
| 399 | { |
| 400 | const RsaKeyParams& rsaParams = static_cast<const RsaKeyParams&>(params); |
| 401 | keySize = rsaParams.getKeySize(); |
| 402 | break; |
| 403 | } |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 404 | case KEY_TYPE_ECDSA: |
| 405 | { |
| 406 | const EcdsaKeyParams& ecdsaParams = static_cast<const EcdsaKeyParams&>(params); |
| 407 | keySize = ecdsaParams.getKeySize(); |
| 408 | break; |
| 409 | } |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 410 | default: |
| 411 | throw Error("Fail to create a key pair: Unsupported key type"); |
| 412 | } |
| 413 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 414 | CFReleaser<CFNumberRef> cfKeySize = CFNumberCreate(0, kCFNumberIntType, &keySize); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 415 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 416 | CFDictionaryAddValue(attrDict.get(), kSecAttrKeyType, m_impl->getAsymKeyType(keyType)); |
| 417 | CFDictionaryAddValue(attrDict.get(), kSecAttrKeySizeInBits, cfKeySize.get()); |
| 418 | CFDictionaryAddValue(attrDict.get(), kSecAttrLabel, keyLabel.get()); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 419 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 420 | CFReleaser<SecKeyRef> publicKey, privateKey; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 421 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 422 | OSStatus res = SecKeyGeneratePair((CFDictionaryRef)attrDict.get(), |
| 423 | &publicKey.get(), &privateKey.get()); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 424 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 425 | if (res == errSecSuccess) |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 426 | { |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 427 | return; |
| 428 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 429 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 430 | if (res == errSecAuthFailed && !needRetry) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 431 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 432 | if (unlockTpm(0, 0, false)) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 433 | generateKeyPairInTpmInternal(keyName, params, true); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 434 | else |
| 435 | throw Error("Fail to unlock the keychain"); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 436 | } |
| 437 | else |
| 438 | { |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 439 | throw Error("Fail to create a key pair"); |
| 440 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 444 | SecTpmOsx::deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 445 | { |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 446 | CFReleaser<CFStringRef> keyLabel = |
| 447 | CFStringCreateWithCString(0, |
| 448 | keyName.toUri().c_str(), |
| 449 | kCFStringEncodingUTF8); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 450 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 451 | CFReleaser<CFMutableDictionaryRef> searchDict = |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 452 | CFDictionaryCreateMutable(0, 5, |
| 453 | &kCFTypeDictionaryKeyCallBacks, |
| 454 | &kCFTypeDictionaryValueCallBacks); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 455 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 456 | CFDictionaryAddValue(searchDict.get(), kSecClass, kSecClassKey); |
| 457 | CFDictionaryAddValue(searchDict.get(), kSecAttrLabel, keyLabel.get()); |
| 458 | CFDictionaryAddValue(searchDict.get(), kSecMatchLimit, kSecMatchLimitAll); |
| 459 | OSStatus res = SecItemDelete(searchDict.get()); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 460 | |
| 461 | if (res == errSecSuccess) |
| 462 | return; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 463 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 464 | if (res == errSecAuthFailed && !needRetry) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 465 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 466 | if (unlockTpm(0, 0, false)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 467 | deleteKeyPairInTpmInternal(keyName, true); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 468 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 471 | void |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 472 | SecTpmOsx::generateSymmetricKeyInTpm(const Name& keyName, const KeyParams& params) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 473 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 474 | throw Error("SecTpmOsx::generateSymmetricKeyInTpm is not supported"); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 475 | // if (doesKeyExistInTpm(keyName, KEY_CLASS_SYMMETRIC)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 476 | // throw Error("keyName has existed!"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 477 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 478 | // string keyNameUri = m_impl->toInternalKeyName(keyName, KEY_CLASS_SYMMETRIC); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 479 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 480 | // CFReleaser<CFMutableDictionaryRef> attrDict = |
| 481 | // CFDictionaryCreateMutable(kCFAllocatorDefault, |
| 482 | // 0, |
| 483 | // &kCFTypeDictionaryKeyCallBacks, |
| 484 | // &kCFTypeDictionaryValueCallBacks); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 485 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 486 | // CFReleaser<CFStringRef> keyLabel = |
| 487 | // CFStringCreateWithCString(0, |
| 488 | // keyNameUri.c_str(), |
| 489 | // kCFStringEncodingUTF8); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 490 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 491 | // CFReleaser<CFNumberRef> cfKeySize = CFNumberCreate(0, kCFNumberIntType, &keySize); |
| 492 | |
| 493 | // CFDictionaryAddValue(attrDict.get(), kSecAttrKeyType, m_impl->getSymKeyType(keyType)); |
| 494 | // CFDictionaryAddValue(attrDict.get(), kSecAttrKeySizeInBits, cfKeySize.get()); |
| 495 | // CFDictionaryAddValue(attrDict.get(), kSecAttrIsPermanent, kCFBooleanTrue); |
| 496 | // CFDictionaryAddValue(attrDict.get(), kSecAttrLabel, keyLabel.get()); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 497 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 498 | // CFErrorRef error = 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 499 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 500 | // SecKeyRef symmetricKey = SecKeyGenerateSymmetric(attrDict, &error); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 501 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 502 | // if (error) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 503 | // throw Error("Fail to create a symmetric key"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 506 | shared_ptr<PublicKey> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 507 | SecTpmOsx::getPublicKeyFromTpm(const Name& keyName) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 508 | { |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 509 | CFReleaser<SecKeychainItemRef> publicKey = m_impl->getKey(keyName, KEY_CLASS_PUBLIC); |
| 510 | if (publicKey.get() == 0) |
| 511 | { |
| 512 | throw Error("Requested public key [" + keyName.toUri() + "] does not exist in OSX Keychain"); |
| 513 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 514 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 515 | CFReleaser<CFDataRef> exportedKey; |
| 516 | OSStatus res = SecItemExport(publicKey.get(), |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 517 | kSecFormatOpenSSL, |
| 518 | 0, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 519 | 0, |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 520 | &exportedKey.get()); |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 521 | if (res != errSecSuccess) |
| 522 | { |
| 523 | throw Error("Cannot export requested public key from OSX Keychain"); |
| 524 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 525 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 526 | shared_ptr<PublicKey> key = make_shared<PublicKey>(CFDataGetBytePtr(exportedKey.get()), |
| 527 | CFDataGetLength(exportedKey.get())); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 528 | return key; |
| 529 | } |
| 530 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 531 | std::string |
| 532 | SecTpmOsx::getScheme() |
| 533 | { |
| 534 | return SCHEME; |
| 535 | } |
| 536 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 537 | ConstBufferPtr |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 538 | SecTpmOsx::exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 539 | { |
| 540 | using namespace CryptoPP; |
| 541 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 542 | CFReleaser<SecKeychainItemRef> privateKey = m_impl->getKey(keyName, KEY_CLASS_PRIVATE); |
| 543 | if (privateKey.get() == 0) |
| 544 | { |
| 545 | /// @todo Can this happen because of keychain is locked? |
| 546 | throw Error("Private key [" + keyName.toUri() + "] does not exist in OSX Keychain"); |
| 547 | } |
| 548 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 549 | shared_ptr<PublicKey> publicKey = getPublicKeyFromTpm(keyName); |
| 550 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 551 | CFReleaser<CFDataRef> exportedKey; |
| 552 | OSStatus res = SecItemExport(privateKey.get(), |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 553 | kSecFormatOpenSSL, |
| 554 | 0, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 555 | 0, |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 556 | &exportedKey.get()); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 557 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 558 | if (res != errSecSuccess) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 559 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 560 | if (res == errSecAuthFailed && !needRetry) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 561 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 562 | if (unlockTpm(0, 0, false)) |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 563 | return exportPrivateKeyPkcs8FromTpmInternal(keyName, true); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 564 | else |
| 565 | return shared_ptr<Buffer>(); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 566 | } |
| 567 | else |
| 568 | return shared_ptr<Buffer>(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 571 | uint32_t version = 0; |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 572 | OID algorithm; |
| 573 | bool hasParameters = false; |
| 574 | OID algorithmParameter; |
| 575 | switch (publicKey->getKeyType()) { |
| 576 | case KEY_TYPE_RSA: |
| 577 | { |
| 578 | algorithm = oid::RSA; // "RSA encryption" |
| 579 | hasParameters = false; |
| 580 | break; |
| 581 | } |
| 582 | case KEY_TYPE_ECDSA: |
| 583 | { |
| 584 | // "ECDSA encryption" |
| 585 | StringSource src(publicKey->get().buf(), publicKey->get().size(), true); |
| 586 | BERSequenceDecoder subjectPublicKeyInfo(src); |
| 587 | { |
| 588 | BERSequenceDecoder algorithmInfo(subjectPublicKeyInfo); |
| 589 | { |
| 590 | algorithm.decode(algorithmInfo); |
| 591 | algorithmParameter.decode(algorithmInfo); |
| 592 | } |
| 593 | } |
| 594 | hasParameters = true; |
| 595 | break; |
| 596 | } |
| 597 | default: |
| 598 | throw Error("Unsupported key type" + |
| 599 | boost::lexical_cast<std::string>(publicKey->getKeyType())); |
| 600 | } |
| 601 | |
| 602 | OBufferStream pkcs8Os; |
| 603 | FileSink sink(pkcs8Os); |
| 604 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 605 | SecByteBlock rawKeyBits; |
| 606 | // PrivateKeyInfo ::= SEQUENCE { |
| 607 | // version INTEGER, |
| 608 | // privateKeyAlgorithm SEQUENCE, |
| 609 | // privateKey OCTECT STRING} |
| 610 | DERSequenceEncoder privateKeyInfo(sink); |
| 611 | { |
| 612 | DEREncodeUnsigned<uint32_t>(privateKeyInfo, version, INTEGER); |
| 613 | DERSequenceEncoder privateKeyAlgorithm(privateKeyInfo); |
| 614 | { |
| 615 | algorithm.encode(privateKeyAlgorithm); |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 616 | if (hasParameters) |
| 617 | algorithmParameter.encode(privateKeyAlgorithm); |
| 618 | else |
| 619 | DEREncodeNull(privateKeyAlgorithm); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 620 | } |
| 621 | privateKeyAlgorithm.MessageEnd(); |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 622 | DEREncodeOctetString(privateKeyInfo, |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 623 | CFDataGetBytePtr(exportedKey.get()), |
| 624 | CFDataGetLength(exportedKey.get())); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 625 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 626 | privateKeyInfo.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 627 | |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 628 | return pkcs8Os.buf(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 631 | #ifdef __GNUC__ |
| 632 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 633 | #pragma GCC diagnostic push |
| 634 | #endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 635 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 636 | #endif // __GNUC__ |
| 637 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 638 | bool |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 639 | SecTpmOsx::importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 640 | const uint8_t* buf, size_t size, |
| 641 | bool needRetry) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 642 | { |
| 643 | using namespace CryptoPP; |
| 644 | |
| 645 | StringSource privateKeySource(buf, size, true); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 646 | SecByteBlock rawKeyBits; |
| 647 | // PrivateKeyInfo ::= SEQUENCE { |
| 648 | // INTEGER, |
| 649 | // SEQUENCE, |
| 650 | // OCTECT STRING} |
| 651 | BERSequenceDecoder privateKeyInfo(privateKeySource); |
| 652 | { |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 653 | uint32_t versionNum; |
| 654 | BERDecodeUnsigned<uint32_t>(privateKeyInfo, versionNum, INTEGER); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 655 | BERSequenceDecoder sequenceDecoder(privateKeyInfo); |
| 656 | { |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 657 | OID keyTypeOID; |
| 658 | keyTypeOID.decode(sequenceDecoder); |
| 659 | |
| 660 | if (keyTypeOID == oid::RSA) |
| 661 | BERDecodeNull(sequenceDecoder); |
| 662 | else if (keyTypeOID == oid::ECDSA) |
| 663 | { |
| 664 | OID parameterOID; |
| 665 | parameterOID.decode(sequenceDecoder); |
| 666 | } |
| 667 | else |
| 668 | return false; // Unsupported key type; |
| 669 | |
| 670 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 671 | } |
| 672 | BERDecodeOctetString(privateKeyInfo, rawKeyBits); |
| 673 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 674 | privateKeyInfo.MessageEnd(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 675 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 676 | CFReleaser<CFDataRef> importedKey = CFDataCreateWithBytesNoCopy(0, |
| 677 | rawKeyBits.BytePtr(), |
| 678 | rawKeyBits.size(), |
| 679 | kCFAllocatorNull); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 680 | |
| 681 | SecExternalFormat externalFormat = kSecFormatOpenSSL; |
| 682 | SecExternalItemType externalType = kSecItemTypePrivateKey; |
| 683 | SecKeyImportExportParameters keyParams; |
| 684 | memset(&keyParams, 0, sizeof(keyParams)); |
| 685 | keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION; |
| 686 | keyParams.keyAttributes = CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_PERMANENT; |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 687 | CFReleaser<SecAccessRef> access; |
| 688 | CFReleaser<CFStringRef> keyLabel = CFStringCreateWithCString(0, |
| 689 | keyName.toUri().c_str(), |
| 690 | kCFStringEncodingUTF8); |
| 691 | SecAccessCreate(keyLabel.get(), 0, &access.get()); |
| 692 | keyParams.accessRef = access.get(); |
| 693 | CFReleaser<CFArrayRef> outItems; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 694 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 695 | #ifdef __clang__ |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 696 | #pragma clang diagnostic push |
| 697 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 698 | #endif // __clang__ |
| 699 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 700 | OSStatus res = SecKeychainItemImport(importedKey.get(), |
| 701 | 0, |
| 702 | &externalFormat, |
| 703 | &externalType, |
| 704 | 0, |
| 705 | &keyParams, |
| 706 | m_impl->m_keyChainRef, |
| 707 | &outItems.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 708 | |
| 709 | #ifdef __clang__ |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 710 | #pragma clang diagnostic pop |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 711 | #endif // __clang__ |
| 712 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 713 | if (res != errSecSuccess) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 714 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 715 | if (res == errSecAuthFailed && !needRetry) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 716 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 717 | if (unlockTpm(0, 0, false)) |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 718 | return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, true); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 719 | else |
| 720 | return false; |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 721 | } |
| 722 | else |
| 723 | return false; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 724 | } |
| 725 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 726 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 727 | SecKeychainItemRef privateKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems.get(), 0); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 728 | SecKeychainAttribute attrs[1]; // maximum number of attributes |
| 729 | SecKeychainAttributeList attrList = { 0, attrs }; |
| 730 | string keyUri = keyName.toUri(); |
| 731 | { |
| 732 | attrs[attrList.count].tag = kSecKeyPrintName; |
| 733 | attrs[attrList.count].length = keyUri.size(); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 734 | attrs[attrList.count].data = const_cast<char*>(keyUri.c_str()); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 735 | attrList.count++; |
| 736 | } |
| 737 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 738 | res = SecKeychainItemModifyAttributesAndData(privateKey, |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 739 | &attrList, |
| 740 | 0, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 741 | 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 742 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 743 | if (res != errSecSuccess) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 744 | { |
| 745 | return false; |
| 746 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 747 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 748 | return true; |
| 749 | } |
| 750 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 751 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 752 | #pragma GCC diagnostic pop |
| 753 | #endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 754 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 755 | bool |
| 756 | SecTpmOsx::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 757 | { |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 758 | CFReleaser<CFDataRef> importedKey = CFDataCreateWithBytesNoCopy(0, |
| 759 | buf, |
| 760 | size, |
| 761 | kCFAllocatorNull); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 762 | |
| 763 | SecExternalFormat externalFormat = kSecFormatOpenSSL; |
| 764 | SecExternalItemType externalType = kSecItemTypePublicKey; |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 765 | CFReleaser<CFArrayRef> outItems; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 766 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 767 | OSStatus res = SecItemImport(importedKey.get(), |
| 768 | 0, |
| 769 | &externalFormat, |
| 770 | &externalType, |
| 771 | 0, |
| 772 | 0, |
| 773 | m_impl->m_keyChainRef, |
| 774 | &outItems.get()); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 775 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 776 | if (res != errSecSuccess) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 777 | return false; |
| 778 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 779 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 780 | SecKeychainItemRef publicKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems.get(), 0); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 781 | SecKeychainAttribute attrs[1]; // maximum number of attributes |
| 782 | SecKeychainAttributeList attrList = { 0, attrs }; |
| 783 | string keyUri = keyName.toUri(); |
| 784 | { |
| 785 | attrs[attrList.count].tag = kSecKeyPrintName; |
| 786 | attrs[attrList.count].length = keyUri.size(); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 787 | attrs[attrList.count].data = const_cast<char*>(keyUri.c_str()); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 788 | attrList.count++; |
| 789 | } |
| 790 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 791 | res = SecKeychainItemModifyAttributesAndData(publicKey, |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 792 | &attrList, |
| 793 | 0, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 794 | 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 795 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 796 | if (res != errSecSuccess) |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 797 | return false; |
| 798 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 799 | return true; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | Block |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 803 | SecTpmOsx::signInTpmInternal(const uint8_t* data, size_t dataLength, |
| 804 | const Name& keyName, DigestAlgorithm digestAlgorithm, bool needRetry) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 805 | { |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 806 | CFReleaser<CFDataRef> dataRef = CFDataCreateWithBytesNoCopy(0, |
| 807 | data, |
| 808 | dataLength, |
| 809 | kCFAllocatorNull); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 810 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 811 | CFReleaser<SecKeychainItemRef> privateKey = m_impl->getKey(keyName, KEY_CLASS_PRIVATE); |
| 812 | if (privateKey.get() == 0) |
| 813 | { |
| 814 | throw Error("Private key [" + keyName.toUri() + "] does not exist in OSX Keychain"); |
| 815 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 816 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 817 | CFReleaser<CFErrorRef> error; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 818 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 819 | CFReleaser<SecTransformRef> signer = SecSignTransformCreate((SecKeyRef)privateKey.get(), |
| 820 | &error.get()); |
| 821 | if (error.get() != 0) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 822 | throw Error("Fail to create signer"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 823 | |
| 824 | // Set input |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 825 | SecTransformSetAttribute(signer.get(), |
| 826 | kSecTransformInputAttributeName, |
| 827 | dataRef.get(), |
| 828 | &error.get()); |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 829 | if (error.get() != 0) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 830 | throw Error("Fail to configure input of signer"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 831 | |
| 832 | // Enable use of padding |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 833 | SecTransformSetAttribute(signer.get(), |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 834 | kSecPaddingKey, |
| 835 | kSecPaddingPKCS1Key, |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 836 | &error.get()); |
| 837 | if (error.get() != 0) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 838 | throw Error("Fail to configure digest algorithm of signer"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 839 | |
| 840 | // Set padding type |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 841 | SecTransformSetAttribute(signer.get(), |
| 842 | kSecDigestTypeAttribute, |
| 843 | m_impl->getDigestAlgorithm(digestAlgorithm), |
| 844 | &error.get()); |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 845 | if (error.get() != 0) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 846 | throw Error("Fail to configure digest algorithm of signer"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 847 | |
| 848 | // Set padding attribute |
| 849 | long digestSize = m_impl->getDigestSize(digestAlgorithm); |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 850 | CFReleaser<CFNumberRef> cfDigestSize = CFNumberCreate(0, kCFNumberLongType, &digestSize); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 851 | SecTransformSetAttribute(signer.get(), |
| 852 | kSecDigestLengthAttribute, |
| 853 | cfDigestSize.get(), |
| 854 | &error.get()); |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 855 | if (error.get() != 0) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 856 | throw Error("Fail to configure digest size of signer"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 857 | |
| 858 | // Actually sign |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 859 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 860 | CFReleaser<CFDataRef> signature = (CFDataRef)SecTransformExecute(signer.get(), &error.get()); |
| 861 | if (error.get() != 0) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 862 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 863 | if (!needRetry) |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 864 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 865 | if (unlockTpm(0, 0, false)) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 866 | return signInTpmInternal(data, dataLength, keyName, digestAlgorithm, true); |
| 867 | else |
| 868 | throw Error("Fail to unlock the keychain"); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 869 | } |
| 870 | else |
| 871 | { |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 872 | CFShow(error.get()); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 873 | throw Error("Fail to sign data"); |
| 874 | } |
| 875 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 876 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 877 | if (signature.get() == 0) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 878 | throw Error("Signature is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 879 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 880 | return Block(tlv::SignatureValue, |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 881 | make_shared<Buffer>(CFDataGetBytePtr(signature.get()), |
| 882 | CFDataGetLength(signature.get()))); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | ConstBufferPtr |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 886 | 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] | 887 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 888 | throw Error("SecTpmOsx::decryptInTpm is not supported"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 889 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 890 | // KeyClass keyClass; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 891 | // if (sym) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 892 | // keyClass = KEY_CLASS_SYMMETRIC; |
| 893 | // else |
| 894 | // keyClass = KEY_CLASS_PRIVATE; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 895 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 896 | // CFDataRef dataRef = CFDataCreate(0, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 897 | // reinterpret_cast<const unsigned char*>(data), |
| 898 | // dataLength |
| 899 | // ); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 900 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 901 | // CFReleaser<SecKeyRef> decryptKey = (SecKeyRef)m_impl->getKey(keyName, keyClass); |
| 902 | // if (decryptKey.get() == 0) |
| 903 | // { |
| 904 | // /// @todo Can this happen because of keychain is locked? |
| 905 | // throw Error("Decruption key [" + ??? + "] does not exist in OSX Keychain"); |
| 906 | // } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 907 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 908 | // CFErrorRef error; |
| 909 | // SecTransformRef decrypt = SecDecryptTransformCreate(decryptKey, &error); |
| 910 | // if (error) throw Error("Fail to create decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 911 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 912 | // Boolean set_res = SecTransformSetAttribute(decrypt, |
| 913 | // kSecTransformInputAttributeName, |
| 914 | // dataRef, |
| 915 | // &error); |
| 916 | // if (error) throw Error("Fail to configure decrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 917 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 918 | // CFDataRef output = (CFDataRef) SecTransformExecute(decrypt, &error); |
| 919 | // if (error) |
| 920 | // { |
| 921 | // CFShow(error); |
| 922 | // throw Error("Fail to decrypt data"); |
| 923 | // } |
| 924 | // if (!output) throw Error("Output is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 925 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 926 | // return make_shared<Buffer>(CFDataGetBytePtr(output), CFDataGetLength(output)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 927 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 928 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 929 | void |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 930 | SecTpmOsx::addAppToAcl(const Name& keyName, KeyClass keyClass, const string& appPath, AclType acl) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 931 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 932 | if (keyClass == KEY_CLASS_PRIVATE && acl == ACL_TYPE_PRIVATE) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 933 | { |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 934 | CFReleaser<SecKeychainItemRef> privateKey = m_impl->getKey(keyName, keyClass); |
| 935 | if (privateKey.get() == 0) |
| 936 | { |
| 937 | throw Error("Private key [" + keyName.toUri() + "] does not exist in OSX Keychain"); |
| 938 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 939 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 940 | CFReleaser<SecAccessRef> accRef; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 941 | SecKeychainItemCopyAccess(privateKey.get(), &accRef.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 942 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 943 | CFReleaser<CFArrayRef> signACL = SecAccessCopyMatchingACLList(accRef.get(), |
| 944 | kSecACLAuthorizationSign); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 945 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 946 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 947 | SecACLRef aclRef = (SecACLRef)CFArrayGetValueAtIndex(signACL.get(), 0); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 948 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 949 | CFReleaser<CFArrayRef> appList; |
| 950 | CFReleaser<CFStringRef> description; |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 951 | SecKeychainPromptSelector promptSelector; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 952 | SecACLCopyContents(aclRef, |
| 953 | &appList.get(), |
| 954 | &description.get(), |
| 955 | &promptSelector); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 956 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 957 | CFReleaser<CFMutableArrayRef> newAppList = CFArrayCreateMutableCopy(0, |
| 958 | 0, |
| 959 | appList.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 960 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 961 | CFReleaser<SecTrustedApplicationRef> trustedApp; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 962 | SecTrustedApplicationCreateFromPath(appPath.c_str(), |
| 963 | &trustedApp.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 964 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 965 | CFArrayAppendValue(newAppList.get(), trustedApp.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 966 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 967 | SecACLSetContents(aclRef, |
| 968 | newAppList.get(), |
| 969 | description.get(), |
| 970 | promptSelector); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 971 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 972 | SecKeychainItemSetAccess(privateKey.get(), accRef.get()); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 973 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 976 | ConstBufferPtr |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 977 | 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] | 978 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 979 | throw Error("SecTpmOsx::encryptInTpm is not supported"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 980 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 981 | // KeyClass keyClass; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 982 | // if (sym) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 983 | // keyClass = KEY_CLASS_SYMMETRIC; |
| 984 | // else |
| 985 | // keyClass = KEY_CLASS_PUBLIC; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 986 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 987 | // CFDataRef dataRef = CFDataCreate(0, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 988 | // reinterpret_cast<const unsigned char*>(data), |
| 989 | // dataLength |
| 990 | // ); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 991 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 992 | // CFReleaser<SecKeyRef> encryptKey = (SecKeyRef)m_impl->getKey(keyName, keyClass); |
| 993 | // if (encryptKey.get() == 0) |
| 994 | // { |
| 995 | // throw Error("Encryption key [" + ???? + "] does not exist in OSX Keychain"); |
| 996 | // } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 997 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 998 | // CFErrorRef error; |
| 999 | // SecTransformRef encrypt = SecEncryptTransformCreate(encryptKey, &error); |
| 1000 | // if (error) throw Error("Fail to create encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1001 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 1002 | // Boolean set_res = SecTransformSetAttribute(encrypt, |
| 1003 | // kSecTransformInputAttributeName, |
| 1004 | // dataRef, |
| 1005 | // &error); |
| 1006 | // if (error) throw Error("Fail to configure encrypt"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1007 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 1008 | // CFDataRef output = (CFDataRef) SecTransformExecute(encrypt, &error); |
| 1009 | // if (error) throw Error("Fail to encrypt data"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1010 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 1011 | // if (!output) throw Error("Output is NULL!\n"); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1012 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 1013 | // return make_shared<Buffer> (CFDataGetBytePtr(output), CFDataGetLength(output)); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 1017 | SecTpmOsx::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1018 | { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1019 | string keyNameUri = m_impl->toInternalKeyName(keyName, keyClass); |
| 1020 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1021 | CFReleaser<CFStringRef> keyLabel = CFStringCreateWithCString(0, |
| 1022 | keyNameUri.c_str(), |
| 1023 | kCFStringEncodingUTF8); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1024 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1025 | CFReleaser<CFMutableDictionaryRef> attrDict = |
| 1026 | CFDictionaryCreateMutable(0, |
| 1027 | 4, |
| 1028 | &kCFTypeDictionaryKeyCallBacks, |
| 1029 | 0); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1030 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1031 | CFDictionaryAddValue(attrDict.get(), kSecClass, kSecClassKey); |
| 1032 | // CFDictionaryAddValue(attrDict.get(), kSecAttrKeyClass, m_impl->getKeyClass(keyClass)); |
| 1033 | CFDictionaryAddValue(attrDict.get(), kSecAttrLabel, keyLabel.get()); |
| 1034 | CFDictionaryAddValue(attrDict.get(), kSecReturnRef, kCFBooleanTrue); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1035 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1036 | CFReleaser<SecKeychainItemRef> itemRef; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1037 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1038 | OSStatus res = SecItemCopyMatching((CFDictionaryRef)attrDict.get(), (CFTypeRef*)&itemRef.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1039 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 1040 | if (res == errSecSuccess) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1041 | return true; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 1042 | else |
| 1043 | return false; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1044 | |
| 1045 | } |
| 1046 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 1047 | bool |
| 1048 | SecTpmOsx::generateRandomBlock(uint8_t* res, size_t size) |
| 1049 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1050 | return SecRandomCopyBytes(kSecRandomDefault, size, res) == 0; |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 1051 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1052 | |
| 1053 | //////////////////////////////// |
| 1054 | // OSXPrivateKeyStorage::Impl // |
| 1055 | //////////////////////////////// |
| 1056 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1057 | CFReleaser<SecKeychainItemRef> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 1058 | SecTpmOsx::Impl::getKey(const Name& keyName, KeyClass keyClass) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1059 | { |
| 1060 | string keyNameUri = toInternalKeyName(keyName, keyClass); |
| 1061 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1062 | CFReleaser<CFStringRef> keyLabel = CFStringCreateWithCString(0, |
| 1063 | keyNameUri.c_str(), |
| 1064 | kCFStringEncodingUTF8); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1065 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1066 | CFReleaser<CFMutableDictionaryRef> attrDict = |
| 1067 | CFDictionaryCreateMutable(0, |
| 1068 | 5, |
| 1069 | &kCFTypeDictionaryKeyCallBacks, |
| 1070 | 0); |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1071 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1072 | CFDictionaryAddValue(attrDict.get(), kSecClass, kSecClassKey); |
| 1073 | CFDictionaryAddValue(attrDict.get(), kSecAttrLabel, keyLabel.get()); |
| 1074 | CFDictionaryAddValue(attrDict.get(), kSecAttrKeyClass, getKeyClass(keyClass)); |
| 1075 | CFDictionaryAddValue(attrDict.get(), kSecReturnRef, kCFBooleanTrue); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1076 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1077 | CFReleaser<SecKeychainItemRef> keyItem; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1078 | // C-style cast is used as per Apple convention |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 1079 | OSStatus res = SecItemCopyMatching((CFDictionaryRef)attrDict.get(), (CFTypeRef*)&keyItem.get()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1080 | |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 1081 | if (res != errSecSuccess) |
| 1082 | return 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1083 | else |
| 1084 | return keyItem; |
| 1085 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1086 | |
| 1087 | string |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 1088 | SecTpmOsx::Impl::toInternalKeyName(const Name& keyName, KeyClass keyClass) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1089 | { |
| 1090 | string keyUri = keyName.toUri(); |
| 1091 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 1092 | if (KEY_CLASS_SYMMETRIC == keyClass) |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1093 | return keyUri + "/symmetric"; |
| 1094 | else |
| 1095 | return keyUri; |
| 1096 | } |
| 1097 | |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 1098 | CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1099 | SecTpmOsx::Impl::getAsymKeyType(KeyType keyType) |
| 1100 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1101 | switch (keyType) { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1102 | case KEY_TYPE_RSA: |
| 1103 | return kSecAttrKeyTypeRSA; |
Yingdi Yu | c8f883c | 2014-06-20 23:25:22 -0700 | [diff] [blame] | 1104 | case KEY_TYPE_ECDSA: |
| 1105 | return kSecAttrKeyTypeECDSA; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1106 | default: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 1107 | return 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 1111 | CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1112 | SecTpmOsx::Impl::getSymKeyType(KeyType keyType) |
| 1113 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1114 | switch (keyType) { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1115 | case KEY_TYPE_AES: |
| 1116 | return kSecAttrKeyTypeAES; |
| 1117 | default: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 1118 | return 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 1122 | CFTypeRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1123 | SecTpmOsx::Impl::getKeyClass(KeyClass keyClass) |
| 1124 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1125 | switch (keyClass) { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1126 | case KEY_CLASS_PRIVATE: |
| 1127 | return kSecAttrKeyClassPrivate; |
| 1128 | case KEY_CLASS_PUBLIC: |
| 1129 | return kSecAttrKeyClassPublic; |
| 1130 | case KEY_CLASS_SYMMETRIC: |
| 1131 | return kSecAttrKeyClassSymmetric; |
| 1132 | default: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 1133 | return 0; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 1137 | CFStringRef |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1138 | SecTpmOsx::Impl::getDigestAlgorithm(DigestAlgorithm digestAlgo) |
| 1139 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1140 | switch (digestAlgo) { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1141 | case DIGEST_ALGORITHM_SHA256: |
| 1142 | return kSecDigestSHA2; |
| 1143 | default: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 1144 | return 0; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 1145 | } |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1146 | } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 1147 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1148 | long |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1149 | SecTpmOsx::Impl::getDigestSize(DigestAlgorithm digestAlgo) |
| 1150 | { |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1151 | switch (digestAlgo) { |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1152 | case DIGEST_ALGORITHM_SHA256: |
| 1153 | return 256; |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1154 | default: |
Yingdi Yu | 2b2b479 | 2014-02-04 16:27:07 -0800 | [diff] [blame] | 1155 | return -1; |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 1156 | } |
Jeff Thompson | 2747dc0 | 2013-10-04 19:11:34 -0700 | [diff] [blame] | 1157 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 1158 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 1159 | } // namespace ndn |