How to verify a SAML signature for HTTP-redirect binding

前端 未结 6 1441
生来不讨喜
生来不讨喜 2020-12-30 09:40

I\'m receiving a SAML request via HTTP-redirect binding the content of the SAML request look like this

{\"SigAlg\"=>\"http://www.w3.org/2000/09/xmldsi

6条回答
  •  伪装坚强ぢ
    2020-12-30 10:27

    I'am trying using the above answer but don't success.

    Then, read the documentation and a little time, i have sucess to validate signature with Java and the fast answer is:

    final String samlRequest = request.getParameter("SAMLRequest");
    final String relayState = request.getParameter("RelayState");
    final String sigAlg = request.getParameter("SigAlg");
    final String signature = request.getParameter("Signature");
    
    FileInputStream fis = new FileInputStream(new File("path-to-service-provider-x509-certificate"));
    
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate cert = cf.generateCertificate(fis);
    
    // ps: java.net.URLEncoder;
    String query = "SAMLRequest=" + URLEncoder.encode(samlRequest, "UTF-8");
    query += "&RelayState=" +URLEncoder.encode(relayState, "UTF-8");
    query += "&SigAlg=" + URLEncoder.encode(sigAlg, "UTF-8");
    
    // ps: org.opensaml.xml.util.Base64
    byte[] signatureBytes = Base64.decode(signature);
    
    org.apache.xml.security.Init.init();
    Signature sig = Signature.getInstance("SHA1withRSA"); // or other alg (i, e: SHA256WithRSA or others)
    sig.initVerify(cert.getPublicKey());
    sig.update(query.getBytes());
    Boolean valid = sig.verify(signatureBytes);
    

提交回复
热议问题