public static String generateSAMLRequestSignature(final String urlEncodedString, final PrivateKey signingKey, final String sigAlgorithmName) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, UnsupportedEncodingException { if (signingKey == null) { return urlEncodedString; } String opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1; String javaSignatureAlgorithmName = "SHA1withRSA"; if (sigAlgorithmName.equalsIgnoreCase("SHA256")) { opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256; javaSignatureAlgorithmName = "SHA256withRSA"; } else if (sigAlgorithmName.equalsIgnoreCase("SHA384")) { opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA384; javaSignatureAlgorithmName = "SHA384withRSA"; } else if (sigAlgorithmName.equalsIgnoreCase("SHA512")) { opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA512; javaSignatureAlgorithmName = "SHA512withRSA"; } String url = urlEncodedString + "&SigAlg=" + URLEncoder.encode(opensamlAlgoIdSignature, HttpUtils.UTF_8); Signature signature = Signature.getInstance(javaSignatureAlgorithmName); signature.initSign(signingKey); signature.update(url.getBytes(Charset.forName("UTF-8"))); String signatureString = Base64.encodeBytes(signature.sign(), Base64.DONT_BREAK_LINES); if (signatureString != null) { return url + "&Signature=" + URLEncoder.encode(signatureString, HttpUtils.UTF_8); } return url; }
public static String encodeSAMLRequest(XMLObject authnRequest) throws MarshallingException, IOException { Marshaller marshaller = Configuration.getMarshallerFactory() .getMarshaller(authnRequest); Element authDOM = marshaller.marshall(authnRequest); StringWriter requestWriter = new StringWriter(); XMLHelper.writeNode(authDOM, requestWriter); String requestMessage = requestWriter.toString(); Deflater deflater = new Deflater(Deflater.DEFLATED, true); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater); deflaterOutputStream.write(requestMessage.getBytes(Charset.forName("UTF-8"))); deflaterOutputStream.close(); String encodedRequestMessage = Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES); encodedRequestMessage = URLEncoder.encode(encodedRequestMessage, HttpUtils.UTF_8).trim(); return encodedRequestMessage; }
/** * Encodes a byte array into Base64 notation. Does not GZip-compress data. * * @param source The data to convert * @since 1.4 */ public static String encodeBytes(byte[] source) { return encodeBytes(source, 0, source.length, NO_OPTIONS); } // end encodeBytes
/** * Encodes a byte array into Base64 notation. Does not GZip-compress data. * * @param source The data to convert * @since 1.4 */ public static String encodeBytes(byte[] source) { return encodeBytes(source, 0, source.length, NO_OPTIONS); } // end encodeBytes
/** * Encodes a byte array into Base64 notation. Does not GZip-compress data. * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @since 1.4 */ public static String encodeBytes(byte[] source, int off, int len) { return encodeBytes(source, off, len, NO_OPTIONS); } // end encodeBytes
/** * Encodes a byte array into Base64 notation. Does not GZip-compress data. * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @since 1.4 */ public static String encodeBytes(byte[] source, int off, int len) { return encodeBytes(source, off, len, NO_OPTIONS); } // end encodeBytes
public static String encode(String authReq) { return Base64.encodeBytes(authReq.getBytes(StandardCharsets.UTF_8)); } }
public static String encode(String authReq) { return Base64.encodeBytes(authReq.getBytes(StandardCharsets.UTF_8)); } }
/** * Encoding the response * * @param xmlString String to be encoded * @return encoded String */ public static String encode(String xmlString) throws Exception { String encodedRequestMessage = Base64.encodeBytes(xmlString.getBytes(), Base64.DONT_BREAK_LINES); return encodedRequestMessage.trim(); }
/** * Compressing and Encoding the response * * @param xmlString String to be encoded * @return compressed and encoded String */ public static String encode(String xmlString) throws Exception { // Encoding the compressed message String encodedRequestMessage = Base64.encodeBytes(xmlString.getBytes("UTF-8"), Base64.DONT_BREAK_LINES); return encodedRequestMessage.trim(); }
/** * Encoding the response * * @param xmlString String to be encoded * @return encoded String */ public static String encode(String xmlString) { // Encoding the message String encodedRequestMessage = Base64.encodeBytes(xmlString.getBytes(StandardCharsets.UTF_8), Base64.DONT_BREAK_LINES); return encodedRequestMessage.trim(); }
/** * Encoding the response * * @param xmlString String to be encoded * @return encoded String */ public static String encode(String xmlString) throws Exception { String encodedRequestMessage = Base64.encodeBytes(xmlString.getBytes(), Base64.DONT_BREAK_LINES); return encodedRequestMessage.trim(); }
/** * Encoding the response * * @param xmlString String to be encoded * @return encoded String */ public static String encode(String xmlString) { // Encoding the message String encodedRequestMessage = Base64.encodeBytes(xmlString.getBytes(StandardCharsets.UTF_8), Base64.DONT_BREAK_LINES); return encodedRequestMessage.trim(); }
/** * Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value. * * @param bigInt the BigInteger value * @return the encoded CryptoBinary value */ public static final String encodeCryptoBinaryFromBigInteger(BigInteger bigInt) { // This code is really complicated, for now just use the Apache xmlsec lib code directly. byte[] bigIntBytes = org.apache.xml.security.utils.Base64.encode(bigInt, bigInt.bitLength()); return Base64.encodeBytes(bigIntBytes); }
/** * Encode a native Java BigInteger type to a base64-encoded ds:CryptoBinary value. * * @param bigInt the BigInteger value * @return the encoded CryptoBinary value */ public static final String encodeCryptoBinaryFromBigInteger(BigInteger bigInt) { // This code is really complicated, for now just use the Apache xmlsec lib code directly. byte[] bigIntBytes = org.apache.xml.security.utils.Base64.encode(bigInt, bigInt.bitLength()); return Base64.encodeBytes(bigIntBytes); }
/** * Gets the Base64 encoded artifact. * * @return Base64 encoded artifact. */ public String base64Encode() { return new String(Base64.encodeBytes(getArtifactBytes())); }
/** * Compresses the response String * * @param response * @return * @throws IOException */ public static String compressResponse(String response) throws IOException { Deflater deflater = new Deflater(Deflater.DEFLATED, true); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater); try { deflaterOutputStream.write(response.getBytes(StandardCharsets.UTF_8)); } finally { deflaterOutputStream.close(); } return Base64.encodeBytes(byteArrayOutputStream.toByteArray(), Base64.DONT_BREAK_LINES); }
public static void addDeflateSignatureToHTTPQueryString(StringBuilder httpQueryString, X509Credential cred) throws SSOAgentException { doBootstrap(); try { httpQueryString.append("&SigAlg=" + URLEncoder.encode(XMLSignature.ALGO_ID_SIGNATURE_RSA, "UTF-8").trim()); java.security.Signature signature = java.security.Signature.getInstance("SHA1withRSA"); signature.initSign(cred.getPrivateKey()); signature.update(httpQueryString.toString().getBytes(Charset.forName("UTF-8"))); byte[] signatureByteArray = signature.sign(); String signatureBase64encodedString = Base64.encodeBytes(signatureByteArray, Base64.DONT_BREAK_LINES); httpQueryString.append("&Signature=" + URLEncoder.encode(signatureBase64encodedString, "UTF-8").trim()); } catch (Exception e) { throw new SSOAgentException("Error applying SAML2 Redirect Binding signature", e); } }
/** * Builds an {@link org.opensaml.xml.signature.X509Certificate} XMLObject from a native * Java {@link java.security.cert.X509Certificate}. * * @param cert the Java {@link java.security.cert.X509Certificate} to convert * @return a {@link org.opensaml.xml.signature.X509Certificate} XMLObject * @throws CertificateEncodingException thrown when there is an error converting the Java * certificate representation to the XMLObject representation */ public static org.opensaml.xml.signature.X509Certificate buildX509Certificate(X509Certificate cert) throws CertificateEncodingException { org.opensaml.xml.signature.X509Certificate xmlCert = (org.opensaml.xml.signature.X509Certificate) Configuration.getBuilderFactory() .getBuilder(org.opensaml.xml.signature.X509Certificate.DEFAULT_ELEMENT_NAME) .buildObject(org.opensaml.xml.signature.X509Certificate.DEFAULT_ELEMENT_NAME); xmlCert.setValue(Base64.encodeBytes(cert.getEncoded())); return xmlCert; }
@SuppressWarnings("unchecked") private static Attribute getAttribute(String attributeName, byte[] attributeValue) { Attribute attribute = buildXMLObject(Attribute.class, Attribute.DEFAULT_ELEMENT_NAME); attribute.setName(attributeName); XMLObjectBuilder<XSBase64Binary> builder = Configuration .getBuilderFactory().getBuilder(XSBase64Binary.TYPE_NAME); XSBase64Binary xmlAttributeValue = builder.buildObject( AttributeValue.DEFAULT_ELEMENT_NAME, XSBase64Binary.TYPE_NAME); xmlAttributeValue.setValue(Base64.encodeBytes(attributeValue)); attribute.getAttributeValues().add(xmlAttributeValue); return attribute; }