6ccc49c4aea9e1d95595c544adc1228c37958561
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / Signature.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 Signature.h\r
19  * \r
20  * XMLObject representing XML Digital Signature, version 20020212, Signature element. \r
21  */\r
22 \r
23 #if !defined(__xmltooling_sig_h__) && !defined(XMLTOOLING_NO_XMLSEC)\r
24 #define __xmltooling_sig_h__\r
25 \r
26 #include <xmltooling/exceptions.h>\r
27 #include <xmltooling/XMLObjectBuilder.h>\r
28 #include <xmltooling/signature/ContentReference.h>\r
29 #include <xmltooling/util/XMLConstants.h>\r
30 \r
31 #include <xsec/dsig/DSIGSignature.hpp>\r
32 \r
33 /**\r
34  * @namespace xmlsignature\r
35  * Public namespace of XML Signature classes\r
36  */\r
37 namespace xmlsignature {\r
38 \r
39     class XMLTOOL_API KeyInfo;\r
40 \r
41     /**\r
42      * XMLObject representing XML Digital Signature, version 20020212, Signature element.\r
43      * The default signature settings include Exclusive c14n w/o comments, SHA-1 digests,\r
44      * and RSA-SHA1 signing. \r
45      */\r
46     class XMLTOOL_API Signature : public virtual xmltooling::XMLObject\r
47     {\r
48     public:\r
49         virtual ~Signature() {}\r
50 \r
51         /** Element local name */\r
52         static const XMLCh LOCAL_NAME[];\r
53 \r
54         /**\r
55          * Sets the canonicalization method for the ds:SignedInfo element\r
56          * \r
57          * @param c14n  the canonicalization method\r
58          */\r
59         virtual void setCanonicalizationMethod(const XMLCh* c14n)=0;\r
60         \r
61         /**\r
62          * Sets the signing algorithm for the signature.\r
63          * \r
64          * @param sm    the signature algorithm\r
65          */\r
66         virtual void setSignatureAlgorithm(const XMLCh* sm)=0;\r
67 \r
68         /**\r
69          * Sets the signing key used to create the signature.\r
70          * \r
71          * @param signingKey the secret/private key used to create the signature\r
72          */\r
73         virtual void setSigningKey(XSECCryptoKey* signingKey)=0;\r
74 \r
75         /**\r
76          * Sets a KeyInfo object to embed in the Signature.\r
77          * \r
78          * @param keyInfo   pointer to a KeyInfo object, or NULL\r
79          */\r
80         virtual void setKeyInfo(KeyInfo* keyInfo)=0;\r
81 \r
82         /**\r
83          * Gets the KeyInfo object associated with the Signature.\r
84          * This is <strong>NOT</strong> provided for access to the\r
85          * data associated with an unmarshalled signature. It is\r
86          * used only in the creation of signatures. Access to data\r
87          * for validation purposes is provided through the native\r
88          * DSIGSignature object.\r
89          * \r
90          * @return  pointer to a KeyInfo object, or NULL\r
91          */\r
92         virtual KeyInfo* getKeyInfo() const=0;\r
93 \r
94         /**\r
95          * Sets the ContentReference object to the Signature to be applied\r
96          * when the signature is created.\r
97          * \r
98          * @param reference the reference to attach, or NULL \r
99          */\r
100         virtual void setContentReference(ContentReference* reference)=0;\r
101 \r
102         /**\r
103          * Gets the ContentReference object associated with the Signature.\r
104          * This is <strong>NOT</strong> provided for access to the\r
105          * data associated with an unmarshalled signature. It is\r
106          * used only in the creation of signatures. Access to data\r
107          * for validation purposes is provided through the native\r
108          * DSIGSignature object.\r
109          * \r
110          * @return  pointer to a ContentReference object, or NULL\r
111          */\r
112         virtual ContentReference* getContentReference() const=0;\r
113 \r
114         \r
115         /**\r
116          * Gets the native Apache signature object, if present.\r
117          * \r
118          * @return  the native Apache signature interface\r
119          */\r
120         virtual DSIGSignature* getXMLSignature() const=0;\r
121 \r
122         /**\r
123          * Compute and append the signature based on the assigned\r
124          * ContentReference, KeyInfo, and signing key.\r
125          */\r
126         virtual void sign()=0;\r
127 \r
128         /**\r
129          * Type-safe clone operation.\r
130          * \r
131          * @return  copy of object\r
132          */\r
133         virtual Signature* cloneSignature() const=0;\r
134 \r
135     protected:\r
136         Signature() {}\r
137     };\r
138 \r
139     /**\r
140      * Builder for Signature objects.\r
141      */\r
142     class XMLTOOL_API SignatureBuilder : public xmltooling::XMLObjectBuilder\r
143     {\r
144     public:\r
145         virtual Signature* buildObject(\r
146             const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const xmltooling::QName* schemaType=NULL\r
147             ) const;\r
148             \r
149         /**\r
150          * Default builder\r
151          * \r
152          * @return empty Signature object\r
153          */\r
154         virtual Signature* buildObject() const;\r
155 \r
156         static Signature* buildSignature() {\r
157             const SignatureBuilder* b = dynamic_cast<const SignatureBuilder*>(\r
158                 xmltooling::XMLObjectBuilder::getBuilder(\r
159                     xmltooling::QName(xmltooling::XMLConstants::XMLSIG_NS,Signature::LOCAL_NAME)\r
160                     )\r
161                 );\r
162             if (b)\r
163                 return b->buildObject();\r
164             throw xmltooling::XMLObjectException("Unable to obtain typed builder for Signature.");\r
165         }\r
166     };\r
167 \r
168     DECL_XMLTOOLING_EXCEPTION(SignatureException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmlsignature,xmltooling::XMLSecurityException,Exceptions in signature processing);\r
169 \r
170 };\r
171 \r
172 #endif /* __xmltooling_sig_h__ */\r