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; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | MemoryPrivateKeyStorage::~MemoryPrivateKeyStorage() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void MemoryPrivateKeyStorage::setKeyPairForKeyName |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 24 | (const Name& keyName, uint8_t *publicKeyDer, size_t publicKeyDerLength, uint8_t *privateKeyDer, |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 25 | size_t privateKeyDerLength) |
| 26 | { |
| 27 | publicKeyStore_[keyName.toUri()] = PublicKey::fromDer(Blob(publicKeyDer, publicKeyDerLength)); |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame^] | 28 | privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void |
| 32 | MemoryPrivateKeyStorage::generateKeyPair(const Name& keyName, KeyType keyType, int keySize) |
| 33 | { |
| 34 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 35 | throw runtime_error("MemoryPrivateKeyStorage::generateKeyPair not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 36 | #endif |
| 37 | } |
| 38 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame^] | 39 | ptr_lib::shared_ptr<PublicKey> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 40 | MemoryPrivateKeyStorage::getPublicKey(const Name& keyName) |
| 41 | { |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame^] | 42 | map<string, ptr_lib::shared_ptr<PublicKey> >::iterator publicKey = publicKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 43 | if (publicKey == publicKeyStore_.end()) |
| 44 | throw SecurityException(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri()); |
| 45 | return publicKey->second; |
| 46 | } |
| 47 | |
| 48 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 49 | 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] | 50 | { |
| 51 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
| 52 | return Blob(); |
| 53 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 54 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 55 | ndn_digestSha256(data, dataLength, digest); |
| 56 | // 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] | 57 | uint8_t signatureBits[1000]; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 58 | unsigned int signatureBitsLength; |
| 59 | |
| 60 | // Find the private key and sign. |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame^] | 61 | map<string, ptr_lib::shared_ptr<RsaPrivateKey> >::iterator privateKey = privateKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 62 | 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 Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 67 | return Blob(signatureBits, (size_t)signatureBitsLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 74 | throw runtime_error("MemoryPrivateKeyStorage::decrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
| 78 | Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 82 | throw runtime_error("MemoryPrivateKeyStorage::encrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 83 | #endif |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | MemoryPrivateKeyStorage::generateKey(const Name& keyName, KeyType keyType, int keySize) |
| 88 | { |
| 89 | #if 1 |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 90 | throw runtime_error("MemoryPrivateKeyStorage::generateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 91 | #endif |
| 92 | } |
| 93 | |
| 94 | bool |
| 95 | MemoryPrivateKeyStorage::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 Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 106 | MemoryPrivateKeyStorage::RsaPrivateKey::RsaPrivateKey(uint8_t *keyDer, size_t keyDerLength) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 107 | { |
| 108 | // Use a temporary pointer since d2i updates it. |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 109 | const uint8_t *derPointer = keyDer; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 110 | privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength); |
| 111 | if (!privateKey_) |
| 112 | throw SecurityException("RsaPrivateKey constructor: Error decoding private key DER"); |
| 113 | } |
| 114 | |
| 115 | MemoryPrivateKeyStorage::RsaPrivateKey::~RsaPrivateKey() |
| 116 | { |
| 117 | if (privateKey_) |
| 118 | RSA_free(privateKey_); |
| 119 | } |
| 120 | |
| 121 | } |