blob: bab5df83528533d2b94272c47469aa6a71a8ea77 [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
8#include "../../c/util/crypto.h"
9#include "../security-exception.hpp"
10#include "memory-private-key-storage.hpp"
11
12using namespace std;
13using namespace ndn::ptr_lib;
14
15namespace ndn {
16
17MemoryPrivateKeyStorage::~MemoryPrivateKeyStorage()
18{
19}
20
21void MemoryPrivateKeyStorage::setKeyPairForKeyName
Jeff Thompson10ad12a2013-09-24 16:19:11 -070022 (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer,
Jeff Thompson6c314bc2013-09-23 18:09:38 -070023 size_t privateKeyDerLength)
24{
25 publicKeyStore_[keyName.toUri()] = PublicKey::fromDer(Blob(publicKeyDer, publicKeyDerLength));
26 privateKeyStore_[keyName.toUri()] = shared_ptr<RsaPrivateKey>(new RsaPrivateKey(privateKeyDer, privateKeyDerLength));
27}
28
29void
30MemoryPrivateKeyStorage::generateKeyPair(const Name& keyName, KeyType keyType, int keySize)
31{
32#if 1
33 throw std::runtime_error("MemoryPrivateKeyStorage::generateKeyPair not implemented");
34#endif
35}
36
37shared_ptr<PublicKey>
38MemoryPrivateKeyStorage::getPublicKey(const Name& keyName)
39{
40 map<string, shared_ptr<PublicKey> >::iterator publicKey = publicKeyStore_.find(keyName.toUri());
41 if (publicKey == publicKeyStore_.end())
42 throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
43 return publicKey->second;
44}
45
46Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070047MemoryPrivateKeyStorage::sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070048{
49 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
50 return Blob();
51
Jeff Thompson10ad12a2013-09-24 16:19:11 -070052 uint8_t digest[SHA256_DIGEST_LENGTH];
Jeff Thompson6c314bc2013-09-23 18:09:38 -070053 ndn_digestSha256(data, dataLength, digest);
54 // TODO: use RSA_size to get the proper size of the signature buffer.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070055 uint8_t signatureBits[1000];
Jeff Thompson6c314bc2013-09-23 18:09:38 -070056 unsigned int signatureBitsLength;
57
58 // Find the private key and sign.
59 map<string, shared_ptr<RsaPrivateKey> >::iterator privateKey = privateKeyStore_.find(keyName.toUri());
60 if (privateKey == privateKeyStore_.end())
61 throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
62 if (!RSA_sign(NID_sha256, digest, sizeof(digest), signatureBits, &signatureBitsLength, privateKey->second->getPrivateKey()))
63 throw SecurityException("Error in RSA_sign");
64
Jeff Thompson97223af2013-09-24 17:01:27 -070065 return Blob(signatureBits, (size_t)signatureBitsLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070066}
67
68Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070069MemoryPrivateKeyStorage::decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070070{
71#if 1
72 throw std::runtime_error("MemoryPrivateKeyStorage::decrypt not implemented");
73#endif
74}
75
76Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070077MemoryPrivateKeyStorage::encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070078{
79#if 1
80 throw std::runtime_error("MemoryPrivateKeyStorage::encrypt not implemented");
81#endif
82}
83
84void
85MemoryPrivateKeyStorage::generateKey(const Name& keyName, KeyType keyType, int keySize)
86{
87#if 1
88 throw std::runtime_error("MemoryPrivateKeyStorage::generateKey not implemented");
89#endif
90}
91
92bool
93MemoryPrivateKeyStorage::doesKeyExist(const Name& keyName, KeyClass keyClass)
94{
95 if (keyClass == KEY_CLASS_PUBLIC)
96 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
97 else if (keyClass == KEY_CLASS_PRIVATE)
98 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
99 else
100 // KEY_CLASS_SYMMETRIC not implemented yet.
101 return false;
102}
103
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700104MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700105{
106 // Use a temporary pointer since d2i updates it.
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700107 const uint8_t *derPointer = keyDer;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700108 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
109 if (!privateKey_)
110 throw SecurityException("RsaPrivateKey constructor: Error decoding private key DER");
111}
112
113MemoryPrivateKeyStorage::RsaPrivateKey::~RsaPrivateKey()
114{
115 if (privateKey_)
116 RSA_free(privateKey_);
117}
118
119}