SecItemShim.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "SecItemShim.h"
  27. #if USE(SECURITY_FRAMEWORK)
  28. #include "BlockingResponseMap.h"
  29. #include "ChildProcess.h"
  30. #include "SecItemRequestData.h"
  31. #include "SecItemResponseData.h"
  32. #include "SecItemShimLibrary.h"
  33. #include "SecItemShimMessages.h"
  34. #include "SecItemShimProxyMessages.h"
  35. #include <Security/Security.h>
  36. #include <dlfcn.h>
  37. namespace WebKit {
  38. static BlockingResponseMap<SecItemResponseData>& responseMap()
  39. {
  40. AtomicallyInitializedStatic(BlockingResponseMap<SecItemResponseData>*, responseMap = new BlockingResponseMap<SecItemResponseData>);
  41. return *responseMap;
  42. }
  43. static ChildProcess* sharedProcess;
  44. SecItemShim& SecItemShim::shared()
  45. {
  46. static SecItemShim* shim;
  47. static dispatch_once_t once;
  48. dispatch_once(&once, ^{
  49. shim = adoptRef(new SecItemShim).leakRef();
  50. });
  51. return *shim;
  52. }
  53. SecItemShim::SecItemShim()
  54. : m_queue(WorkQueue::create("com.apple.WebKit.SecItemShim"))
  55. {
  56. }
  57. static uint64_t generateSecItemRequestID()
  58. {
  59. static int64_t uniqueSecItemRequestID;
  60. return atomicIncrement(&uniqueSecItemRequestID);
  61. }
  62. static PassOwnPtr<SecItemResponseData> sendSecItemRequest(SecItemRequestData::Type requestType, CFDictionaryRef query, CFDictionaryRef attributesToMatch = 0)
  63. {
  64. uint64_t requestID = generateSecItemRequestID();
  65. if (!sharedProcess->parentProcessConnection()->send(Messages::SecItemShimProxy::SecItemRequest(requestID, SecItemRequestData(requestType, query, attributesToMatch)), 0))
  66. return nullptr;
  67. return responseMap().waitForResponse(requestID);
  68. }
  69. static OSStatus webSecItemCopyMatching(CFDictionaryRef query, CFTypeRef* result)
  70. {
  71. OwnPtr<SecItemResponseData> response = sendSecItemRequest(SecItemRequestData::CopyMatching, query);
  72. if (!response)
  73. return errSecInteractionNotAllowed;
  74. *result = response->resultObject().leakRef();
  75. return response->resultCode();
  76. }
  77. static OSStatus webSecItemAdd(CFDictionaryRef query, CFTypeRef* result)
  78. {
  79. OwnPtr<SecItemResponseData> response = sendSecItemRequest(SecItemRequestData::Add, query);
  80. if (!response)
  81. return errSecInteractionNotAllowed;
  82. if (result)
  83. *result = response->resultObject().leakRef();
  84. return response->resultCode();
  85. }
  86. static OSStatus webSecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate)
  87. {
  88. OwnPtr<SecItemResponseData> response = sendSecItemRequest(SecItemRequestData::Update, query, attributesToUpdate);
  89. if (!response)
  90. return errSecInteractionNotAllowed;
  91. return response->resultCode();
  92. }
  93. static OSStatus webSecItemDelete(CFDictionaryRef query)
  94. {
  95. OwnPtr<SecItemResponseData> response = sendSecItemRequest(SecItemRequestData::Delete, query);
  96. if (!response)
  97. return errSecInteractionNotAllowed;
  98. return response->resultCode();
  99. }
  100. void SecItemShim::secItemResponse(uint64_t requestID, const SecItemResponseData& response)
  101. {
  102. responseMap().didReceiveResponse(requestID, adoptPtr(new SecItemResponseData(response)));
  103. }
  104. void SecItemShim::initialize(ChildProcess* process)
  105. {
  106. sharedProcess = process;
  107. const SecItemShimCallbacks callbacks = {
  108. webSecItemCopyMatching,
  109. webSecItemAdd,
  110. webSecItemUpdate,
  111. webSecItemDelete
  112. };
  113. SecItemShimInitializeFunc func = reinterpret_cast<SecItemShimInitializeFunc>(dlsym(RTLD_DEFAULT, "WebKitSecItemShimInitialize"));
  114. func(callbacks);
  115. }
  116. void SecItemShim::initializeConnection(CoreIPC::Connection* connection)
  117. {
  118. connection->addWorkQueueMessageReceiver(Messages::SecItemShim::messageReceiverName(), m_queue.get(), this);
  119. }
  120. } // namespace WebKit
  121. #endif // USE(SECURITY_FRAMEWORK)