java - Trying to use the certificate to connect in a web server -
i ve been trying use 2 different certificates communicate in web server( web server 1). when use first one, can communicate web server without problem. when try use second one, have problems: if change certificate , make conection web server, returns information had used in first certificate. when close , open tomcat 8, can use second one, can't use first 1 again. i´ve been using more 1 certificate , , using same method in web server(web server 2) without problem.
follow code bellow:
public string sendxml(string xmlcabecalho, string xmlenvnfse) throws exceptionservicoabrasf { try { signxml signxml = new signxml(this.keystoreservice.getkeystore(), this.keystoreservice.getaliascert(), this.keystoreservice.getpasswordpfx()); xmlenvnfse = signxml.signandsend(xmlenvnfse); loadinfocertificate(); return executarservicoenvionfse(xmlcabecalho, xmlenvnfse); } catch (exception ex) { throw new exceptionservicoabrasf("failure sing. " + ex.getmessage()); } } //add xml sign public string signandsend(string xml) throws exception { //sign return sign(xml, "infdeclaracaoprestacaoservico", "rps"); }
//sign
private string sign(string xml, string tag, string tagparentsignature) throws exception { document document = documentfactory(xml); xmlsignaturefactory signaturefactory = xmlsignaturefactory .getinstance("dom"); arraylist<transform> transformlist = signaturefactory(signaturefactory); loadcertificates(signaturefactory); nodelist elements = document.getelementsbytagname(tag); org.w3c.dom.element el = (org.w3c.dom.element) elements.item(0); el.setidattribute("id", true); string id = el.getattribute("id"); reference ref = signaturefactory.newreference("#" + id, signaturefactory.newdigestmethod(digestmethod.sha1, null), transformlist, null, null); signedinfo si = signaturefactory.newsignedinfo(signaturefactory .newcanonicalizationmethod(canonicalizationmethod.inclusive, (c14nmethodparameterspec) null), signaturefactory .newsignaturemethod(signaturemethod.rsa_sha1, null), collections.singletonlist(ref)); xmlsignature signature = signaturefactory.newxmlsignature(si, keyinfo); domsigncontext dsc = new domsigncontext(privatekey, document.getelementsbytagname(tagparentsignature).item(0)); signature.sign(dsc); return outputxml(document); } //load certificate protected void loadcertificates(xmlsignaturefactory signaturefactory) throws exception { if (keystore != null) { keystore.privatekeyentry pkentry = null; if (keystore.iskeyentry(alias)) { char[] pin = (password == null ? "" : password).tochararray(); pkentry = (keystore.privatekeyentry) keystore.getentry(alias, new keystore.passwordprotection(pin)); privatekey = pkentry.getprivatekey(); x509certificate cert = (x509certificate) pkentry.getcertificate(); keyinfofactory keyinfofactory = signaturefactory.getkeyinfofactory(); list<x509certificate> x509content = new arraylist<>(); x509content.add(cert); x509data x509data = keyinfofactory.newx509data(x509content); keyinfo = keyinfofactory.newkeyinfo(collections.singletonlist(x509data)); } else { throw new exception("alias invalid."); } } else { throw new exception("invalid certificate information."); } } ///normalize xml protected string outputxml(document doc) throws transformerexception { bytearrayoutputstream os = new bytearrayoutputstream(); transformerfactory tf = transformerfactory.newinstance(); transformer trans = tf.newtransformer(); trans.transform(new domsource(doc), new streamresult(os)); string xml = os.tostring(); if ((xml != null) && (!"".equals(xml))) { xml = xml.replaceall("\\r\\n", ""); xml = xml.replaceall(" standalone=\"no\"", ""); } return xml; } //load info certificate private void loadinfocertificate() { system.setproperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); security.addprovider(new com.sun.net.ssl.internal.ssl.provider()); system.setproperty("javax.net.ssl.keystoretype", "pkcs12"); system.clearproperty("javax.net.ssl.keystore"); system.clearproperty("javax.net.ssl.keystorepassword"); system.clearproperty("javax.net.ssl.truststore"); system.setproperty("javax.net.ssl.keystore", this.keystoreservice.getpathpfx()); system.setproperty("javax.net.ssl.keystorepassword", this.keystoreservice.getpasswordpfx()); system.setproperty("javax.net.ssl.truststoretype", "jks"); system.setproperty("javax.net.ssl.truststore", this.pathcacerts); } //send web service private string executarservicoenvionfse(string xmlcabecalho, string xmlcorpo) throws exceptionservicoabrasf { input input = new input(xmlcabecalho, xmlcorpo); output output = generatereturn(input); return output.getoutputxml(); } //generate conection web service private output generatereturn(ws.input parameters) throws exceptionservicoabrasf { ws.nfse port = loadnfsedivport(); return port.generatereturn(parameters); } //return web service private ws.nfse loadnfsedivport() throws exceptionservicoabrasf { try { string urlstr = "https://homolog.govdigital.com.br/ws/div?wsdl"; url url = new url(urlstr); ws.nfseserviceimpldivservice service = new ws.nfseserviceimpldivservice(url); ws.nfse port = service.getnfsedivport(); return port; } catch (malformedurlexception ex) { throw new exceptionservicoabrasf("invalid url. " + ex.getmessage()); } }
Comments
Post a Comment