Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | using namespace std; |
| 13 | using namespace ndn::ptr_lib; |
| 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | MemoryPrivateKeyStorage::~MemoryPrivateKeyStorage() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | void MemoryPrivateKeyStorage::setKeyPairForKeyName |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 22 | (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer, |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 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 | |
| 29 | void |
| 30 | MemoryPrivateKeyStorage::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 | |
| 37 | shared_ptr<PublicKey> |
| 38 | MemoryPrivateKeyStorage::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 | |
| 46 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 47 | MemoryPrivateKeyStorage::sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 48 | { |
| 49 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 50 | return Blob(); |
| 51 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 52 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 53 | ndn_digestSha256(data, dataLength, digest); |
| 54 | // TODO: use RSA_size to get the proper size of the signature buffer. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 55 | uint8_t signatureBits[1000]; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 65 | return Blob(signatureBits, (size_t)signatureBitsLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 69 | MemoryPrivateKeyStorage::decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 70 | { |
| 71 | #if 1 |
| 72 | throw std::runtime_error("MemoryPrivateKeyStorage::decrypt not implemented"); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 77 | MemoryPrivateKeyStorage::encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 78 | { |
| 79 | #if 1 |
| 80 | throw std::runtime_error("MemoryPrivateKeyStorage::encrypt not implemented"); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | MemoryPrivateKeyStorage::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 | |
| 92 | bool |
| 93 | MemoryPrivateKeyStorage::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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 104 | MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 105 | { |
| 106 | // Use a temporary pointer since d2i updates it. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 107 | const uint8_t *derPointer = keyDer; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 108 | privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength); |
| 109 | if (!privateKey_) |
| 110 | throw SecurityException("RsaPrivateKey constructor: Error decoding private key DER"); |
| 111 | } |
| 112 | |
| 113 | MemoryPrivateKeyStorage::RsaPrivateKey::~RsaPrivateKey() |
| 114 | { |
| 115 | if (privateKey_) |
| 116 | RSA_free(privateKey_); |
| 117 | } |
| 118 | |
| 119 | } |