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.LoggerLogger.private static CertificateFactoryFactory forX509CertificateandX509CRLcreation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddCertificate(KeyInfo keyInfo, X509Certificate cert) Converts a native JavaX509Certificateinto the corresponding XMLObject and stores it in aKeyInfoin the firstX509Dataelement.static voidstatic voidaddDEREncodedPublicKey(KeyInfo keyInfo, PublicKey pk) Converts a Java public key into the corresponding XMLObject and stores it in aKeyInfoin a newDEREncodedKeyValueelement.static voidaddKeyName(KeyInfo keyInfo, String keyNameValue) Add a newKeyNamevalue to a KeyInfo.static voidaddPublicKey(KeyInfo keyInfo, PublicKey pk) static DHKeyValuebuildDHKeyValue(DHPublicKey dhPubKey) Builds aDHKeyValueXMLObject from the Java security DH public key type.static DSAKeyValuebuildDSAKeyValue(DSAPublicKey dsaPubKey) Builds aDSAKeyValueXMLObject from the Java security DSA public key type.static ECKeyValuebuildECKeyValue(ECPublicKey ecPubKey) Builds anECKeyValueXMLObject from the Java security EC public key type.protected static PublicKeyGenerates a public key from the given key spec.static RSAKeyValuebuildRSAKeyValue(RSAPublicKey rsaPubKey) Builds anRSAKeyValueXMLObject from the Java security RSA public key type.static X509CertificateBuilds anX509CertificateXMLObject from a native JavaX509Certificate.static X509CRLbuildX509CRL(X509CRL crl) static X509DigestbuildX509Digest(X509Certificate javaCert, String algorithmURI) Build anX509Digestcontaining the digest of the specified certificate.static X509IssuerSerialbuildX509IssuerSerial(String issuerName, BigInteger serialNumber) Build anX509IssuerSerialcontaining a given issuer name and serial number.static X509SKIbuildX509SKI(X509Certificate javaCert) Build anX509SKIcontaining the subject key identifier extension value contained within a certificate.static X509SubjectNamebuildX509SubjectName(String subjectName) Build anX509SubjectNamecontaining a given subject name.static final BigIntegerdecodeBigIntegerFromCryptoBinary(String base64Value) Decode a base64-encoded ds:CryptoBinary value to a native Java BigInteger type.static final StringEncode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value.static X509CertificategetCertificate(X509Certificate xmlCert) Convert anX509Certificateinto a native Java representation.static List<X509Certificate>getCertificates(KeyInfo keyInfo) Get a list of the JavaX509Certificatewithin the given KeyInfo.static List<X509Certificate>getCertificates(X509Data x509Data) Get a list of the JavaX509Certificatewithin the givenX509Data.static X509CRLConvert anX509CRLinto a native Java representation.static PublicKeygetDHKey(DHKeyValue keyDescriptor) Builds a DH key from aDHKeyValueelement.static PublicKeygetDSAKey(DSAKeyValue keyDescriptor) Builds an DSA key from aDSAKeyValueelement.static PublicKeygetDSAKey(DSAKeyValue keyDescriptor, DSAParams dsaParams) Builds a DSA key from anDSAKeyValueelement and the supplied JavaDSAParams, which supplies key material from a shared key family.static PublicKeygetECKey(ECKeyValue keyDescriptor) Builds an EC key from anECKeyValueelement.static PublicKeygetKey(DEREncodedKeyValue keyValue) Extracts the public key within theDEREncodedKeyValue.static PublicKeyExtracts the DSA or RSA public key within theKeyValue.static KeyInfoGeneratorgetKeyInfoGenerator(Credential credential, NamedKeyInfoGeneratorManager manager, String keyInfoProfileName) Obtains aKeyInfoGeneratorfor the specifiedCredential.getKeyNames(KeyInfo keyInfo) Get the set of key names inside the specifiedKeyInfoas a list of strings.getPublicKeys(KeyInfo keyInfo) static PublicKeygetRSAKey(RSAKeyValue keyDescriptor) Builds an RSA key from anRSAKeyValueelement.protected static CertificateFactoryGet the Java certificate factory singleton.static booleanhasCompleteDHParams(DHKeyValue keyDescriptor) Check whether the specifiedDHKeyValueelement 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 booleanhasCompleteDSAParams(DSAKeyValue keyDescriptor) Check whether the specifiedDSAKeyValueelement 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 StringparseKeyType(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 forX509CertificateandX509CRLcreation.
-
-
Constructor Details
-
KeyInfoSupport
private KeyInfoSupport()Constructor.
-
-
Method Details
-
getKeyNames
Get the set of key names inside the specifiedKeyInfoas a list of strings.- Parameters:
keyInfo-KeyInfoto retrieve key names from- Returns:
- a list of key name strings
-
addKeyName
Add a newKeyNamevalue to a KeyInfo.- Parameters:
keyInfo- the KeyInfo to which to add the new valuekeyNameValue- the new key name value to add
-
getCertificates
@Nonnull @Unmodifiable @NotLive public static List<X509Certificate> getCertificates(@Nullable KeyInfo keyInfo) throws CertificateException Get a list of the JavaX509Certificatewithin the given KeyInfo.- Parameters:
keyInfo- key info to extract the certificates from- Returns:
- a list of Java
X509Certificates - Throws:
CertificateException- thrown if there is a problem converting the X509 data intoX509Certificates.
-
getCertificates
@Nonnull @Unmodifiable @NotLive public static List<X509Certificate> getCertificates(@Nullable X509Data x509Data) throws CertificateException Get a list of the JavaX509Certificatewithin the givenX509Data.- Parameters:
x509Data-X509Datafrom which to extract the certificate- Returns:
- a list of Java
X509Certificates - Throws:
CertificateException- thrown if there is a problem converting the X509 data intoX509Certificates.
-
getCertificate
@Nullable public static X509Certificate getCertificate(@Nullable X509Certificate xmlCert) throws CertificateException Convert anX509Certificateinto a native Java representation.- Parameters:
xmlCert- anX509Certificate- Returns:
- a
X509Certificate - Throws:
CertificateException- thrown if there is a problem converting the X509 data intoX509Certificates.
-
getCRLs
@Nonnull @Unmodifiable @NotLive public static List<X509CRL> getCRLs(@Nullable KeyInfo keyInfo) throws CRLException - Parameters:
keyInfo- theKeyInfoto extract the CRLs from- Returns:
- a list of Java
X509CRLs - Throws:
CRLException- thrown if there is a problem converting the CRL data intoX509CRLs
-
getCRLs
@Nonnull @Unmodifiable @NotLive public static List<X509CRL> getCRLs(@Nullable X509Data x509Data) throws CRLException - Parameters:
x509Data-X509Datato extract the CRLs from- Returns:
- a list of Java
X509CRLs - Throws:
CRLException- thrown if there is a problem converting the CRL data intoX509CRLs
-
getCRL
Convert anX509CRLinto a native Java representation.- Parameters:
xmlCRL- object to extract the CRL from- Returns:
- a native Java
X509CRLobject - 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 JavaX509Certificateinto the corresponding XMLObject and stores it in aKeyInfoin the firstX509Dataelement. The X509Data element will be created if necessary.- Parameters:
keyInfo- theKeyInfoobject into which to add the certificatecert- the JavaX509Certificateto add- Throws:
CertificateEncodingException- thrown when there is an error converting the Java certificate representation to the XMLObject representation
-
addCRL
Converts a native JavaX509CRLinto the corresponding XMLObject and stores it in aKeyInfoin the firstX509Dataelement. The X509Data element will be created if necessary.- Parameters:
keyInfo- theKeyInfoobject into which to add the CRLcrl- the JavaX509CRLto 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 anX509CertificateXMLObject from a native JavaX509Certificate.- Parameters:
cert- the JavaX509Certificateto convert- Returns:
- a
X509CertificateXMLObject - Throws:
CertificateEncodingException- thrown when there is an error converting the Java certificate representation to the XMLObject representation
-
buildX509CRL
- Parameters:
crl- the JavaX509CRLto convert- Returns:
- a
X509CRLXMLObject - Throws:
CRLException- thrown when there is an error converting the Java CRL representation to the XMLObject representation
-
buildX509SubjectName
Build anX509SubjectNamecontaining 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 anX509IssuerSerialcontaining 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 anX509SKIcontaining 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 anX509Digestcontaining 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, @Nonnull PublicKey pk) throws EncodingException Converts a Java RSA, EC, DSA or DH public key into the corresponding XMLObject and stores it in aKeyInfoin a newKeyValueelement.As input, only supports
PublicKeyinstances which are:- Parameters:
keyInfo- theKeyInfoelement to which to add the keypk- the native JavaPublicKeyto add- Throws:
EncodingException- if base64 encoding the components of the public keypkfails
-
buildDHKeyValue
@Nonnull public static DHKeyValue buildDHKeyValue(@Nonnull DHPublicKey dhPubKey) throws EncodingException Builds aDHKeyValueXMLObject from the Java security DH public key type.- Parameters:
dhPubKey- a native JavaDHPublicKey- Returns:
- an
DHKeyValueXMLObject - 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 anECKeyValueXMLObject from the Java security EC public key type.Only curve parameters specified by a
NamedCurveare supported. Use of explicit curve parameters will throw.- Parameters:
ecPubKey- a naive javaECPublicKey- Returns:
- an
ECKeyValueXMLObject - 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 anRSAKeyValueXMLObject from the Java security RSA public key type.- Parameters:
rsaPubKey- a native JavaRSAPublicKey- Returns:
- an
RSAKeyValueXMLObject - 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 aDSAKeyValueXMLObject from the Java security DSA public key type.- Parameters:
dsaPubKey- a native JavaDSAPublicKey- Returns:
- an
DSAKeyValueXMLObject - 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 aKeyInfoin a newDEREncodedKeyValueelement.- Parameters:
keyInfo- theKeyInfoelement to which to add the keypk- the native JavaPublicKeyto convert- Throws:
NoSuchAlgorithmException- if the key type is unsupportedInvalidKeySpecException- if the key type does not support X.509 SPKI encoding
-
getPublicKeys
- Parameters:
keyInfo-KeyInfoto extract the keys out of- Returns:
- a list of native Java
PublicKeyobjects - 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- theKeyValueto extract the key from- Returns:
- a native Java security
Keyobject - Throws:
KeyException- thrown if the given key data can not be converted intoPublicKey
-
getECKey
Builds an EC key from anECKeyValueelement.- Parameters:
keyDescriptor- theECKeyValuekey descriptor- Returns:
- a new
ECPublicKeyinstance 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 aDHKeyValueelement. 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- theDHKeyValuekey descriptor- Returns:
- a new
DHPublicKeyinstance 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 specifiedDHKeyValueelement 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- theDHKeyValueelement to check- Returns:
- true if all parameters are present and non-empty, false otherwise
-
getDSAKey
Builds an DSA key from aDSAKeyValueelement. 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- theDSAKeyValuekey descriptor- Returns:
- a new
DSAPublicKeyinstance 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 anDSAKeyValueelement and the supplied JavaDSAParams, which supplies key material from a shared key family.- Parameters:
keyDescriptor- theDSAKeyValuekey descriptordsaParams- theDSAParamsDSA key family parameters- Returns:
- a new
DSAPublicKeyinstance 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 specifiedDSAKeyValueelement 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- theDSAKeyValueelement to check- Returns:
- true if all parameters are present and non-empty, false otherwise
-
getRSAKey
Builds an RSA key from anRSAKeyValueelement.- Parameters:
keyDescriptor- theRSAKeyValuekey descriptor- Returns:
- a new
RSAPublicKeyinstance 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 @NotEmpty 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-KeySpecspecification 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- theDEREncodedKeyValueto extract the key from- Returns:
- a native Java security
Keyobject - 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 theAlgorithmIdentifierfor 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:
CertificateFactorythe 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 aKeyInfoGeneratorfor the specifiedCredential.The KeyInfoGenerator returned is resolved via the supplied
NamedKeyInfoGeneratorManagerand 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
-