blob: 56054af335060cc590a452fb9f77b46d394315dc [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
22 (const Name& keyName, unsigned char *publicKeyDer, size_t publicKeyDerLength, unsigned char *privateKeyDer,
23 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
47MemoryPrivateKeyStorage::sign(const unsigned char *data, unsigned int dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
48{
49 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
50 return Blob();
51
52 unsigned char digest[SHA256_DIGEST_LENGTH];
53 ndn_digestSha256(data, dataLength, digest);
54 // TODO: use RSA_size to get the proper size of the signature buffer.
55 unsigned char signatureBits[1000];
56 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
65 return Blob(signatureBits, signatureBitsLength);
66}
67
68Blob
69MemoryPrivateKeyStorage::decrypt(const Name& keyName, const unsigned char* data, unsigned int dataLength, bool isSymmetric)
70{
71#if 1
72 throw std::runtime_error("MemoryPrivateKeyStorage::decrypt not implemented");
73#endif
74}
75
76Blob
77MemoryPrivateKeyStorage::encrypt(const Name& keyName, const unsigned char* data, unsigned int dataLength, bool isSymmetric)
78{
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
104MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(unsigned char *keyDer, size_t keyDerLength)
105{
106 // Use a temporary pointer since d2i updates it.
107 const unsigned char *derPointer = keyDer;
108 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}