blautenb 2003/10/19 04:00:28
Modified: c/src/tools/cipher MerlinFiveInteropResolver.cpp cipher.cpp
Log:
InputStream format output of decryption
Revision Changes Path
1.4 +134 -1
xml-security/c/src/tools/cipher/MerlinFiveInteropResolver.cpp
Index: MerlinFiveInteropResolver.cpp
===================================================================
RCS file:
/home/cvs/xml-security/c/src/tools/cipher/MerlinFiveInteropResolver.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MerlinFiveInteropResolver.cpp 13 Oct 2003 11:08:02 -0000 1.3
+++ MerlinFiveInteropResolver.cpp 19 Oct 2003 11:00:27 -0000 1.4
@@ -74,6 +74,7 @@
#include <xsec/framework/XSECDefs.hpp>
#include <xsec/enc/XSECKeyInfoResolver.hpp>
#include <xsec/dsig/DSIGKeyInfoName.hpp>
+#include <xsec/dsig/DSIGKeyInfoX509.hpp>
#include <xsec/utils/XSECDOMUtils.hpp>
#include <xsec/enc/OpenSSL/OpenSSLCryptoSymmetricKey.hpp>
#include <xsec/enc/WinCAPI/WinCAPICryptoSymmetricKey.hpp>
@@ -88,6 +89,13 @@
# error Require OpenSSL or Windows Crypto API for the Merlin Resolver
#endif
+#if defined (HAVE_OPENSSL)
+# include <openssl/x509.h>
+# include <openssl/pem.h>
+# include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
+# include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
+#endif
+
//
--------------------------------------------------------------------------------
// Strings and keys
//
--------------------------------------------------------------------------------
@@ -106,8 +114,24 @@
chNull
};
+static XMLCh s_jebName[] = {
+ chLatin_j,
+ chLatin_e,
+ chLatin_b,
+ chNull
+};
+
+static XMLCh s_jedName[] = {
+ chLatin_j,
+ chLatin_e,
+ chLatin_d,
+ chNull
+};
+
static char s_bobKey[] = "abcdefghijklmnopqrstuvwx";
static char s_jobKey[] = "abcdefghijklmnop";
+static char s_jebKey[] = "abcdefghijklmnopqrstuvwx";
+static char s_jedKey[] = "abcdefghijklmnopqrstuvwxyz012345";
//
--------------------------------------------------------------------------------
@@ -138,6 +162,20 @@
//
--------------------------------------------------------------------------------
// Utility functions
//
--------------------------------------------------------------------------------
+#if defined(_WIN32)
+
+void reverseSlash(safeBuffer &path) {
+
+ for (int i = 0; i < strlen(path.rawCharBuffer()); ++i) {
+
+ if (path[i] == '/')
+ path[i] = '\\';
+
+ }
+
+}
+
+#endif
XSECCryptoSymmetricKey *
MerlinFiveInteropResolver::makeSymmetricKey(XSECCryptoSymmetricKey::SymmetricKeyType
type) {
@@ -159,6 +197,35 @@
}
+BIO * createFileBIO(const XMLCh * baseURI, const char * name) {
+
+ // Open file URI relative to the encrypted file
+
+ BIO * bioFile;
+ if ((bioFile = BIO_new(BIO_s_file())) == NULL) {
+
+ return NULL;
+
+ }
+
+ safeBuffer fname;
+ fname.sbTranscodeIn(baseURI);
+ fname.sbStrcatIn("/");
+ fname.sbStrcatIn(name);
+
+#if defined(_WIN32)
+ reverseSlash(fname);
+#endif
+
+ if (BIO_read_filename(bioFile, fname.rawCharBuffer()) <= 0) {
+
+ return NULL;
+
+ }
+
+ return bioFile;
+}
+
//
--------------------------------------------------------------------------------
// Resolver
//
--------------------------------------------------------------------------------
@@ -201,9 +268,75 @@
}
return k;
}
+ if (strEquals(s_jebName, name)) {
+ XSECCryptoSymmetricKey * k =
+
XSECPlatformUtils::g_cryptoProvider->keySymmetric(XSECCryptoSymmetricKey::KEY_AES_ECB_192);
+ try {
+ k->setKey((unsigned char *) s_jebKey,
strlen(s_jebKey));
+ } catch(...) {
+ delete k;
+ throw;
+ }
+ return k;
+ }
+ if (strEquals(s_jedName, name)) {
+ XSECCryptoSymmetricKey * k =
+
XSECPlatformUtils::g_cryptoProvider->keySymmetric(XSECCryptoSymmetricKey::KEY_AES_ECB_256);
+ try {
+ k->setKey((unsigned char *) s_jedKey,
strlen(s_jedKey));
+ } catch(...) {
+ delete k;
+ throw;
+ }
+ return k;
+ }
}
+ else if (ki->getKeyInfoType() == DSIGKeyInfo::KEYINFO_X509) {
+
+ DSIGKeyInfoX509 * kix = dynamic_cast<DSIGKeyInfoX509 *>
(ki);
+
+ XSECCryptoX509 * XCX509 =
kix->getCertificateCryptoItem(0);
+
+ if (XCX509 != 0) {
+#if defined (HAVE_OPENSSL)
+
+
+ if
(strEquals(XCX509->getProviderName(),DSIGConstants::s_unicodeStrPROVOpenSSL)) {
+
+ OpenSSLCryptoX509 * OSSLX509 =
dynamic_cast<OpenSSLCryptoX509 *>(XCX509);
+ X509 * x509 =
OSSLX509->getOpenSSLX509();
+
+ // Check the serial number
+ BIGNUM * bnserial =
ASN1_INTEGER_to_BN(x509->cert_info->serialNumber, NULL);
+ char * xserial = BN_bn2dec(bnserial);
+ BN_free(bnserial);
+
+ BIO * rsaFile =
createFileBIO(mp_baseURI, "rsa.p8");
+ if (rsaFile == NULL)
+ return NULL;
+
+ PKCS8_PRIV_KEY_INFO * p8inf;
+ p8inf =
d2i_PKCS8_PRIV_KEY_INFO_bio(rsaFile, NULL);
+
+ EVP_PKEY * pk = EVP_PKCS82PKEY(p8inf);
+ OpenSSLCryptoKeyRSA * k = new
OpenSSLCryptoKeyRSA(pk);
+ return k;
+
//d2i_PKCS8PrivateKey_bio(rsaFile, NULL, NULL, NULL);
+
+/* if (strcmp(xserial, cserial) == 0) {
+
+ OPENSSL_free(xserial);
+ delete[] cserial;
+ return true;
+
+ }*/
+
+ }
+ }
+#endif
+ }
}
return NULL;
1.6 +70 -22 xml-security/c/src/tools/cipher/cipher.cpp
Index: cipher.cpp
===================================================================
RCS file: /home/cvs/xml-security/c/src/tools/cipher/cipher.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- cipher.cpp 15 Oct 2003 07:55:37 -0000 1.5
+++ cipher.cpp 19 Oct 2003 11:00:27 -0000 1.6
@@ -75,6 +75,7 @@
#include <xsec/framework/XSECException.hpp>
#include <xsec/enc/XSECCryptoException.hpp>
#include <xsec/enc/OpenSSL/OpenSSLCryptoSymmetricKey.hpp>
+#include <xsec/utils/XSECBinTXFMInputStream.hpp>
#include "MerlinFiveInteropResolver.hpp"
@@ -343,41 +344,88 @@
if (useInteropResolver == true) {
- MerlinFiveInteropResolver ires(NULL);
+ // Map out base path of the file
+ char path[_MAX_PATH];
+ char baseURI[(_MAX_PATH * 2) + 10];
+ getcwd(path, _MAX_PATH);
+
+ strcpy(baseURI, "file:///");
+
+ // Ugly and nasty but quick
+ if (filename[0] != '\\' && filename[0] != '/' &&
filename[1] != ':') {
+ strcat(baseURI, path);
+ strcat(baseURI, "/");
+ } else if (path[1] == ':') {
+ path[2] = '\0';
+ strcat(baseURI, path);
+ }
+
+ strcat(baseURI, filename);
+
+ // Find any ':' and "\" characters
+ int lastSlash = 0;
+ for (unsigned int i = 8; i < strlen(baseURI); ++i) {
+ if (baseURI[i] == '\\') {
+ lastSlash = i;
+ baseURI[i] = '/';
+ }
+ else if (baseURI[i] == '/')
+ lastSlash = i;
+ }
+
+ // The last "\\" must prefix the filename
+ baseURI[lastSlash + 1] = '\0';
+
+ XMLUri uri(MAKE_UNICODE_STRING(baseURI));
+
+ MerlinFiveInteropResolver ires(&(uri.getUriText()[8]));
cipher->setKeyInfoResolver(&ires);
}
+ if (doDecryptElement) {
+ cipher->decryptElement(static_cast<DOMElement *>(n));
- cipher->decryptElement(static_cast<DOMElement *>(n));
+ // Output the result
- // Output the result
+ XMLCh core[] = {
+ XERCES_CPP_NAMESPACE :: chLatin_C,
+ XERCES_CPP_NAMESPACE :: chLatin_o,
+ XERCES_CPP_NAMESPACE :: chLatin_r,
+ XERCES_CPP_NAMESPACE :: chLatin_e,
+ XERCES_CPP_NAMESPACE :: chNull
+ };
- XMLCh core[] = {
- XERCES_CPP_NAMESPACE :: chLatin_C,
- XERCES_CPP_NAMESPACE :: chLatin_o,
- XERCES_CPP_NAMESPACE :: chLatin_r,
- XERCES_CPP_NAMESPACE :: chLatin_e,
- XERCES_CPP_NAMESPACE :: chNull
- };
+ DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(core);
+ DOMWriter *theSerializer =
((DOMImplementationLS*)impl)->createDOMWriter();
- DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(core);
- DOMWriter *theSerializer =
((DOMImplementationLS*)impl)->createDOMWriter();
+
theSerializer->setEncoding(MAKE_UNICODE_STRING("UTF-8"));
+ if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false))
+
theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false);
- theSerializer->setEncoding(MAKE_UNICODE_STRING("UTF-8"));
- if
(theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false))
-
theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false);
+ XMLFormatTarget *formatTarget = new
StdOutFormatTarget();
- XMLFormatTarget *formatTarget = new StdOutFormatTarget();
+ theSerializer->writeNode(formatTarget, *doc);
+
+ cout << endl;
- theSerializer->writeNode(formatTarget, *doc);
-
- cout << endl;
+ delete theSerializer;
+ delete formatTarget;
- delete theSerializer;
- delete formatTarget;
+ }
+ else {
+ XSECBinTXFMInputStream * bis =
cipher->decryptToBinInputStream(static_cast<DOMElement *>(n));
+ XMLByte buf[1024];
+ unsigned int read = bis->readBytes(buf, 1023);
+ while (read > 0) {
+ buf[read] = '\0';
+ cout << buf;
+ read = bis->readBytes(buf, 1023);
+ }
+ delete bis;
+ }
}
catch (XSECException &e) {