Package org.opensaml.xmlsec.keyinfo
Class KeyInfoSupport
java.lang.Object
org.opensaml.xmlsec.keyinfo.KeyInfoSupport
Utility class for working with data inside a KeyInfo object.
Methods are provided for converting the representation stored in the XMLTooling KeyInfo to Java java.security native
types, and for storing these Java native types inside a KeyInfo.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.slf4j.Logger
Logger.private static CertificateFactory
Factory forX509Certificate
andX509CRL
creation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addCertificate
(KeyInfo keyInfo, X509Certificate cert) Converts a native JavaX509Certificate
into the corresponding XMLObject and stores it in aKeyInfo
in the firstX509Data
element.static void
static void
addDEREncodedPublicKey
(KeyInfo keyInfo, PublicKey pk) Converts a Java public key into the corresponding XMLObject and stores it in aKeyInfo
in a newDEREncodedKeyValue
element.static void
addKeyName
(KeyInfo keyInfo, String keyNameValue) Add a newKeyName
value to a KeyInfo.static void
addPublicKey
(KeyInfo keyInfo, PublicKey pk) static DHKeyValue
buildDHKeyValue
(DHPublicKey dhPubKey) Builds aDHKeyValue
XMLObject from the Java security DH public key type.static DSAKeyValue
buildDSAKeyValue
(DSAPublicKey dsaPubKey) Builds aDSAKeyValue
XMLObject from the Java security DSA public key type.static ECKeyValue
buildECKeyValue
(ECPublicKey ecPubKey) Builds anECKeyValue
XMLObject from the Java security EC public key type.protected static PublicKey
Generates a public key from the given key spec.static RSAKeyValue
buildRSAKeyValue
(RSAPublicKey rsaPubKey) Builds anRSAKeyValue
XMLObject from the Java security RSA public key type.static X509Certificate
Builds anX509Certificate
XMLObject from a native JavaX509Certificate
.static X509CRL
buildX509CRL
(X509CRL crl) static X509Digest
buildX509Digest
(X509Certificate javaCert, String algorithmURI) Build anX509Digest
containing the digest of the specified certificate.static X509IssuerSerial
buildX509IssuerSerial
(String issuerName, BigInteger serialNumber) Build anX509IssuerSerial
containing a given issuer name and serial number.static X509SKI
buildX509SKI
(X509Certificate javaCert) Build anX509SKI
containing the subject key identifier extension value contained within a certificate.static X509SubjectName
buildX509SubjectName
(String subjectName) Build anX509SubjectName
containing a given subject name.static final BigInteger
decodeBigIntegerFromCryptoBinary
(String base64Value) Decode a base64-encoded ds:CryptoBinary value to a native Java BigInteger type.static final String
Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value.static X509Certificate
getCertificate
(X509Certificate xmlCert) Convert anX509Certificate
into a native Java representation.static List<X509Certificate>
getCertificates
(KeyInfo keyInfo) Get a list of the JavaX509Certificate
within the given KeyInfo.static List<X509Certificate>
getCertificates
(X509Data x509Data) Get a list of the JavaX509Certificate
within the givenX509Data
.static X509CRL
Convert anX509CRL
into a native Java representation.static PublicKey
getDHKey
(DHKeyValue keyDescriptor) Builds a DH key from aDHKeyValue
element.static PublicKey
getDSAKey
(DSAKeyValue keyDescriptor) Builds an DSA key from aDSAKeyValue
element.static PublicKey
getDSAKey
(DSAKeyValue keyDescriptor, DSAParams dsaParams) Builds a DSA key from anDSAKeyValue
element and the supplied JavaDSAParams
, which supplies key material from a shared key family.static PublicKey
getECKey
(ECKeyValue keyDescriptor) Builds an EC key from anECKeyValue
element.static PublicKey
getKey
(DEREncodedKeyValue keyValue) Extracts the public key within theDEREncodedKeyValue
.static PublicKey
Extracts the DSA or RSA public key within theKeyValue
.static KeyInfoGenerator
getKeyInfoGenerator
(Credential credential, NamedKeyInfoGeneratorManager manager, String keyInfoProfileName) Obtains aKeyInfoGenerator
for the specifiedCredential
.getKeyNames
(KeyInfo keyInfo) Get the set of key names inside the specifiedKeyInfo
as a list of strings.getPublicKeys
(KeyInfo keyInfo) static PublicKey
getRSAKey
(RSAKeyValue keyDescriptor) Builds an RSA key from anRSAKeyValue
element.protected static CertificateFactory
Get the Java certificate factory singleton.static boolean
hasCompleteDHParams
(DHKeyValue keyDescriptor) Check whether the specifiedDHKeyValue
element has the all optional DH values which can be shared amongst many keys in a DH "key family", and are presumed to be known from context.static boolean
hasCompleteDSAParams
(DSAKeyValue keyDescriptor) Check whether the specifiedDSAKeyValue
element has the all optional DSA values which can be shared amongst many keys in a DSA "key family", and are presumed to be known from context.private static String
parseKeyType
(byte[] encodedKey) Parse the JCA key algorithm type from the ASN.1 encoded form of the public key.
-
Field Details
-
LOG
@Nonnull private static final org.slf4j.Logger LOGLogger. -
x509CertFactory
Factory forX509Certificate
andX509CRL
creation.
-
-
Constructor Details
-
KeyInfoSupport
protected KeyInfoSupport()Constructor.
-
-
Method Details
-
getKeyNames
Get the set of key names inside the specifiedKeyInfo
as a list of strings.- Parameters:
keyInfo
-KeyInfo
to retrieve key names from- Returns:
- a list of key name strings
-
addKeyName
Add a newKeyName
value to a KeyInfo.- Parameters:
keyInfo
- the KeyInfo to which to add the new valuekeyNameValue
- the new key name value to add
-
getCertificates
@Nonnull public static List<X509Certificate> getCertificates(@Nullable KeyInfo keyInfo) throws CertificateException Get a list of the JavaX509Certificate
within the given KeyInfo.- Parameters:
keyInfo
- key info to extract the certificates from- Returns:
- a list of Java
X509Certificate
s - Throws:
CertificateException
- thrown if there is a problem converting the X509 data intoX509Certificate
s.
-
getCertificates
@Nonnull public static List<X509Certificate> getCertificates(@Nullable X509Data x509Data) throws CertificateException Get a list of the JavaX509Certificate
within the givenX509Data
.- Parameters:
x509Data
-X509Data
from which to extract the certificate- Returns:
- a list of Java
X509Certificate
s - Throws:
CertificateException
- thrown if there is a problem converting the X509 data intoX509Certificate
s.
-
getCertificate
@Nullable public static X509Certificate getCertificate(@Nullable X509Certificate xmlCert) throws CertificateException Convert anX509Certificate
into a native Java representation.- Parameters:
xmlCert
- anX509Certificate
- Returns:
- a
X509Certificate
- Throws:
CertificateException
- thrown if there is a problem converting the X509 data intoX509Certificate
s.
-
getCRLs
- Parameters:
keyInfo
- theKeyInfo
to extract the CRLs from- Returns:
- a list of Java
X509CRL
s - Throws:
CRLException
- thrown if there is a problem converting the CRL data intoX509CRL
s
-
getCRLs
- Parameters:
x509Data
-X509Data
to extract the CRLs from- Returns:
- a list of Java
X509CRL
s - Throws:
CRLException
- thrown if there is a problem converting the CRL data intoX509CRL
s
-
getCRL
Convert anX509CRL
into a native Java representation.- Parameters:
xmlCRL
- object to extract the CRL from- Returns:
- a native Java
X509CRL
object - Throws:
CRLException
- thrown if there is a problem converting the CRL data intoX509CRL
-
addCertificate
public static void addCertificate(@Nonnull KeyInfo keyInfo, @Nonnull X509Certificate cert) throws CertificateEncodingException Converts a native JavaX509Certificate
into the corresponding XMLObject and stores it in aKeyInfo
in the firstX509Data
element. The X509Data element will be created if necessary.- Parameters:
keyInfo
- theKeyInfo
object into which to add the certificatecert
- the JavaX509Certificate
to add- Throws:
CertificateEncodingException
- thrown when there is an error converting the Java certificate representation to the XMLObject representation
-
addCRL
Converts a native JavaX509CRL
into the corresponding XMLObject and stores it in aKeyInfo
in the firstX509Data
element. The X509Data element will be created if necessary.- Parameters:
keyInfo
- theKeyInfo
object into which to add the CRLcrl
- the JavaX509CRL
to add- Throws:
CRLException
- thrown when there is an error converting the Java CRL representation to the XMLObject representation
-
buildX509Certificate
@Nonnull public static X509Certificate buildX509Certificate(X509Certificate cert) throws CertificateEncodingException Builds anX509Certificate
XMLObject from a native JavaX509Certificate
.- Parameters:
cert
- the JavaX509Certificate
to convert- Returns:
- a
X509Certificate
XMLObject - Throws:
CertificateEncodingException
- thrown when there is an error converting the Java certificate representation to the XMLObject representation
-
buildX509CRL
- Parameters:
crl
- the JavaX509CRL
to convert- Returns:
- a
X509CRL
XMLObject - Throws:
CRLException
- thrown when there is an error converting the Java CRL representation to the XMLObject representation
-
buildX509SubjectName
Build anX509SubjectName
containing a given subject name.- Parameters:
subjectName
- the name content- Returns:
- the new X509SubjectName
-
buildX509IssuerSerial
@Nonnull public static X509IssuerSerial buildX509IssuerSerial(@Nullable String issuerName, @Nullable BigInteger serialNumber) Build anX509IssuerSerial
containing a given issuer name and serial number.- Parameters:
issuerName
- the name contentserialNumber
- the serial number content- Returns:
- the new X509IssuerSerial
-
buildX509SKI
@Nullable public static X509SKI buildX509SKI(@Nonnull X509Certificate javaCert) throws SecurityException Build anX509SKI
containing the subject key identifier extension value contained within a certificate.- Parameters:
javaCert
- the Java X509Certificate from which to extract the subject key identifier value.- Returns:
- a new X509SKI object, or null if the certificate did not contain the subject key identifier extension, or the subject key identifier binary can not be base64-encoded.
- Throws:
SecurityException
- if there is a problem building the subject key identifier.
-
buildX509Digest
@Nonnull public static X509Digest buildX509Digest(@Nonnull X509Certificate javaCert, @Nonnull String algorithmURI) throws NoSuchAlgorithmException, CertificateEncodingException Build anX509Digest
containing the digest of the specified certificate.- Parameters:
javaCert
- the Java X509Certificate to digestalgorithmURI
- digest algorithm URI- Returns:
- a new X509Digest object
- Throws:
NoSuchAlgorithmException
- if the algorithm specified cannot be usedCertificateEncodingException
- if the certificate cannot be encoded
-
addPublicKey
public static void addPublicKey(@Nonnull KeyInfo keyInfo, @Nullable PublicKey pk) throws EncodingException Converts a Java RSA, EC, DSA or DH public key into the corresponding XMLObject and stores it in aKeyInfo
in a newKeyValue
element.As input, only supports
PublicKey
instances which are:- Parameters:
keyInfo
- theKeyInfo
element to which to add the keypk
- the native JavaPublicKey
to add- Throws:
EncodingException
- if base64 encoding the components of the public keypk
fails
-
buildDHKeyValue
@Nonnull public static DHKeyValue buildDHKeyValue(@Nonnull DHPublicKey dhPubKey) throws EncodingException Builds aDHKeyValue
XMLObject from the Java security DH public key type.- Parameters:
dhPubKey
- a native JavaDHPublicKey
- Returns:
- an
DHKeyValue
XMLObject - Throws:
EncodingException
- if the DH public key parameters can not be base64 encoded
-
buildECKeyValue
@Nonnull public static ECKeyValue buildECKeyValue(@Nonnull ECPublicKey ecPubKey) throws EncodingException Builds anECKeyValue
XMLObject from the Java security EC public key type.Only curve parameters specified by a
NamedCurve
are supported. Use of explicit curve parameters will throw.- Parameters:
ecPubKey
- a naive javaECPublicKey
- Returns:
- an
ECKeyValue
XMLObject - Throws:
EncodingException
- if the NamedCurve variant was not used, if the EC PublicKey value is invalid or if the EC PublicKey value can not be Base64 encoded
-
buildRSAKeyValue
@Nonnull public static RSAKeyValue buildRSAKeyValue(@Nonnull RSAPublicKey rsaPubKey) throws EncodingException Builds anRSAKeyValue
XMLObject from the Java security RSA public key type.- Parameters:
rsaPubKey
- a native JavaRSAPublicKey
- Returns:
- an
RSAKeyValue
XMLObject - Throws:
EncodingException
- if the RSA public key modulus/exponent can not be base64 encoded
-
buildDSAKeyValue
@Nonnull public static DSAKeyValue buildDSAKeyValue(@Nonnull DSAPublicKey dsaPubKey) throws EncodingException Builds aDSAKeyValue
XMLObject from the Java security DSA public key type.- Parameters:
dsaPubKey
- a native JavaDSAPublicKey
- Returns:
- an
DSAKeyValue
XMLObject - Throws:
EncodingException
- if the DSA public key parameters can not be base64 encoded
-
addDEREncodedPublicKey
public static void addDEREncodedPublicKey(@Nonnull KeyInfo keyInfo, @Nonnull PublicKey pk) throws NoSuchAlgorithmException, InvalidKeySpecException Converts a Java public key into the corresponding XMLObject and stores it in aKeyInfo
in a newDEREncodedKeyValue
element.- Parameters:
keyInfo
- theKeyInfo
element to which to add the keypk
- the native JavaPublicKey
to convert- Throws:
NoSuchAlgorithmException
- if the key type is unsupportedInvalidKeySpecException
- if the key type does not support X.509 SPKI encoding
-
getPublicKeys
- Parameters:
keyInfo
-KeyInfo
to extract the keys out of- Returns:
- a list of native Java
PublicKey
objects - Throws:
KeyException
- thrown if the given key data can not be converted intoPublicKey
-
getKey
Extracts the DSA or RSA public key within theKeyValue
.- Parameters:
keyValue
- theKeyValue
to extract the key from- Returns:
- a native Java security
Key
object - Throws:
KeyException
- thrown if the given key data can not be converted intoPublicKey
-
getECKey
Builds an EC key from anECKeyValue
element.- Parameters:
keyDescriptor
- theECKeyValue
key descriptor- Returns:
- a new
ECPublicKey
instance ofPublicKey
- Throws:
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not contain valid information
-
getDHKey
Builds a DH key from aDHKeyValue
element. The element must contain values for all required DH public key parameters, including values for shared key family values P, Q and G (aka Generator).- Parameters:
keyDescriptor
- theDHKeyValue
key descriptor- Returns:
- a new
DHPublicKey
instance ofPublicKey
- Throws:
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not contain valid information
-
hasCompleteDHParams
Check whether the specifiedDHKeyValue
element has the all optional DH values which can be shared amongst many keys in a DH "key family", and are presumed to be known from context.- Parameters:
keyDescriptor
- theDHKeyValue
element to check- Returns:
- true if all parameters are present and non-empty, false otherwise
-
getDSAKey
Builds an DSA key from aDSAKeyValue
element. The element must contain values for all required DSA public key parameters, including values for shared key family values P, Q and G.- Parameters:
keyDescriptor
- theDSAKeyValue
key descriptor- Returns:
- a new
DSAPublicKey
instance ofPublicKey
- Throws:
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not contain valid information
-
getDSAKey
@Nonnull public static PublicKey getDSAKey(@Nonnull DSAKeyValue keyDescriptor, @Nonnull DSAParams dsaParams) throws KeyException Builds a DSA key from anDSAKeyValue
element and the supplied JavaDSAParams
, which supplies key material from a shared key family.- Parameters:
keyDescriptor
- theDSAKeyValue
key descriptordsaParams
- theDSAParams
DSA key family parameters- Returns:
- a new
DSAPublicKey
instance ofPublicKey
- Throws:
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not contain valid information
-
hasCompleteDSAParams
Check whether the specifiedDSAKeyValue
element has the all optional DSA values which can be shared amongst many keys in a DSA "key family", and are presumed to be known from context.- Parameters:
keyDescriptor
- theDSAKeyValue
element to check- Returns:
- true if all parameters are present and non-empty, false otherwise
-
getRSAKey
Builds an RSA key from anRSAKeyValue
element.- Parameters:
keyDescriptor
- theRSAKeyValue
key descriptor- Returns:
- a new
RSAPublicKey
instance ofPublicKey
- Throws:
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not contain valid information
-
decodeBigIntegerFromCryptoBinary
@Nonnull public static final BigInteger decodeBigIntegerFromCryptoBinary(@Nonnull String base64Value) throws DecodingException Decode a base64-encoded ds:CryptoBinary value to a native Java BigInteger type.- Parameters:
base64Value
- base64-encoded CryptoBinary value- Returns:
- the decoded BigInteger
- Throws:
DecodingException
- if the base64 value can not be decoded.
-
encodeCryptoBinaryFromBigInteger
@Nonnull public static final String encodeCryptoBinaryFromBigInteger(@Nonnull BigInteger bigInt) throws EncodingException Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value.- Parameters:
bigInt
- the BigInteger value- Returns:
- the encoded CryptoBinary value
- Throws:
EncodingException
- if the BigInteger as bytes can not be base64 encoded.
-
buildKey
@Nonnull protected static PublicKey buildKey(@Nonnull KeySpec keySpec, @Nonnull String keyAlgorithm) throws KeyException Generates a public key from the given key spec.- Parameters:
keySpec
-KeySpec
specification for the keykeyAlgorithm
- key generation algorithm, only DSA and RSA supported- Returns:
- the generated
PublicKey
- Throws:
KeyException
- thrown if the key algorithm is not supported by the JCE or the key spec does not contain valid information
-
getKey
Extracts the public key within theDEREncodedKeyValue
.- Parameters:
keyValue
- theDEREncodedKeyValue
to extract the key from- Returns:
- a native Java security
Key
object - Throws:
KeyException
- thrown if the given key data can not be converted intoPublicKey
-
parseKeyType
Parse the JCA key algorithm type from the ASN.1 encoded form of the public key.Methodology is to parse the ASN.1 data to the
SubjectPublicKeyInfo
, read theAlgorithmIdentifier
for the key type's OID, then map the OID to the JCA key algorithm.- Parameters:
encodedKey
- the ASN.1 encoded key- Returns:
- the JCA key algorithm, or null if the OID parsing or OID-to-algorithm mapping fails
-
getX509CertFactory
Get the Java certificate factory singleton.- Returns:
CertificateFactory
the factory used to create X509 certificate objects- Throws:
CertificateException
- thrown if the factory can not be created
-
getKeyInfoGenerator
@Nullable public static KeyInfoGenerator getKeyInfoGenerator(@Nonnull Credential credential, @Nonnull NamedKeyInfoGeneratorManager manager, @Nullable String keyInfoProfileName) Obtains aKeyInfoGenerator
for the specifiedCredential
.The KeyInfoGenerator returned is resolved via the supplied
NamedKeyInfoGeneratorManager
and is determined by the type of the signing credential and an optional KeyInfo generator profile configuration name. If the latter is ommited, the default manager (NamedKeyInfoGeneratorManager.getDefaultManager()
) of the security configuration's named generator manager will be used.- Parameters:
credential
- the credential for which a generator is desiredmanager
- the NamedKeyInfoGeneratorManager instance to usekeyInfoProfileName
- the named KeyInfoGeneratorManager configuration to use (may be null)- Returns:
- a KeyInfoGenerator appropriate for the specified credential
-