You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by bl...@apache.org on 2003/10/25 12:29:15 UTC

cvs commit: xml-security/c/src/xenc/impl XENCCipherImpl.cpp XENCCipherImpl.hpp

blautenb    2003/10/25 03:29:15

  Modified:    c/src/xenc XENCCipher.hpp
               c/src/xenc/impl XENCCipherImpl.cpp XENCCipherImpl.hpp
  Log:
  Added ability to get raw decrypted data via BinInputStreams
  
  Revision  Changes    Path
  1.9       +48 -1     xml-security/c/src/xenc/XENCCipher.hpp
  
  Index: XENCCipher.hpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/xenc/XENCCipher.hpp,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- XENCCipher.hpp	19 Oct 2003 10:58:59 -0000	1.8
  +++ XENCCipher.hpp	25 Oct 2003 10:29:15 -0000	1.9
  @@ -79,12 +79,14 @@
   
   XSEC_DECLARE_XERCES_CLASS(DOMElement);
   XSEC_DECLARE_XERCES_CLASS(DOMDocument);
  +XSEC_DECLARE_XERCES_CLASS(BinInputStream);
   
   class XSECCryptoKey;
   class XENCEncryptedData;
   class XENCEncryptedKey;
   class XSECKeyInfoResolver;
   class XSECBinTXFMInputStream;
  +class TXFMChain;
   
   /**
    * @defgroup xenc XML Encryption Implementation
  @@ -254,6 +256,51 @@
   	virtual XENCEncryptedKey * encryptKey(
   		const unsigned char * keyBuffer,
   		unsigned int keyLen,
  +		encryptionMethod em,
  +		const XMLCh * algorithmURI = NULL
  +	) = 0;
  +
  +	/**
  +	 * \brief Encrypt an input stream to a CipherValue
  +	 *
  +	 * Encrypts the data passed in via a Xerces BinInputStream and places it
  +	 * directly into a new EncryptedData element that contains a CipherValue
  +	 *
  +	 * @param plainText The InputStream to read the plain text from
  +	 * @param em The encryptionMethod to use for this encryption.  Use
  +	 * ENCRYPT_NONE if a user defined type is required.
  +	 * @param algorithmURI if ENCRYPT_NONE is used for em, this will be used
  +	 * as the algorithm URI
  +	 *
  +	 * @returns the EncryptedData element containing the CipherValue of the data
  +	 */
  +
  +	virtual XENCEncryptedData * encryptBinInputStream(
  +		XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream * plainText,
  +		encryptionMethod em,
  +		const XMLCh * algorithmURI = NULL
  +	) = 0;
  +
  +	/**
  +	 * \brief Encrypt a TXFMChain to a CipherValue
  +	 *
  +	 * Encrypts the data passed in via a TXFMChain and places it
  +	 * directly into a new EncryptedData element that contains a CipherValue.
  +	 *
  +	 * @note This is not really intended for client apps, but is used internally
  +	 * and is provided for flexibility.  The "formal" method is encryptBinInputStream
  +	 *
  +	 * @param plainText The TXFMChain to read the plain text from
  +	 * @param em The encryptionMethod to use for this encryption.  Use
  +	 * ENCRYPT_NONE if a user defined type is required.
  +	 * @param algorithmURI if ENCRYPT_NONE is used for em, this will be used
  +	 * as the algorithm URI
  +	 *
  +	 * @returns the EncryptedData element containing the CipherValue of the data
  +	 */
  +
  +	virtual XENCEncryptedData * encryptTXFMChain(
  +		TXFMChain * plainText,
   		encryptionMethod em,
   		const XMLCh * algorithmURI = NULL
   	) = 0;
  
  
  
  1.10      +112 -71   xml-security/c/src/xenc/impl/XENCCipherImpl.cpp
  
  Index: XENCCipherImpl.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/xenc/impl/XENCCipherImpl.cpp,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- XENCCipherImpl.cpp	19 Oct 2003 10:58:59 -0000	1.9
  +++ XENCCipherImpl.cpp	25 Oct 2003 10:29:15 -0000	1.10
  @@ -75,6 +75,7 @@
   #include <xsec/transformers/TXFMBase.hpp>
   #include <xsec/transformers/TXFMC14n.hpp>
   #include <xsec/transformers/TXFMSB.hpp>
  +#include <xsec/transformers/TXFMURL.hpp>
   #include <xsec/transformers/TXFMDocObject.hpp>
   #include <xsec/utils/XSECDOMUtils.hpp>
   #include <xsec/framework/XSECEnv.hpp>
  @@ -767,6 +768,114 @@
   
   }
   
  +// --------------------------------------------------------------------------------
  +//			Encrypt a BinInputStream
  +// --------------------------------------------------------------------------------
  +
  +XENCEncryptedData * XENCCipherImpl::encryptBinInputStream(
  +	XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream * plainText,
  +	encryptionMethod em,
  +	const XMLCh * algorithmURI) {
  +
  +	TXFMURL * uri;
  +	XSECnew(uri, TXFMURL(mp_doc, NULL));
  +
  +	uri->setInput(plainText);
  +	TXFMChain c(uri);
  +
  +	return encryptTXFMChain(&c, em, algorithmURI);
  +
  +}
  +
  +// --------------------------------------------------------------------------------
  +//			Encrypt a TXFMChain
  +// --------------------------------------------------------------------------------
  +
  +XENCEncryptedData * XENCCipherImpl::encryptTXFMChain(
  +	TXFMChain * plainText,
  +	encryptionMethod em,
  +	const XMLCh * algorithmURI) {
  +
  +	// Make sure we have a key before we do anything too drastic
  +	if (mp_key == NULL) {
  +		throw XSECException(XSECException::CipherError, 
  +			"XENCCipherImpl::encryptElement - No key set");
  +	}
  +
  +	// Map the encryption method to a URI
  +	safeBuffer algorithmSB;
  +	const XMLCh * algorithm;
  +
  +	if (em == ENCRYPT_NONE) {
  +		algorithm = algorithmURI;
  +	}
  +	else {
  +		if (encryptionMethod2URI(algorithmSB, em) != true) {
  +			throw XSECException(XSECException::CipherError, 
  +				"XENCCipherImpl::encryptElement - Unknown encryption method");
  +		}
  +		algorithm = algorithmSB.sbStrToXMLCh();
  +	}
  +
  +	// Create the element with a dummy encrypted value
  +
  +	if (mp_encryptedData != NULL) {
  +		delete mp_encryptedData;
  +		mp_encryptedData = NULL;
  +	}
  +	
  +	XSECnew(mp_encryptedData, XENCEncryptedDataImpl(mp_env));
  +	mp_encryptedData->createBlankEncryptedData(
  +		XENCCipherData::VALUE_TYPE, 
  +		algorithm,
  +		s_noData);
  +
  +	// Perform the encryption
  +	XSECAlgorithmHandler *handler;
  +
  +	if (algorithm != NULL) {
  +		
  +		handler = 
  +			XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(algorithm);
  +	
  +	}
  +
  +	else {
  +
  +		handler =
  +			XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(
  +				XSECAlgorithmMapper::s_defaultEncryptionMapping);
  +
  +	}
  +
  +	safeBuffer sb;
  +
  +	if (handler != NULL) {
  +
  +		handler->encryptToSafeBuffer(plainText, 
  +			mp_encryptedData->getEncryptionMethod(), 
  +			mp_key,
  +			mp_env->getParentDocument(),
  +			sb);
  +	}
  +	else {
  +
  +		// Very strange if we get here - any problems should throw an
  +		// exception in the AlgorithmMapper.
  +
  +		throw XSECException(XSECException::CipherError, 
  +			"XENCCipherImpl::encryptTXFMChain - Error retrieving a handler for algorithm");
  +
  +	}
  +
  +	// Set the value
  +	XENCCipherValue * val = mp_encryptedData->getCipherData()->getCipherValue();
  +
  +	val->setCipherString(sb.sbStrToXMLCh());
  +
  +	return mp_encryptedData;
  +
  +}
   
   // --------------------------------------------------------------------------------
   //			Encrypt a key
  @@ -909,35 +1018,6 @@
   			"XENCCipherImpl::encryptElement - No key set");
   	}
   
  -	// Map the encryption method to a URI
  -	safeBuffer algorithmSB;
  -	const XMLCh * algorithm;
  -
  -	if (em == ENCRYPT_NONE) {
  -		algorithm = algorithmURI;
  -	}
  -	else {
  -		if (encryptionMethod2URI(algorithmSB, em) != true) {
  -			throw XSECException(XSECException::CipherError, 
  -				"XENCCipherImpl::encryptElement - Unknown encryption method");
  -		}
  -		algorithm = algorithmSB.sbStrToXMLCh();
  -	}
  -
  -	// Create the element with a dummy encrypted value
  -
  -	if (mp_encryptedData != NULL) {
  -		delete mp_encryptedData;
  -		mp_encryptedData = NULL;
  -	}
  -	
  -	XSECnew(mp_encryptedData, XENCEncryptedDataImpl(mp_env));
  -	mp_encryptedData->createBlankEncryptedData(
  -		XENCCipherData::VALUE_TYPE, 
  -		algorithm,
  -		s_noData);
  -
  -
   	// Create a transform chain to do the encryption
   	TXFMDocObject * tdocObj;
   	XSECnew(tdocObj, TXFMDocObject(mp_doc));
  @@ -956,48 +1036,9 @@
   	tc14n->activateComments();
   	tc14n->setExclusive();
   
  -	// Perform the encryption
  -	XSECAlgorithmHandler *handler;
  -
  -	if (algorithm != NULL) {
  -		
  -		handler = 
  -			XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(algorithm);
  -	
  -	}
  -
  -	else {
  -
  -		handler =
  -			XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(
  -				XSECAlgorithmMapper::s_defaultEncryptionMapping);
  -
  -	}
  -
  -	safeBuffer sb;
  -
  -	if (handler != NULL) {
  -
  -		handler->encryptToSafeBuffer(c, 
  -			mp_encryptedData->getEncryptionMethod(), 
  -			mp_key,
  -			mp_env->getParentDocument(),
  -			sb);
  -	}
  -	else {
  -
  -		// Very strange if we get here - any problems should throw an
  -		// exception in the AlgorithmMapper.
  -
  -		throw XSECException(XSECException::CipherError, 
  -			"XENCCipherImpl::decryptElement - Error retrieving a handler for algorithm");
  -
  -	}
  +	// Do the hard work
   
  -	// Set the value
  -	XENCCipherValue * val = mp_encryptedData->getCipherData()->getCipherValue();
  -
  -	val->setCipherString(sb.sbStrToXMLCh());
  +	encryptTXFMChain(c, em, algorithmURI);
   
   	// Replace original element
   	DOMNode * p = element->getParentNode();
  
  
  
  1.11      +12 -2     xml-security/c/src/xenc/impl/XENCCipherImpl.hpp
  
  Index: XENCCipherImpl.hpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/xenc/impl/XENCCipherImpl.hpp,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- XENCCipherImpl.hpp	19 Oct 2003 12:50:35 -0000	1.10
  +++ XENCCipherImpl.hpp	25 Oct 2003 10:29:15 -0000	1.11
  @@ -124,7 +124,17 @@
   		encryptionMethod em,
   		const XMLCh * algorithmURI = NULL
   	);
  -
  +	// Encrypt octet streams
  +	virtual XENCEncryptedData * encryptBinInputStream(
  +		XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream * plainText,
  +		encryptionMethod em,
  +		const XMLCh * algorithmURI = NULL
  +	);
  +	virtual XENCEncryptedData * encryptTXFMChain(
  +		TXFMChain * plainText,
  +		encryptionMethod em,
  +		const XMLCh * algorithmURI = NULL
  +	);
   
   	// Getter methods
   	XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * getDocument(void)