public final class Util extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
ASSERTION_SIGNATURE_XPATH |
static String |
RESPONSE_SIGNATURE_XPATH |
static String |
UNIQUE_ID_PREFIX |
| Modifier and Type | Method and Description |
|---|---|
static String |
addSign(Document document,
PrivateKey key,
X509Certificate certificate,
String signAlgorithm)
Signs the Document using the specified signature algorithm with the private key and the public certificate.
|
static String |
addSign(Document document,
PrivateKey key,
X509Certificate certificate,
String signAlgorithm,
String digestAlgorithm)
Signs the Document using the specified signature algorithm with the private key and the public certificate.
|
static String |
addSign(Node node,
PrivateKey key,
X509Certificate certificate,
String signAlgorithm)
Signs a Node using the specified signature algorithm with the private key and the public certificate.
|
static String |
addSign(Node node,
PrivateKey key,
X509Certificate certificate,
String signAlgorithm,
String digestAlgorithm)
Signs a Node using the specified signature algorithm with the private key and the public certificate.
|
static String |
base64decodedInflated(String input)
Returns String Base64 decoded and inflated
|
static byte[] |
base64decoder(byte[] input)
Returns String base64 decoded
|
static byte[] |
base64decoder(String input)
Returns String base64 decoded
|
static String |
base64encoder(byte[] input)
Returns String base64 encoded
|
static String |
base64encoder(String input)
Returns String base64 encoded
|
static String |
calculateX509Fingerprint(X509Certificate x509cert)
Calculates the SHA-1 fingerprint of a x509cert
|
static String |
calculateX509Fingerprint(X509Certificate x509cert,
String alg)
Calculates the fingerprint of a x509cert
|
static String |
convertDocumentToString(Document doc)
Converts an XML in Document format in a String without applying the c14n transformation
|
static String |
convertDocumentToString(Document doc,
Boolean c14n)
Converts an XML in Document format in a String
|
static Document |
convertStringToDocument(String xmlStr)
Converts an XML in string format in a Document object
|
static String |
convertToPem(X509Certificate certificate)
Converts an X509Certificate in a well formated PEM string
|
static Document |
copyDocument(Document source)
Clone a Document object.
|
static void |
decryptElement(Element encryptedDataElement,
PrivateKey inputKey)
Decrypt an encrypted element.
|
static String |
deflatedBase64encoded(String input)
Returns String Deflated and base64 encoded
|
static String |
formatCert(String cert,
Boolean heads)
Returns a certificate in String format (adding header and footer if required)
|
static String |
formatDateTime(long timeInMillis)
Create string form time In Millis with format yyyy-MM-ddTHH:mm:ssZ
|
static String |
formatDateTime(long time,
boolean millis)
Create string form time In Millis with format yyyy-MM-ddTHH:mm:ssZ
|
static String |
formatPrivateKey(String key,
boolean heads)
Returns a private key (adding header and footer if required).
|
static String |
generateNameId(String value)
Generates a nameID.
|
static String |
generateNameId(String value,
String spnq,
String format)
Generates a nameID.
|
static String |
generateNameId(String value,
String spnq,
String format,
String nq,
X509Certificate cert)
Generates a nameID.
|
static String |
generateNameId(String value,
String spnq,
String format,
X509Certificate cert)
Generates a nameID.
|
static String |
generateUniqueID()
Generates a unique string (used for example as ID of assertions)
|
static String |
generateUniqueID(String prefix)
Generates a unique string (used for example as ID of assertions)
|
static Long |
getCurrentTimeStamp() |
static long |
getExpireTime(String cacheDuration,
long validUntil)
Compare 2 dates and return the the earliest
|
static long |
getExpireTime(String cacheDuration,
String validUntil)
Compare 2 dates and return the the earliest
|
static String |
getFileAsString(String relativeResourcePath)
Loads a resource located at a relative path
|
static SamlResponseStatus |
getStatus(String statusXpath,
Document dom)
Get Status from a Response
|
static boolean |
isAlgorithmWhitelisted(String alg)
Whitelist the XMLSignature algorithm
|
static boolean |
isJaxp15Supported()
Method which uses the recommended way ( https://docs.oracle.com/javase/tutorial/jaxp/properties/error.html )
of checking if JAXP >= 1.5 options are supported.
|
static X509Certificate |
loadCert(String certString)
Load X.509 certificate
|
static PrivateKey |
loadPrivateKey(String keyString)
Load private key
|
static Document |
loadXML(String xml)
This function load an XML string in a save way.
|
static org.joda.time.DateTime |
parseDateTime(String dateTime)
Create calendar form string with format yyyy-MM-ddTHH:mm:ssZ // yyyy-MM-ddTHH:mm:ss.SSSZ
|
static long |
parseDuration(String duration)
Interprets a ISO8601 duration value relative to a current time timestamp.
|
static long |
parseDuration(String durationString,
long timestamp)
Interprets a ISO8601 duration value relative to a given timestamp.
|
static Document |
parseXML(InputSource inputSource)
Parse an XML from input source to a Document object
|
static NodeList |
query(Document dom,
String query)
Extracts a node from the DOMDocument
|
static NodeList |
query(Document dom,
String query,
Node context)
Extracts a node from the DOMDocument
|
static byte[] |
sign(String text,
PrivateKey key,
String signAlgorithm)
Generates a signature from a string
|
static String |
signatureAlgConversion(String sign)
Converts Signature algorithm method name
|
static String |
urlDecoder(String input)
Returns String URL decoded
|
static String |
urlEncoder(String input)
Returns String URL encoded
|
static Boolean |
validateBinarySignature(String signedQuery,
byte[] signature,
List<X509Certificate> certList,
String signAlg)
Validates signed binary data (Used to validate GET Signature).
|
static Boolean |
validateBinarySignature(String signedQuery,
byte[] signature,
X509Certificate cert,
String signAlg)
Validates signed binary data (Used to validate GET Signature).
|
static Boolean |
validateMetadataSign(Document doc,
X509Certificate cert,
String fingerprint,
String alg)
Validate signature (Metadata).
|
static boolean |
validateSign(Document doc,
List<X509Certificate> certList,
String fingerprint,
String alg,
String xpath)
Validate the signature pointed to by the xpath
|
static boolean |
validateSign(Document doc,
X509Certificate cert,
String fingerprint,
String alg,
String xpath)
Validate the signature pointed to by the xpath
|
static Boolean |
validateSignNode(Node signNode,
X509Certificate cert,
String fingerprint,
String alg)
Validate signature of the Node.
|
static boolean |
validateXML(Document xmlDocument,
URL schemaUrl)
This function attempts to validate an XML against the specified schema.
|
public static final String UNIQUE_ID_PREFIX
public static final String RESPONSE_SIGNATURE_XPATH
public static final String ASSERTION_SIGNATURE_XPATH
public static boolean isJaxp15Supported()
public static Document loadXML(String xml)
xml - String. The XML string to be loaded.public static NodeList query(Document dom, String query, Node context) throws XPathExpressionException
dom - The DOMDocumentquery - Xpath Expressioncontext - Context Node (DomElement)XPathExpressionExceptionpublic static NodeList query(Document dom, String query) throws XPathExpressionException
dom - The DOMDocumentquery - Xpath ExpressionXPathExpressionExceptionpublic static boolean validateXML(Document xmlDocument, URL schemaUrl)
xmlDocument - The XML document which should be validatedschemaUrl - The schema filename which should be usedpublic static Document convertStringToDocument(String xmlStr) throws ParserConfigurationException, SAXException, IOException
xmlStr - The XML string which should be convertedParserConfigurationExceptionSAXExceptionIOExceptionpublic static Document parseXML(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException
xmlStr - The XML string which should be convertedParserConfigurationExceptionSAXExceptionIOExceptionpublic static String convertDocumentToString(Document doc, Boolean c14n)
doc - The Document objectc14n - If c14n transformation should be appliedpublic static String convertDocumentToString(Document doc)
doc - The Document objectpublic static String formatCert(String cert, Boolean heads)
cert - A x509 unformatted certheads - True if we want to include head and footerpublic static String formatPrivateKey(String key, boolean heads)
key - A private keyheads - True if we want to include head and footerpublic static X509Certificate loadCert(String certString) throws CertificateException
certString - certificate in string formatCertificateExceptionpublic static PrivateKey loadPrivateKey(String keyString) throws GeneralSecurityException
keyString - private key in string formatGeneralSecurityExceptionpublic static String calculateX509Fingerprint(X509Certificate x509cert, String alg)
x509cert - x509 certificatealg - Digest Algorithmpublic static String calculateX509Fingerprint(X509Certificate x509cert)
x509cert - x509 certificatepublic static String convertToPem(X509Certificate certificate)
certificate - The public certificatepublic static String getFileAsString(String relativeResourcePath) throws IOException
relativeResourcePath - Relative path of the resourceIOExceptionpublic static String base64decodedInflated(String input)
input - String inputpublic static String deflatedBase64encoded(String input) throws IOException
input - String inputIOExceptionpublic static String base64encoder(byte[] input)
input - Stream inputpublic static String base64encoder(String input)
input - String inputpublic static byte[] base64decoder(byte[] input)
input - Stream inputpublic static byte[] base64decoder(String input)
input - String inputpublic static String urlEncoder(String input)
input - String inputpublic static String urlDecoder(String input)
input - URL encoded inputpublic static byte[] sign(String text, PrivateKey key, String signAlgorithm) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException
text - The string we should signkey - The private key to sign the stringsignAlgorithm - Signature algorithm methodNoSuchAlgorithmExceptionInvalidKeyExceptionSignatureExceptionpublic static String signatureAlgConversion(String sign)
sign - signature algorithm methodpublic static boolean validateSign(Document doc, X509Certificate cert, String fingerprint, String alg, String xpath)
doc - The document we should validatecert - The public certificatefingerprint - The fingerprint of the public certificatealg - The signature algorithm methodxpath - the xpath of the ds:Signture node to validatepublic static boolean validateSign(Document doc, List<X509Certificate> certList, String fingerprint, String alg, String xpath)
doc - The document we should validatecerts - The public certificatesfingerprint - The fingerprint of the public certificatealg - The signature algorithm methodxpath - the xpath of the ds:Signture node to validatepublic static Boolean validateMetadataSign(Document doc, X509Certificate cert, String fingerprint, String alg)
doc - The document we should validatecert - The public certificatefingerprint - The fingerprint of the public certificatealg - The signature algorithm methodpublic static Boolean validateSignNode(Node signNode, X509Certificate cert, String fingerprint, String alg)
signNode - The document we should validatecert - The public certificatefingerprint - The fingerprint of the public certificatealg - The signature algorithm methodpublic static boolean isAlgorithmWhitelisted(String alg)
signNode - The document we should validatecert - The public certificatefingerprint - The fingerprint of the public certificatealg - The signature algorithm methodpublic static void decryptElement(Element encryptedDataElement, PrivateKey inputKey)
encryptedDataElement - The encrypted element.inputKey - The private key to decrypt.public static Document copyDocument(Document source) throws ParserConfigurationException
source - The Document object to be cloned.ParserConfigurationExceptionpublic static String addSign(Document document, PrivateKey key, X509Certificate certificate, String signAlgorithm) throws org.apache.xml.security.exceptions.XMLSecurityException, XPathExpressionException
document - The document to be signedkey - The private keycertificate - The public certificatesignAlgorithm - Signature Algorithmorg.apache.xml.security.exceptions.XMLSecurityExceptionXPathExpressionExceptionpublic static String addSign(Document document, PrivateKey key, X509Certificate certificate, String signAlgorithm, String digestAlgorithm) throws org.apache.xml.security.exceptions.XMLSecurityException, XPathExpressionException
document - The document to be signedkey - The private keycertificate - The public certificatesignAlgorithm - Signature AlgorithmdigestAlgorithm - Digest Algorithmorg.apache.xml.security.exceptions.XMLSecurityExceptionXPathExpressionExceptionpublic static String addSign(Node node, PrivateKey key, X509Certificate certificate, String signAlgorithm, String digestAlgorithm) throws ParserConfigurationException, XPathExpressionException, org.apache.xml.security.exceptions.XMLSecurityException
node - The Node to be signedkey - The private keycertificate - The public certificatesignAlgorithm - Signature AlgorithmdigestAlgorithm - Digest AlgorithmParserConfigurationExceptionorg.apache.xml.security.exceptions.XMLSecurityExceptionXPathExpressionExceptionpublic static String addSign(Node node, PrivateKey key, X509Certificate certificate, String signAlgorithm) throws ParserConfigurationException, XPathExpressionException, org.apache.xml.security.exceptions.XMLSecurityException
node - The Node to be signedkey - The private keycertificate - The public certificatesignAlgorithm - Signature AlgorithmParserConfigurationExceptionorg.apache.xml.security.exceptions.XMLSecurityExceptionXPathExpressionExceptionpublic static Boolean validateBinarySignature(String signedQuery, byte[] signature, X509Certificate cert, String signAlg) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException
signedQuery - The element we should validatesignature - The signature that will be validatecert - The public certificatesignAlg - Signature AlgorithmNoSuchAlgorithmExceptionNoSuchProviderExceptionInvalidKeyExceptionSignatureExceptionpublic static Boolean validateBinarySignature(String signedQuery, byte[] signature, List<X509Certificate> certList, String signAlg) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException
signedQuery - The element we should validatesignature - The signature that will be validatecertList - The List of certificatessignAlg - Signature AlgorithmNoSuchAlgorithmExceptionNoSuchProviderExceptionInvalidKeyExceptionSignatureExceptionpublic static SamlResponseStatus getStatus(String statusXpath, Document dom) throws ValidationError
dom - The Response as XMLIllegalArgumentExceptionValidationErrorpublic static String generateNameId(String value, String spnq, String format, String nq, X509Certificate cert)
value - The valuespnq - SP Name Qualifierformat - SP Formatnq - Name Qualifiercert - IdP Public certificate to encrypt the nameIDpublic static String generateNameId(String value, String spnq, String format, X509Certificate cert)
value - The valuespnq - SP Name Qualifierformat - SP Formatcert - IdP Public certificate to encrypt the nameIDpublic static String generateNameId(String value, String spnq, String format)
value - The valuespnq - SP Name Qualifierformat - SP Formatpublic static String generateNameId(String value)
value - The valuepublic static String generateUniqueID(String prefix)
prefix - Prefix for the Unique ID.
Use property onelogin.saml2.unique_id_prefix to set this.public static String generateUniqueID()
public static long parseDuration(String duration) throws IllegalArgumentException
duration - The duration, as a string.IllegalArgumentExceptionpublic static long parseDuration(String durationString, long timestamp) throws IllegalArgumentException
durationString - The duration, as a string.timestamp - The unix timestamp we should apply the duration to.IllegalArgumentExceptionpublic static Long getCurrentTimeStamp()
public static long getExpireTime(String cacheDuration, String validUntil)
cacheDuration - The duration, as a string.validUntil - The valid until date, as a stringpublic static long getExpireTime(String cacheDuration, long validUntil)
cacheDuration - The duration, as a string.validUntil - The valid until date, as a timestamppublic static String formatDateTime(long timeInMillis)
timeInMillis - The time in Millispublic static String formatDateTime(long time, boolean millis)
time - The timemillis - Defines if the time is in Millispublic static org.joda.time.DateTime parseDateTime(String dateTime)
dateTime - string with format yyyy-MM-ddTHH:mm:ssZ // yyyy-MM-ddTHH:mm:ss.SSSZCopyright © 2019. All rights reserved.