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 | |
Jeff Thompson | 0f2096f | 2013-10-01 14:49:42 -0700 | [diff] [blame] | 8 | #if 1 |
| 9 | #include <stdexcept> |
| 10 | #endif |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 11 | #include "../../c/util/crypto.h" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame^] | 12 | #include <ndn-cpp/security/security-exception.hpp> |
| 13 | #include <ndn-cpp/security/identity/memory-private-key-storage.hpp> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 14 | |
| 15 | using namespace std; |
| 16 | using namespace ndn::ptr_lib; |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
| 20 | MemoryPrivateKeyStorage::~MemoryPrivateKeyStorage() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | void MemoryPrivateKeyStorage::setKeyPairForKeyName |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 25 | (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer, |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | 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 | |
| 32 | void |
| 33 | MemoryPrivateKeyStorage::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 | |
| 40 | shared_ptr<PublicKey> |
| 41 | MemoryPrivateKeyStorage::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 | |
| 49 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 50 | 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] | 51 | { |
| 52 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 53 | return Blob(); |
| 54 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 55 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 56 | ndn_digestSha256(data, dataLength, digest); |
| 57 | // 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] | 58 | uint8_t signatureBits[1000]; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 59 | 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 68 | return Blob(signatureBits, (size_t)signatureBitsLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 72 | 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] | 73 | { |
| 74 | #if 1 |
| 75 | throw std::runtime_error("MemoryPrivateKeyStorage::decrypt not implemented"); |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 80 | 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] | 81 | { |
| 82 | #if 1 |
| 83 | throw std::runtime_error("MemoryPrivateKeyStorage::encrypt not implemented"); |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | MemoryPrivateKeyStorage::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 | |
| 95 | bool |
| 96 | MemoryPrivateKeyStorage::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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 107 | MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 108 | { |
| 109 | // Use a temporary pointer since d2i updates it. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 110 | const uint8_t *derPointer = keyDer; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 111 | privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength); |
| 112 | if (!privateKey_) |
| 113 | throw SecurityException("RsaPrivateKey constructor: Error decoding private key DER"); |
| 114 | } |
| 115 | |
| 116 | MemoryPrivateKeyStorage::RsaPrivateKey::~RsaPrivateKey() |
| 117 | { |
| 118 | if (privateKey_) |
| 119 | RSA_free(privateKey_); |
| 120 | } |
| 121 | |
| 122 | } |