1 /* 2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.] 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 package org.opensaml.xml.security.keyinfo.provider; 18 19 import java.security.Key; 20 import java.util.HashSet; 21 import java.util.Set; 22 23 import org.opensaml.xml.security.credential.Credential; 24 import org.opensaml.xml.security.keyinfo.KeyInfoCredentialContext; 25 import org.opensaml.xml.security.keyinfo.KeyInfoProvider; 26 import org.opensaml.xml.security.keyinfo.KeyInfoResolutionContext; 27 28 /** 29 * Abstract super class for {@link KeyInfoProvider} implementations. 30 */ 31 public abstract class AbstractKeyInfoProvider implements KeyInfoProvider { 32 33 /** 34 * Utility method to extract any key that might be present in the specified Credential. 35 * 36 * @param cred the Credential to evaluate 37 * @return the Key contained in the credential, or null if it does not contain a key. 38 */ 39 protected Key extractKeyValue(Credential cred) { 40 if (cred == null) { 41 return null; 42 } 43 if (cred.getPublicKey() != null) { 44 return cred.getPublicKey(); 45 } 46 // This could happen if key is derived, e.g. key agreement, etc 47 if (cred.getSecretKey() != null) { 48 return cred.getSecretKey(); 49 } 50 // Perhaps unlikely, but go ahead and check 51 if (cred.getPrivateKey() != null) { 52 return cred.getPrivateKey(); 53 } 54 return null; 55 } 56 57 /** 58 * Convenience method to create a credential set out of a single credential. 59 * 60 * @param credential the credential to return 61 * @return a set containing the supplied credential 62 */ 63 protected Set<Credential> singletonSet(Credential credential) { 64 HashSet<Credential> set = new HashSet<Credential>(1); 65 set.add(credential); 66 return set; 67 } 68 69 /** 70 * Build a credential context based on the current KeyInfo context, for return 71 * in a resolved credential. 72 * 73 * @param kiContext the current KeyInfo resolution context 74 * 75 * @return a new KeyInfo credential context 76 */ 77 protected KeyInfoCredentialContext buildCredentialContext(KeyInfoResolutionContext kiContext) { 78 // Simple for now, might do other stuff later. 79 // Just want to provide a single place to build credential contexts for 80 // a provider. 81 if (kiContext != null) { 82 return new KeyInfoCredentialContext(kiContext.getKeyInfo()); 83 } else { 84 return null; 85 } 86 } 87 88 }