Verificação de assinatura digital - XML

6 respostas
R

Pessoal eu gerei um xml assinado até ai funcionou, quando eu vou recurepar o xml ele não acha a tag Signature.

Código java

NodeList nl = xml.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");

if (nl.getLength() == 0) {
     throw new Exception("Não foi possivel achar o elemente de assinatura");
}
Segue o xml
<?xml version="1.0" encoding="UTF-8"?>
<PurchaseOrder>
    <Item number="130046593231">
        <Description>Video Game</Description>
        <Price>10.29</Price>
    </Item>
    <Buyer id="8492340">
        <Name>My Name</Name>
        <Address>
            <Street>One Network Drive</Street>
            <Town>Burlington</Town>
            <State>MA</State>
            <Country>United States</Country>
            <PostalCode>01803</PostalCode>
        </Address>
    </Buyer>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
        <SignedInfo>
            <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
            <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
            <Reference URI="">
                <Transforms>
                    <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                <DigestValue>tVicGh6V+8c.....</DigestValue>
            </Reference>
        </SignedInfo>
        <SignatureValue>ewqy0rhVPeFrqeimvHRhT5ASeEb....</SignatureValue>
        <KeyInfo>
            <X509Data>
                <X509SubjectName>
                  CN=LAROD:[telefone removido],OU=teste,OU=teste,O=teste,C=BR
                </X509SubjectName>
                <X509Certificate>
                    MIIFIjCCBAqgAwIBAgIQIT...
                </X509Certificate>
            </X509Data>
        </KeyInfo>
    </Signature>
</PurchaseOrder>

Alguém pode me ajudar ??? Vlw

6 Respostas

nextuser

usa getElementsByTagName

R

Consegui nextuser, agora está dando outro erro quando eu vou gerar um XMLSignature ele da uma excpetion.

Código Java

DOMValidateContext valContext = new DOMValidateContext (new X509KeySelector(), nl.item(0));
 XMLSignature signature = fac.unmarshalXMLSignature(valContext);

Exception

Exception in thread "main" javax.xml.crypto.MarshalException: Document implementation must support DOM Level 2 and be namespace aware at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:157) at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:125)

Andei pesquisando no google mas ainda não encontrei nada.

nextuser

o erro da em qual das duas linhas?

R

Eu esqueci de colocar kkkk, é na linha 2.

nextuser

é isso que vc ta tentando fazer...

NodeList signedDataList = data.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature");
			int sizeList = signedDataList.getLength();
			if (sizeList == 0) {
				Exception ex = new Exception("No XML signature provided");
				throw ex;
			} else {
				Element envelopedSig = (Element) signedDataList.item(sizeList - 1);

				X509Certificate cert = null;

				XMLSignature sig;
				try {
					sig = new XMLSignature(envelopedSig, null);
				} catch (Exception e) {
					Exception ex = new Exception("No XML signature provided");
					throw ex;
				}

				//				Recupera o keyinfo.
				KeyInfo ki = sig.getKeyInfo();
				if (ki == null || !ki.containsX509Data()) {
					return Boolean.FALSE;
				}

				try {
					cert = sig.getKeyInfo().getX509Certificate();
					cert.checkValidity();
				} catch (Exception e) {
					return Boolean.FALSE;
				}

				if (cert == null) {
					return Boolean.FALSE;
				}

				boolean isValid;
				try {
					isValid = sig.checkSignatureValue(cert);
				} catch (XMLSignatureException e) {
					return Boolean.FALSE;
				}

				return Boolean.valueOf(isValid);

			}
R

Acho que descobri o problema é quando eu dou um getElementByTagName ele não ta trazendo o localName do node.

Criado 16 de janeiro de 2009
Ultima resposta 16 de jan. de 2009
Respostas 6
Participantes 2