Implemented EncryptionMethod schema snippet.
[shibboleth/xmltooling.git] / xmltooling / encryption / impl / EncryptionImpl.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * EncryptionImpl.cpp
19  * 
20  * Implementation classes for XML Encryption schema
21  */
22
23 #include "internal.h"
24 #include "AbstractChildlessElement.h"
25 #include "AbstractComplexElement.h"
26 #include "AbstractSimpleElement.h"
27 #include "exceptions.h"
28 #include "encryption/Encryption.h"
29 #include "io/AbstractXMLObjectMarshaller.h"
30 #include "io/AbstractXMLObjectUnmarshaller.h"
31 #include "util/XMLHelper.h"
32 #include "validation/AbstractValidatingXMLObject.h"
33
34 #include <xercesc/util/XMLUniDefs.hpp>
35
36 using namespace xmlencryption;
37 using namespace xmltooling;
38 using namespace std;
39
40 #if defined (_MSC_VER)
41     #pragma warning( push )
42     #pragma warning( disable : 4250 4251 )
43 #endif
44
45 namespace xmlencryption {
46
47     DECL_XMLOBJECTIMPL_SIMPLE(XMLTOOL_DLLLOCAL,KeySize);
48     DECL_XMLOBJECTIMPL_SIMPLE(XMLTOOL_DLLLOCAL,OAEPparams);
49
50     class XMLTOOL_DLLLOCAL EncryptionMethodImpl : public virtual EncryptionMethod,
51         public AbstractComplexElement,
52         public AbstractDOMCachingXMLObject,
53         public AbstractValidatingXMLObject,
54         public AbstractXMLObjectMarshaller,
55         public AbstractXMLObjectUnmarshaller
56     {
57         void init() {
58             m_Algorithm=NULL;
59             m_KeySize=NULL;
60             m_OAEPparams=NULL;
61             m_children.push_back(NULL);
62             m_children.push_back(NULL);
63             m_pos_KeySize=m_children.begin();
64             m_pos_OAEPparams=m_pos_KeySize;
65             ++m_pos_OAEPparams;
66         }
67     public:
68         virtual ~EncryptionMethodImpl() {
69             XMLString::release(&m_Algorithm);
70         }
71
72         EncryptionMethodImpl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType)
73                 : AbstractXMLObject(nsURI, localName, prefix, schemaType) {
74             init();
75         }
76             
77         EncryptionMethodImpl(const EncryptionMethodImpl& src)
78                 : AbstractXMLObject(src), AbstractDOMCachingXMLObject(src), AbstractValidatingXMLObject(src) {
79             init();
80             setAlgorithm(src.getAlgorithm());
81             if (src.getKeySize())
82                 setKeySize(src.getKeySize()->cloneKeySize());
83             if (src.getOAEPparams())
84                 setOAEPparams(src.getOAEPparams()->cloneOAEPparams());
85             VectorOf(XMLObject) v=getOtherParameters();
86             for (vector<XMLObject*>::const_iterator i=src.m_OtherParameters.begin(); i!=src.m_OtherParameters.end(); i++) {
87                 if (*i) {
88                     v.push_back((*i)->clone());
89                 }
90             }
91         }
92         
93         IMPL_XMLOBJECT_CLONE(EncryptionMethod);
94         IMPL_STRING_ATTRIB(Algorithm);
95         IMPL_TYPED_CHILD(KeySize);
96         IMPL_TYPED_CHILD(OAEPparams);
97         IMPL_XMLOBJECT_CHILDREN(OtherParameter,m_children.end());
98
99     protected:
100         void marshallAttributes(DOMElement* domElement) const {
101             MARSHALL_STRING_ATTRIB(Algorithm,ALGORITHM,NULL);
102         }
103
104         void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
105             PROC_TYPED_CHILD(KeySize,XMLConstants::XMLENC_NS,false);
106             PROC_TYPED_CHILD(OAEPparams,XMLConstants::XMLENC_NS,false);
107             
108             // Unknown child.
109             const XMLCh* nsURI=root->getNamespaceURI();
110             if (!XMLString::equals(nsURI,XMLConstants::XMLENC_NS) && nsURI && *nsURI)
111                 getOtherParameters().push_back(childXMLObject);
112             
113             AbstractXMLObjectUnmarshaller::processChildElement(childXMLObject,root);
114         }
115
116         void processAttribute(const DOMAttr* attribute) {
117             PROC_STRING_ATTRIB(Algorithm,ALGORITHM,NULL);
118         }
119     };
120
121 };
122
123 #if defined (_MSC_VER)
124     #pragma warning( pop )
125 #endif
126
127 // Builder Implementations
128
129 IMPL_XMLOBJECTBUILDER(KeySize);
130 IMPL_XMLOBJECTBUILDER(OAEPparams);
131 IMPL_XMLOBJECTBUILDER(EncryptionMethod);
132
133 // Unicode literals
134
135 const XMLCh KeySize::LOCAL_NAME[] =                     UNICODE_LITERAL_7(K,e,y,S,i,z,e);
136 const XMLCh OAEPparams::LOCAL_NAME[] =                  UNICODE_LITERAL_10(O,A,E,P,p,a,r,a,m,s);
137 const XMLCh EncryptionMethod::LOCAL_NAME[] =            UNICODE_LITERAL_16(E,n,c,r,y,p,t,i,o,n,M,e,t,h,o,d);
138 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);
139 const XMLCh EncryptionMethod::ALGORITHM_ATTRIB_NAME[] = UNICODE_LITERAL_9(A,l,g,o,r,i,t,h,m);