예제 #1
0
        public bool Logout(authTokenType token)
        {
            xmCryptoService client = new xmCryptoService();

            try
            {
                logout log = new logout();
                log.AuthToken = token;
                client.logout(log);
                return(true);
            }
            catch (Exception ex)
            {
                Log.Error("Error al intentar realizar logout: " + ex);
                return(false);
            }
            finally
            {
                client.Dispose();
                //if (client.State == CommunicationState.Faulted)
                //    client.Abort();
                //else
                //    client.Close();
            }
        }
예제 #2
0
 public bool CreateSectretKey(ref authTokenType token, string keyAlias)
 {
     LunaXml.xmCryptoService client = new xmCryptoService();
     try
     {
         generateSecretKey sec = new generateSecretKey();
         sec.AuthToken     = token;
         sec.KeyAlgorithm  = KeyAlgorithmType.DES3;
         sec.KeyAlias      = keyAlias;
         sec.KeySize       = "1024";
         sec.ReturnKeyInfo = true;
         generateSecretKeyResponse res = client.generateSecretKey(sec);
         token = res.AuthToken;
         if (res.Result.ResultMajor != null && res.Result.ResultMajor == "urn:oasis:names:tc:dss:resultmajor:Success")
         {
             return(true);
         }
         else
         {
             Log.Error((res.Result.ResultMessage != null ? res.Result.ResultMessage.Value: ""));
             return(false);
         }
     }
     catch (Exception ex)
     {
         Log.Error("Error al crear la llave privada " + ex);
         return(false);
     }
     finally
     {
         client.Dispose();
     }
 }
예제 #3
0
 public string Firmar(ref authTokenType token, string texto, string alias, SignatureModeType tipodefirma)
 {
     LunaXml.xmCryptoService client = new xmCryptoService();
     try
     {
         sign textoafirmar = new sign();
         textoafirmar.AuthToken = token;
         textoafirmar.SignatureModeSpecified = true;
         textoafirmar.DataToSign             = Convert.ToBase64String(Encoding.UTF8.GetBytes(texto));
         textoafirmar.SignatureMode          = tipodefirma;
         textoafirmar.SigningKeyAlias        = alias;
         signResponse respuesta = client.sign(textoafirmar);
         token = respuesta.AuthToken;
         if (respuesta.Result.ResultMajor != null && respuesta.Result.ResultMajor == "urn:oasis:names:tc:dss:resultmajor:Success")
         {
             return(respuesta.Signature);
         }
         else
         {
             Log.Error((respuesta.Result.ResultMessage != null ? respuesta.Result.ResultMessage.Value : ""));
             return(null);
         }
     }
     catch (Exception ex)
     {
         Log.Error("Error al intentar firmar: " + ex);
         return(null);
     }
     finally
     {
         client.Dispose();
     }
 }
예제 #4
0
 public bool BorrarObjeto(ref authTokenType token, string alias)
 {
     LunaXml.xmCryptoService client = new xmCryptoService();
     try
     {
         deleteObject del = new deleteObject();
         del.AuthToken   = token;
         del.ObjectAlias = alias;
         deleteObjectResponse res = client.deleteObject(del);
         token = res.AuthToken;
         if (res.Result.ResultMajor != null && res.Result.ResultMajor == "urn:oasis:names:tc:dss:resultmajor:Success")
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         Log.Error("Error al crear la llave privada " + ex);
         return(false);
     }
     finally
     {
         client.Dispose();
     }
 }
예제 #5
0
        public bool GenerarCsr(ref authTokenType token, string privateKeyalias, string publicKeyAlias, string subject, string fileName, string challenge)
        {
            LunaXml.xmCryptoService client = new xmCryptoService();
            try
            {
                byte[]  modulus  = new byte[] {};
                byte [] exponent = new byte[] {};
                this.Extraer(ref token, publicKeyAlias, ref modulus, ref exponent);
                RsaKeyParameters param  = new RsaKeyParameters(false, new BigInteger(modulus), new BigInteger(exponent));
                DerSet           derset = null;
                if (challenge != null)
                {
                    ChallengePassword chpass = new ChallengePassword(challenge);
                    derset = new DerSet(chpass);
                    //IList oid = new ArrayList();
                    //IList values = new ArrayList();
                    //oid.Add(PkcsObjectIdentifiers.Pkcs9AtChallengePassword);
                    //var pass = new DerPrintableString(challenge);
                    ////Asn1OctetString oct = pass.ToAsn1Object(); //new DerOctetString(pass);//Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes("AABBccc22"))));

                    //X509Extension ext = new X509Extension(false,new DerOctetString(pass.GetEncoded()));
                    //values.Add(pass);
                    //X509Extensions extensions = new X509Extensions(oid, values);
                    //derset = new DerSet(extensions.ToAsn1Object());
                }
                else
                {
                    derset = new DerSet();
                }

                //string sub =
                //"2.5.4.45=SAT970701NN3 / GATF730321GG5, SERIALNUMBER= / GATF730321HJCRRR01, O=SERVICIO DE ADMINISTRACION TRIBUTARIA, OU=PACNLC091211KC657202";
                //+ ", 1.2.840.113549.1.9.7= NtLink2012"
                X509Name sub = new X509Name(subject, new ConverterSidetec());
                Pkcs10CertificationRequestDelaySigned ds = new Pkcs10CertificationRequestDelaySigned("SHA1WITHRSA", sub, param, derset);
                string pafirmar = Convert.ToBase64String(ds.GetDataToSign());
                string firmados = Firmar(ref token, pafirmar, privateKeyalias, SignatureModeType.SHA1withRSA);
                byte[] bytes    = Convert.FromBase64String(firmados);

                ds.SignRequest(bytes);
                File.WriteAllBytes(fileName, ds.GetDerEncoded());
                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return(false);
            }
            finally
            {
                client.Dispose();
            }
        }
예제 #6
0
 public string Cifrar(ref authTokenType token, string texto, string alias)
 {
     LunaXml.xmCryptoService client = new xmCryptoService();
     try
     {
         encrypt en = new encrypt();
         en.DataToEncrypt      = texto;
         en.AuthToken          = token;
         en.EncryptionKeyAlias = alias;
         en.Mechanism          = MechanismType.CBC;
         en.Padding            = paddingType.PKCS5Padding;
         en.Parameter          = new[] { new ParameterType()
                                         {
                                             ParameterName = "IV", ParameterValue = "MTIzNDU2Nzg="
                                         } };
         encryptResponse res = client.encrypt(en);
         token = res.AuthToken;
         if (res.Result.ResultMajor != null && res.Result.ResultMajor == "urn:oasis:names:tc:dss:resultmajor:Success")
         {
             return(res.EncryptedData);
         }
         else
         {
             Log.Error((res.Result.ResultMessage != null ? res.Result.ResultMessage.Value : ""));
             return(null);
         }
     }
     catch (Exception ex)
     {
         Log.Error("Error al intentar firmar: " + ex);
         return(null);
     }
     finally
     {
         client.Dispose();
     }
 }
예제 #7
0
        private bool Extraer(ref authTokenType token, string alias, ref byte[] modulus, ref byte[] exponent)
        {
            LunaXml.xmCryptoService client = new xmCryptoService();
            try
            {
                extract ex = new extract();
                ex.KeyAlias = alias;
                //ex.WrappingOptions = new WrappingOptionsType();
                ex.AuthToken = token;
                var res = client.extract(ex);
                token = res.AuthToken;
                if (res.Result.ResultMajor != null && res.Result.ResultMajor == "urn:oasis:names:tc:dss:resultmajor:Success")
                {
                    KeyValueType    type    = (KeyValueType)res.KeyInfo.Items[0];
                    RSAKeyValueType rsatype = (RSAKeyValueType)type.Item;
                    modulus  = rsatype.Modulus;
                    exponent = rsatype.Exponent;
                    return(true);
                }

                else
                {
                    Log.Error((res.Result.ResultMessage != null ? res.Result.ResultMessage.Value : ""));
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error al crear la llave privada " + ex);
                return(false);
            }
            finally
            {
                client.Dispose();
            }
        }