예제 #1
0
            internal ASN1 GetASN1()
            {
                if (this.key == null || this.hashAlgorithm == null)
                {
                    return(null);
                }
                byte[] data = new byte[]
                {
                    this.version
                };
                ASN1 asn = new ASN1(48);

                asn.Add(new ASN1(2, data));
                asn.Add(PKCS7.IssuerAndSerialNumber(this.x509));
                string oid = CryptoConfig.MapNameToOID(this.hashAlgorithm);

                asn.Add(PKCS7.AlgorithmIdentifier(oid));
                ASN1 asn2 = null;

                if (this.authenticatedAttributes.Count > 0)
                {
                    asn2 = asn.Add(new ASN1(160));
                    foreach (object obj in this.authenticatedAttributes)
                    {
                        ASN1 asn3 = (ASN1)obj;
                        asn2.Add(asn3);
                    }
                }
                if (this.key is RSA)
                {
                    asn.Add(PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1"));
                    if (asn2 != null)
                    {
                        RSAPKCS1SignatureFormatter rsapkcs1SignatureFormatter = new RSAPKCS1SignatureFormatter(this.key);
                        rsapkcs1SignatureFormatter.SetHashAlgorithm(this.hashAlgorithm);
                        byte[] bytes = asn2.GetBytes();
                        bytes[0] = 49;
                        HashAlgorithm hashAlgorithm = HashAlgorithm.Create(this.hashAlgorithm);
                        byte[]        rgbHash       = hashAlgorithm.ComputeHash(bytes);
                        this.signature = rsapkcs1SignatureFormatter.CreateSignature(rgbHash);
                    }
                    asn.Add(new ASN1(4, this.signature));
                    if (this.unauthenticatedAttributes.Count > 0)
                    {
                        ASN1 asn4 = asn.Add(new ASN1(161));
                        foreach (object obj2 in this.unauthenticatedAttributes)
                        {
                            ASN1 asn5 = (ASN1)obj2;
                            asn4.Add(asn5);
                        }
                    }
                    return(asn);
                }
                if (this.key is DSA)
                {
                    throw new NotImplementedException("not yet");
                }
                throw new CryptographicException("Unknown assymetric algorithm");
            }
예제 #2
0
            internal ASN1 GetASN1()
            {
                ASN1 asn = new ASN1(48);

                byte[] data = new byte[]
                {
                    this.version
                };
                asn.Add(new ASN1(2, data));
                ASN1 asn2 = asn.Add(new ASN1(49));

                if (this.hashAlgorithm != null)
                {
                    string oid = CryptoConfig.MapNameToOID(this.hashAlgorithm);
                    asn2.Add(PKCS7.AlgorithmIdentifier(oid));
                }
                ASN1 asn3 = this.contentInfo.ASN1;

                asn.Add(asn3);
                if (!this.signed && this.hashAlgorithm != null)
                {
                    if (this.mda)
                    {
                        ASN1 value = PKCS7.Attribute("1.2.840.113549.1.9.3", asn3[0]);
                        this.signerInfo.AuthenticatedAttributes.Add(value);
                        HashAlgorithm hashAlgorithm = HashAlgorithm.Create(this.hashAlgorithm);
                        byte[]        data2         = hashAlgorithm.ComputeHash(asn3[1][0].Value);
                        ASN1          asn4          = new ASN1(48);
                        ASN1          value2        = PKCS7.Attribute("1.2.840.113549.1.9.4", asn4.Add(new ASN1(4, data2)));
                        this.signerInfo.AuthenticatedAttributes.Add(value2);
                    }
                    else
                    {
                        RSAPKCS1SignatureFormatter rsapkcs1SignatureFormatter = new RSAPKCS1SignatureFormatter(this.signerInfo.Key);
                        rsapkcs1SignatureFormatter.SetHashAlgorithm(this.hashAlgorithm);
                        HashAlgorithm hashAlgorithm2 = HashAlgorithm.Create(this.hashAlgorithm);
                        byte[]        rgbHash        = hashAlgorithm2.ComputeHash(asn3[1][0].Value);
                        this.signerInfo.Signature = rsapkcs1SignatureFormatter.CreateSignature(rgbHash);
                    }
                    this.signed = true;
                }
                if (this.certs.Count > 0)
                {
                    ASN1 asn5 = asn.Add(new ASN1(160));
                    foreach (X509Certificate x509Certificate in this.certs)
                    {
                        asn5.Add(new ASN1(x509Certificate.RawData));
                    }
                }
                if (this.crls.Count > 0)
                {
                    ASN1 asn6 = asn.Add(new ASN1(161));
                    foreach (object obj in this.crls)
                    {
                        byte[] data3 = (byte[])obj;
                        asn6.Add(new ASN1(data3));
                    }
                }
                ASN1 asn7 = asn.Add(new ASN1(49));

                if (this.signerInfo.Key != null)
                {
                    asn7.Add(this.signerInfo.ASN1);
                }
                return(asn);
            }