summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorrafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-06 18:55:35 +0000
committerrafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-06 18:55:35 +0000
commite90ed8ae06d460f74fc32207ddaca7e1d1de787d (patch)
tree65d98d20e777c0cc1aab3c31cf9ed05e1dc550bd /base
parenta112832e7f2b611a814f68b6f1335159f8f4dec0 (diff)
downloadchromium_src-e90ed8ae06d460f74fc32207ddaca7e1d1de787d.zip
chromium_src-e90ed8ae06d460f74fc32207ddaca7e1d1de787d.tar.gz
chromium_src-e90ed8ae06d460f74fc32207ddaca7e1d1de787d.tar.bz2
Implement signature_creator_mac. Also moved CSSM memory functions to a common shared location.
BUG=20669 Review URL: http://codereview.chromium.org/259026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28135 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp7
-rw-r--r--base/crypto/cssm_init.cc27
-rw-r--r--base/crypto/cssm_init.h8
-rw-r--r--base/crypto/rsa_private_key_mac.cc28
-rw-r--r--base/crypto/signature_creator.h5
-rw-r--r--base/crypto/signature_creator_mac.cc90
-rw-r--r--base/crypto/signature_verifier_mac.cc30
7 files changed, 129 insertions, 66 deletions
diff --git a/base/base.gyp b/base/base.gyp
index 383791f..0ad3f16 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -24,6 +24,7 @@
'crypto/rsa_private_key_nss.cc',
'crypto/rsa_private_key_win.cc',
'crypto/signature_creator.h',
+ 'crypto/signature_creator_mac.cc',
'crypto/signature_creator_nss.cc',
'crypto/signature_creator_win.cc',
'crypto/signature_verifier.h',
@@ -677,11 +678,7 @@
'message_pump_glib_unittest.cc',
]
}],
- ['OS == "mac"', {
- 'sources!': [
- 'crypto/signature_creator_unittest.cc',
- ],
- }, { # OS != "mac"
+ ['OS != "mac"', {
'sources!': [
'mac_util_unittest.cc',
],
diff --git a/base/crypto/cssm_init.cc b/base/crypto/cssm_init.cc
index 3483ef9..c3cbbd2 100644
--- a/base/crypto/cssm_init.cc
+++ b/base/crypto/cssm_init.cc
@@ -3,9 +3,6 @@
// found in the LICENSE file.
#include "base/crypto/cssm_init.h"
-
-#include <Security/cssm.h>
-
#include "base/logging.h"
#include "base/singleton.h"
@@ -69,4 +66,28 @@ void EnsureCSSMInit() {
Singleton<CSSMInitSingleton>::get();
}
+void* CSSMMalloc(CSSM_SIZE size, void *alloc_ref) {
+ return malloc(size);
+}
+
+void CSSMFree(void* mem_ptr, void* alloc_ref) {
+ free(mem_ptr);
+}
+
+void* CSSMRealloc(void* ptr, CSSM_SIZE size, void* alloc_ref) {
+ return realloc(ptr, size);
+}
+
+void* CSSMCalloc(uint32 num, CSSM_SIZE size, void* alloc_ref) {
+ return calloc(num, size);
+}
+
+const CSSM_API_MEMORY_FUNCS kCssmMemoryFunctions = {
+ CSSMMalloc,
+ CSSMFree,
+ CSSMRealloc,
+ CSSMCalloc,
+ NULL
+};
+
} // namespace base
diff --git a/base/crypto/cssm_init.h b/base/crypto/cssm_init.h
index 20eda50..2637542 100644
--- a/base/crypto/cssm_init.h
+++ b/base/crypto/cssm_init.h
@@ -5,6 +5,11 @@
#ifndef BASE_CRYPTO_CSSM_INIT_H_
#define BASE_CRYPTO_CSSM_INIT_H_
+#include <Security/cssm.h>
+
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+
namespace base {
// Initialize CSSM if it isn't already initialized. This must be called before
@@ -12,6 +17,9 @@ namespace base {
// ever be initialized once. CSSM will be properly shut down on program exit.
void EnsureCSSMInit();
+// Set of pointers to memory function wrappers that are required for CSSM
+extern const CSSM_API_MEMORY_FUNCS kCssmMemoryFunctions;
+
} // namespace base
#endif // BASE_CRYPTO_CSSM_INIT_H_
diff --git a/base/crypto/rsa_private_key_mac.cc b/base/crypto/rsa_private_key_mac.cc
index 0efbd14..f44115c 100644
--- a/base/crypto/rsa_private_key_mac.cc
+++ b/base/crypto/rsa_private_key_mac.cc
@@ -25,32 +25,6 @@ const uint8 kNullTag = 0x05;
const uint8 kOctetStringTag = 0x04;
const uint8 kBitStringTag = 0x03;
-// TODO(hawk): Move the App* functions into a shared location,
-// perhaps cssm_init.cc.
-void* AppMalloc(CSSM_SIZE size, void *alloc_ref) {
- return malloc(size);
-}
-
-void AppFree(void* mem_ptr, void* alloc_ref) {
- free(mem_ptr);
-}
-
-void* AppRealloc(void* ptr, CSSM_SIZE size, void* alloc_ref) {
- return realloc(ptr, size);
-}
-
-void* AppCalloc(uint32 num, CSSM_SIZE size, void* alloc_ref) {
- return calloc(num, size);
-}
-
-const CSSM_API_MEMORY_FUNCS mem_funcs = {
- AppMalloc,
- AppFree,
- AppRealloc,
- AppCalloc,
- NULL
-};
-
// Helper for error handling during key import.
#define READ_ASSERT(truth) \
if (!(truth)) { \
@@ -317,7 +291,7 @@ RSAPrivateKey::RSAPrivateKey() : csp_handle_(0) {
static CSSM_VERSION version = {2, 0};
CSSM_RETURN crtn;
- crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &mem_funcs, 0,
+ crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &kCssmMemoryFunctions, 0,
CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE,
NULL, 0, NULL, &csp_handle_);
DCHECK(crtn == CSSM_OK);
diff --git a/base/crypto/signature_creator.h b/base/crypto/signature_creator.h
index 6c7ddbc..97a6e79 100644
--- a/base/crypto/signature_creator.h
+++ b/base/crypto/signature_creator.h
@@ -10,7 +10,7 @@
#if defined(USE_NSS)
#include <cryptoht.h>
#elif defined(OS_MACOSX)
-// TODO(port)
+#include <Security/cssm.h>
#elif defined(OS_WIN)
#include <windows.h>
#include <wincrypt.h>
@@ -48,7 +48,8 @@ class SignatureCreator {
#if defined(USE_NSS)
SGNContext* sign_context_;
#elif defined(OS_MACOSX)
- // TODO(port)
+ CSSM_CSP_HANDLE csp_handle_;
+ CSSM_CC_HANDLE sig_handle_;
#elif defined(OS_WIN)
HCRYPTHASH hash_object_;
#endif
diff --git a/base/crypto/signature_creator_mac.cc b/base/crypto/signature_creator_mac.cc
new file mode 100644
index 0000000..ee0ca05
--- /dev/null
+++ b/base/crypto/signature_creator_mac.cc
@@ -0,0 +1,90 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/crypto/signature_creator.h"
+
+#include <stdlib.h>
+
+#include "base/crypto/cssm_init.h"
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+
+namespace base {
+
+// static
+SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
+ scoped_ptr<SignatureCreator> result(new SignatureCreator);
+ result->key_ = key;
+
+ CSSM_RETURN crtn;
+ crtn = CSSM_CSP_CreateSignatureContext(result->csp_handle_,
+ CSSM_ALGID_SHA1WithRSA,
+ NULL,
+ key->key(),
+ &result->sig_handle_);
+ if (crtn) {
+ NOTREACHED();
+ return NULL;
+ }
+
+ crtn = CSSM_SignDataInit(result->sig_handle_);
+ if (crtn) {
+ NOTREACHED();
+ return false;
+ }
+
+ return result.release();
+}
+
+SignatureCreator::SignatureCreator() : csp_handle_(0), sig_handle_(0) {
+ EnsureCSSMInit();
+
+ static CSSM_VERSION version = {2, 0};
+ CSSM_RETURN crtn;
+ crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &kCssmMemoryFunctions, 0,
+ CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE,
+ NULL, 0, NULL, &csp_handle_);
+ DCHECK(crtn == CSSM_OK);
+}
+
+SignatureCreator::~SignatureCreator() {
+ CSSM_RETURN crtn;
+ if (sig_handle_) {
+ crtn = CSSM_DeleteContext(sig_handle_);
+ DCHECK(crtn == CSSM_OK);
+ }
+
+ if (csp_handle_) {
+ CSSM_RETURN crtn = CSSM_ModuleDetach(csp_handle_);
+ DCHECK(crtn == CSSM_OK);
+ }
+}
+
+bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
+ CSSM_DATA data;
+ data.Data = const_cast<uint8*>(data_part);
+ data.Length = data_part_len;
+ CSSM_RETURN crtn = CSSM_SignDataUpdate(sig_handle_, &data, 1);
+ DCHECK(crtn == CSSM_OK);
+ return true;
+}
+
+bool SignatureCreator::Final(std::vector<uint8>* signature) {
+ CSSM_DATA sig;
+ memset(&sig, 0, sizeof(CSSM_DATA)); // Allow CSSM allocate memory;
+ CSSM_RETURN crtn = CSSM_SignDataFinal(sig_handle_, &sig);
+
+ if (crtn) {
+ NOTREACHED();
+ return false;
+ }
+
+ signature->assign(sig.Data, sig.Data + sig.Length);
+ kCssmMemoryFunctions.free_func(sig.Data, NULL); // Release data alloc'd
+ // by CSSM
+
+ return true;
+}
+
+} // namespace base
diff --git a/base/crypto/signature_verifier_mac.cc b/base/crypto/signature_verifier_mac.cc
index 7dcd869..a4c1870 100644
--- a/base/crypto/signature_verifier_mac.cc
+++ b/base/crypto/signature_verifier_mac.cc
@@ -9,34 +9,6 @@
#include "base/crypto/cssm_init.h"
#include "base/logging.h"
-namespace {
-
-void* AppMalloc(CSSM_SIZE size, void *alloc_ref) {
- return malloc(size);
-}
-
-void AppFree(void* mem_ptr, void* alloc_ref) {
- free(mem_ptr);
-}
-
-void* AppRealloc(void* ptr, CSSM_SIZE size, void* alloc_ref) {
- return realloc(ptr, size);
-}
-
-void* AppCalloc(uint32 num, CSSM_SIZE size, void* alloc_ref) {
- return calloc(num, size);
-}
-
-const CSSM_API_MEMORY_FUNCS mem_funcs = {
- AppMalloc,
- AppFree,
- AppRealloc,
- AppCalloc,
- NULL
-};
-
-} // namespace
-
namespace base {
SignatureVerifier::SignatureVerifier() : csp_handle_(0), sig_handle_(0) {
@@ -44,7 +16,7 @@ SignatureVerifier::SignatureVerifier() : csp_handle_(0), sig_handle_(0) {
static CSSM_VERSION version = {2, 0};
CSSM_RETURN crtn;
- crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &mem_funcs, 0,
+ crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &kCssmMemoryFunctions, 0,
CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE,
NULL, 0, NULL, &csp_handle_);
DCHECK(crtn == CSSM_OK);