blob: 09f1c664d9e12e041f8f2f133c6885b965e3ef2a [file] [log] [blame]
Jeff Thompson6c314bc2013-09-23 18:09:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson0f2096f2013-10-01 14:49:42 -07008#if 1
9#include <stdexcept>
10#endif
Jeff Thompson6c314bc2013-09-23 18:09:38 -070011#include "../../c/util/crypto.h"
Jeff Thompson25b4e612013-10-10 16:03:24 -070012#include <ndn-cpp/security/security-exception.hpp>
13#include <ndn-cpp/security/identity/memory-private-key-storage.hpp>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070014
15using namespace std;
16using namespace ndn::ptr_lib;
17
18namespace ndn {
19
20MemoryPrivateKeyStorage::~MemoryPrivateKeyStorage()
21{
22}
23
24void MemoryPrivateKeyStorage::setKeyPairForKeyName
Jeff Thompson10ad12a2013-09-24 16:19:11 -070025 (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer,
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026 size_t privateKeyDerLength)
27{
28 publicKeyStore_[keyName.toUri()] = PublicKey::fromDer(Blob(publicKeyDer, publicKeyDerLength));
29 privateKeyStore_[keyName.toUri()] = shared_ptr<RsaPrivateKey>(new RsaPrivateKey(privateKeyDer, privateKeyDerLength));
30}
31
32void
33MemoryPrivateKeyStorage::generateKeyPair(const Name& keyName, KeyType keyType, int keySize)
34{
35#if 1
36 throw std::runtime_error("MemoryPrivateKeyStorage::generateKeyPair not implemented");
37#endif
38}
39
40shared_ptr<PublicKey>
41MemoryPrivateKeyStorage::getPublicKey(const Name& keyName)
42{
43 map<string, shared_ptr<PublicKey> >::iterator publicKey = publicKeyStore_.find(keyName.toUri());
44 if (publicKey == publicKeyStore_.end())
45 throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
46 return publicKey->second;
47}
48
49Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070050MemoryPrivateKeyStorage::sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070051{
52 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
53 return Blob();
54
Jeff Thompson10ad12a2013-09-24 16:19:11 -070055 uint8_t digest[SHA256_DIGEST_LENGTH];
Jeff Thompson6c314bc2013-09-23 18:09:38 -070056 ndn_digestSha256(data, dataLength, digest);
57 // TODO: use RSA_size to get the proper size of the signature buffer.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070058 uint8_t signatureBits[1000];
Jeff Thompson6c314bc2013-09-23 18:09:38 -070059 unsigned int signatureBitsLength;
60
61 // Find the private key and sign.
62 map<string, shared_ptr<RsaPrivateKey> >::iterator privateKey = privateKeyStore_.find(keyName.toUri());
63 if (privateKey == privateKeyStore_.end())
64 throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
65 if (!RSA_sign(NID_sha256, digest, sizeof(digest), signatureBits, &signatureBitsLength, privateKey->second->getPrivateKey()))
66 throw SecurityException("Error in RSA_sign");
67
Jeff Thompson97223af2013-09-24 17:01:27 -070068 return Blob(signatureBits, (size_t)signatureBitsLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070069}
70
71Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070072MemoryPrivateKeyStorage::decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070073{
74#if 1
75 throw std::runtime_error("MemoryPrivateKeyStorage::decrypt not implemented");
76#endif
77}
78
79Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070080MemoryPrivateKeyStorage::encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070081{
82#if 1
83 throw std::runtime_error("MemoryPrivateKeyStorage::encrypt not implemented");
84#endif
85}
86
87void
88MemoryPrivateKeyStorage::generateKey(const Name& keyName, KeyType keyType, int keySize)
89{
90#if 1
91 throw std::runtime_error("MemoryPrivateKeyStorage::generateKey not implemented");
92#endif
93}
94
95bool
96MemoryPrivateKeyStorage::doesKeyExist(const Name& keyName, KeyClass keyClass)
97{
98 if (keyClass == KEY_CLASS_PUBLIC)
99 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
100 else if (keyClass == KEY_CLASS_PRIVATE)
101 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
102 else
103 // KEY_CLASS_SYMMETRIC not implemented yet.
104 return false;
105}
106
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700107MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700108{
109 // Use a temporary pointer since d2i updates it.
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700110 const uint8_t *derPointer = keyDer;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700111 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
112 if (!privateKey_)
113 throw SecurityException("RsaPrivateKey constructor: Error decoding private key DER");
114}
115
116MemoryPrivateKeyStorage::RsaPrivateKey::~RsaPrivateKey()
117{
118 if (privateKey_)
119 RSA_free(privateKey_);
120}
121
122}