/** * Get the key length in bits of the specified key. * * @param key the key to evaluate * @return length of the key in bits, or null if the length can not be determined */ public static Integer getKeyLength(Key key) { Logger log = getLogger(); // TODO investigate techniques (and use cases) to determine length in other cases, // e.g. RSA and DSA keys, and non-RAW format symmetric keys if (key instanceof SecretKey && "RAW".equals(key.getFormat())) { return key.getEncoded().length * 8; } log.debug("Unable to determine length in bits of specified Key instance"); return null; }
/** * Get the key length in bits of the specified key. * * @param key the key to evaluate * @return length of the key in bits, or null if the length can not be determined */ public static Integer getKeyLength(Key key) { Logger log = getLogger(); // TODO investigate techniques (and use cases) to determine length in other cases, // e.g. RSA and DSA keys, and non-RAW format symmetric keys if (key instanceof SecretKey && "RAW".equals(key.getFormat())) { return key.getEncoded().length * 8; } log.debug("Unable to determine length in bits of specified Key instance"); return null; }
/** * Get the length of the key indicated by the algorithm URI, if applicable and available. * * @param algorithmURI the algorithm URI to evaluate * @return the length of the key indicated by the algorithm URI, or null if the length is either unavailable or * indeterminable from the URI */ public static Integer getKeyLengthFromURI(String algorithmURI) { Logger log = getLogger(); String algoClass = DatatypeHelper.safeTrimOrNullString(JCEMapper.getAlgorithmClassFromURI(algorithmURI)); if (ApacheXMLSecurityConstants.ALGO_CLASS_BLOCK_ENCRYPTION.equals(algoClass) || ApacheXMLSecurityConstants.ALGO_CLASS_SYMMETRIC_KEY_WRAP.equals(algoClass)) { try { int keyLength = JCEMapper.getKeyLengthFromURI(algorithmURI); return new Integer(keyLength); } catch (NumberFormatException e) { log.warn("XML Security config contained invalid key length value for algorithm URI: " + algorithmURI); } } log.info("Mapping from algorithm URI {} to key length not available", algorithmURI); return null; }
/** * Get the length of the key indicated by the algorithm URI, if applicable and available. * * @param algorithmURI the algorithm URI to evaluate * @return the length of the key indicated by the algorithm URI, or null if the length is either unavailable or * indeterminable from the URI */ public static Integer getKeyLengthFromURI(String algorithmURI) { Logger log = getLogger(); String algoClass = DatatypeHelper.safeTrimOrNullString(JCEMapper.getAlgorithmClassFromURI(algorithmURI)); if (ApacheXMLSecurityConstants.ALGO_CLASS_BLOCK_ENCRYPTION.equals(algoClass) || ApacheXMLSecurityConstants.ALGO_CLASS_SYMMETRIC_KEY_WRAP.equals(algoClass)) { try { int keyLength = JCEMapper.getKeyLengthFromURI(algorithmURI); return new Integer(keyLength); } catch (NumberFormatException e) { log.warn("XML Security config contained invalid key length value for algorithm URI: " + algorithmURI); } } log.info("Mapping from algorithm URI {} to key length not available", algorithmURI); return null; }
/** * Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI. * * @param algoURI The XML Encryption algorithm URI * @return a randomly-generated symmetric Key * @throws NoSuchAlgorithmException thrown if the specified algorithm is invalid * @throws KeyException thrown if the length of the key to generate could not be determined */ public static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException { Logger log = getLogger(); String jceAlgorithmName = getKeyAlgorithmFromURI(algoURI); if (DatatypeHelper.isEmpty(jceAlgorithmName)) { log.error("Mapping from algorithm URI '" + algoURI + "' to key algorithm not available, key generation failed"); throw new NoSuchAlgorithmException("Algorithm URI'" + algoURI + "' is invalid for key generation"); } Integer keyLength = getKeyLengthFromURI(algoURI); if (keyLength == null) { log.error("Key length could not be determined from algorithm URI, can't generate key"); throw new KeyException("Key length not determinable from algorithm URI, could not generate new key"); } KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName); keyGenerator.init(keyLength); return keyGenerator.generateKey(); }
/** * Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI. * * @param algoURI The XML Encryption algorithm URI * @return a randomly-generated symmetric Key * @throws NoSuchAlgorithmException thrown if the specified algorithm is invalid * @throws KeyException thrown if the length of the key to generate could not be determined */ public static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException { Logger log = getLogger(); String jceAlgorithmName = getKeyAlgorithmFromURI(algoURI); if (DatatypeHelper.isEmpty(jceAlgorithmName)) { log.error("Mapping from algorithm URI '" + algoURI + "' to key algorithm not available, key generation failed"); throw new NoSuchAlgorithmException("Algorithm URI'" + algoURI + "' is invalid for key generation"); } Integer keyLength = getKeyLengthFromURI(algoURI); if (keyLength == null) { log.error("Key length could not be determined from algorithm URI, can't generate key"); throw new KeyException("Key length not determinable from algorithm URI, could not generate new key"); } KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName); keyGenerator.init(keyLength); return keyGenerator.generateKey(); }
Logger log = getLogger();
Logger log = getLogger();
Logger log = getLogger();
Logger log = getLogger();
String wrappedKeyAlgorithm, SecurityConfiguration config, String keyInfoGenName, String recipient) throws SecurityException { Logger log = getLogger();
String wrappedKeyAlgorithm, SecurityConfiguration config, String keyInfoGenName, String recipient) throws SecurityException { Logger log = getLogger();
Logger log = getLogger();
Logger log = getLogger();