1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CRYPTO_CSSM_INIT_H_
6#define CRYPTO_CSSM_INIT_H_
7
8#include <Security/cssm.h>
9
10#include "base/basictypes.h"
11#include "crypto/crypto_export.h"
12
13namespace crypto {
14
15// Initialize CSSM if it isn't already initialized.  This must be called before
16// any other CSSM functions.  This function is thread-safe, and CSSM will only
17// ever be initialized once.  CSSM will be properly shut down on program exit.
18CRYPTO_EXPORT void EnsureCSSMInit();
19
20// Returns the shared CSP handle used by CSSM functions.
21CRYPTO_EXPORT CSSM_CSP_HANDLE GetSharedCSPHandle();
22
23// Returns the shared CL handle used by CSSM functions.
24CRYPTO_EXPORT CSSM_CL_HANDLE GetSharedCLHandle();
25
26// Returns the shared TP handle used by CSSM functions.
27CRYPTO_EXPORT CSSM_TP_HANDLE GetSharedTPHandle();
28
29// Set of pointers to memory function wrappers that are required for CSSM
30extern const CSSM_API_MEMORY_FUNCS kCssmMemoryFunctions;
31
32// Utility function to log an error message including the error name.
33CRYPTO_EXPORT void LogCSSMError(const char *function_name, CSSM_RETURN err);
34
35// Utility functions to allocate and release CSSM memory.
36void* CSSMMalloc(CSSM_SIZE size);
37CRYPTO_EXPORT void CSSMFree(void* ptr);
38
39// Wrapper class for CSSM_DATA type. This should only be used when using the
40// CL/TP/CSP handles from above, since that's the only time we're guaranteed (or
41// supposed to be guaranteed) that our memory management functions will be used.
42// Apple's Sec* APIs manage their own memory so it shouldn't be used for those.
43// The constructor initializes data_ to zero and the destructor releases the
44// data properly.
45class ScopedCSSMData {
46 public:
47  ScopedCSSMData();
48  ~ScopedCSSMData();
49  operator CSSM_DATA*() { return &data_; }
50  CSSM_DATA* operator ->() { return &data_; }
51
52 private:
53  CSSM_DATA data_;
54
55  DISALLOW_COPY_AND_ASSIGN(ScopedCSSMData);
56};
57
58}  // namespace crypto
59
60#endif  // CRYPTO_CSSM_INIT_H_
61