| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
SecureDataManager.h |
| 29 |
|
| 30 |
The secure data manager main class. Every token instance has a secure data |
| 31 |
manager instance member that is used to decrypt and encrypt sensitive object |
| 32 |
attributes such as key material. The secure data manager maintains a key blob |
| 33 |
containing a 256-bit AES key that is used in this decryption and encryption |
| 34 |
process. The key blob itself is encrypted using a PBE derived key that is |
| 35 |
derived from the user PIN and a PBE key that is derived from the SO PIN. It |
| 36 |
is up to the token to enforce access control based on which user is logged |
| 37 |
in; authentication using the SO PIN is required to be able to change the |
| 38 |
user PIN. The master key that is used to decrypt/encrypt sensitive attributes |
| 39 |
is stored in memory under a mask that is changed every time the key is used. |
| 40 |
*****************************************************************************/ |
| 41 |
|
| 42 |
#ifndef _SOFTHSM_V2_SECUREDATAMANAGER_H |
| 43 |
#define _SOFTHSM_V2_SECUREDATAMANAGER_H |
| 44 |
|
| 45 |
#include "config.h" |
| 46 |
#include "ByteString.h" |
| 47 |
#include "log.h" |
| 48 |
#include "AESKey.h" |
| 49 |
#include "RNG.h" |
| 50 |
#include "SymmetricAlgorithm.h" |
| 51 |
#include "MutexFactory.h" |
| 52 |
|
| 53 |
class SecureDataManager |
| 54 |
{ |
| 55 |
public: |
| 56 |
// Constructors |
| 57 |
|
| 58 |
// Constructs a new SecureDataManager for a blank token; actual |
| 59 |
// initialisation is done by setting the SO PIN |
| 60 |
SecureDataManager(); |
| 61 |
|
| 62 |
// Constructs a SecureDataManager using the specified SO PIN and user PIN |
| 63 |
SecureDataManager(const ByteString& soPINBlob, const ByteString& userPINBlob); |
| 64 |
|
| 65 |
// Destructor |
| 66 |
virtual ~SecureDataManager(); |
| 67 |
|
| 68 |
// Set the SO PIN (requires either a blank SecureDataManager or the |
| 69 |
// SO to have logged in previously) |
| 70 |
bool setSOPIN(const ByteString& soPIN); |
| 71 |
|
| 72 |
// Set the user PIN (requires either the SO or the user to have logged |
| 73 |
// in previously) |
| 74 |
bool setUserPIN(const ByteString& userPIN); |
| 75 |
|
| 76 |
// Log in using the SO PIN |
| 77 |
bool loginSO(const ByteString& soPIN); |
| 78 |
bool isSOLoggedIn(); |
| 79 |
|
| 80 |
// Log in using the user PIN |
| 81 |
bool loginUser(const ByteString& userPIN); |
| 82 |
bool isUserLoggedIn(); |
| 83 |
|
| 84 |
// Re-authentication |
| 85 |
bool reAuthenticateSO(const ByteString& soPIN); |
| 86 |
bool reAuthenticateUser(const ByteString& userPIN); |
| 87 |
|
| 88 |
// Log out |
| 89 |
void logout(); |
| 90 |
|
| 91 |
// Decrypt the supplied data |
| 92 |
bool decrypt(const ByteString& encrypted, ByteString& plaintext); |
| 93 |
|
| 94 |
// Encrypt the supplied data |
| 95 |
bool encrypt(const ByteString& plaintext, ByteString& encrypted); |
| 96 |
|
| 97 |
// Returns the key blob for the SO PIN |
| 98 |
ByteString getSOPINBlob(); |
| 99 |
|
| 100 |
// Returns the key blob for the user PIN |
| 101 |
ByteString getUserPINBlob(); |
| 102 |
|
| 103 |
private: |
| 104 |
// Initialise the object |
| 105 |
void initObject(); |
| 106 |
|
| 107 |
// Generic login function |
| 108 |
bool login(const ByteString& passphrase, const ByteString& encryptedKey); |
| 109 |
|
| 110 |
// Generic re-authentication function |
| 111 |
bool reAuthenticate(const ByteString& passphrase, const ByteString& encryptedKey); |
| 112 |
|
| 113 |
// Generic function for creating an encrypted version of the key from the specified passphrase |
| 114 |
bool pbeEncryptKey(const ByteString& passphrase, ByteString& encryptedKey); |
| 115 |
|
| 116 |
// Unmask the key |
| 117 |
void unmask(ByteString& key); |
| 118 |
|
| 119 |
// Remask the key |
| 120 |
void remask(ByteString& key); |
| 121 |
|
| 122 |
// The user PIN encrypted key |
| 123 |
ByteString userEncryptedKey; |
| 124 |
|
| 125 |
// The SO PIN encrypted key |
| 126 |
ByteString soEncryptedKey; |
| 127 |
|
| 128 |
// Which users are logged in |
| 129 |
bool soLoggedIn; |
| 130 |
bool userLoggedIn; |
| 131 |
|
| 132 |
// The masked version of the actual key |
| 133 |
ByteString maskedKey; |
| 134 |
|
| 135 |
// The "magic" data used to detect if a PIN was likely to be correct |
| 136 |
ByteString magic; |
| 137 |
|
| 138 |
// The mask; this is not a stack member but a heap member. This |
| 139 |
// hopefully ensures that the mask ends up in a memory location |
| 140 |
// that is not logically linked to the masked key |
| 141 |
ByteString* mask; |
| 142 |
|
| 143 |
// Random number generator instance |
| 144 |
RNG* rng; |
| 145 |
|
| 146 |
// AES instance |
| 147 |
SymmetricAlgorithm* aes; |
| 148 |
|
| 149 |
// Mutex |
| 150 |
Mutex* dataMgrMutex; |
| 151 |
}; |
| 152 |
|
| 153 |
#endif // !_SOFTHSM_V2_SECUREDATAMANAGER_H |
| 154 |
|