public TimeStampTokenGenerator(AsymmetricKeyParameter key, X509Certificate cert, string digestOID, string tsaPolicyOID, Org.BouncyCastle.Asn1.Cms.AttributeTable signedAttr, Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttr) { this.key = key; this.cert = cert; this.digestOID = digestOID; this.tsaPolicyOID = tsaPolicyOID; this.unsignedAttr = unsignedAttr; TspUtil.ValidateCertificate(cert); IDictionary dictionary; if (signedAttr != null) { dictionary = signedAttr.ToDictionary(); } else { dictionary = Platform.CreateHashtable(); } try { byte[] hash = DigestUtilities.CalculateDigest("SHA-1", cert.GetEncoded()); EssCertID essCertID = new EssCertID(hash); Org.BouncyCastle.Asn1.Cms.Attribute attribute = new Org.BouncyCastle.Asn1.Cms.Attribute(PkcsObjectIdentifiers.IdAASigningCertificate, new DerSet(new SigningCertificate(essCertID))); dictionary[attribute.AttrType] = attribute; } catch (CertificateEncodingException e) { throw new TspException("Exception processing certificate.", e); } catch (SecurityUtilityException e2) { throw new TspException("Can't find a SHA-1 implementation.", e2); } this.signedAttr = new Org.BouncyCastle.Asn1.Cms.AttributeTable(dictionary); }
internal SignerInfo ToSignerInfo(DerObjectIdentifier contentType, CmsProcessable content, SecureRandom random) { //IL_00bf: Unknown result type (might be due to invalid IL or missing references) //IL_00c6: Expected O, but got Unknown AlgorithmIdentifier digestAlgorithmID = DigestAlgorithmID; string digestAlgName = Helper.GetDigestAlgName(digestOID); string algorithm = digestAlgName + "with" + Helper.GetEncryptionAlgName(encOID); byte[] array; if (outer._digests.Contains((object)digestOID)) { array = (byte[])outer._digests.get_Item((object)digestOID); } else { IDigest digestInstance = Helper.GetDigestInstance(digestAlgName); content?.Write((Stream)(object)new DigOutputStream(digestInstance)); array = DigestUtilities.DoFinal(digestInstance); outer._digests.Add((object)digestOID, ((global::System.Array)array).Clone()); } IStreamCalculator streamCalculator = sigCalc.CreateCalculator(); Stream val = (Stream) new BufferedStream(streamCalculator.Stream); Asn1Set asn1Set = null; if (sAttr != null) { IDictionary baseParameters = outer.GetBaseParameters(contentType, digestAlgorithmID, array); Org.BouncyCastle.Asn1.Cms.AttributeTable attributeTable = sAttr.GetAttributes(baseParameters); if (contentType == null && attributeTable != null && attributeTable[CmsAttributes.ContentType] != null) { IDictionary val2 = attributeTable.ToDictionary(); val2.Remove((object)CmsAttributes.ContentType); attributeTable = new Org.BouncyCastle.Asn1.Cms.AttributeTable(val2); } asn1Set = outer.GetAttributeSet(attributeTable); new DerOutputStream(val).WriteObject(asn1Set); } else { content?.Write(val); } Platform.Dispose(val); byte[] array2 = ((IBlockResult)streamCalculator.GetResult()).Collect(); Asn1Set unauthenticatedAttributes = null; if (unsAttr != null) { IDictionary baseParameters2 = outer.GetBaseParameters(contentType, digestAlgorithmID, array); baseParameters2.set_Item((object)CmsAttributeTableParameter.Signature, ((global::System.Array)array2).Clone()); Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = unsAttr.GetAttributes(baseParameters2); unauthenticatedAttributes = outer.GetAttributeSet(attributes); } Asn1Encodable defaultX509Parameters = SignerUtilities.GetDefaultX509Parameters(algorithm); AlgorithmIdentifier encAlgorithmIdentifier = Helper.GetEncAlgorithmIdentifier(new DerObjectIdentifier(encOID), defaultX509Parameters); return(new SignerInfo(signerIdentifier, digestAlgorithmID, asn1Set, encAlgorithmIdentifier, new DerOctetString(array2), unauthenticatedAttributes)); }
internal SignerInfo ToSignerInfo(DerObjectIdentifier contentType, CmsProcessable content, SecureRandom random) { AlgorithmIdentifier digestAlgorithmID = DigestAlgorithmID; string digestAlgName = Helper.GetDigestAlgName(digestOID); string algorithm = digestAlgName + "with" + Helper.GetEncryptionAlgName(encOID); byte[] array; if (outer._digests.Contains(digestOID)) { array = (byte[])outer._digests[digestOID]; } else { IDigest digestInstance = Helper.GetDigestInstance(digestAlgName); content?.Write(new DigOutputStream(digestInstance)); array = DigestUtilities.DoFinal(digestInstance); outer._digests.Add(digestOID, array.Clone()); } IStreamCalculator streamCalculator = sigCalc.CreateCalculator(); Stream stream = new BufferedStream(streamCalculator.Stream); Asn1Set asn1Set = null; if (sAttr != null) { IDictionary baseParameters = outer.GetBaseParameters(contentType, digestAlgorithmID, array); Org.BouncyCastle.Asn1.Cms.AttributeTable attributeTable = sAttr.GetAttributes(baseParameters); if (contentType == null && attributeTable != null && attributeTable[CmsAttributes.ContentType] != null) { IDictionary dictionary = attributeTable.ToDictionary(); dictionary.Remove(CmsAttributes.ContentType); attributeTable = new Org.BouncyCastle.Asn1.Cms.AttributeTable(dictionary); } asn1Set = outer.GetAttributeSet(attributeTable); new DerOutputStream(stream).WriteObject(asn1Set); } else { content?.Write(stream); } Platform.Dispose(stream); byte[] array2 = ((IBlockResult)streamCalculator.GetResult()).Collect(); Asn1Set unauthenticatedAttributes = null; if (unsAttr != null) { IDictionary baseParameters2 = outer.GetBaseParameters(contentType, digestAlgorithmID, array); baseParameters2[CmsAttributeTableParameter.Signature] = array2.Clone(); Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = unsAttr.GetAttributes(baseParameters2); unauthenticatedAttributes = outer.GetAttributeSet(attributes); } Asn1Encodable defaultX509Parameters = SignerUtilities.GetDefaultX509Parameters(algorithm); AlgorithmIdentifier encAlgorithmIdentifier = Helper.GetEncAlgorithmIdentifier(new DerObjectIdentifier(encOID), defaultX509Parameters); return(new SignerInfo(signerIdentifier, digestAlgorithmID, asn1Set, encAlgorithmIdentifier, new DerOctetString(array2), unauthenticatedAttributes)); }
/** * Initialise with some extra attributes or overrides. * * @param attributeTable initial attribute table to use. */ public DefaultSignedAttributeTableGenerator( AttributeTable attributeTable) { if (attributeTable != null) { table = attributeTable.ToDictionary(); } else { table = Platform.CreateHashtable(); } }
public SignerInfo Generate(DerObjectIdentifier contentType, AlgorithmIdentifier digestAlgorithm, byte[] calculatedDigest) { SignerInfo result; try { string digestAlgName = CmsSignedDataStreamGenerator.Helper.GetDigestAlgName(this._digestOID); string algorithm = digestAlgName + "with" + this._encName; byte[] array = calculatedDigest; Asn1Set asn1Set = null; if (this._sAttr != null) { IDictionary baseParameters = this.outer.GetBaseParameters(contentType, digestAlgorithm, calculatedDigest); Org.BouncyCastle.Asn1.Cms.AttributeTable attributeTable = this._sAttr.GetAttributes(baseParameters); if (contentType == null && attributeTable != null && attributeTable[CmsAttributes.ContentType] != null) { IDictionary dictionary = attributeTable.ToDictionary(); dictionary.Remove(CmsAttributes.ContentType); attributeTable = new Org.BouncyCastle.Asn1.Cms.AttributeTable(dictionary); } asn1Set = this.outer.GetAttributeSet(attributeTable); array = asn1Set.GetEncoded("DER"); } else if (this._encName.Equals("RSA")) { DigestInfo digestInfo = new DigestInfo(digestAlgorithm, calculatedDigest); array = digestInfo.GetEncoded("DER"); } this._sig.BlockUpdate(array, 0, array.Length); byte[] array2 = this._sig.GenerateSignature(); Asn1Set unauthenticatedAttributes = null; if (this._unsAttr != null) { IDictionary baseParameters2 = this.outer.GetBaseParameters(contentType, digestAlgorithm, calculatedDigest); baseParameters2[CmsAttributeTableParameter.Signature] = array2.Clone(); Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = this._unsAttr.GetAttributes(baseParameters2); unauthenticatedAttributes = this.outer.GetAttributeSet(attributes); } Asn1Encodable defaultX509Parameters = SignerUtilities.GetDefaultX509Parameters(algorithm); AlgorithmIdentifier encAlgorithmIdentifier = CmsSignedGenerator.GetEncAlgorithmIdentifier(new DerObjectIdentifier(this._encOID), defaultX509Parameters); result = new SignerInfo(this._signerIdentifier, digestAlgorithm, asn1Set, encAlgorithmIdentifier, new DerOctetString(array2), unauthenticatedAttributes); } catch (IOException e) { throw new CmsStreamException("encoding error.", e); } catch (SignatureException e2) { throw new CmsStreamException("error creating signature.", e2); } return(result); }
internal SignerInfo ToSignerInfo(DerObjectIdentifier contentType, CmsProcessable content, SecureRandom random) { AlgorithmIdentifier digestAlgorithmID = this.DigestAlgorithmID; string digestAlgName = CmsSignedDataGenerator.Helper.GetDigestAlgName(this.digestOID); IDigest digestInstance = CmsSignedDataGenerator.Helper.GetDigestInstance(digestAlgName); string algorithm = digestAlgName + "with" + CmsSignedDataGenerator.Helper.GetEncryptionAlgName(this.encOID); ISigner signatureInstance = CmsSignedDataGenerator.Helper.GetSignatureInstance(algorithm); if (content != null) { content.Write(new DigOutputStream(digestInstance)); } byte[] array = DigestUtilities.DoFinal(digestInstance); this.outer._digests.Add(this.digestOID, array.Clone()); signatureInstance.Init(true, new ParametersWithRandom(this.key, random)); Stream stream = new BufferedStream(new SigOutputStream(signatureInstance)); Asn1Set asn1Set = null; if (this.sAttr != null) { IDictionary baseParameters = this.outer.GetBaseParameters(contentType, digestAlgorithmID, array); Org.BouncyCastle.Asn1.Cms.AttributeTable attributeTable = this.sAttr.GetAttributes(baseParameters); if (contentType == null && attributeTable != null && attributeTable[CmsAttributes.ContentType] != null) { IDictionary dictionary = attributeTable.ToDictionary(); dictionary.Remove(CmsAttributes.ContentType); attributeTable = new Org.BouncyCastle.Asn1.Cms.AttributeTable(dictionary); } asn1Set = this.outer.GetAttributeSet(attributeTable); new DerOutputStream(stream).WriteObject(asn1Set); } else if (content != null) { content.Write(stream); } stream.Close(); byte[] array2 = signatureInstance.GenerateSignature(); Asn1Set unauthenticatedAttributes = null; if (this.unsAttr != null) { IDictionary baseParameters2 = this.outer.GetBaseParameters(contentType, digestAlgorithmID, array); baseParameters2[CmsAttributeTableParameter.Signature] = array2.Clone(); Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = this.unsAttr.GetAttributes(baseParameters2); unauthenticatedAttributes = this.outer.GetAttributeSet(attributes); } Asn1Encodable defaultX509Parameters = SignerUtilities.GetDefaultX509Parameters(algorithm); AlgorithmIdentifier encAlgorithmIdentifier = CmsSignedGenerator.GetEncAlgorithmIdentifier(new DerObjectIdentifier(this.encOID), defaultX509Parameters); return(new SignerInfo(this.signerIdentifier, digestAlgorithmID, asn1Set, encAlgorithmIdentifier, new DerOctetString(array2), unauthenticatedAttributes)); }
public SignerInfo Generate(DerObjectIdentifier contentType, AlgorithmIdentifier digestAlgorithm, byte[] calculatedDigest) { //IL_016e: Expected O, but got Unknown try { string digestAlgName = Helper.GetDigestAlgName(_digestOID); string algorithm = digestAlgName + "with" + _encName; byte[] array = calculatedDigest; Asn1Set asn1Set = null; if (_sAttr != null) { IDictionary baseParameters = outer.GetBaseParameters(contentType, digestAlgorithm, calculatedDigest); Org.BouncyCastle.Asn1.Cms.AttributeTable attributeTable = _sAttr.GetAttributes(baseParameters); if (contentType == null && attributeTable != null && attributeTable[CmsAttributes.ContentType] != null) { IDictionary val = attributeTable.ToDictionary(); val.Remove((object)CmsAttributes.ContentType); attributeTable = new Org.BouncyCastle.Asn1.Cms.AttributeTable(val); } asn1Set = outer.GetAttributeSet(attributeTable); array = asn1Set.GetEncoded("DER"); } else if (_encName.Equals("RSA")) { DigestInfo digestInfo = new DigestInfo(digestAlgorithm, calculatedDigest); array = digestInfo.GetEncoded("DER"); } _sig.BlockUpdate(array, 0, array.Length); byte[] array2 = _sig.GenerateSignature(); Asn1Set unauthenticatedAttributes = null; if (_unsAttr != null) { IDictionary baseParameters2 = outer.GetBaseParameters(contentType, digestAlgorithm, calculatedDigest); baseParameters2.set_Item((object)CmsAttributeTableParameter.Signature, ((global::System.Array)array2).Clone()); Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = _unsAttr.GetAttributes(baseParameters2); unauthenticatedAttributes = outer.GetAttributeSet(attributes); } Asn1Encodable defaultX509Parameters = SignerUtilities.GetDefaultX509Parameters(algorithm); AlgorithmIdentifier encAlgorithmIdentifier = Helper.GetEncAlgorithmIdentifier(new DerObjectIdentifier(_encOID), defaultX509Parameters); return(new SignerInfo(_signerIdentifier, digestAlgorithm, asn1Set, encAlgorithmIdentifier, new DerOctetString(array2), unauthenticatedAttributes)); } catch (IOException val2) { IOException e = val2; throw new CmsStreamException("encoding error.", (global::System.Exception)(object) e); } catch (SignatureException e2) { throw new CmsStreamException("error creating signature.", e2); } }
public override void PerformTest() { Asn1EncodableVector v = new Asn1EncodableVector( new Asn1Cms.Attribute(type1, new DerSet(type1)), new Asn1Cms.Attribute(type2, new DerSet(type2))); Asn1Cms.AttributeTable table = new Asn1Cms.AttributeTable(v); Asn1Cms.Attribute a = table[type1]; if (a == null) { Fail("type1 attribute not found."); } if (!a.AttrValues.Equals(new DerSet(type1))) { Fail("wrong value retrieved for type1!"); } a = table[type2]; if (a == null) { Fail("type2 attribute not found."); } if (!a.AttrValues.Equals(new DerSet(type2))) { Fail("wrong value retrieved for type2!"); } a = table[type3]; if (a != null) { Fail("type3 attribute found when none expected."); } Asn1EncodableVector vec = table.GetAll(type1); if (vec.Count != 1) { Fail("wrong vector size for type1."); } vec = table.GetAll(type3); if (vec.Count != 0) { Fail("wrong vector size for type3."); } vec = table.ToAsn1EncodableVector(); if (vec.Count != 2) { Fail("wrong vector size for single."); } IDictionary t = table.ToDictionary(); if (t.Count != 2) { Fail("hashtable wrong size."); } // multiple v = new Asn1EncodableVector( new Asn1Cms.Attribute(type1, new DerSet(type1)), new Asn1Cms.Attribute(type1, new DerSet(type2)), new Asn1Cms.Attribute(type1, new DerSet(type3)), new Asn1Cms.Attribute(type2, new DerSet(type2))); table = new Asn1Cms.AttributeTable(v); a = table[type1]; if (!a.AttrValues.Equals(new DerSet(type1))) { Fail("wrong value retrieved for type1 multi Get!"); } vec = table.GetAll(type1); if (vec.Count != 3) { Fail("wrong vector size for multiple type1."); } a = (Asn1Cms.Attribute)vec[0]; if (!a.AttrValues.Equals(new DerSet(type1))) { Fail("wrong value retrieved for type1(0)!"); } a = (Asn1Cms.Attribute)vec[1]; if (!a.AttrValues.Equals(new DerSet(type2))) { Fail("wrong value retrieved for type1(1)!"); } a = (Asn1Cms.Attribute)vec[2]; if (!a.AttrValues.Equals(new DerSet(type3))) { Fail("wrong value retrieved for type1(2)!"); } vec = table.GetAll(type2); if (vec.Count != 1) { Fail("wrong vector size for multiple type2."); } vec = table.ToAsn1EncodableVector(); if (vec.Count != 4) { Fail("wrong vector size for multiple."); } }
internal SignerInfo ToSignerInfo( DerObjectIdentifier contentType, CmsProcessable content, SecureRandom random) { AlgorithmIdentifier digAlgId = DigestAlgorithmID; string digestName = Helper.GetDigestAlgName(digestOID); IDigest dig = Helper.GetDigestInstance(digestName); //jbonilla string signatureName = digestName + "with" + Helper.GetEncryptionAlgName(encOID); ISigner sig = outer._signerProvider != null ? outer._signerProvider : Helper.GetSignatureInstance(signatureName); byte[] hash = null; byte[] preCalculatedHash = ((CustomCMSSignedDataGenerator)outer).PreCalculatedDigest; if (preCalculatedHash != null) { hash = preCalculatedHash; } else if (content != null) { content.Write(new DigOutputStream(dig)); hash = DigestUtilities.DoFinal(dig); } outer._digests.Add(digestOID, hash.Clone()); sig.Init(true, new ParametersWithRandom(key, random)); #if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT Stream sigStr = new SigOutputStream(sig); #else Stream sigStr = new BufferedStream(new SigOutputStream(sig)); #endif Asn1Set signedAttr = null; if (sAttr != null) { IDictionary parameters = outer.GetBaseParameters(contentType, digAlgId, hash); //Asn1.Cms.AttributeTable signed = sAttr.GetAttributes(Collections.unmodifiableMap(parameters)); Org.BouncyCastle.Asn1.Cms.AttributeTable signed = sAttr.GetAttributes(parameters); if (contentType == null) //counter signature { if (signed != null && signed[CmsAttributes.ContentType] != null) { IDictionary tmpSigned = signed.ToDictionary(); tmpSigned.Remove(CmsAttributes.ContentType); signed = new Org.BouncyCastle.Asn1.Cms.AttributeTable(tmpSigned); } } // TODO Validate proposed signed attributes signedAttr = outer.GetAttributeSet(signed); // sig must be composed from the DER encoding. new DerOutputStream(sigStr).WriteObject(signedAttr); } else if (content != null) { // TODO Use raw signature of the hash value instead content.Write(sigStr); } sigStr.Close(); byte[] sigBytes = sig.GenerateSignature(); Asn1Set unsignedAttr = null; if (unsAttr != null) { IDictionary baseParameters = outer.GetBaseParameters(contentType, digAlgId, hash); baseParameters[CmsAttributeTableParameter.Signature] = sigBytes.Clone(); // Asn1.Cms.AttributeTable unsigned = unsAttr.GetAttributes(Collections.unmodifiableMap(baseParameters)); Asn1.Cms.AttributeTable unsigned = unsAttr.GetAttributes(baseParameters); // TODO Validate proposed unsigned attributes unsignedAttr = outer.GetAttributeSet(unsigned); } // TODO[RSAPSS] Need the ability to specify non-default parameters Asn1Encodable sigX509Parameters = SignerUtilities.GetDefaultX509Parameters(signatureName); AlgorithmIdentifier encAlgId = CmsSignedGenerator.GetEncAlgorithmIdentifier( new DerObjectIdentifier(encOID), sigX509Parameters); return(new SignerInfo(signerIdentifier, digAlgId, signedAttr, encAlgId, new DerOctetString(sigBytes), unsignedAttr)); }