예제 #1
1
        private X509Certificate _LoadCertificate(string path, string password, out PrivateKey privateKey, out Provider provider)
        {
            X509Certificate certificate = null;
            provider = null;
            privateKey = null;

            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(new BufferedInputStream(new FileInputStream(path)), password.ToCharArray());
            IPKStoreManager storeManager = new KSStore(ks, new PassStoreKS(password));
            List certificates = storeManager.getSignCertificates();

            if (certificates.size() == 1)
            {
                certificate = (X509Certificate)certificates.get(0);
                privateKey = storeManager.getPrivateKey(certificate);
                provider = storeManager.getProvider(certificate);
                return certificate;
            }

            return certificate;
        }
예제 #2
0
        public static X509Certificate LoadCertificate(string path, string password, ref PrivateKey privateKey, ref Provider provider)
        {
            //versión no usada que lee el certificado desde un archivo
            //ver siguiente método

            X509Certificate certificate = null;

            provider   = null;
            privateKey = null;

            //Cargar certificado de fichero PFX
            KeyStore ks = KeyStore.getInstance("PKCS12");

            ks.load(new BufferedInputStream(new FileInputStream(path)), password.ToCharArray());

            IPKStoreManager storeManager = new KSStore(ks, new PassStoreKS(password));
            List            certificates = storeManager.getSignCertificates();


            //Si encontramos el certificado...
            if (certificates.size() > 0)
            {
                certificate = ((X509Certificate)certificates.get(1));

                // Obtención de la clave privada asociada al certificado
                privateKey = storeManager.getPrivateKey(certificate);

                // Obtención del provider encargado de las labores criptográficas
                provider = storeManager.getProvider(certificate);
            }

            return(certificate);
        }
예제 #3
0
        private static X509Certificate LoadCertificate(string RutaCertificado, string Clave, out PrivateKey privateKey, out Provider provider)
        {
            X509Certificate certificate = null;

            provider   = null;
            privateKey = null;

            //Cargar certificado de fichero PFX
            KeyStore ks = KeyStore.getInstance("PKCS12");

            ks.load(new FileInputStream(RutaCertificado), Clave.ToCharArray());
            IPKStoreManager storeManager = new KSStore(ks, new PassStoreKS(""));
            List            certificates = storeManager.getSignCertificates();

            //Si encontramos el certificado...
            if (certificates.size() >= 1)
            {
                certificate = (X509Certificate)certificates.get(0);

                // Obtención de la clave privada asociada al certificado
                privateKey = storeManager.getPrivateKey(certificate);

                // Obtención del provider encargado de las labores criptográficas
                provider = storeManager.getProvider(certificate);
            }

            return(certificate);
        }
예제 #4
0
        public static java.security.cert.X509Certificate loadCertificate(string patchCerticate, string clave, out PrivateKey privatekey, out Provider provider)
        {
            java.security.cert.X509Certificate certificate = null;
            provider   = null;
            privatekey = null;

            //Cargar el certificado Digital
            KeyStore ks = KeyStore.getInstance("PKCS12");

            ks.load(new BufferedInputStream(new FileInputStream(patchCerticate)), clave.ToCharArray());
            IPKStoreManager storeManager = new KSStore(ks, new PassStoreKS(clave));
            //Cargar certificados
            List certificates = storeManager.getSignCertificates();

            if (certificates.size() > 0)
            {
                certificate = (java.security.cert.X509Certificate)certificates.get(1);

                //Obtener la clave privada asociada al certificado
                privatekey = storeManager.getPrivateKey(certificate);

                //Obtener el Proveedorde la criptografía
                provider = storeManager.getProvider(certificate);
            }

            return(certificate);
        }
예제 #5
0
        private X509Certificate _LoadCertificate(string path, string password, out PrivateKey privateKey, out Provider provider)
        {
            X509Certificate certificate = null;

            provider   = null;
            privateKey = null;

            KeyStore ks = KeyStore.getInstance("PKCS12");

            ks.load(new BufferedInputStream(new FileInputStream(path)), password.ToCharArray());
            IPKStoreManager storeManager = new KSStore(ks, new PassStoreKS(password));
            List            certificates = storeManager.getSignCertificates();

            if (certificates.size() == 1)
            {
                certificate = (X509Certificate)certificates.get(0);
                privateKey  = storeManager.getPrivateKey(certificate);
                provider    = storeManager.getProvider(certificate);
                return(certificate);
            }

            return(certificate);
        }
예제 #6
0
        public bool SignXml(string RutaCertificado, string Clave, string RutaXML, string RutaFirmado, ref string mensaje)
        {
            //if (!ValidateAccessKey)
            //    return false;

            PrivateKey privateKey;
            Provider   provider;

            try
            {
                //X509Certificate certificate = LoadCertificate("","", out privateKey, out provider);

                X509Certificate certificate = null;
                provider   = null;
                privateKey = null;

                //Cargar certificado de fichero PFX
                KeyStore ks = KeyStore.getInstance("PKCS12");
                ks.load(new FileInputStream(RutaCertificado), Clave.ToCharArray());
                IPKStoreManager storeManager = new KSStore(ks, new PassStoreKS(Clave));
                List            certificates = storeManager.getSignCertificates();

                //Si encontramos el certificado...
                if (certificates.size() >= 1)
                {
                    certificate = (X509Certificate)certificates.get(0);

                    // Obtención de la clave privada asociada al certificado
                    privateKey = storeManager.getPrivateKey(certificate);

                    // Obtención del provider encargado de las labores criptográficas
                    provider = storeManager.getProvider(certificate);
                }

                if (certificate != null)
                {
                    TrustFactory.instance       = es.mityc.javasign.trust.TrustExtendFactory.newInstance();
                    TrustFactory.truster        = es.mityc.javasign.trust.MyPropsTruster.getInstance();
                    PoliciesManager.POLICY_SIGN = new es.mityc.javasign.xml.xades.policy.facturae.Facturae31Manager();

                    [email protected] s = new [email protected]();

                    PoliciesManager.POLICY_VALIDATION = new es.mityc.javasign.xml.xades.policy.facturae.Facturae31Manager();

                    DataToSign dataToSign = new DataToSign();
                    dataToSign.setXadesFormat(EnumFormatoFirma.XAdES_BES); //XAdES-EPES
                    dataToSign.setEsquema(XAdESSchemas.XAdES_132);
                    dataToSign.setPolicyKey("facturae31");
                    //dataToSign.setAddPolicy(true);
                    dataToSign.setAddPolicy(false);
                    dataToSign.setXMLEncoding("UTF-8");
                    dataToSign.setEnveloped(true);
                    dataToSign.addObject(new ObjectToSign(new InternObjectToSign("comprobante"), "contenido comprobante", null, "text/xml", null));

                    //string fileToSign = Path.Combine("", fileName);

                    Document doc = LoadXML(RutaXML);
                    dataToSign.setDocument(doc);

                    //dataToSign.setDocument(IDocumentoElectronicoExtensions.LoadXml(fileToSign));

                    Object[] res = new FirmaXML().signFile(certificate, dataToSign, privateKey, provider);

                    java.io.FileOutputStream file = new FileOutputStream(RutaFirmado);

                    UtilidadTratarNodo.saveDocumentToOutputStream((Document)res[0], file, true);
                    file.flush();
                    file.close();

                    //DeleteFile(fileToSign);
                }

                return(true);
            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.Message);
                Console.WriteLine(ex.Message);

                //System.Diagnostics.EventLog.WriteEntry("BcLog", "SignXml - Error en Certificado " + ex.Message);
                return(false);
            }
        }