/** * Build Java private key from base64 encoding. The key should have no password. * * @param base64EncodedKey base64-encoded private key * @return a native Java PrivateKey * @throws KeyException thrown if there is an error constructing key */ public static PrivateKey buildJavaPrivateKey(String base64EncodedKey) throws KeyException { return SecurityHelper.decodePrivateKey(Base64.decode(base64EncodedKey), null); }
/** * Build Java private key from base64 encoding. The key should have no password. * * @param base64EncodedKey base64-encoded private key * @return a native Java PrivateKey * @throws KeyException thrown if there is an error constructing key */ public static PrivateKey buildJavaPrivateKey(String base64EncodedKey) throws KeyException { return SecurityHelper.decodePrivateKey(Base64.decode(base64EncodedKey), null); }
/** * Parses the private key from the credential configuration. * * @param configChildren children of the credential element * @param builder credential build */ protected void parsePrivateKey(Map<QName, List<Element>> configChildren, BeanDefinitionBuilder builder) { List<Element> keyElems = configChildren.get(new QName(SecurityNamespaceHandler.NAMESPACE, "PrivateKey")); if (keyElems == null || keyElems.isEmpty()) { return; } log.debug("Parsing credential private key"); Element privKeyElem = keyElems.get(0); byte[] encodedKey = getEncodedPrivateKey(DatatypeHelper.safeTrimOrNullString(privKeyElem.getTextContent())); String keyPassword = DatatypeHelper.safeTrimOrNullString(privKeyElem.getAttributeNS(null, "password")); char[] keyPasswordCharArray = null; if (keyPassword != null) { keyPasswordCharArray = keyPassword.toCharArray(); } try { PrivateKey privKey = SecurityHelper.decodePrivateKey(encodedKey, keyPasswordCharArray); builder.addPropertyValue("privateKey", privKey); } catch (KeyException e) { throw new FatalBeanException("Unable to create credential, unable to parse private key", e); } }
/** * Decodes RSA/DSA private keys in DER, PEM, or PKCS#8 (encrypted or unencrypted) formats. * * @param key encoded key * @param password decryption password or null if the key is not encrypted * * @return deocded private key * * @throws KeyException thrown if the key can not be decoded */ public static PrivateKey decodePrivateKey(File key, char[] password) throws KeyException { if (!key.exists()) { throw new KeyException("Key file " + key.getAbsolutePath() + " does not exist"); } if (!key.canRead()) { throw new KeyException("Key file " + key.getAbsolutePath() + " is not readable"); } try { return decodePrivateKey(DatatypeHelper.fileToByteArray(key), password); } catch (IOException e) { throw new KeyException("Error reading Key file " + key.getAbsolutePath(), e); } }
/** * Decodes RSA/DSA private keys in DER, PEM, or PKCS#8 (encrypted or unencrypted) formats. * * @param key encoded key * @param password decryption password or null if the key is not encrypted * * @return deocded private key * * @throws KeyException thrown if the key can not be decoded */ public static PrivateKey decodePrivateKey(File key, char[] password) throws KeyException { if (!key.exists()) { throw new KeyException("Key file " + key.getAbsolutePath() + " does not exist"); } if (!key.canRead()) { throw new KeyException("Key file " + key.getAbsolutePath() + " is not readable"); } try { return decodePrivateKey(DatatypeHelper.fileToByteArray(key), password); } catch (IOException e) { throw new KeyException("Error reading Key file " + key.getAbsolutePath(), e); } }