1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmCryptoFactory.h"
#include <cstdio>
#include <cstring>
#include <memory>
#include <vector>
#include "gdcmFilename.h"
#include "gdcmTesting.h"
static bool LoadFile(const char * filename, char* & buffer, size_t & bufLen)
{
FILE * f = fopen(filename, "rb");
if (f == nullptr)
{
//gdcmErrorMacro("Couldn't open the file: " << filename);
return false;
}
fseek(f, 0L, SEEK_END);
long sz = ftell(f);
rewind(f);
buffer = new char[sz];
bufLen = sz;
while (sz)
sz -= (long)fread(buffer + bufLen - sz, sizeof(char), sz, f);
return true;
}
static const gdcm::CryptographicMessageSyntax::CipherTypes ciphers[] = {
gdcm::CryptographicMessageSyntax::AES128_CIPHER,
gdcm::CryptographicMessageSyntax::AES192_CIPHER,
gdcm::CryptographicMessageSyntax::AES256_CIPHER,
gdcm::CryptographicMessageSyntax::DES3_CIPHER
};
static std::pair<gdcm::CryptographicMessageSyntax::CipherTypes, std::string> cip2str_data[] = {
std::make_pair(gdcm::CryptographicMessageSyntax::AES128_CIPHER, "AES128"),
std::make_pair(gdcm::CryptographicMessageSyntax::AES192_CIPHER, "AES192"),
std::make_pair(gdcm::CryptographicMessageSyntax::AES256_CIPHER, "AES256"),
std::make_pair(gdcm::CryptographicMessageSyntax::DES3_CIPHER, "3DES")
};
static std::map<gdcm::CryptographicMessageSyntax::CipherTypes, std::string> cip2str(cip2str_data,
cip2str_data + sizeof cip2str_data / sizeof cip2str_data[0]);
const char * const tstr = "12345";
const size_t tstr_l = strlen(tstr);
#define BUFSZ 5000
bool TestCMSProvider(gdcm::CryptographicMessageSyntax& cms, const char * provName)
{
const std::string certpath = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/certificate.pem" );
const std::string keypath = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/privatekey.pem" );
const std::string encrypted_vector = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/encrypted_text" );
bool ret = true;
for (unsigned int i = 0; i < 4; i++)
{
char encout[BUFSZ] = {0}, decout[BUFSZ] = {0};
size_t encoutlen = BUFSZ, decoutlen = BUFSZ;
cms.SetCipherType(ciphers[i]);
bool encryptSuccess = cms.Encrypt(encout, encoutlen, tstr, tstr_l);
if (!encryptSuccess)
{
std::cerr << provName << " using " << cip2str[ciphers[i]] << ": encryption failed" << std::endl;
ret = false;
continue;
}
bool decryptSuccess = cms.Decrypt(decout, decoutlen, encout, encoutlen);
if (!decryptSuccess)
{
std::cerr << provName << " using " << cip2str[ciphers[i]] << ": decryption failed" << std::endl;
ret = false;
continue;
}
if (decoutlen != tstr_l)
{
std::cerr << provName << " using " << cip2str[ciphers[i]] << ": decryted length different from original (" << decoutlen << " != " << tstr_l << ")" << std::endl;
ret = false;
continue;
}
if (memcmp(tstr, decout, tstr_l) != 0)
{
std::cerr << provName << " using " << cip2str[ciphers[i]] << ": decryted data different from original" << std::endl;
ret = false;
continue;
}
}
return ret;
}
bool TestCMSVector(gdcm::CryptographicMessageSyntax& cms, const char * provName)
{
char decout[BUFSZ] = {0};
size_t decoutlen = BUFSZ;
std::string encrypted_filename = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/encrypted_text" );
const char * tv_plaintext = "1234567890abcdefghijklmnopqrstuvwxyz";
size_t tv_plaintext_len = strlen(tv_plaintext);
char * test_vector;
// FIXME : should I delete test_vector ?
size_t tvlen;
if (!LoadFile(encrypted_filename.c_str(), test_vector, tvlen))
{
std::cerr << "Couldn't load encrypted file: " << encrypted_filename << std::endl;
return false;
}
bool decryptSuccess = cms.Decrypt(decout, decoutlen, test_vector, tvlen);
if (!decryptSuccess)
{
std::cerr << provName << " test vector decryption failed" << std::endl;
return false;
}
if (decoutlen != tv_plaintext_len)
{
std::cerr << provName << " test vector decryted length different from original (" << decoutlen << " != " << tstr_l << ")" << std::endl;
return false;
}
if (memcmp(tv_plaintext, decout, tv_plaintext_len) != 0)
{
std::cerr << provName << " test vector decryted data different from original" << std::endl;
return false;
}
return true;
}
bool TestCMSCompatibility(gdcm::CryptographicMessageSyntax& cms1, const char * provName1, gdcm::CryptographicMessageSyntax& cms2, const char * provName2)
{
const std::string encrypted_vector = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/encrypted_text" );
bool ret = true;
for (int i = 0; i < 4; i++)
{
char encout[BUFSZ] = {0}, decout[BUFSZ] = {0};
size_t encoutlen = BUFSZ, decoutlen = BUFSZ;
cms1.SetCipherType(ciphers[i]);
cms2.SetCipherType(ciphers[i]);
bool encryptSuccess = cms1.Encrypt(encout, encoutlen, tstr, tstr_l);
if (!encryptSuccess)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": encryption failed" << std::endl;
ret = false;
break;
}
bool decryptSuccess = cms2.Decrypt(decout, decoutlen, encout, encoutlen);
if (!decryptSuccess)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": decryption failed" << std::endl;
ret = false;
break;
}
if (decoutlen != tstr_l)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": decryted length different from original (" << decoutlen << " != " << tstr_l << ")" << std::endl;
ret = false;
break;
}
if (memcmp(tstr, decout, tstr_l) != 0)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": decryted data different from original" << std::endl;
ret = false;
break;
}
}
/*
char encout[BUFSZ] = {0}, decout[BUFSZ] = {0};
size_t encoutlen = BUFSZ, decoutlen = BUFSZ;
for (int i = 0; i < 4; i++)
{
bool ret = true;
char encout[BUFSZ] = {0}, decout[BUFSZ] = {0};
size_t encoutlen = BUFSZ, decoutlen = BUFSZ;
cms1.SetCipherType(ciphers[i]);
cms2.SetCipherType(ciphers[i]);
//cms2.Encrypt(encout, encoutlen, tstr, tstr_l);
//cms1.Decrypt(decout, decoutlen, encout, encoutlen);
//assert(decoutlen == tstr_l);
//assert(memcmp(tstr, decout, tstr_l) == 0);
bool encryptSuccess = cms1.Encrypt(encout, encoutlen, tstr, tstr_l);
if (!encryptSuccess)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": encryption failed" << std::endl;
ret = false;
break;
}
bool decryptSuccess = cms2.Decrypt(decout, decoutlen, encout, encoutlen);
if (!decryptSuccess)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": decryption failed" << std::endl;
ret = false;
break;
}
if (decoutlen != tstr_l)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": decryted length different from original (" << decoutlen << " != " << tstr_l << ")" << std::endl;
ret = false;
break;
}
if (memcmp(tstr, decout, tstr_l) != 0)
{
std::cerr << provName1 << " & " << provName2 << " using " << cip2str[ciphers[i]] << ": decryted data different from original" << std::endl;
ret = false;
break;
}
}*/
return ret;
}
int TestCryptographicMessageSyntax(int, char *[])
{
std::string certpath = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/certificate.pem" );
std::string keypath = gdcm::Filename::Join(gdcm::Testing::GetSourceDirectory(), "/Testing/Source/Data/privatekey.pem" );
bool bret = true;
//typedef std::tuple<std::string, gdcm::CryptographicMessageSyntax&> StringCMSPairType;
std::vector<gdcm::CryptographicMessageSyntax*> availableCMS;
std::vector<std::string> availableCMSName;
#ifdef GDCM_USE_SYSTEM_OPENSSL
gdcm::CryptoFactory* osslp7 = gdcm::CryptoFactory::GetFactoryInstance(gdcm::CryptoFactory::OPENSSLP7);
std::unique_ptr<gdcm::CryptographicMessageSyntax> ocmsp7(osslp7->CreateCMSProvider());
ocmsp7->ParseKeyFile(keypath.c_str());
ocmsp7->ParseCertificateFile(certpath.c_str());
bret = TestCMSProvider(*ocmsp7, "OpenSSL PKCS7") && bret;
bret = TestCMSVector(*ocmsp7, "OpenSSL PKCS7") && bret;
availableCMS.push_back(ocmsp7.get());
availableCMSName.push_back("OpenSSL PKCS7");
#endif
#ifdef GDCM_USE_SYSTEM_OPENSSL
#ifdef GDCM_HAVE_CMS_RECIPIENT_PASSWORD
gdcm::CryptoFactory* ossl = gdcm::CryptoFactory::GetFactoryInstance(gdcm::CryptoFactory::OPENSSL);
std::unique_ptr<gdcm::CryptographicMessageSyntax> ocms(ossl->CreateCMSProvider());
ocms->ParseKeyFile(keypath.c_str());
ocms->ParseCertificateFile(certpath.c_str());
bret = TestCMSProvider(*ocms, "OpenSSL CMS") && bret;
bret = TestCMSVector(*ocms, "OpenSSL CMS") && bret;
availableCMS.push_back(ocms.get());
availableCMSName.push_back("OpenSSL CMS");
#endif
#endif
#ifdef WIN32
gdcm::CryptoFactory* capi = gdcm::CryptoFactory::GetFactoryInstance(gdcm::CryptoFactory::CAPI);
std::unique_ptr<gdcm::CryptographicMessageSyntax> ccms(capi->CreateCMSProvider());
ccms->ParseCertificateFile(certpath.c_str());
ccms->ParseKeyFile(keypath.c_str());
bret = TestCMSProvider(*ccms, "CAPI") && bret;
bret = TestCMSVector(*ccms, "CAPI") && bret;
availableCMS.push_back(ccms.get());
availableCMSName.push_back("CAPI");
#endif
for (size_t i = 0; i < availableCMS.size(); ++i)
for (size_t j = i+1; j < availableCMS.size(); ++j)
bret = TestCMSCompatibility(*availableCMS[i], availableCMSName[i].c_str(), *availableCMS[j], availableCMSName[j].c_str()) && bret;
return (bret ? 0 : 1);
}
int TestPasswordBasedEncryption(int, char *[])
{
const char *directory = gdcm::Testing::GetDataRoot();
std::string encrypted_dicomdir =
gdcm::Filename::Join(directory, "/securedicomfileset/DICOMDIR" );
std::string encrypted_image =
gdcm::Filename::Join(directory, "/securedicomfileset/IMAGES/IMAGE1" );
#ifdef GDCM_USE_SYSTEM_OPENSSL
gdcm::CryptoFactory* ossl = gdcm::CryptoFactory::GetFactoryInstance(gdcm::CryptoFactory::OPENSSL);
std::unique_ptr<gdcm::CryptographicMessageSyntax> ocms(ossl->CreateCMSProvider());
ocms->SetPassword("password", strlen("password"));
if (!TestCMSProvider(*ocms, "OpenSSL"))
return 1;
char decout[BUFSZ] = {0};
size_t decoutlen = BUFSZ;
char * ddir = new char[5000];
size_t ddirlen = 5000;
LoadFile(encrypted_dicomdir.c_str(), ddir, ddirlen);
bool decryptSuccess = ocms->Decrypt(decout, decoutlen, ddir, ddirlen);
if (!decryptSuccess)
{
std::cerr << "OpenSSL sample DICOMDIR decryption failed" << std::endl;
return 1;
}
if (decoutlen == 0)
{
std::cerr << "OpenSSL sample DICOMDIR decrypted length == 0" << std::endl;
return 1;
}
#endif
return 0;
}
|