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"); }
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); }