https://issues.shibboleth.net/jira/browse/SSPCPP-187
[shibboleth/cpp-opensaml.git] / samlsign / samlsign.cpp
index d20f256..e0e2990 100644 (file)
@@ -1,6 +1,6 @@
 /*
- *  Copyright 2001-2007 Internet2
- * 
+ *  Copyright 2001-2009 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
@@ -48,7 +48,6 @@
 
 #include <fstream>
 #include <xercesc/framework/LocalFileInputSource.hpp>
-#include <xercesc/framework/URLInputSource.hpp>
 #include <xercesc/framework/StdInInputSource.hpp>
 #include <xercesc/framework/Wrapper4InputSource.hpp>
 
@@ -67,7 +66,7 @@ template<class T> T* buildPlugin(const char* path, PluginManager<T,string,const
     ifstream in(path);
     DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
     XercesJanitor<DOMDocument> janitor(doc);
-    
+
     static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
     auto_ptr_char type(doc->getDocumentElement()->getAttributeNS(NULL,_type));
     if (type.get() && *type.get())
@@ -101,10 +100,10 @@ class DummyCredentialResolver : public CredentialResolver
 public:
     DummyCredentialResolver() {}
     ~DummyCredentialResolver() {}
-    
+
     Lockable* lock() {return this;}
     void unlock() {}
-    
+
     const Credential* resolve(const CredentialCriteria* criteria=NULL) const {return NULL;}
     vector<const Credential*>::size_type resolve(
         vector<const Credential*>& results, const CredentialCriteria* criteria=NULL
@@ -182,26 +181,27 @@ int main(int argc,char* argv[])
         return -1;
     }
 
+    XMLToolingConfig& xmlconf = XMLToolingConfig::getConfig();
+    xmlconf.log_config();
     SAMLConfig& conf=SAMLConfig::getConfig();
     if (!conf.init())
         return -2;
-    XMLToolingConfig& xmlconf = XMLToolingConfig::getConfig();
     Category& log = Category::getInstance("OpenSAML.Utility.SAMLSign");
 
     int ret = 0;
 
     try {
         // Parse the specified document.
-        static XMLCh base[]={chLatin_f, chLatin_i, chLatin_l, chLatin_e, chColon, chForwardSlash, chForwardSlash, chForwardSlash, chNull};
         DOMDocument* doc=NULL;
         if (url_param) {
-            URLInputSource src(base,url_param);
+            auto_ptr_XMLCh wideurl(url_param);
+            URLInputSource src(wideurl.get());
             Wrapper4InputSource dsrc(&src,false);
             doc=xmlconf.getParser().parse(dsrc);
         }
         else if (path_param) {
             auto_ptr_XMLCh widenit(path_param);
-            LocalFileInputSource src(base,widenit.get());
+            LocalFileInputSource src(widenit.get());
             Wrapper4InputSource dsrc(&src,false);
             doc=xmlconf.getParser().parse(dsrc);
         }
@@ -210,7 +210,7 @@ int main(int argc,char* argv[])
             Wrapper4InputSource dsrc(&src,false);
             doc=xmlconf.getParser().parse(dsrc);
         }
-    
+
         // Unmarshall it.
         XercesJanitor<DOMDocument> jan(doc);
         auto_ptr<XMLObject> sourcewrapper(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
@@ -246,7 +246,7 @@ int main(int argc,char* argv[])
 
                 // Set up criteria.
                 CredentialCriteria cc;
-                cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
+                cc.setUsage(Credential::SIGNING_CREDENTIAL);
                 cc.setSignature(*(signable->getSignature()), CredentialCriteria::KEYINFO_EXTRACTION_KEY);
                 if (issuer)
                     cc.setPeerName(issuer);
@@ -264,7 +264,8 @@ int main(int argc,char* argv[])
                             good = true;
                             break;
                         }
-                        catch (exception&) {
+                        catch (exception& e) {
+                            log.info("error trying verification key: %s", e.what());
                         }
                     }
                     if (!good)
@@ -279,47 +280,48 @@ int main(int argc,char* argv[])
                 auto_ptr<TrustEngine> trust(buildPlugin(t_param, xmlconf.TrustEngineManager));
                 SignatureTrustEngine* sigtrust = dynamic_cast<SignatureTrustEngine*>(trust.get());
                 if (m_param && rname && issuer) {
-                    if (!protocol) {\r
-                        if (prot)\r
-                            protocol = XMLString::transcode(prot);\r
-                    }\r
-                    if (!protocol) {\r
-                        conf.term();\r
-                        cerr << "use of metadata option requires a protocol option" << endl;\r
-                        return -1;\r
-                    }\r
+                    if (!protocol) {
+                        if (prot)
+                            protocol = XMLString::transcode(prot);
+                    }
+                    if (!protocol) {
+                        conf.term();
+                        cerr << "use of metadata option requires a protocol option" << endl;
+                        return -1;
+                    }
                     auto_ptr<MetadataProvider> metadata(buildPlugin(m_param, conf.MetadataProviderManager));
                     metadata->init();
-                    
+
+                    const XMLCh* ns = rns ? XMLString::transcode(rns) : samlconstants::SAML20MD_NS;
+                    auto_ptr_XMLCh n(rname);
+                    xmltooling::QName q(ns, n.get());
+
                     Locker locker(metadata.get());
-                    const EntityDescriptor* entity = metadata->getEntityDescriptor(issuer);\r
-                    if (!entity)\r
-                        throw MetadataException("no metadata found for ($1)", params(1, issuer));\r
-                    const XMLCh* ns = rns ? XMLString::transcode(rns) : samlconstants::SAML20MD_NS;\r
-                    auto_ptr_XMLCh n(rname);\r
-                    QName q(ns, n.get());\r
-                    const RoleDescriptor* role = entity->getRoleDescriptor(q, protocol);\r
-                    if (!role)\r
-                        throw MetadataException("compatible role $1 not found for ($2)", params(2, q.toString().c_str(), issuer));\r
-\r
-                    MetadataCredentialCriteria mcc(*role);\r
-                    if (sigtrust->validate(*signable->getSignature(), *metadata.get(), &mcc))\r
+                    MetadataProvider::Criteria mc(issuer, &q, protocol);
+                    pair<const EntityDescriptor*,const RoleDescriptor*> entity = metadata->getEntityDescriptor(mc);
+                    if (!entity.first)
+                        throw MetadataException("no metadata found for ($1)", params(1, issuer));
+                    else if (!entity.second)
+                        throw MetadataException("compatible role $1 not found for ($2)", params(2, q.toString().c_str(), issuer));
+
+                    MetadataCredentialCriteria mcc(*entity.second);
+                    if (sigtrust->validate(*signable->getSignature(), *metadata.get(), &mcc))
                         log.info("successful signature verification");
-                    else\r
-                        throw SignatureException("Unable to verify signature with TrustEngine and supplied metadata.");\r
+                    else
+                        throw SignatureException("Unable to verify signature with TrustEngine and supplied metadata.");
                 }
                 else {
                     // Set up criteria.
                     CredentialCriteria cc;
-                    cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
+                    cc.setUsage(Credential::SIGNING_CREDENTIAL);
                     cc.setSignature(*(signable->getSignature()), CredentialCriteria::KEYINFO_EXTRACTION_KEY);
                     if (issuer)
                         cc.setPeerName(issuer);
                     DummyCredentialResolver dummy;
-                    if (sigtrust->validate(*signable->getSignature(), dummy, &cc))\r
+                    if (sigtrust->validate(*signable->getSignature(), dummy, &cc))
                         log.info("successful signature verification");
-                    else\r
-                        throw SignatureException("Unable to verify signature with TrustEngine (no metadata supplied).");\r
+                    else
+                        throw SignatureException("Unable to verify signature with TrustEngine (no metadata supplied).");
                 }
             }
         }
@@ -330,7 +332,7 @@ int main(int argc,char* argv[])
                 );
             Locker locker(cr.get());
             CredentialCriteria cc;
-            cc.setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
+            cc.setUsage(Credential::SIGNING_CREDENTIAL);
             const Credential* cred = cr->resolve(&cc);
             if (!cred)
                 throw XMLSecurityException("Unable to resolve a signing credential.");
@@ -345,14 +347,9 @@ int main(int argc,char* argv[])
         }
     }
     catch(exception& e) {
-        log.errorStream() << "caught an exception: " << e.what() << CategoryStream::ENDLINE;
+        log.errorStream() << "caught an exception: " << e.what() << logging::eol;
         ret=-10;
     }
-    catch(XMLException& e) {
-        auto_ptr_char temp(e.getMessage());
-        log.errorStream() << "caught a Xerces exception: " << temp.get() << CategoryStream::ENDLINE;
-        ret=-20;
-    }
 
     conf.term();
     return ret;