コード例 #1
0
        public async Task <string> Get()
        {
            string json = string.Empty;
            var    path = Path.Combine(_env.ContentRootPath, "PhoneRecordsFile.json");

            if (System.IO.File.Exists(path))
            {
                json = await System.IO.File.ReadAllTextAsync(path);

                List <PhoneRecord> phoneRecords = JsonConvert.DeserializeObject <List <PhoneRecord> >(json);
                _digester.PhoneRecords = phoneRecords;
                await _digester.Digest();
            }
            return(JsonConvert.SerializeObject(_digester.PhoneRecords, Formatting.Indented));
        }
コード例 #2
0
        public IExpression Refine(Digester digest)
        {
            if (digest.Done)
            {
                return(this);
            }

            string accessor = digest.Digest();

            if (accessor.Equals("learnset") || accessor.Equals("ls"))
            {
                return(new LearnsetSelectorExpression(Pokemon).Refine(digest));
            }

            return(this);
        }
コード例 #3
0
        public static IExpression Parse(string query)
        {
            Digester digest = new Digester(query);

            if (digest.Done)
            {
                return(new EmptyExpression());
            }

            string type = digest.Digest();

            if (Utils.IsPokemon(type))
            {
                return(new PokemonExpression(Data.Pokemons.Find(p => p.Name.Equals(type))).Refine(digest));
            }
            else
            {
            }

            return(new EmptyExpression());
        }
コード例 #4
0
        public IExpression Refine(Digester digest)
        {
            if (digest.Done)
            {
                return(this);
            }

            string def = digest.Digest();

            //	if (Utils.IsGen(digest.Peek()) || Utils.IsGame(digest.Peek())) {
            //	}

            if (Utils.IsGen(def))
            {
                // default is level up when unspecified
                return(new LearnsetExpression(Pokemon, Data.GetMoveMethod("level up").Id, Utils.GetGen(def)));
            }
            else if (Utils.IsGame(def))
            {
                return(new LearnsetExpression(Pokemon, Data.GetMoveMethod("level up").Id, -1, Utils.GetGameId(def)));
            }

            return(this);
        }
コード例 #5
0
        public static byte[] generateSignedData(
            P7ContentSignerParameters parameters,
            bool omitContent,
            AdESPolicy policy,
            bool signingCertificateV2,
            X509Certificate2 keyEntry,
            byte[] messageDigest,
            bool padesMode,
            String contentType,
            String contentDescription)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("Los parametros no pueden ser nulos");
            }
            String signatureAlgorithm = parameters.GetSignatureAlgorithm();

            X509Certificate2[] signerCertificateChain = parameters.GetSignerCertificateChain();

            DateTime signDate = DateTime.Now;

            // Ya que el contenido de la firma puede ser grande, lo obtenemos solo al principio
            byte[] content = parameters.GetContent();

            byte[] preSignature = CAdESTriPhaseSigner.PreSign(
                AOSignConstants.GetDigestAlgorithmName(signatureAlgorithm),
                (omitContent) ? null : content,
                signerCertificateChain,
                policy,
                signingCertificateV2,
                (messageDigest == null && content != null) ?
                Digester.Digest(content, AOSignConstants.GetDigestAlgorithmName(signatureAlgorithm)) : messageDigest,
                signDate,
                padesMode,
                contentType,
                contentDescription
                );

            byte[] signature = new AOPkcs1Signer().sign(preSignature, signatureAlgorithm, keyEntry);

            return(CAdESTriPhaseSigner.PostSign(
                       AOSignConstants.GetDigestAlgorithmName(signatureAlgorithm),
                       (omitContent) ? null : content,
                       signerCertificateChain,
                       signature,
                       // Volvemos a crear la prefirma simulando una firma trifasica en la que la postfirma no cuenta con el
                       // resultado de la prefirma
                       CAdESTriPhaseSigner.PreSign(
                           AOSignConstants.GetDigestAlgorithmName(signatureAlgorithm),
                           (omitContent) ? null : content,
                           signerCertificateChain,
                           policy,
                           signingCertificateV2,
                           (messageDigest == null && content != null) ?
                           Digester.Digest(content, AOSignConstants.GetDigestAlgorithmName(signatureAlgorithm)) : messageDigest,
                           signDate,
                           padesMode,
                           contentType,
                           contentDescription
                           )
                       ));
        }
コード例 #6
0
ファイル: CAdESUtils.cs プロジェクト: luchees/clienteafirmaEN
        static Asn1EncodableVector InitContexExpecific(String digestAlgorithm,
                                                       byte[] datos,
                                                       String dataType,
                                                       byte[] messageDigest,
                                                       DateTime signDate,
                                                       bool padesMode)
        {
            // authenticatedAttributes
            Asn1EncodableVector contexExpecific = new Asn1EncodableVector();

            // tipo de contenido
            if (dataType != null)
            {
                contexExpecific.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(CmsAttributes.ContentType, new DerSet(new DerObjectIdentifier(dataType))));
            }

            // fecha de firma, no se anade en modo PAdES
            if (!padesMode)
            {
                contexExpecific.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(CmsAttributes.SigningTime, new DerSet(new DerUtcTime(signDate))));
            }

            // MessageDigest
            contexExpecific.Add(
                new Org.BouncyCastle.Asn1.Cms.Attribute(
                    CmsAttributes.MessageDigest,
                    new DerSet(new DerOctetString((messageDigest != null) ? messageDigest : Digester.Digest(datos, digestAlgorithm)))
                    )
                );

            return(contexExpecific);
        }
コード例 #7
0
ファイル: CAdESUtils.cs プロジェクト: luchees/clienteafirmaEN
        public static Asn1EncodableVector GenerateSignerInfo(X509Certificate2 cert,
                                                             String digestAlgorithmName,
                                                             byte[] datos,
                                                             AdESPolicy policy,
                                                             bool signingCertificateV2,
                                                             byte[] messageDigest,
                                                             DateTime signDate,
                                                             bool padesMode,
                                                             String contentType,
                                                             String contentDescription)
        {
            // ALGORITMO DE HUELLA DIGITAL
            AlgorithmIdentifier digestAlgorithmOID = SigUtils.MakeAlgId(AOAlgorithmID.GetOID(digestAlgorithmName));

            // // ATRIBUTOS

            // authenticatedAttributes
            Asn1EncodableVector contexExpecific = InitContexExpecific(
                digestAlgorithmName,
                datos,
                Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.Data.Id,
                messageDigest,
                signDate,
                padesMode
                );

            // Serial Number
            // comentar lo de abajo para version del rfc 3852

            if (signingCertificateV2)
            {
                // INICIO SINGING CERTIFICATE-V2

                /** IssuerSerial ::= SEQUENCE { issuer GeneralNames, serialNumber
                 * CertificateSerialNumber */



                TbsCertificateStructure tbs = TbsCertificateStructure.GetInstance(
                    Asn1Object.FromByteArray(
                        new Org.BouncyCastle.X509.X509Certificate(
                            X509CertificateStructure.GetInstance(
                                Asn1Object.FromByteArray(
                                    cert.GetRawCertData()))).GetTbsCertificate()));

                GeneralNames gns = new GeneralNames(new GeneralName(tbs.Issuer));

                IssuerSerial isuerSerial = new IssuerSerial(gns, tbs.SerialNumber);

                /** ESSCertIDv2 ::= SEQUENCE { hashAlgorithm AlgorithmIdentifier
                 * DEFAULT {algorithm id-sha256}, certHash Hash, issuerSerial
                 * IssuerSerial OPTIONAL }
                 * Hash ::= OCTET STRING */

                byte[]        certHash    = Digester.Digest(cert.GetRawCertData(), digestAlgorithmName);
                EssCertIDv2[] essCertIDv2 = { new EssCertIDv2(digestAlgorithmOID, certHash, isuerSerial) };

                /** PolicyInformation ::= SEQUENCE { policyIdentifier CertPolicyId,
                 * policyQualifiers SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo
                 * OPTIONAL }
                 * CertPolicyId ::= OBJECT IDENTIFIER
                 * PolicyQualifierInfo ::= SEQUENCE { policyQualifierId
                 * PolicyQualifierId, qualifier ANY DEFINED BY policyQualifierId } */

                SigningCertificateV2 scv2;
                if (policy.GetPolicyIdentifier() != null)
                {
                    /** SigningCertificateV2 ::= SEQUENCE { certs SEQUENCE OF
                     * ESSCertIDv2, policies SEQUENCE OF PolicyInformation OPTIONAL
                     * } */
                    scv2 = new SigningCertificateV2(essCertIDv2, GetPolicyInformation(policy)); // con politica
                }
                else
                {
                    scv2 = new SigningCertificateV2(essCertIDv2); // Sin politica
                }

                // Secuencia con singningCertificate
                contexExpecific.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAASigningCertificateV2, new DerSet(scv2)));

                // FIN SINGING CERTIFICATE-V2
            }
            else
            {
                // INICIO SINGNING CERTIFICATE

                /** IssuerSerial ::= SEQUENCE { issuer GeneralNames, serialNumber
                 * CertificateSerialNumber } */

                TbsCertificateStructure tbs = TbsCertificateStructure.GetInstance(
                    Asn1Object.FromByteArray(
                        new Org.BouncyCastle.X509.X509Certificate(
                            X509CertificateStructure.GetInstance(
                                Asn1Object.FromByteArray(
                                    cert.GetRawCertData()))).GetTbsCertificate()));

                GeneralName  gn  = new GeneralName(tbs.Issuer);
                GeneralNames gns = new GeneralNames(gn);

                IssuerSerial isuerSerial = new IssuerSerial(gns, tbs.SerialNumber);

                /** ESSCertID ::= SEQUENCE { certHash Hash, issuerSerial IssuerSerial
                 * OPTIONAL }
                 * Hash ::= OCTET STRING -- SHA1 hash of entire certificate */
                byte[] certHash = Digester.Digest(cert.GetRawCertData(), digestAlgorithmName);

                EssCertID essCertID = new EssCertID(certHash, isuerSerial);

                /** PolicyInformation ::= SEQUENCE { policyIdentifier CertPolicyId,
                 * policyQualifiers SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo
                 * OPTIONAL }
                 * CertPolicyId ::= OBJECT IDENTIFIER
                 * PolicyQualifierInfo ::= SEQUENCE { policyQualifierId
                 * PolicyQualifierId, qualifier ANY DEFINED BY policyQualifierId } */

                SigningCertificate scv;
                if (policy.GetPolicyIdentifier() != null)
                {
                    /** SigningCertificateV2 ::= SEQUENCE { certs SEQUENCE OF
                     * ESSCertIDv2, policies SEQUENCE OF PolicyInformation OPTIONAL
                     * } */
                    /*
                     * HAY QUE HACER UN SEQUENCE, YA QUE EL CONSTRUCTOR DE BOUNCY
                     * CASTLE NO TIENE DICHO CONSTRUCTOR.
                     */
                    Asn1EncodableVector v = new Asn1EncodableVector();
                    v.Add(new DerSequence(essCertID));
                    v.Add(new DerSequence(GetPolicyInformation(policy)));
                    scv = SigningCertificate.GetInstance(new DerSequence(v)); // con politica
                }
                else
                {
                    scv = new SigningCertificate(essCertID); // Sin politica
                }

                /** id-aa-signingCertificate OBJECT IDENTIFIER ::= { iso(1)
                 * member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs9(9) smime(16)
                 * id-aa(2) 12 } */
                // Secuencia con singningCertificate
                contexExpecific.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAASigningCertificate, new DerSet(scv)));
            }

            // INICIO SIGPOLICYID ATTRIBUTE

            if (policy.GetPolicyIdentifier() != null)
            {
                /**
                 * SigPolicyId ::= OBJECT IDENTIFIER Politica de firma.
                 */
                DerObjectIdentifier doiSigPolicyId = new DerObjectIdentifier(policy.GetPolicyIdentifier().ToLower().Replace("urn:oid:", ""));

                /**
                 *   OtherHashAlgAndValue ::= SEQUENCE {
                 *     hashAlgorithm    AlgorithmIdentifier,
                 *     hashValue        OCTET STRING }
                 *
                 */


                // Algoritmo para el hash
                AlgorithmIdentifier hashid;
                // si tenemos algoritmo de calculo de hash, lo ponemos
                if (policy.GetPolicyIdentifierHashAlgorithm() != null)
                {
                    hashid = SigUtils.MakeAlgId(
                        AOAlgorithmID.GetOID(
                            AOSignConstants.GetDigestAlgorithmName(
                                policy.GetPolicyIdentifierHashAlgorithm())));
                }
                // si no tenemos, ponemos el algoritmo de firma.
                else
                {
                    hashid = digestAlgorithmOID;
                }
                // hash del documento, descifrado en b64
                byte[] hashed;
                if (policy.GetPolicyIdentifierHash() != null)
                {
                    hashed = System.Convert.FromBase64String(policy.GetPolicyIdentifierHash());
                }
                else
                {
                    hashed = new byte[] { 0 };
                }

                DigestInfo otherHashAlgAndValue = new DigestInfo(hashid, hashed);

                /**
                 *   SigPolicyQualifierInfo ::= SEQUENCE {
                 *       SigPolicyQualifierId  SigPolicyQualifierId,
                 *       SigQualifier          ANY DEFINED BY policyQualifierId }
                 */

                AOSigPolicyQualifierInfo spqInfo = null;
                if (policy.GetPolicyQualifier() != null)
                {
                    spqInfo = new AOSigPolicyQualifierInfo(policy.GetPolicyQualifier().ToString());
                }

                /**
                 * SignaturePolicyId ::= SEQUENCE {
                 *  sigPolicyId           SigPolicyId,
                 *  sigPolicyHash         SigPolicyHash,
                 *  sigPolicyQualifiers   SEQUENCE SIZE (1..MAX) OF
                 *                          AOSigPolicyQualifierInfo OPTIONAL}
                 *
                 */
                Asn1EncodableVector v = new Asn1EncodableVector();
                // sigPolicyId
                v.Add(doiSigPolicyId);
                // sigPolicyHash
                v.Add(otherHashAlgAndValue.ToAsn1Object()); // como sequence
                // sigPolicyQualifiers
                if (spqInfo != null)
                {
                    v.Add(spqInfo.toASN1Primitive());
                }

                DerSequence ds = new DerSequence(v);

                // Secuencia con singningCertificate
                contexExpecific.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAAEtsSigPolicyID, new DerSet(ds.ToAsn1Object())));
                // FIN SIGPOLICYID ATTRIBUTE
            }

            /**
             * Secuencia con el tipo de contenido firmado. No se agrega en firmas PAdES.
             *
             * ContentHints ::= SEQUENCE {
             *	  contentDescription UTF8String (SIZE (1..MAX)) OPTIONAL,
             *	  contentType ContentType }
             */
            if (contentType != null && !padesMode)
            {
                ContentHints contentHints;
                if (contentDescription != null)
                {
                    contentHints = new ContentHints(new DerObjectIdentifier(contentType),
                                                    new DerUtf8String(contentDescription));
                }
                else
                {
                    contentHints = new ContentHints(new DerObjectIdentifier(contentType));
                }
                contexExpecific.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(
                                        Org.BouncyCastle.Asn1.Pkcs.PkcsObjectIdentifiers.IdAAContentHint,
                                        new DerSet(contentHints.ToAsn1Object())));
            }

            return(contexExpecific);
        }
コード例 #8
0
        private void SetValues(String identifier,
                               String identifierHash,
                               String identifierHashAlgorithm,
                               String qualifier)
        {
            if (identifier == null || "".Equals(identifier))
            {
                throw new ArgumentNullException("El identificador de politica no puede ser nulo ni vacio");
            }

            this.policyIdentifier = identifier;

            if (identifierHash != null && (!"0".Equals(identifierHash)) && (identifierHashAlgorithm == null || "".Equals(identifierHashAlgorithm)))
            {
                throw new ArgumentNullException("Si se indica la huella digital del identificador de politica es obligatorio indicar tambien el algoritmo");
            }

            if (identifierHash == null)
            {
                try
                {
                    //this.policyIdentifierHash = System.Convert.ToBase64String(Digester.Digest(UrlReader.ReadUri(new Uri(identifier)), "SHA-512"));
                    //MODIFICACION DAL -> NO DEBERÍA SER "new Uri(qualifier)" EN VEZ DE "new Uri(identifier)" ????
                    if (qualifier != null)
                    {
                        this.policyIdentifierHash          = System.Convert.ToBase64String(Digester.Digest(UrlReader.ReadUri(new Uri(qualifier)), "SHA-512"));
                        this.policyIdentifierHashAlgorithm = "SHA-512";
                    }
                }
                catch (Exception e)
                {
                    throw new ArgumentNullException("Si no se especifica la huella digital de la politica es necesario que el identificador sea una URL accesible universalmente: " + e, e);
                }
            }
            else
            {
                if ("0".Equals(identifierHash))
                {
                    this.policyIdentifierHash = null;
                }
                else
                {
                    if (!AOUtil.IsBase64(new System.Text.UTF8Encoding().GetBytes(identifierHash)))
                    {
                        throw new ArgumentNullException("La huella digital de la politica debe estar en formato Base64");
                    }
                    try
                    {
                        this.policyIdentifierHashAlgorithm = AOSignConstants.GetDigestAlgorithmName(identifierHashAlgorithm);
                    }
                    catch (Exception e)
                    {
                        throw new ArgumentNullException("El algoritmo de huella digital no esta soportado: " + identifierHashAlgorithm, e);
                    }
                    this.policyIdentifierHash = identifierHash;
                }
            }

            if (qualifier != null && (!"".Equals(qualifier)))
            {
                try
                {
                    this.policyQualifier = new Uri(qualifier);
                }
                catch (Exception e)
                {
                    throw new ArgumentNullException("El calificador de la politica debe ser una URL valida", e);
                }
            }
        }