blob: 7c3a2e0b23f599144d4e018dce3bcb473ecef442 [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;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070016
17namespace ndn {
18
19MemoryPrivateKeyStorage::~MemoryPrivateKeyStorage()
20{
21}
22
23void MemoryPrivateKeyStorage::setKeyPairForKeyName
Jeff Thompson10ad12a2013-09-24 16:19:11 -070024 (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer,
Jeff Thompson6c314bc2013-09-23 18:09:38 -070025 size_t privateKeyDerLength)
26{
27 publicKeyStore_[keyName.toUri()] = PublicKey::fromDer(Blob(publicKeyDer, publicKeyDerLength));
Jeff Thompsonce115762013-12-18 14:59:56 -080028 privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070029}
30
31void
32MemoryPrivateKeyStorage::generateKeyPair(const Name& keyName, KeyType keyType, int keySize)
33{
34#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070035 throw runtime_error("MemoryPrivateKeyStorage::generateKeyPair not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070036#endif
37}
38
Jeff Thompsonce115762013-12-18 14:59:56 -080039ptr_lib::shared_ptr<PublicKey>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070040MemoryPrivateKeyStorage::getPublicKey(const Name& keyName)
41{
Jeff Thompsonce115762013-12-18 14:59:56 -080042 map<string, ptr_lib::shared_ptr<PublicKey> >::iterator publicKey = publicKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070043 if (publicKey == publicKeyStore_.end())
44 throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
45 return publicKey->second;
46}
47
48Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070049MemoryPrivateKeyStorage::sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070050{
51 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
52 return Blob();
53
Jeff Thompson10ad12a2013-09-24 16:19:11 -070054 uint8_t digest[SHA256_DIGEST_LENGTH];
Jeff Thompson6c314bc2013-09-23 18:09:38 -070055 ndn_digestSha256(data, dataLength, digest);
56 // TODO: use RSA_size to get the proper size of the signature buffer.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070057 uint8_t signatureBits[1000];
Jeff Thompson6c314bc2013-09-23 18:09:38 -070058 unsigned int signatureBitsLength;
59
60 // Find the private key and sign.
Jeff Thompsonce115762013-12-18 14:59:56 -080061 map<string, ptr_lib::shared_ptr<RsaPrivateKey> >::iterator privateKey = privateKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070062 if (privateKey == privateKeyStore_.end())
63 throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
64 if (!RSA_sign(NID_sha256, digest, sizeof(digest), signatureBits, &signatureBitsLength, privateKey->second->getPrivateKey()))
65 throw SecurityException("Error in RSA_sign");
66
Jeff Thompson97223af2013-09-24 17:01:27 -070067 return Blob(signatureBits, (size_t)signatureBitsLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070068}
69
70Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070071MemoryPrivateKeyStorage::decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070072{
73#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070074 throw runtime_error("MemoryPrivateKeyStorage::decrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070075#endif
76}
77
78Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070079MemoryPrivateKeyStorage::encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070080{
81#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070082 throw runtime_error("MemoryPrivateKeyStorage::encrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070083#endif
84}
85
86void
87MemoryPrivateKeyStorage::generateKey(const Name& keyName, KeyType keyType, int keySize)
88{
89#if 1
Jeff Thompson4affbf52013-10-18 14:36:46 -070090 throw runtime_error("MemoryPrivateKeyStorage::generateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070091#endif
92}
93
94bool
95MemoryPrivateKeyStorage::doesKeyExist(const Name& keyName, KeyClass keyClass)
96{
97 if (keyClass == KEY_CLASS_PUBLIC)
98 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
99 else if (keyClass == KEY_CLASS_PRIVATE)
100 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
101 else
102 // KEY_CLASS_SYMMETRIC not implemented yet.
103 return false;
104}
105
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700106MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700107{
108 // Use a temporary pointer since d2i updates it.
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700109 const uint8_t *derPointer = keyDer;
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700110 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
111 if (!privateKey_)
112 throw SecurityException("RsaPrivateKey constructor: Error decoding private key DER");
113}
114
115MemoryPrivateKeyStorage::RsaPrivateKey::~RsaPrivateKey()
116{
117 if (privateKey_)
118 RSA_free(privateKey_);
119}
120
121}