private Asn1Object GetSingleValuedSignedAttribute(DerObjectIdentifier attrOID, string printableName) { Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = this.UnsignedAttributes; if (unsignedAttributes != null && unsignedAttributes.GetAll(attrOID).Count > 0) { throw new CmsException("The " + printableName + " attribute MUST NOT be an unsigned attribute"); } Org.BouncyCastle.Asn1.Cms.AttributeTable signedAttributes = this.SignedAttributes; if (signedAttributes == null) { return(null); } Asn1EncodableVector all = signedAttributes.GetAll(attrOID); switch (all.Count) { case 0: return(null); case 1: { Org.BouncyCastle.Asn1.Cms.Attribute attribute = (Org.BouncyCastle.Asn1.Cms.Attribute)all[0]; Asn1Set attrValues = attribute.AttrValues; if (attrValues.Count != 1) { throw new CmsException("A " + printableName + " attribute MUST have a single attribute value"); } return(attrValues[0].ToAsn1Object()); } default: throw new CmsException("The SignedAttributes in a signerInfo MUST NOT include multiple instances of the " + printableName + " attribute"); } }
public SignerInformationStore GetCounterSignatures() { Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = UnsignedAttributes; if (unsignedAttributes == null) { return(new SignerInformationStore((global::System.Collections.ICollection)Platform.CreateArrayList(0))); } global::System.Collections.IList list = Platform.CreateArrayList(); Asn1EncodableVector all = unsignedAttributes.GetAll(CmsAttributes.CounterSignature); global::System.Collections.IEnumerator enumerator = all.GetEnumerator(); try { while (enumerator.MoveNext()) { Attribute attribute = (Attribute)enumerator.get_Current(); Asn1Set attrValues = attribute.AttrValues; _ = attrValues.Count; _ = 1; { global::System.Collections.IEnumerator enumerator2 = attrValues.GetEnumerator(); try { while (enumerator2.MoveNext()) { Asn1Encodable asn1Encodable = (Asn1Encodable)enumerator2.get_Current(); Org.BouncyCastle.Asn1.Cms.SignerInfo instance = Org.BouncyCastle.Asn1.Cms.SignerInfo.GetInstance(asn1Encodable.ToAsn1Object()); string digestAlgName = CmsSignedHelper.Instance.GetDigestAlgName(instance.DigestAlgorithm.Algorithm.Id); list.Add((object)new SignerInformation(instance, null, null, new CounterSignatureDigestCalculator(digestAlgName, GetSignature()))); } } finally { global::System.IDisposable disposable2 = enumerator2 as global::System.IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } } } } finally { global::System.IDisposable disposable = enumerator as global::System.IDisposable; if (disposable != null) { disposable.Dispose(); } } return(new SignerInformationStore((global::System.Collections.ICollection)list)); }
public SignerInformationStore GetCounterSignatures() { Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = this.UnsignedAttributes; if (unsignedAttributes == null) { return(new SignerInformationStore(Platform.CreateArrayList(0))); } IList list = Platform.CreateArrayList(); Asn1EncodableVector all = unsignedAttributes.GetAll(CmsAttributes.CounterSignature); foreach (Org.BouncyCastle.Asn1.Cms.Attribute attribute in all) { Asn1Set attrValues = attribute.AttrValues; int arg_53_0 = attrValues.Count; foreach (Asn1Encodable asn1Encodable in attrValues) { Org.BouncyCastle.Asn1.Cms.SignerInfo instance = Org.BouncyCastle.Asn1.Cms.SignerInfo.GetInstance(asn1Encodable.ToAsn1Object()); string digestAlgName = CmsSignedHelper.Instance.GetDigestAlgName(instance.DigestAlgorithm.ObjectID.Id); list.Add(new SignerInformation(instance, null, null, new CounterSignatureDigestCalculator(digestAlgName, this.GetSignature()))); } } return(new SignerInformationStore(list)); }
public static ICollection GetSignatureTimestamps(SignerInformation signerInfo) { IList list = Platform.CreateArrayList(); Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = signerInfo.UnsignedAttributes; if (unsignedAttributes != null) { foreach (Org.BouncyCastle.Asn1.Cms.Attribute item in unsignedAttributes.GetAll(PkcsObjectIdentifiers.IdAASignatureTimeStampToken)) { foreach (Asn1Encodable attrValue in item.AttrValues) { try { Org.BouncyCastle.Asn1.Cms.ContentInfo instance = Org.BouncyCastle.Asn1.Cms.ContentInfo.GetInstance(attrValue.ToAsn1Object()); TimeStampToken timeStampToken = new TimeStampToken(instance); TimeStampTokenInfo timeStampInfo = timeStampToken.TimeStampInfo; byte[] a = DigestUtilities.CalculateDigest(GetDigestAlgName(timeStampInfo.MessageImprintAlgOid), signerInfo.GetSignature()); if (!Arrays.ConstantTimeAreEqual(a, timeStampInfo.GetMessageImprintDigest())) { throw new TspValidationException("Incorrect digest in message imprint"); } list.Add(timeStampToken); } catch (SecurityUtilityException) { throw new TspValidationException("Unknown hash algorithm specified in timestamp"); } catch (Exception) { throw new TspValidationException("Timestamp could not be parsed"); } } } return(list); } return(list); }
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."); } }
private bool DoVerify(AsymmetricKeyParameter key) { string digestAlgName = SignerInformation.Helper.GetDigestAlgName(this.DigestAlgOid); IDigest digestInstance = SignerInformation.Helper.GetDigestInstance(digestAlgName); DerObjectIdentifier objectID = this.encryptionAlgorithm.ObjectID; Asn1Encodable parameters = this.encryptionAlgorithm.Parameters; ISigner signer; if (objectID.Equals(PkcsObjectIdentifiers.IdRsassaPss)) { if (parameters == null) { throw new CmsException("RSASSA-PSS signature must specify algorithm parameters"); } try { RsassaPssParameters instance = RsassaPssParameters.GetInstance(parameters.ToAsn1Object()); if (!instance.HashAlgorithm.ObjectID.Equals(this.digestAlgorithm.ObjectID)) { throw new CmsException("RSASSA-PSS signature parameters specified incorrect hash algorithm"); } if (!instance.MaskGenAlgorithm.ObjectID.Equals(PkcsObjectIdentifiers.IdMgf1)) { throw new CmsException("RSASSA-PSS signature parameters specified unknown MGF"); } IDigest digest = DigestUtilities.GetDigest(instance.HashAlgorithm.ObjectID); int intValue = instance.SaltLength.Value.IntValue; byte b = (byte)instance.TrailerField.Value.IntValue; if (b != 1) { throw new CmsException("RSASSA-PSS signature parameters must have trailerField of 1"); } signer = new PssSigner(new RsaBlindedEngine(), digest, intValue); goto IL_142; } catch (Exception e) { throw new CmsException("failed to set RSASSA-PSS signature parameters", e); } } string algorithm = digestAlgName + "with" + SignerInformation.Helper.GetEncryptionAlgName(this.EncryptionAlgOid); signer = SignerInformation.Helper.GetSignatureInstance(algorithm); try { IL_142: if (this.digestCalculator != null) { this.resultDigest = this.digestCalculator.GetDigest(); } else { if (this.content != null) { this.content.Write(new DigOutputStream(digestInstance)); } else if (this.signedAttributeSet == null) { throw new CmsException("data not encapsulated in signature - use detached constructor."); } this.resultDigest = DigestUtilities.DoFinal(digestInstance); } } catch (IOException e2) { throw new CmsException("can't process mime object to create signature.", e2); } Asn1Object singleValuedSignedAttribute = this.GetSingleValuedSignedAttribute(CmsAttributes.ContentType, "content-type"); if (singleValuedSignedAttribute == null) { if (!this.isCounterSignature && this.signedAttributeSet != null) { throw new CmsException("The content-type attribute type MUST be present whenever signed attributes are present in signed-data"); } } else { if (this.isCounterSignature) { throw new CmsException("[For counter signatures,] the signedAttributes field MUST NOT contain a content-type attribute"); } if (!(singleValuedSignedAttribute is DerObjectIdentifier)) { throw new CmsException("content-type attribute value not of ASN.1 type 'OBJECT IDENTIFIER'"); } DerObjectIdentifier derObjectIdentifier = (DerObjectIdentifier)singleValuedSignedAttribute; if (!derObjectIdentifier.Equals(this.contentType)) { throw new CmsException("content-type attribute value does not match eContentType"); } } Asn1Object singleValuedSignedAttribute2 = this.GetSingleValuedSignedAttribute(CmsAttributes.MessageDigest, "message-digest"); if (singleValuedSignedAttribute2 == null) { if (this.signedAttributeSet != null) { throw new CmsException("the message-digest signed attribute type MUST be present when there are any signed attributes present"); } } else { if (!(singleValuedSignedAttribute2 is Asn1OctetString)) { throw new CmsException("message-digest attribute value not of ASN.1 type 'OCTET STRING'"); } Asn1OctetString asn1OctetString = (Asn1OctetString)singleValuedSignedAttribute2; if (!Arrays.AreEqual(this.resultDigest, asn1OctetString.GetOctets())) { throw new CmsException("message-digest attribute value does not match calculated value"); } } Org.BouncyCastle.Asn1.Cms.AttributeTable signedAttributes = this.SignedAttributes; if (signedAttributes != null && signedAttributes.GetAll(CmsAttributes.CounterSignature).Count > 0) { throw new CmsException("A countersignature attribute MUST NOT be a signed attribute"); } Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = this.UnsignedAttributes; if (unsignedAttributes != null) { foreach (Org.BouncyCastle.Asn1.Cms.Attribute attribute in unsignedAttributes.GetAll(CmsAttributes.CounterSignature)) { if (attribute.AttrValues.Count < 1) { throw new CmsException("A countersignature attribute MUST contain at least one AttributeValue"); } } } bool result; try { signer.Init(false, key); if (this.signedAttributeSet == null) { if (this.digestCalculator != null) { result = this.VerifyDigest(this.resultDigest, key, this.GetSignature()); return(result); } if (this.content != null) { this.content.Write(new SigOutputStream(signer)); } } else { byte[] encodedSignedAttributes = this.GetEncodedSignedAttributes(); signer.BlockUpdate(encodedSignedAttributes, 0, encodedSignedAttributes.Length); } result = signer.VerifySignature(this.GetSignature()); } catch (InvalidKeyException e3) { throw new CmsException("key not appropriate to signature in message.", e3); } catch (IOException e4) { throw new CmsException("can't process mime object to create signature.", e4); } catch (SignatureException ex) { throw new CmsException("invalid signature format in message: " + ex.Message, ex); } return(result); }
public static global::System.Collections.ICollection GetSignatureTimestamps(SignerInformation signerInfo) { global::System.Collections.IList list = Platform.CreateArrayList(); Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = signerInfo.UnsignedAttributes; if (unsignedAttributes != null) { global::System.Collections.IEnumerator enumerator = unsignedAttributes.GetAll(PkcsObjectIdentifiers.IdAASignatureTimeStampToken).GetEnumerator(); try { while (enumerator.MoveNext()) { Attribute attribute = (Attribute)enumerator.get_Current(); { global::System.Collections.IEnumerator enumerator2 = attribute.AttrValues.GetEnumerator(); try { while (enumerator2.MoveNext()) { Asn1Encodable asn1Encodable = (Asn1Encodable)enumerator2.get_Current(); try { Org.BouncyCastle.Asn1.Cms.ContentInfo instance = Org.BouncyCastle.Asn1.Cms.ContentInfo.GetInstance(asn1Encodable.ToAsn1Object()); TimeStampToken timeStampToken = new TimeStampToken(instance); TimeStampTokenInfo timeStampInfo = timeStampToken.TimeStampInfo; byte[] a = DigestUtilities.CalculateDigest(GetDigestAlgName(timeStampInfo.MessageImprintAlgOid), signerInfo.GetSignature()); if (!Arrays.ConstantTimeAreEqual(a, timeStampInfo.GetMessageImprintDigest())) { throw new TspValidationException("Incorrect digest in message imprint"); } list.Add((object)timeStampToken); } catch (SecurityUtilityException) { throw new TspValidationException("Unknown hash algorithm specified in timestamp"); } catch (global::System.Exception) { throw new TspValidationException("Timestamp could not be parsed"); } } } finally { global::System.IDisposable disposable2 = enumerator2 as global::System.IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } } } return((global::System.Collections.ICollection)list); } finally { global::System.IDisposable disposable = enumerator as global::System.IDisposable; if (disposable != null) { disposable.Dispose(); } } } return((global::System.Collections.ICollection)list); }
private bool DoVerify(AsymmetricKeyParameter key) { //IL_019b: Expected O, but got Unknown //IL_03a0: Expected O, but got Unknown string digestAlgName = Helper.GetDigestAlgName(DigestAlgOid); IDigest digestInstance = Helper.GetDigestInstance(digestAlgName); DerObjectIdentifier algorithm = encryptionAlgorithm.Algorithm; Asn1Encodable parameters = encryptionAlgorithm.Parameters; ISigner signer; if (algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss)) { if (parameters == null) { throw new CmsException("RSASSA-PSS signature must specify algorithm parameters"); } try { RsassaPssParameters instance = RsassaPssParameters.GetInstance(parameters.ToAsn1Object()); if (!instance.HashAlgorithm.Algorithm.Equals(digestAlgorithm.Algorithm)) { throw new CmsException("RSASSA-PSS signature parameters specified incorrect hash algorithm"); } if (!instance.MaskGenAlgorithm.Algorithm.Equals(PkcsObjectIdentifiers.IdMgf1)) { throw new CmsException("RSASSA-PSS signature parameters specified unknown MGF"); } IDigest digest = DigestUtilities.GetDigest(instance.HashAlgorithm.Algorithm); int intValue = instance.SaltLength.Value.IntValue; byte b = (byte)instance.TrailerField.Value.IntValue; if (b != 1) { throw new CmsException("RSASSA-PSS signature parameters must have trailerField of 1"); } signer = new PssSigner(new RsaBlindedEngine(), digest, intValue); } catch (global::System.Exception e) { throw new CmsException("failed to set RSASSA-PSS signature parameters", e); } } else { string algorithm2 = digestAlgName + "with" + Helper.GetEncryptionAlgName(EncryptionAlgOid); signer = Helper.GetSignatureInstance(algorithm2); } try { if (digestCalculator != null) { resultDigest = digestCalculator.GetDigest(); } else { if (content != null) { content.Write((Stream)(object)new DigOutputStream(digestInstance)); } else if (signedAttributeSet == null) { throw new CmsException("data not encapsulated in signature - use detached constructor."); } resultDigest = DigestUtilities.DoFinal(digestInstance); } } catch (IOException val) { IOException e2 = val; throw new CmsException("can't process mime object to create signature.", (global::System.Exception)(object) e2); } Asn1Object singleValuedSignedAttribute = GetSingleValuedSignedAttribute(CmsAttributes.ContentType, "content-type"); if (singleValuedSignedAttribute == null) { if (!isCounterSignature && signedAttributeSet != null) { throw new CmsException("The content-type attribute type MUST be present whenever signed attributes are present in signed-data"); } } else { if (isCounterSignature) { throw new CmsException("[For counter signatures,] the signedAttributes field MUST NOT contain a content-type attribute"); } if (!(singleValuedSignedAttribute is DerObjectIdentifier)) { throw new CmsException("content-type attribute value not of ASN.1 type 'OBJECT IDENTIFIER'"); } DerObjectIdentifier derObjectIdentifier = (DerObjectIdentifier)singleValuedSignedAttribute; if (!derObjectIdentifier.Equals(contentType)) { throw new CmsException("content-type attribute value does not match eContentType"); } } Asn1Object singleValuedSignedAttribute2 = GetSingleValuedSignedAttribute(CmsAttributes.MessageDigest, "message-digest"); if (singleValuedSignedAttribute2 == null) { if (signedAttributeSet != null) { throw new CmsException("the message-digest signed attribute type MUST be present when there are any signed attributes present"); } } else { if (!(singleValuedSignedAttribute2 is Asn1OctetString)) { throw new CmsException("message-digest attribute value not of ASN.1 type 'OCTET STRING'"); } Asn1OctetString asn1OctetString = (Asn1OctetString)singleValuedSignedAttribute2; if (!Arrays.AreEqual(resultDigest, asn1OctetString.GetOctets())) { throw new CmsException("message-digest attribute value does not match calculated value"); } } Org.BouncyCastle.Asn1.Cms.AttributeTable signedAttributes = SignedAttributes; if (signedAttributes != null && signedAttributes.GetAll(CmsAttributes.CounterSignature).Count > 0) { throw new CmsException("A countersignature attribute MUST NOT be a signed attribute"); } Org.BouncyCastle.Asn1.Cms.AttributeTable unsignedAttributes = UnsignedAttributes; if (unsignedAttributes != null) { { global::System.Collections.IEnumerator enumerator = unsignedAttributes.GetAll(CmsAttributes.CounterSignature).GetEnumerator(); try { while (enumerator.MoveNext()) { Attribute attribute = (Attribute)enumerator.get_Current(); if (attribute.AttrValues.Count < 1) { throw new CmsException("A countersignature attribute MUST contain at least one AttributeValue"); } } } finally { global::System.IDisposable disposable = enumerator as global::System.IDisposable; if (disposable != null) { disposable.Dispose(); } } } } try { signer.Init(forSigning: false, key); if (signedAttributeSet == null) { if (digestCalculator != null) { return(VerifyDigest(resultDigest, key, GetSignature())); } if (content != null) { content.Write((Stream)(object)new SigOutputStream(signer)); } } else { byte[] encodedSignedAttributes = GetEncodedSignedAttributes(); signer.BlockUpdate(encodedSignedAttributes, 0, encodedSignedAttributes.Length); } return(signer.VerifySignature(GetSignature())); } catch (InvalidKeyException e3) { throw new CmsException("key not appropriate to signature in message.", e3); } catch (IOException val2) { IOException e4 = val2; throw new CmsException("can't process mime object to create signature.", (global::System.Exception)(object) e4); } catch (SignatureException ex) { throw new CmsException("invalid signature format in message: " + ((global::System.Exception)ex).get_Message(), ex); } }