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 21 import org.opensaml.xml.security.credential.Credential; 22 import org.opensaml.xml.security.keyinfo.KeyInfoCredentialContext; 23 import org.opensaml.xml.security.keyinfo.KeyInfoProvider; 24 import org.opensaml.xml.security.keyinfo.KeyInfoResolutionContext; 25 26 /** 27 * Abstract super class for {@link KeyInfoProvider} implementations. 28 */ 29 public abstract class AbstractKeyInfoProvider implements KeyInfoProvider { 30 31 /** 32 * Utility method to extract any key that might be present in the specified Credential. 33 * 34 * @param cred the Credential to evaluate 35 * @return the Key contained in the credential, or null if it does not contain a key. 36 */ 37 protected Key extractKeyValue(Credential cred) { 38 if (cred == null) { 39 return null; 40 } 41 if (cred.getPublicKey() != null) { 42 return cred.getPublicKey(); 43 } 44 // This could happen if key is derived, e.g. key agreement, etc 45 if (cred.getSecretKey() != null) { 46 return cred.getSecretKey(); 47 } 48 // Perhaps unlikely, but go ahead and check 49 if (cred.getPrivateKey() != null) { 50 return cred.getPrivateKey(); 51 } 52 return null; 53 } 54 55 /** 56 * Build a credential context based on the current KeyInfo context, for return 57 * in a resolved credential. 58 * 59 * @param kiContext the current KeyInfo resolution context 60 * 61 * @return a new KeyInfo credential context 62 */ 63 protected KeyInfoCredentialContext buildCredentialContext(KeyInfoResolutionContext kiContext) { 64 // Simple for now, might do other stuff later. 65 // Just want to provide a single place to build credential contexts for 66 // a provider. 67 if (kiContext != null) { 68 return new KeyInfoCredentialContext(kiContext.getKeyInfo()); 69 } else { 70 return null; 71 } 72 } 73 74 }