From 194cac7ed6c7bc37220178657d6e353bec801004 Mon Sep 17 00:00:00 2001 From: cantor Date: Sat, 27 May 2006 01:28:35 +0000 Subject: [PATCH] Implemented EncryptionMethod schema snippet. git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@110 de75baf8-a10c-0410-a50a-987c0e22f00f --- .cdtproject | 4 +- xmltooling/Makefile.am | 7 ++ xmltooling/XMLToolingConfig.cpp | 3 + xmltooling/base.h | 119 ++++++++++++++++++ xmltooling/encryption/Encryption.h | 66 ++++++++++ xmltooling/encryption/impl/EncryptionImpl.cpp | 139 +++++++++++++++++++++ .../encryption/impl/EncryptionSchemaValidators.cpp | 59 +++++++++ xmltooling/signature/KeyInfo.h | 1 - xmltooling/xmltooling.vcproj | 24 ++++ 9 files changed, 420 insertions(+), 2 deletions(-) create mode 100644 xmltooling/encryption/Encryption.h create mode 100644 xmltooling/encryption/impl/EncryptionImpl.cpp create mode 100644 xmltooling/encryption/impl/EncryptionSchemaValidators.cpp diff --git a/.cdtproject b/.cdtproject index 87a1045..2882d5e 100644 --- a/.cdtproject +++ b/.cdtproject @@ -60,7 +60,7 @@ - + @@ -68,6 +68,8 @@ + + diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index eae502f..9dcf6d8 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -4,6 +4,8 @@ lib_LTLIBRARIES = libxmltooling.la libxmltoolingincludedir = $(includedir)/xmltooling +encincludedir = $(includedir)/xmltooling/encryption + implincludedir = $(includedir)/xmltooling/impl ioincludedir = $(includedir)/xmltooling/io @@ -38,6 +40,9 @@ libxmltoolinginclude_HEADERS = \ XMLObjectBuilder.h \ XMLToolingConfig.h +encinclude_HEADERS = \ + encryption/Encryption.h + implinclude_HEADERS = \ impl/AnyElement.h \ impl/UnknownElement.h @@ -88,6 +93,8 @@ libxmltooling_la_SOURCES = \ unicode.cpp \ XMLObjectBuilder.cpp \ XMLToolingConfig.cpp \ + encryption/impl/EncryptionImpl.cpp \ + encryption/impl/EncryptionSchemaValidators.cpp \ impl/AnyElement.cpp \ impl/UnknownElement.cpp \ io/AbstractXMLObjectMarshaller.cpp \ diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 7b9d378..5550b65 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -23,6 +23,7 @@ #include "internal.h" #include "exceptions.h" #include "XMLToolingConfig.h" +#include "encryption/Encryption.h" #include "impl/UnknownElement.h" #include "signature/KeyInfo.h" #include "signature/Signature.h" @@ -44,6 +45,7 @@ #include +using namespace xmlencryption; using namespace xmlsignature; using namespace xmltooling; using namespace log4cpp; @@ -161,6 +163,7 @@ bool XMLToolingInternalConfig::init() XMLObjectBuilder::registerDefaultBuilder(new UnknownElementBuilder()); registerKeyInfoClasses(); + registerEncryptionClasses(); REGISTER_EXCEPTION_FACTORY(XMLParserException,xmltooling); REGISTER_EXCEPTION_FACTORY(XMLObjectException,xmltooling); diff --git a/xmltooling/base.h b/xmltooling/base.h index 7398e0c..c377e4d 100644 --- a/xmltooling/base.h +++ b/xmltooling/base.h @@ -240,6 +240,29 @@ static const XMLCh LOCAL_NAME[] /** + * Begins the declaration of an XMLObject specialization with two base classes. + * Basic boilerplate includes a protected constructor, empty virtual destructor, + * and Unicode constants for the default associated element's name and prefix. + * + * @param linkage linkage specifier for the class + * @param cname the name of the class to declare + * @param base the first base class to derive from using public virtual inheritance + * @param base2 the second base class to derive from using public virtual inheritance + * @param desc documentation comment for class + */ +#define BEGIN_XMLOBJECT2(linkage,cname,base,base2,desc) \ + XMLTOOLING_DOXYGEN(desc) \ + class linkage cname : public virtual base, public virtual base2, public virtual xmltooling::ValidatingXMLObject { \ + protected: \ + cname() {} \ + public: \ + virtual ~cname() {} \ + XMLTOOLING_DOXYGEN(Type-specific clone method.) \ + virtual cname* clone##cname() const=0; \ + XMLTOOLING_DOXYGEN(Element local name) \ + static const XMLCh LOCAL_NAME[] + +/** * Ends the declaration of an XMLObject specialization. */ #define END_XMLOBJECT } @@ -298,6 +321,21 @@ virtual void set##proper(int proper)=0 /** + * Declares abstract get/set methods for a boolean XML attribute. + * + * @param proper the proper name of the attribute + * @param upcased the upcased name of the attribute + */ +#define DECL_BOOLEAN_ATTRIB(proper,upcased) \ + public: \ + XMLTOOLING_DOXYGEN(proper attribute name) \ + static const XMLCh upcased##_ATTRIB_NAME[]; \ + XMLTOOLING_DOXYGEN(Returns the proper attribute.) \ + virtual bool proper() const=0; \ + XMLTOOLING_DOXYGEN(Sets the proper attribute.) \ + virtual void proper(bool value)=0 + +/** * Implements get/set methods and a private member for a typed XML attribute. * * @param proper the proper name of the attribute @@ -356,6 +394,25 @@ } /** + * Implements get/set methods and a private member for a boolean XML attribute. + * + * @param proper the proper name of the attribute + */ +#define IMPL_BOOLEAN_ATTRIB(proper) \ + protected: \ + bool m_##proper; \ + public: \ + bool proper() const { \ + return m_##proper; \ + } \ + void proper(bool value) { \ + if (m_##proper != value) { \ + releaseThisandParentDOM(); \ + m_##proper = value; \ + } \ + } + +/** * Declares abstract get/set methods for a typed XML child object in a foreign namespace. * * @param proper the proper name of the child type @@ -442,6 +499,19 @@ virtual const std::vector& get##proper##s() const=0 /** + * Declares abstract get/set methods for a typed XML child collection in a foreign namespace. + * + * @param proper the proper name of the child type + * @param ns the C++ namespace for the type + */ +#define DECL_TYPED_FOREIGN_CHILDREN(proper,ns) \ + public: \ + XMLTOOLING_DOXYGEN(Returns modifiable proper collection.) \ + virtual VectorOf(ns::proper) get##proper##s()=0; \ + XMLTOOLING_DOXYGEN(Returns reference to immutable proper collection.) \ + virtual const std::vector& get##proper##s() const=0 + +/** * Declares abstract get/set methods for a generic XML child collection. * * @param proper the proper name of the child @@ -525,6 +595,19 @@ domElement->setAttributeNS(namespaceURI, ucase##_ATTRIB_NAME, wide##proper.get()) /** + * Implements marshalling for a boolean attribute + * + * @param proper the proper name of the attribute + * @param ucase the upcased name of the attribute + * @param namespaceURI the XML namespace of the attribute + */ +#define MARSHALL_BOOLEAN_ATTRIB(proper,ucase,namespaceURI) \ + XMLCh flag##proper[2]; \ + flag##proper[0]=m_##proper ? chDigit_1 : chDigit_0; \ + flag##proper[1]=chNull; \ + domElement->setAttributeNS(namespaceURI, ucase##_ATTRIB_NAME, flag##proper) + +/** * Implements marshalling for a QName attribute * * @param proper the proper name of the attribute @@ -613,6 +696,24 @@ return; \ } +/** + * Implements unmarshalling process branch for a boolean attribute + * + * @param proper the proper name of the attribute + * @param ucase the upcased name of the attribute + * @param namespaceURI the XML namespace of the attribute + */ +#define PROC_BOOLEAN_ATTRIB(proper,ucase,namespaceURI) \ + if (xmltooling::XMLHelper::isNodeNamed(attribute, namespaceURI, ucase##_ATTRIB_NAME)) { \ + const XMLCh* value=attribute->getValue(); \ + if (value) { \ + if (*value==chLatin_t || *value==chDigit_1) \ + m_##proper=true; \ + else if (*value==chLatin_f || *value==chDigit_0) \ + m_##proper=false; \ + } \ + return; \ + } /** * Implements unmarshalling process branch for typed child collection element @@ -662,6 +763,24 @@ } /** + * Declares aliased get/set methods for named integer XML element content. + * + * @param proper the proper name to label the element's content + */ +#define DECL_INTEGER_CONTENT(proper) \ + XMLTOOLING_DOXYGEN(Returns proper.) \ + int get##proper() const { \ + return XMLString::parseInt(getTextContent()); \ + } \ + XMLTOOLING_DOXYGEN(Sets or clears proper.) \ + void set##proper(int proper) { \ + char buf[64]; \ + sprintf(buf,"%d",proper); \ + xmltooling::auto_ptr_XMLCh widebuf(buf); \ + setTextContent(widebuf.get()); \ + } + +/** * Implements marshalling/unmarshalling for element content. */ #define IMPL_XMLOBJECT_CONTENT \ diff --git a/xmltooling/encryption/Encryption.h b/xmltooling/encryption/Encryption.h new file mode 100644 index 0000000..b25baba --- /dev/null +++ b/xmltooling/encryption/Encryption.h @@ -0,0 +1,66 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file Encryption.h + * + * XMLObjects representing XML Encryption content + */ + +#ifndef __xmltooling_encrypt_h__ +#define __xmltooling_encrypt_h__ + +#include +#include +#include +#include +#include + +#define DECL_XMLENCOBJECTBUILDER(cname) \ + DECL_XMLOBJECTBUILDER(XMLTOOL_API,cname,xmltooling::XMLConstants::XMLENC_NS,xmltooling::XMLConstants::XMLENC_PREFIX) + +/** + * @namespace xmlencryption + * Namespace for XML Encryption schema objects + */ +namespace xmlencryption { + + DECL_XMLOBJECT_SIMPLE(XMLTOOL_API,OAEPparams,Name,XML Encryption OAEPparams element); + + BEGIN_XMLOBJECT(XMLTOOL_API,KeySize,xmltooling::SimpleElement,XML Encryption KeySize element); + DECL_INTEGER_CONTENT(Size); + END_XMLOBJECT; + + BEGIN_XMLOBJECT(XMLTOOL_API,EncryptionMethod,xmltooling::XMLObject,XML Encryption EncryptionMethod element); + DECL_STRING_ATTRIB(Algorithm,ALGORITHM); + DECL_TYPED_CHILD(KeySize); + DECL_TYPED_CHILD(OAEPparams); + DECL_XMLOBJECT_CHILDREN(OtherParameter); + /** EncryptionMethodType local name */ + static const XMLCh TYPE_NAME[]; + END_XMLOBJECT; + + DECL_XMLENCOBJECTBUILDER(OAEPparams); + DECL_XMLENCOBJECTBUILDER(KeySize); + DECL_XMLENCOBJECTBUILDER(EncryptionMethod); + + /** + * Registers builders and validators for XML Encryption classes into the runtime. + */ + void XMLTOOL_API registerEncryptionClasses(); +}; + +#endif /* __xmltooling_encrypt_h__ */ diff --git a/xmltooling/encryption/impl/EncryptionImpl.cpp b/xmltooling/encryption/impl/EncryptionImpl.cpp new file mode 100644 index 0000000..7b3cf21 --- /dev/null +++ b/xmltooling/encryption/impl/EncryptionImpl.cpp @@ -0,0 +1,139 @@ +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * EncryptionImpl.cpp + * + * Implementation classes for XML Encryption schema + */ + +#include "internal.h" +#include "AbstractChildlessElement.h" +#include "AbstractComplexElement.h" +#include "AbstractSimpleElement.h" +#include "exceptions.h" +#include "encryption/Encryption.h" +#include "io/AbstractXMLObjectMarshaller.h" +#include "io/AbstractXMLObjectUnmarshaller.h" +#include "util/XMLHelper.h" +#include "validation/AbstractValidatingXMLObject.h" + +#include + +using namespace xmlencryption; +using namespace xmltooling; +using namespace std; + +#if defined (_MSC_VER) + #pragma warning( push ) + #pragma warning( disable : 4250 4251 ) +#endif + +namespace xmlencryption { + + DECL_XMLOBJECTIMPL_SIMPLE(XMLTOOL_DLLLOCAL,KeySize); + DECL_XMLOBJECTIMPL_SIMPLE(XMLTOOL_DLLLOCAL,OAEPparams); + + class XMLTOOL_DLLLOCAL EncryptionMethodImpl : public virtual EncryptionMethod, + public AbstractComplexElement, + public AbstractDOMCachingXMLObject, + public AbstractValidatingXMLObject, + public AbstractXMLObjectMarshaller, + public AbstractXMLObjectUnmarshaller + { + void init() { + m_Algorithm=NULL; + m_KeySize=NULL; + m_OAEPparams=NULL; + m_children.push_back(NULL); + m_children.push_back(NULL); + m_pos_KeySize=m_children.begin(); + m_pos_OAEPparams=m_pos_KeySize; + ++m_pos_OAEPparams; + } + public: + virtual ~EncryptionMethodImpl() { + XMLString::release(&m_Algorithm); + } + + EncryptionMethodImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType) + : AbstractXMLObject(nsURI, localName, prefix, schemaType) { + init(); + } + + EncryptionMethodImpl(const EncryptionMethodImpl& src) + : AbstractXMLObject(src), AbstractDOMCachingXMLObject(src), AbstractValidatingXMLObject(src) { + init(); + setAlgorithm(src.getAlgorithm()); + if (src.getKeySize()) + setKeySize(src.getKeySize()->cloneKeySize()); + if (src.getOAEPparams()) + setOAEPparams(src.getOAEPparams()->cloneOAEPparams()); + VectorOf(XMLObject) v=getOtherParameters(); + for (vector::const_iterator i=src.m_OtherParameters.begin(); i!=src.m_OtherParameters.end(); i++) { + if (*i) { + v.push_back((*i)->clone()); + } + } + } + + IMPL_XMLOBJECT_CLONE(EncryptionMethod); + IMPL_STRING_ATTRIB(Algorithm); + IMPL_TYPED_CHILD(KeySize); + IMPL_TYPED_CHILD(OAEPparams); + IMPL_XMLOBJECT_CHILDREN(OtherParameter,m_children.end()); + + protected: + void marshallAttributes(DOMElement* domElement) const { + MARSHALL_STRING_ATTRIB(Algorithm,ALGORITHM,NULL); + } + + void processChildElement(XMLObject* childXMLObject, const DOMElement* root) { + PROC_TYPED_CHILD(KeySize,XMLConstants::XMLENC_NS,false); + PROC_TYPED_CHILD(OAEPparams,XMLConstants::XMLENC_NS,false); + + // Unknown child. + const XMLCh* nsURI=root->getNamespaceURI(); + if (!XMLString::equals(nsURI,XMLConstants::XMLENC_NS) && nsURI && *nsURI) + getOtherParameters().push_back(childXMLObject); + + AbstractXMLObjectUnmarshaller::processChildElement(childXMLObject,root); + } + + void processAttribute(const DOMAttr* attribute) { + PROC_STRING_ATTRIB(Algorithm,ALGORITHM,NULL); + } + }; + +}; + +#if defined (_MSC_VER) + #pragma warning( pop ) +#endif + +// Builder Implementations + +IMPL_XMLOBJECTBUILDER(KeySize); +IMPL_XMLOBJECTBUILDER(OAEPparams); +IMPL_XMLOBJECTBUILDER(EncryptionMethod); + +// Unicode literals + +const XMLCh KeySize::LOCAL_NAME[] = UNICODE_LITERAL_7(K,e,y,S,i,z,e); +const XMLCh OAEPparams::LOCAL_NAME[] = UNICODE_LITERAL_10(O,A,E,P,p,a,r,a,m,s); +const XMLCh EncryptionMethod::LOCAL_NAME[] = UNICODE_LITERAL_16(E,n,c,r,y,p,t,i,o,n,M,e,t,h,o,d); +const XMLCh EncryptionMethod::TYPE_NAME[] = UNICODE_LITERAL_20(E,n,c,r,y,p,t,i,o,n,M,e,t,h,o,d,T,y,p,e); +const XMLCh EncryptionMethod::ALGORITHM_ATTRIB_NAME[] = UNICODE_LITERAL_9(A,l,g,o,r,i,t,h,m); diff --git a/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp b/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp new file mode 100644 index 0000000..aab35b5 --- /dev/null +++ b/xmltooling/encryption/impl/EncryptionSchemaValidators.cpp @@ -0,0 +1,59 @@ +/* +* Copyright 2001-2006 Internet2 + * +* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * EncryptionSchemaValidators.cpp + * + * Schema validators for XML Encryption schema + */ + +#include "internal.h" +#include "exceptions.h" +#include "encryption/Encryption.h" + +using namespace xmlencryption; +using namespace xmltooling; +using namespace std; + +namespace xmlencryption { + + XMLOBJECTVALIDATOR_SIMPLE(XMLTOOL_DLLLOCAL,KeySize); + XMLOBJECTVALIDATOR_SIMPLE(XMLTOOL_DLLLOCAL,OAEPparams); + + BEGIN_XMLOBJECTVALIDATOR(XMLTOOL_DLLLOCAL,EncryptionMethod); + XMLOBJECTVALIDATOR_REQUIRE(EncryptionMethod,Algorithm); + END_XMLOBJECTVALIDATOR; + +}; + +#define REGISTER_ELEMENT(namespaceURI,cname) \ + q=QName(namespaceURI,cname::LOCAL_NAME); \ + XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \ + Validator::registerValidator(q,new cname##SchemaValidator()) + +#define REGISTER_TYPE(namespaceURI,cname) \ + q=QName(namespaceURI,cname::TYPE_NAME); \ + XMLObjectBuilder::registerBuilder(q,new cname##Builder()); \ + Validator::registerValidator(q,new cname##SchemaValidator()) + +void xmlencryption::registerEncryptionClasses() +{ + QName q; + REGISTER_ELEMENT(XMLConstants::XMLENC_NS,KeySize); + REGISTER_ELEMENT(XMLConstants::XMLENC_NS,OAEPparams); + REGISTER_ELEMENT(XMLConstants::XMLENC_NS,EncryptionMethod); + REGISTER_TYPE(XMLConstants::XMLENC_NS,EncryptionMethod); +} diff --git a/xmltooling/signature/KeyInfo.h b/xmltooling/signature/KeyInfo.h index 3981b3d..0f157e5 100644 --- a/xmltooling/signature/KeyInfo.h +++ b/xmltooling/signature/KeyInfo.h @@ -25,7 +25,6 @@ #define __xmltooling_keyinfo_h__ #include -#include #include #include #include diff --git a/xmltooling/xmltooling.vcproj b/xmltooling/xmltooling.vcproj index 3035be2..bb36e0e 100644 --- a/xmltooling/xmltooling.vcproj +++ b/xmltooling/xmltooling.vcproj @@ -309,6 +309,22 @@ + + + + + + + + + + + +