| 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 |
SecureDataMgrTests.cpp |
| 29 |
|
| 30 |
Contains test cases to test the secure data manager |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <cppunit/extensions/HelperMacros.h> |
| 36 |
#include "SecureDataMgrTests.h" |
| 37 |
#include "SecureDataManager.h" |
| 38 |
#include "CryptoFactory.h" |
| 39 |
|
| 40 |
CPPUNIT_TEST_SUITE_REGISTRATION(SecureDataMgrTests); |
| 41 |
|
| 42 |
void SecureDataMgrTests::setUp() |
| 43 |
{ |
| 44 |
CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL); |
| 45 |
} |
| 46 |
|
| 47 |
void SecureDataMgrTests::tearDown() |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
void SecureDataMgrTests::testSecureDataManager() |
| 52 |
{ |
| 53 |
ByteString soPIN = "3132333435363738"; // "12345678" |
| 54 |
ByteString userPIN = "4041424344454647"; // "ABCDEFGH" |
| 55 |
ByteString newSOPIN = "3837363534333231"; // "87654321" |
| 56 |
ByteString newUserPIN = "4746454443424140"; // "HGFEDCBA" |
| 57 |
|
| 58 |
// Instantiate a blank secure data manager |
| 59 |
SecureDataManager s1; |
| 60 |
ByteString plaintext = "010203040506070809"; |
| 61 |
ByteString emptyPlaintext = ""; |
| 62 |
ByteString encrypted; |
| 63 |
|
| 64 |
// Verify that no function other than setting the SO PIN works |
| 65 |
CPPUNIT_ASSERT(!s1.setUserPIN(userPIN)); |
| 66 |
CPPUNIT_ASSERT(!s1.loginSO(soPIN)); |
| 67 |
CPPUNIT_ASSERT(!s1.loginUser(userPIN)); |
| 68 |
CPPUNIT_ASSERT(!s1.reAuthenticateSO(soPIN)); |
| 69 |
CPPUNIT_ASSERT(!s1.reAuthenticateUser(userPIN)); |
| 70 |
CPPUNIT_ASSERT(!s1.encrypt(plaintext, encrypted)); |
| 71 |
CPPUNIT_ASSERT(!s1.decrypt(encrypted, plaintext)); |
| 72 |
CPPUNIT_ASSERT(s1.getSOPINBlob().size() == 0); |
| 73 |
CPPUNIT_ASSERT(s1.getUserPINBlob().size() == 0); |
| 74 |
|
| 75 |
// Now set the SO PIN |
| 76 |
CPPUNIT_ASSERT(s1.setSOPIN(soPIN)); |
| 77 |
|
| 78 |
// Check that it is still not possible to set the user PIN |
| 79 |
CPPUNIT_ASSERT(!s1.setUserPIN(userPIN)); |
| 80 |
|
| 81 |
// Check that it is possible to log in with the SO PIN |
| 82 |
CPPUNIT_ASSERT(s1.loginSO(soPIN)); |
| 83 |
|
| 84 |
// Check that it is now possible to also set the user PIN |
| 85 |
CPPUNIT_ASSERT(s1.setUserPIN(userPIN)); |
| 86 |
|
| 87 |
// Check that is is now also possible to log in with the user PIN |
| 88 |
CPPUNIT_ASSERT(s1.loginUser(userPIN)); |
| 89 |
|
| 90 |
// Check that it is possible to encrypt and decrypt some data |
| 91 |
ByteString decrypted; |
| 92 |
|
| 93 |
CPPUNIT_ASSERT(s1.encrypt(plaintext, encrypted)); |
| 94 |
CPPUNIT_ASSERT(encrypted != plaintext); |
| 95 |
|
| 96 |
CPPUNIT_ASSERT(s1.decrypt(encrypted, decrypted)); |
| 97 |
CPPUNIT_ASSERT(decrypted == plaintext); |
| 98 |
|
| 99 |
// Log out |
| 100 |
s1.logout(); |
| 101 |
|
| 102 |
// Check that it is no longer possible to set the SO PIN |
| 103 |
CPPUNIT_ASSERT(!s1.setSOPIN(soPIN)); |
| 104 |
|
| 105 |
// Check that it is no longer possible to set the user PIN |
| 106 |
CPPUNIT_ASSERT(!s1.setUserPIN(userPIN)); |
| 107 |
|
| 108 |
// Check that encrypting/decrypting no longer works |
| 109 |
CPPUNIT_ASSERT(!s1.encrypt(plaintext, encrypted)); |
| 110 |
CPPUNIT_ASSERT(!s1.decrypt(encrypted, plaintext)); |
| 111 |
|
| 112 |
// Export the key blobs |
| 113 |
ByteString soPINBlob = s1.getSOPINBlob(); |
| 114 |
ByteString userPINBlob = s1.getUserPINBlob(); |
| 115 |
|
| 116 |
// Create a new instance with the exported key blobs |
| 117 |
SecureDataManager s2(soPINBlob, userPINBlob); |
| 118 |
|
| 119 |
// Check that the key blobs match |
| 120 |
CPPUNIT_ASSERT(s1.getSOPINBlob() == s2.getSOPINBlob()); |
| 121 |
CPPUNIT_ASSERT(s1.getUserPINBlob() == s2.getUserPINBlob()); |
| 122 |
|
| 123 |
// Check that it is not possible to set the SO PIN |
| 124 |
CPPUNIT_ASSERT(!s2.setSOPIN(soPIN)); |
| 125 |
|
| 126 |
// Check that it is possible to log in with the SO PIN |
| 127 |
CPPUNIT_ASSERT(s2.loginSO(soPIN)); |
| 128 |
|
| 129 |
// Check that is is now also possible to log in with the user PIN |
| 130 |
CPPUNIT_ASSERT(s2.loginUser(userPIN)); |
| 131 |
|
| 132 |
// Check that encrypting the data results in different ciphertext because of the random IV |
| 133 |
ByteString encrypted2; |
| 134 |
|
| 135 |
CPPUNIT_ASSERT(s2.encrypt(plaintext, encrypted2)); |
| 136 |
CPPUNIT_ASSERT(encrypted != encrypted2); |
| 137 |
|
| 138 |
// Check that decrypting earlier data can be done with the recreated key |
| 139 |
CPPUNIT_ASSERT(s2.decrypt(encrypted, decrypted)); |
| 140 |
CPPUNIT_ASSERT(decrypted == plaintext); |
| 141 |
|
| 142 |
// Log in with the SO PIN |
| 143 |
CPPUNIT_ASSERT(s2.loginSO(soPIN)); |
| 144 |
|
| 145 |
// Check that the SO PIN can be changed |
| 146 |
CPPUNIT_ASSERT(s2.setSOPIN(newSOPIN)); |
| 147 |
|
| 148 |
// Check that it is no longer possible to log in with the old SO PIN |
| 149 |
CPPUNIT_ASSERT(!s2.loginSO(soPIN)); |
| 150 |
|
| 151 |
// Check that encrypting/decrypting no longer works |
| 152 |
CPPUNIT_ASSERT(!s2.encrypt(plaintext, encrypted)); |
| 153 |
CPPUNIT_ASSERT(!s2.decrypt(encrypted, plaintext)); |
| 154 |
|
| 155 |
// Check that the key blobs differ |
| 156 |
CPPUNIT_ASSERT(s1.getSOPINBlob() != s2.getSOPINBlob()); |
| 157 |
|
| 158 |
// Check that it is possible to log in with the new SO PIN |
| 159 |
CPPUNIT_ASSERT(s2.loginSO(newSOPIN)); |
| 160 |
|
| 161 |
// Log in with the user PIN |
| 162 |
CPPUNIT_ASSERT(s2.loginUser(userPIN)); |
| 163 |
|
| 164 |
// Check that it is possible to change the user PIN |
| 165 |
CPPUNIT_ASSERT(s2.setUserPIN(newUserPIN)); |
| 166 |
|
| 167 |
// Check that it is no longer possible to log in with the old user PIN |
| 168 |
CPPUNIT_ASSERT(!s2.loginUser(userPIN)); |
| 169 |
|
| 170 |
// Check that encrypting/decrypting no longer works |
| 171 |
CPPUNIT_ASSERT(!s2.encrypt(plaintext, encrypted)); |
| 172 |
CPPUNIT_ASSERT(!s2.decrypt(encrypted, plaintext)); |
| 173 |
|
| 174 |
// Check that it is possible to log in with the new user PIN |
| 175 |
CPPUNIT_ASSERT(s2.loginUser(newUserPIN)); |
| 176 |
|
| 177 |
// Check that encrypting the data results in the different ciphertext because of the random IV |
| 178 |
CPPUNIT_ASSERT(s2.encrypt(plaintext, encrypted2)); |
| 179 |
CPPUNIT_ASSERT(encrypted != encrypted2); |
| 180 |
|
| 181 |
// Check that decrypting earlier data can be done with the recreated key |
| 182 |
CPPUNIT_ASSERT(s2.decrypt(encrypted, decrypted)); |
| 183 |
CPPUNIT_ASSERT(decrypted == plaintext); |
| 184 |
|
| 185 |
// Check that empty plaintext can be handled |
| 186 |
CPPUNIT_ASSERT(s2.encrypt(emptyPlaintext, encrypted)); |
| 187 |
CPPUNIT_ASSERT(s2.decrypt(encrypted, decrypted)); |
| 188 |
CPPUNIT_ASSERT(decrypted == emptyPlaintext); |
| 189 |
|
| 190 |
// Check that is is possible to log in with the SO PIN and re-authenticate |
| 191 |
CPPUNIT_ASSERT(s1.loginSO(soPIN)); |
| 192 |
CPPUNIT_ASSERT(!s1.reAuthenticateSO(userPIN)); |
| 193 |
CPPUNIT_ASSERT(s1.reAuthenticateSO(soPIN)); |
| 194 |
|
| 195 |
// Check that is is possible to log in with the user PIN and re-authenticate |
| 196 |
CPPUNIT_ASSERT(s1.loginUser(userPIN)); |
| 197 |
CPPUNIT_ASSERT(!s1.reAuthenticateUser(soPIN)); |
| 198 |
CPPUNIT_ASSERT(s1.reAuthenticateUser(userPIN)); |
| 199 |
|
| 200 |
// Check that it is possible to encrypt and decrypt some data |
| 201 |
CPPUNIT_ASSERT(s1.encrypt(plaintext, encrypted)); |
| 202 |
CPPUNIT_ASSERT(encrypted != plaintext); |
| 203 |
|
| 204 |
CPPUNIT_ASSERT(s1.decrypt(encrypted, decrypted)); |
| 205 |
CPPUNIT_ASSERT(decrypted == plaintext); |
| 206 |
} |
| 207 |
|