New -lite library and elimination of SAML libraries from modules.
[shibboleth/sp.git] / siterefresh / siterefresh.cpp
1 /*
2  *  Copyright 2001-2007 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 /* siterefresh.cpp - command-line tool to refresh and verify metadata
18
19    Scott Cantor
20    5/12/03
21
22    $Id$
23 */
24
25 #include <saml/SAMLConfig.h>
26 #include <saml/saml2/metadata/Metadata.h>
27 #include <saml/util/SAMLConstants.h>
28 #include <xmltooling/XMLToolingConfig.h>
29 #include <xmltooling/signature/Signature.h>
30 #include <xmltooling/util/XMLHelper.h>
31
32 #include <fstream>
33 #include <log4cpp/Category.hh>
34 #include <log4cpp/OstreamAppender.hh>
35 #include <xercesc/framework/URLInputSource.hpp>
36 #include <xercesc/framework/StdInInputSource.hpp>
37 #include <xercesc/framework/Wrapper4InputSource.hpp>
38 #include <xsec/enc/XSECCryptoProvider.hpp>
39 #include <xsec/enc/XSECKeyInfoResolverDefault.hpp>
40 #include <xsec/enc/XSECCryptoException.hpp>
41 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
42 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
43 #include <xsec/framework/XSECProvider.hpp>
44 #include <xsec/framework/XSECException.hpp>
45 #include <xsec/dsig/DSIGTransformC14n.hpp>
46 #include <xsec/dsig/DSIGReference.hpp>
47 #include <xsec/dsig/DSIGTransformList.hpp>
48
49 using namespace xmlsignature;
50 using namespace xmlconstants;
51 using namespace xmltooling;
52 using namespace samlconstants;
53 using namespace opensaml::saml2md;
54 using namespace opensaml;
55 using namespace xercesc;
56 using namespace log4cpp;
57 using namespace std;
58
59 void verifySignature(DOMDocument* doc, DOMNode* sigNode, const char* cert=NULL)
60 {
61     Category& log=Category::getInstance("siterefresh");
62     static const XMLCh ID[]={chLatin_I, chLatin_D, chNull};
63
64     // Load the signature.
65     XSECProvider prov;
66     DSIGSignature* sig=NULL;
67     try {
68         sig=prov.newSignatureFromDOM(doc,sigNode);
69         sig->load();
70
71         bool valid=false;
72
73         // Verify the signature coverage.
74         DSIGReferenceList* refs=sig->getReferenceList();
75         if (sig->getSignatureMethod()==SIGNATURE_RSA && refs && refs->getSize()==1) {
76             DSIGReference* ref=refs->item(0);
77             if (ref) {
78                 const XMLCh* URI=ref->getURI();
79                 if (!URI || !*URI || (*URI==chPound &&
80                         !XMLString::compareString(&URI[1],static_cast<DOMElement*>(sigNode->getParentNode())->getAttributeNS(NULL,ID)))) {
81                     DSIGTransformList* tlist=ref->getTransforms();
82                     for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {
83                         if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)
84                             valid=true;
85                         else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N) {
86                             valid=false;
87                             break;
88                         }
89                     }
90                 }
91             }
92         }
93     
94         if (!valid) {
95             log.error("detected an invalid signature profile");
96             throw SignatureException("detected an invalid signature profile");
97         }
98
99         if (cert) {
100             // Load the certificate, stripping the header and trailer.
101             string certbuf,line;
102             auto_ptr<OpenSSLCryptoX509> x509(new OpenSSLCryptoX509());
103             bool sawheader=false;
104             ifstream infile(cert);
105             while (!getline(infile,line).fail()) {
106                 if (line.find("CERTIFICATE-----")==string::npos) {
107                     if (sawheader)
108                         certbuf+=line + '\n';
109                 }
110                 else
111                     sawheader=true;
112             }
113             x509->loadX509Base64Bin(certbuf.data(),certbuf.length());
114             sig->setSigningKey(x509->clonePublicKey());
115         }
116         else {
117             log.warn("verifying with key inside signature, this is a sanity check but provides no security");
118             XSECKeyInfoResolverDefault resolver;
119             sig->setKeyInfoResolver(resolver.clone());
120         }
121         
122         if (!sig->verify()) {
123             log.error("detected an invalid signature value");
124             throw SignatureException("detected an invalid signature value");
125         }
126
127         prov.releaseSignature(sig);
128     }
129     catch(...) {
130         if (sig)
131             prov.releaseSignature(sig);
132         throw;
133     }
134 }
135
136 int main(int argc,char* argv[])
137 {
138     int ret=0;
139     SAMLConfig& conf=SAMLConfig::getConfig();
140     bool verify=true;
141     char* url_param=NULL;
142     char* cert_param=NULL;
143     char* out_param=NULL;
144     char* path=getenv("SHIBSCHEMAS");
145     char* ns_param=NULL;
146     char* name_param=NULL;
147
148     for (int i=1; i<argc; i++) {
149         if (!strcmp(argv[i],"--schema") && i+1<argc)
150             path=argv[++i];
151         else if (!strcmp(argv[i],"--url") && i+1<argc)
152             url_param=argv[++i];
153         else if (!strcmp(argv[i],"--noverify"))
154             verify=false;
155         else if (!strcmp(argv[i],"--cert") && i+1<argc)
156             cert_param=argv[++i];
157         else if (!strcmp(argv[i],"--out") && i+1<argc)
158             out_param=argv[++i];
159         else if (!strcmp(argv[i],"--rootns") && i+1<argc)
160             ns_param=argv[++i];
161         else if (!strcmp(argv[i],"--rootname") && i+1<argc)
162             name_param=argv[++i];
163     }
164
165     if (verify && !cert_param) {
166         cout << "usage: " << argv[0] << endl <<
167             "\t--url <URL of metadata>" << endl <<
168             "\t--noverify OR --cert <PEM Certificate>" << endl <<
169             "\t[--out <pathname to copy data to>]" << endl <<
170             "\t[--schema <schema path>]" << endl <<
171             "\t[--rootns <root element XML namespace>]" << endl <<
172             "\t[--rootname <root element name>]" << endl;
173         return -100;
174     }
175
176     Category::setRootPriority(Priority::WARN);
177     Category::getRoot().addAppender(new OstreamAppender("default",&cerr));
178     Category& log=Category::getInstance("siterefresh");
179     if (!conf.init())
180         return -10;
181
182     /*
183     saml::XML::registerSchema(shibtarget::XML::SAML2META_NS,shibtarget::XML::SAML2META_SCHEMA_ID);
184     saml::XML::registerSchema(shibtarget::XML::SAML2ASSERT_NS,shibtarget::XML::SAML2ASSERT_SCHEMA_ID);
185     saml::XML::registerSchema(shibtarget::XML::XMLENC_NS,shibtarget::XML::XMLENC_SCHEMA_ID);
186     */
187
188     try {
189         // Parse the specified document.
190         static XMLCh base[]={chLatin_f, chLatin_i, chLatin_l, chLatin_e, chColon, chForwardSlash, chForwardSlash, chForwardSlash, chNull};
191         DOMDocument* doc=NULL;
192         if (url_param && *url_param) {
193             URLInputSource src(base,url_param);
194             Wrapper4InputSource dsrc(&src,false);
195             doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
196         }
197         else {
198             StdInInputSource src;
199             Wrapper4InputSource dsrc(&src,false);
200             doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
201         }
202     
203         // Check root element.
204         if (ns_param && name_param) {
205             auto_ptr_XMLCh ns(ns_param);
206             auto_ptr_XMLCh name(name_param);
207             if (!XMLHelper::isNodeNamed(doc->getDocumentElement(),ns.get(),name.get()))
208                 throw XMLObjectException(string("Root element does not match specified QName of {") + ns_param + "}:" + name_param);
209         }
210         else if (!XMLHelper::isNodeNamed(doc->getDocumentElement(),SAML20MD_NS,EntitiesDescriptor::LOCAL_NAME) &&
211                  !XMLHelper::isNodeNamed(doc->getDocumentElement(),SAML20MD_NS,EntityDescriptor::LOCAL_NAME))
212             throw XMLObjectException("Root element does not signify a known metadata format");
213
214         // Verify the "root" signature.
215         DOMElement* rootSig=XMLHelper::getFirstChildElement(doc->getDocumentElement(),XMLSIG_NS,Signature::LOCAL_NAME);
216         if (verify) {
217             if (rootSig) {
218                 verifySignature(doc,rootSig,cert_param);
219             }
220             else {
221                 doc->release();
222                 log.error("unable to locate root signature to verify in document");
223                 throw SignatureException("Verification implies that the document must be signed");
224             }
225         }
226         else if (rootSig) {
227             log.warn("verification of signer disabled, make sure you trust the source of this file!");
228             verifySignature(doc,rootSig,cert_param);
229         }
230         else {
231             log.warn("verification disabled, and file is unsigned!");
232         }
233
234         // Verify all signatures.
235         DOMNodeList* siglist=doc->getElementsByTagNameNS(XMLSIG_NS,Signature::LOCAL_NAME);
236         for (unsigned int i=0; siglist && i<siglist->getLength(); i++)
237             verifySignature(doc,siglist->item(i),cert_param);
238
239         if (out_param) {
240             // Output the data to the specified file.
241             ofstream outfile(out_param);
242             outfile << *(doc->getDocumentElement());
243         }
244         else
245             cout << *(doc->getDocumentElement());
246         doc->release();
247     }
248     catch (SignatureException&) {
249         ret=-1;
250     }
251     catch(XMLToolingException& e) {
252         log.errorStream() << "caught an XMLTooling exception: " << e.what() << CategoryStream::ENDLINE;
253         ret=-2;
254     }
255     catch(XMLException& e) {
256         auto_ptr_char temp(e.getMessage());
257         log.errorStream() << "caught an XML exception: " << temp.get() << CategoryStream::ENDLINE;
258         ret=-3;
259     }
260     catch(XSECException& e) {
261         auto_ptr_char temp(e.getMsg());
262         log.errorStream() << "caught an XMLSec exception: " << temp.get() << CategoryStream::ENDLINE;
263         ret=-4;
264     }
265     catch(XSECCryptoException& e) {
266         log.errorStream() << "caught an XMLSecCrypto exception: " << e.getMsg() << CategoryStream::ENDLINE;
267         ret=-5;
268     }
269     catch(...) {
270         log.errorStream() << "caught an unknown exception" << CategoryStream::ENDLINE;
271         ret=-6;
272     }
273
274     conf.term();
275     return ret;
276 }