Java Code Examples for org.opensaml.xml.security.credential.BasicCredential#setSecretKey()

The following examples show how to use org.opensaml.xml.security.credential.BasicCredential#setSecretKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecurityHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a simple, minimal credential containing a secret (symmetric) key.
 * 
 * @param secretKey the symmetric key to wrap
 * @return a credential containing the secret key specified
 */
public static BasicCredential getSimpleCredential(SecretKey secretKey) {
    if (secretKey == null) {
        throw new IllegalArgumentException("A secret key is required");
    }
    BasicCredential cred = new BasicCredential();
    cred.setSecretKey(secretKey);
    return cred;
}
 
Example 2
Source File: BasicProviderKeyInfoCredentialResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a basic credential containing the specified key and set of key names.
 * 
 * @param key the key to include in the credential
 * @param keyNames the key names to include in the credential
 * @return a basic credential with the specified key and key names
 * @throws SecurityException if there is an error building the credential
 */
protected Credential buildBasicCredential(Key key, Set<String> keyNames) throws SecurityException {
    if (key == null) {
        log.debug("Key supplied was null, could not build credential");
        return null;
    }

    BasicCredential basicCred = new BasicCredential();

    basicCred.getKeyNames().addAll(keyNames);

    if (key instanceof PublicKey) {
        basicCred.setPublicKey((PublicKey) key);
    } else if (key instanceof SecretKey) {
        basicCred.setSecretKey((SecretKey) key);
    } else if (key instanceof PrivateKey) {
        // This would be unusual for most KeyInfo use cases,
        // but go ahead and try and handle it
        PrivateKey privateKey = (PrivateKey) key;
        try {
            PublicKey publicKey = SecurityHelper.derivePublicKey(privateKey);
            if (publicKey != null) {
                basicCred.setPublicKey(publicKey);
                basicCred.setPrivateKey(privateKey);
            } else {
                log.error("Failed to derive public key from private key");
                return null;
            }
        } catch (KeyException e) {
            log.error("Could not derive public key from private key", e);
            return null;
        }
    } else {
        log.error(String.format("Key was of an unsupported type '%s'", key.getClass().getName()));
        return null;
    }

    return basicCred;
}
 
Example 3
Source File: SecurityHelper.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Generate a random symmetric key and return in a BasicCredential.
 * 
 * @param algorithmURI The XML Encryption algorithm URI
 * @return a basic credential containing a randomly generated symmetric key
 * @throws NoSuchAlgorithmException algorithm not found
 * @throws NoSuchProviderException provider not found
 */
public static Credential generateKeyAndCredential(String algorithmURI) 
        throws NoSuchAlgorithmException, NoSuchProviderException {
    SecretKey key = generateKeyFromURI(algorithmURI);
    BasicCredential credential = new BasicCredential();
    credential.setSecretKey(key);
    return credential;
}