Ejemplo n.º 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");
            }
Ejemplo n.º 2
0
            internal ASN1 GetASN1()
            {
                if ((key == null) || (hashAlgorithm == null))
                {
                    return(null);
                }
                byte[] ver        = { version };
                ASN1   signerInfo = new ASN1(0x30);

                // version Version -> Version ::= INTEGER
                signerInfo.Add(new ASN1(0x02, ver));
                // issuerAndSerialNumber IssuerAndSerialNumber,
                signerInfo.Add(PKCS7.IssuerAndSerialNumber(x509));
                // digestAlgorithm DigestAlgorithmIdentifier,
                string hashOid = CryptoConfig.MapNameToOID(hashAlgorithm);

                signerInfo.Add(AlgorithmIdentifier(hashOid));
                // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
                ASN1 aa = null;

                if (authenticatedAttributes.Count > 0)
                {
                    aa = signerInfo.Add(new ASN1(0xA0));
                    authenticatedAttributes.Sort(new SortedSet());
                    foreach (ASN1 attr in authenticatedAttributes)
                    {
                        aa.Add(attr);
                    }
                }
                // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
                if (key is RSA)
                {
                    signerInfo.Add(AlgorithmIdentifier(PKCS7.Oid.rsaEncryption));

                    if (aa != null)
                    {
                        // Calculate the signature here; otherwise it must be set from SignedData
                        RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter(key);
                        r.SetHashAlgorithm(hashAlgorithm);
                        byte[] tbs = aa.GetBytes();
                        tbs [0] = 0x31;                         // not 0xA0 for signature
                        HashAlgorithm ha      = HashAlgorithm.Create(hashAlgorithm);
                        byte[]        tbsHash = ha.ComputeHash(tbs);
                        signature = r.CreateSignature(tbsHash);
                    }
                }
                else if (key is DSA)
                {
                    throw new NotImplementedException("not yet");
                }
                else
                {
                    throw new CryptographicException("Unknown assymetric algorithm");
                }
                // encryptedDigest EncryptedDigest,
                signerInfo.Add(new ASN1(0x04, signature));
                // unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
                if (unauthenticatedAttributes.Count > 0)
                {
                    ASN1 ua = signerInfo.Add(new ASN1(0xA1));
                    unauthenticatedAttributes.Sort(new SortedSet());
                    foreach (ASN1 attr in unauthenticatedAttributes)
                    {
                        ua.Add(attr);
                    }
                }
                return(signerInfo);
            }
Ejemplo n.º 3
0
		public byte[] Decrypt (PKCS7.EncryptedData ed)
		{
			return Decrypt (ed.EncryptionAlgorithm.ContentType, 
				ed.EncryptionAlgorithm.Content [0].Value, 
				ASN1Convert.ToInt32 (ed.EncryptionAlgorithm.Content [1]),
				ed.EncryptedContent);
		}
Ejemplo n.º 4
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);
            }