Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / MetadataCredentialCriteria.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * MetadataCredentialCriteria.cpp
23  * 
24  * Metadata-based CredentialCriteria subclass.
25  */
26
27 #include "internal.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataCredentialContext.h"
30 #include "saml2/metadata/MetadataCredentialCriteria.h"
31
32 #include <xmltooling/security/Credential.h>
33
34 using namespace opensaml::saml2md;
35 using namespace xmltooling;
36
37 MetadataCredentialCriteria::MetadataCredentialCriteria(const RoleDescriptor& role) : m_role(role)
38 {
39     const EntityDescriptor* entity = dynamic_cast<const EntityDescriptor*>(role.getParent());
40     if (entity) {
41         auto_ptr_char name(entity->getEntityID());
42         setPeerName(name.get());
43     }
44 }
45
46 void MetadataCredentialCriteria::reset()
47 {
48     CredentialCriteria::reset();
49     const EntityDescriptor* entity = dynamic_cast<const EntityDescriptor*>(m_role.getParent());
50     if (entity) {
51         auto_ptr_char name(entity->getEntityID());
52         setPeerName(name.get());
53     }
54 }
55
56 bool MetadataCredentialCriteria::matches(const Credential& credential) const
57 {
58     const MetadataCredentialContext* context = dynamic_cast<const MetadataCredentialContext*>(credential.getCredentalContext());
59     if (context) {
60         // Check for a usage mismatch.
61         if ((getUsage() & (xmltooling::Credential::SIGNING_CREDENTIAL | xmltooling::Credential::TLS_CREDENTIAL)) &&
62                 XMLString::equals(context->getKeyDescriptor().getUse(),KeyDescriptor::KEYTYPE_ENCRYPTION))
63             return false;
64         else if ((getUsage() & xmltooling::Credential::ENCRYPTION_CREDENTIAL) &&
65                 XMLString::equals(context->getKeyDescriptor().getUse(),KeyDescriptor::KEYTYPE_SIGNING))
66             return false;
67     }
68     return CredentialCriteria::matches(credential);
69 }