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

The following examples show how to use org.opensaml.xml.security.credential.BasicCredential#setPrivateKey() . 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: SAMLClient.java    From saml-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Decrypt an assertion using the privkey stored in SPConfig.
 */
private Assertion decrypt(EncryptedAssertion encrypted)
    throws DecryptionException
{
    if (spConfig.getPrivateKey() == null)
        throw new DecryptionException("Encrypted assertion found but no SP key available");
    BasicCredential cred = new BasicCredential();
    cred.setPrivateKey(spConfig.getPrivateKey());
    StaticKeyInfoCredentialResolver resolver =
        new StaticKeyInfoCredentialResolver(cred);
    Decrypter decrypter =
        new Decrypter(null, resolver, new InlineEncryptedKeyResolver());
    decrypter.setRootInNewDocument(true);

    return decrypter.decrypt(encrypted);
}
 
Example 2
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 public key, and optionally a private key.
 * 
 * @param publicKey the public key to wrap
 * @param privateKey the private key to wrap, which may be null
 * @return a credential containing the key(s) specified
 */
public static BasicCredential getSimpleCredential(PublicKey publicKey, PrivateKey privateKey) {
    if (publicKey == null) {
        throw new IllegalArgumentException("A public key is required");
    }
    BasicCredential cred = new BasicCredential();
    cred.setPublicKey(publicKey);
    cred.setPrivateKey(privateKey);
    return cred;
}
 
Example 3
Source File: SecurityHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a random asymmetric key pair and return in a BasicCredential.
 * 
 * @param algorithmURI The XML Encryption algorithm URI
 * @param keyLength key length
 * @param includePrivate if true, the private key will be included as well
 * @return a basic credential containing a randomly generated asymmetric key pair
 * @throws NoSuchAlgorithmException algorithm not found
 * @throws NoSuchProviderException provider not found
 */
public static Credential generateKeyPairAndCredential(String algorithmURI, int keyLength, boolean includePrivate) 
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPair keyPair = generateKeyPairFromURI(algorithmURI, keyLength);
    BasicCredential credential = new BasicCredential();
    credential.setPublicKey(keyPair.getPublic());
    if (includePrivate) {
        credential.setPrivateKey(keyPair.getPrivate());
    }
    return credential;
}
 
Example 4
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;
}