Assinar XML :: Certificados Digitais

1 resposta
C

Boa tarde a todos!
Tenho na minha cabeça um grande nó que espero alguém me consiga desfazer…
Vou tentar resumir a informação:
Tenho que assinar um XML com um Root Certificate que me foi enviado por email que diz o seguinte:


Your DER encoded certificate and PKCS#7 certificate chain are attached.

Your PEM encoded certificate is:
-----BEGIN CERTIFICATE-----
MIICLjCCAZegAwIBAgIVAKPB7Vlb9cb2q+H1hg+yiDTIQoCDMA0GCSqGSIb3DQEBBQUAMD4xCzAJ

-----END CERTIFICATE-----

Your PEM encoded PKCS#7 certificate chain is:

-----BEGIN PKCS7-----
MIIGogYJKoZIhvcNAQcCoIIGkzCCBo8CAQExADALBgkqhkiG9w0BBwGgggZ3MIICHzCCAYigAwIB

-----END PKCS7-----


Antes disto criei um keystore no meu servidor, e preciso de importar este root certificate que recebi. Peguei na primeira parte do emails -----BEGIN CERTIFICATE----- (…) -----END CERTIFICATE-----, gravei como ficheiro .Der(MPIclient_certificate.der) e usei o comando do Keytool para importar (alias=MPIclientCA). Não deu erro. Agora a minha dúvida é a PKCS#7. Como gravo, como importo…!?!
Quanto ao meu codigo java para assinar o xml estou a usar o seguinte:


FileInputStream in = null;
        try {
            String keyStore = "C:\\log\\vbvsignstore";
            String certFile = "C:\\log\\MPIclient_certificate.der";
            String keyStoreType = "jks";
            String keyStorePass = "vbvsignpass";
            String keyAlias = "vbvsign";
            LogI.log(dbgLevel, "getSignatureKeyPair: 1");
            KeyStore tokenKeyStore = KeyStore.getInstance(keyStoreType);
            if (tokenKeyStore == null) {
                LogI.log(dbgLevel, "getSignatureKeyPair: Got no key store. Ensure that the provider is properly configured and installed.");
                System.exit(0);
            }
            in = new FileInputStream(keyStore);
            tokenKeyStore.load(in, keyStorePass.toCharArray());
            // this call binds the keystore to the first instance of the IAIKPkcs11
            // provider
            // if you want ot bind it to a different instance, you have to provide the
            // provider name as stream
            // see the other RSASigningDemo classes for examples
            Enumeration aliases = tokenKeyStore.aliases();



            // and we take the first signature (private) key for simplicity
            while (aliases.hasMoreElements()) {
                
                FileInputStream fis = new FileInputStream(certFile);
                LogI.log(dbgLevel, "getSignatureKeyPair: 2");
                BufferedInputStream bis = new BufferedInputStream(fis);
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                LogI.log(dbgLevel, "getSignatureKeyPair: ERRO1->" + cf.getType().toString());
                X509Certificate cert = null;
                while (bis.available() > 0) {
                    cert = (X509Certificate) cf.generateCertificate(bis);
                }
                keyAlias = aliases.nextElement().toString();
                LogI.log(dbgLevel, "getSignatureKeyPair: keyAlias:" + keyAlias);
                Key key = tokenKeyStore.getKey(keyAlias, keyStorePass.toCharArray());
                if (key instanceof RSAPrivateKey && keyAlias.equals("MPIclientCA")) {
                    //tokenKeyStore.setCertificateEntry(keyAlias, cert);
                    LogI.log(dbgLevel, "getSignatureKeyPair: Chave privada RSA");
                    java.security.cert.Certificate[] certificateChain = tokenKeyStore.getCertificateChain(keyAlias);
                    X509Certificate signerCertificate = (X509Certificate) certificateChain[0];
                    signatureKey_ = (PrivateKey) key;
                    signerCertificate_ = signerCertificate;
                    break;
                }
            }

            if (signatureKey_ == null) {
                LogI.log(dbgLevel, "getSignatureKeyPair: Found no signature key. Ensure that a valid card is inserted.");
                System.exit(0);
            }
        } catch (KeyStoreException ex) {
            ex.printStackTrace();
            LogI.log(dbgLevel, "getSignatureKeyPair: ERRO1->" + ex);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
            LogI.log(dbgLevel, "getSignatureKeyPair: ERRO2->" + ex);
        } catch (UnrecoverableKeyException ex) {
            ex.printStackTrace();
            LogI.log(dbgLevel, "getSignatureKeyPair: ERRO3->" + ex);
        } catch (IOException ex) {
            ex.printStackTrace();
            LogI.log(dbgLevel, "getSignatureKeyPair: ERRO4->" + ex);
        } catch (CertificateException ex) {
            ex.printStackTrace();
            LogI.log(dbgLevel, "getSignatureKeyPair: ERRO5->" + ex);
        }
    }

Está a retornar o erro : “Found no signature key. Ensure that a valid card is inserted”, ou seja, falta carregar uma chave…

ALguem me pode ajudar???
PLEASEEEEEEEEE!!!

Obrigado a todos!

Abrax

1 Resposta

T

Use o BouncyCastle ( http://www.bouncycastle.org ) para gerar dados no formato PKCS#7. As classes referenciam o PKCS#7 com o nome de “CMS” (Cryptographic Message Standard).
Para usar adequadamente o BouncyCastle, tente arranjar o livro “Beginning Cryptography in Java”, de David Hook, que tem todas as receitas de bolo.

Criado 1 de setembro de 2008
Ultima resposta 1 de set. de 2008
Respostas 1
Participantes 2