c196be5394fcbc05ad1e387a7479496891c741bb
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / impl / XMLSecSignature.h
1 /*\r
2 *  Copyright 2001-2006 Internet2\r
3  * \r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file XMLSecSignature.h\r
19  * \r
20  * Signature classes for XMLSec-based signature-handling\r
21  */\r
22 \r
23 #if !defined(__xmltooling_xmlsecsig_h__) && !defined(XMLTOOLING_NO_XMLSEC)\r
24 #define __xmltooling_xmlsecsig_h__\r
25 \r
26 #include "internal.h"\r
27 #include "impl/UnknownElement.h"\r
28 #include "signature/Signature.h"\r
29 #include "util/XMLConstants.h"\r
30 \r
31 #include <string>\r
32 \r
33 #if defined (_MSC_VER)\r
34     #pragma warning( push )\r
35     #pragma warning( disable : 4250 4251 )\r
36 #endif\r
37 \r
38 namespace xmltooling {\r
39 \r
40     /**\r
41      * XMLObject representing XML Digital Signature, version 20020212, Signature element.\r
42      * Manages an Apache XML Signature object and the associated DOM.  \r
43      */\r
44     class XMLTOOL_DLLLOCAL XMLSecSignatureImpl : public UnknownElementImpl, public virtual Signature\r
45     {\r
46     public:\r
47         XMLSecSignatureImpl() : UnknownElementImpl(XMLConstants::XMLSIG_NS, Signature::LOCAL_NAME),\r
48             m_signature(NULL), m_c14n(NULL), m_sm(NULL) {}\r
49         virtual ~XMLSecSignatureImpl();\r
50         \r
51         void releaseDOM();\r
52         XMLObject* clone() const;\r
53 \r
54         // Getters\r
55         const XMLCh* getCanonicalizationMethod() const { return m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC; }\r
56         const XMLCh* getSignatureAlgorithm() const { return m_sm ? m_sm : DSIGConstants::s_unicodeStrURIRSA_SHA1; }\r
57         DSIGKeyInfoList* getKeyInfo() const;\r
58 \r
59         // Setters\r
60         void setCanonicalizationMethod(const XMLCh* c14n) { m_c14n = prepareForAssignment(m_c14n,c14n); }\r
61         void setSignatureAlgorithm(const XMLCh* sm) { m_sm = prepareForAssignment(m_sm,sm); }\r
62 \r
63         void sign(const SigningContext* ctx);\r
64 \r
65     private:\r
66         DSIGSignature* m_signature;\r
67         XMLCh* m_c14n;\r
68         XMLCh* m_sm;\r
69 \r
70         friend class XMLTOOL_DLLLOCAL XMLSecSignatureMarshaller;\r
71         friend class XMLTOOL_DLLLOCAL XMLSecSignatureUnmarshaller;\r
72     };\r
73 \r
74     /**\r
75      * Factory for XMLSecSignatureImpl objects\r
76      */\r
77     class XMLTOOL_DLLLOCAL XMLSecSignatureBuilder : public virtual XMLObjectBuilder\r
78     {\r
79     public:\r
80         /**\r
81          * @see XMLObjectBuilder::buildObject()\r
82          */\r
83         XMLObject* buildObject() const {\r
84             return new XMLSecSignatureImpl();\r
85         }\r
86     };\r
87 \r
88     /**\r
89      * Marshaller for XMLSecSignatureImpl objects\r
90      */\r
91     class XMLTOOL_DLLLOCAL XMLSecSignatureMarshaller : public virtual Marshaller\r
92     {\r
93     public:\r
94         /**\r
95          * @see Marshaller::marshall(XMLObject*,DOMDocument*, const MarshallingContext*)\r
96          */\r
97         DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL, MarshallingContext* ctx=NULL) const;\r
98 \r
99         /**\r
100          * @see Marshaller::marshall(XMLObject*,DOMElement*, const MarshallingContext* ctx)\r
101          */\r
102         DOMElement* marshall(XMLObject* xmlObject, DOMElement* parentElement, MarshallingContext* ctx=NULL) const;\r
103         \r
104     protected:\r
105         void setDocumentElement(DOMDocument* document, DOMElement* element) const {\r
106             DOMElement* documentRoot = document->getDocumentElement();\r
107             if (documentRoot)\r
108                 document->replaceChild(documentRoot, element);\r
109             else\r
110                 document->appendChild(element);\r
111         }\r
112     };\r
113 \r
114     /**\r
115      * Unmarshaller for XMLSecSignatureImpl objects\r
116      */\r
117     class XMLTOOL_DLLLOCAL XMLSecSignatureUnmarshaller : public virtual Unmarshaller\r
118     {\r
119     public:\r
120         /**\r
121          * @see Unmarshaller::unmarshall()\r
122          */\r
123         XMLObject* unmarshall(DOMElement* element, bool bindDocument=false) const;\r
124     };\r
125 \r
126 };\r
127 \r
128 #if defined (_MSC_VER)\r
129     #pragma warning( pop )\r
130 #endif\r
131 \r
132 #endif /* __xmltooling_xmlsecsig_h__ */\r