osx-private-key-storage.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_OSX_PRIVATEKEY_STORAGE_H
23 #define NDN_OSX_PRIVATEKEY_STORAGE_H
24 
25 // Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_OSX_SECURITY 1.
26 #include <ndn-cpp/ndn-cpp-config.h>
27 #if NDN_CPP_HAVE_OSX_SECURITY
28 
29 #include "../../common.hpp"
30 #include "private-key-storage.hpp"
31 
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <Security/Security.h>
34 #include <CoreServices/CoreServices.h>
35 
36 namespace ndn
37 {
38 
39 
49 template<class T>
50 class CFReleaser
51 {
52 public:
54  // Construction/destruction //
55 
56  CFReleaser()
57  : m_typeRef(0)
58  {
59  }
60 
61  CFReleaser(const T& typeRef)
62  : m_typeRef(typeRef)
63  {
64  }
65 
66  CFReleaser(const CFReleaser& inReleaser)
67  : m_typeRef(0)
68  {
69  retain(inReleaser.m_typeRef);
70  }
71 
72  CFReleaser&
73  operator=(const T& typeRef)
74  {
75  if (typeRef != m_typeRef) {
76  release();
77  m_typeRef = typeRef;
78  }
79  return *this;
80  }
81 
82  CFReleaser&
83  operator=(const CFReleaser& inReleaser)
84  {
85  retain(inReleaser.m_typeRef);
86  return *this;
87  }
88 
89  ~CFReleaser()
90  {
91  release();
92  }
93 
95  // Access //
96 
97  // operator const T&() const
98  // {
99  // return m_typeRef;
100  // }
101 
102  // operator T&()
103  // {
104  // return m_typeRef;
105  // }
106 
107  const T&
108  get() const
109  {
110  return m_typeRef;
111  }
112 
113  T&
114  get()
115  {
116  return m_typeRef;
117  }
118 
120  // Miscellaneous //
121 
122  void
123  retain(const T& typeRef)
124  {
125  if (typeRef != 0) {
126  CFRetain(typeRef);
127  }
128  release();
129  m_typeRef = typeRef;
130  }
131 
132  void release()
133  {
134  if (m_typeRef != 0) {
135  CFRelease(m_typeRef);
136  m_typeRef = 0;
137  }
138  };
139 
140 private:
141  T m_typeRef;
142 };
143 
144 class OSXPrivateKeyStorage : public PrivateKeyStorage {
145 public:
149  OSXPrivateKeyStorage() {}
150 
154  virtual
155  ~OSXPrivateKeyStorage();
156 
162  virtual void
163  generateKeyPair(const Name& keyName, const KeyParams& params);
164 
169  virtual void
170  deleteKeyPair(const Name& keyName);
171 
177  virtual ptr_lib::shared_ptr<PublicKey>
178  getPublicKey(const Name& keyName);
179 
188  virtual Blob
189  sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256);
190 
199  virtual Blob
200  decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false);
201 
210  virtual Blob
211  encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false);
212 
218  virtual void
219  generateKey(const Name& keyName, const KeyParams& params);
220 
227  virtual bool
228  doesKeyExist(const Name& keyName, KeyClass keyClass);
229 
238  bool
239  setACL(const Name & keyName, KeyClass keyClass, int acl, const std::string & appPath);
240 
241  private:
248  std::string
249  toInternalKeyName(const Name & keyName, KeyClass keyClass);
250 
258  CFReleaser<SecKeychainItemRef>
259  getKey(const Name & keyName, KeyClass keyClass);
260 
266  const CFTypeRef
267  getSymmetricKeyType(KeyType keyType);
268 
274  const CFTypeRef
275  getAsymmetricKeyType(KeyType keyType);
276 
282  const CFTypeRef
283  getKeyClass(KeyClass keyClass);
284 
290  const CFStringRef
291  getDigestAlgorithm(DigestAlgorithm digestAlgorithm);
292 
298  long
299  getDigestSize(DigestAlgorithm digestAlgo);
300 };
301 
302 }
303 
304 #endif // NDN_CPP_HAVE_OSX_SECURITY
305 
306 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35