Inheritance: Asn1Encodable
        internal EncryptionScheme(
			Asn1Sequence seq)
            : base(seq)
        {
            objectID = (Asn1Object) seq[0];
            obj = (Asn1Object) seq[1];
        }
		/**
		* Creates a new instance of DerExternal.
		* See X.690 for more informations about the meaning of these parameters
		* @param directReference The direct reference or <code>null</code> if not set.
		* @param indirectReference The indirect reference or <code>null</code> if not set.
		* @param dataValueDescriptor The data value descriptor or <code>null</code> if not set.
		* @param encoding The encoding to be used for the external data
		* @param externalData The external data
		*/
		public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference, Asn1Object dataValueDescriptor, int encoding, Asn1Object externalData)
		{
			DirectReference = directReference;
			IndirectReference = indirectReference;
			DataValueDescriptor = dataValueDescriptor;
			Encoding = encoding;
			ExternalContent = externalData.ToAsn1Object();
		}
Exemple #3
0
		public SafeBag(
            DerObjectIdentifier	oid,
            Asn1Object			obj)
        {
            this.bagID = oid;
            this.bagValue = obj;
            this.bagAttributes = null;
        }
Exemple #4
0
		public SafeBag(
            DerObjectIdentifier	oid,
            Asn1Object			obj,
            Asn1Set				bagAttributes)
        {
            this.bagID = oid;
            this.bagValue = obj;
            this.bagAttributes = bagAttributes;
        }
 internal static bool IsConstructed(bool isExplicit, Asn1Object obj)
 {
     if (isExplicit || obj is Asn1Sequence || obj is Asn1Set)
         return true;
     Asn1TaggedObject tagged = obj as Asn1TaggedObject;
     if (tagged == null)
         return false;
     return IsConstructed(tagged.IsExplicit(), tagged.GetObject());
 }
		public ServiceLocator(
			X509Name	issuer,
			Asn1Object	locator)
		{
			if (issuer == null)
				throw new ArgumentNullException("issuer");

			this.issuer = issuer;
			this.locator = locator;
		}
		private ServiceLocator(
			Asn1Sequence seq)
		{
			this.issuer = X509Name.GetInstance(seq[0]);

			if (seq.Count > 1)
			{
				this.locator = seq[1].ToAsn1Object();
			}
		}
		public SmimeCapability(
            Asn1Sequence seq)
        {
            capabilityID = (DerObjectIdentifier) seq[0].ToAsn1Object();

			if (seq.Count > 1)
            {
                parameters = seq[1].ToAsn1Object();
            }
        }
		protected override bool Asn1Equals(
			Asn1Object asn1Object)
        {
			DerGeneralString other = asn1Object as DerGeneralString;

			if (other == null)
				return false;

			return this.str.Equals(other.str);
        }
Exemple #10
0
		public SafeBag(
            Asn1Sequence seq)
        {
            this.bagID = (DerObjectIdentifier) seq[0];
            this.bagValue = ((DerTaggedObject) seq[1]).GetObject();
            if (seq.Count == 3)
            {
                this.bagAttributes = (Asn1Set) seq[2];
            }
        }
		protected override bool Asn1Equals(
			Asn1Object asn1Object)
        {
			DerEnumerated other = asn1Object as DerEnumerated;

			if (other == null)
				return false;

			return Arrays.AreEqual(this.bytes, other.bytes);
        }
Exemple #12
0
        protected override bool Asn1Equals(
			Asn1Object asn1Object)
        {
            DerUnknownTag other = asn1Object as DerUnknownTag;

            if (other == null)
                return false;

            return this.tag == other.tag
                && Arrays.AreEqual(this.data, other.data);
        }
Exemple #13
0
		public Time(
            Asn1Object time)
        {
            if (!(time is DerUtcTime)
                && !(time is DerGeneralizedTime))
            {
                throw new ArgumentException("unknown object passed to Time");
            }

			this.time = time;
        }
        protected override bool Asn1Equals(
			Asn1Object asn1Object)
        {
            DerApplicationSpecific other = asn1Object as DerApplicationSpecific;

            if (other == null)
                return false;

            return this.tag == other.tag
                && Arrays.AreEqual(this.octets, other.octets);
        }
		protected override bool Asn1Equals(
			Asn1Object asn1Object)
        {
			Asn1TaggedObject other = asn1Object as Asn1TaggedObject;

			if (other == null)
				return false;

			return this.tagNo == other.tagNo
//				&& this.empty == other.empty
				&& this.explicitly == other.explicitly   // TODO Should this be part of equality?
				&& Platform.Equals(GetObject(), other.GetObject());
		}
		public SmimeCapability(
            DerObjectIdentifier	capabilityID,
            Asn1Encodable		parameters)
        {
			if (capabilityID == null)
				throw new ArgumentNullException("capabilityID");

			this.capabilityID = capabilityID;

			if (parameters != null)
			{
				this.parameters = parameters.ToAsn1Object();
			}
        }
    /**
        * Creates a new <code>CommitmentTypeQualifier</code> instance.
        *
        * @param commitmentTypeIdentifier a <code>CommitmentTypeIdentifier</code> value
        * @param qualifier the qualifier, defined by the above field.
        */
        public CommitmentTypeQualifier(
            DerObjectIdentifier	commitmentTypeIdentifier,
            Asn1Encodable		qualifier)
        {
			if (commitmentTypeIdentifier == null)
				throw new ArgumentNullException("commitmentTypeIdentifier");

			this.commitmentTypeIdentifier = commitmentTypeIdentifier;

			if (qualifier != null)
			{
				this.qualifier = qualifier.ToAsn1Object();
			}
        }
        /**
        * Creates a new <code>CommitmentTypeQualifier</code> instance.
        *
        * @param as <code>CommitmentTypeQualifier</code> structure
        * encoded as an Asn1Sequence.
        */
        public CommitmentTypeQualifier(
            Asn1Sequence seq)
        {
			if (seq == null)
				throw new ArgumentNullException("seq");
			if (seq.Count < 1 || seq.Count > 2)
				throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");

			commitmentTypeIdentifier = (DerObjectIdentifier) seq[0].ToAsn1Object();

			if (seq.Count > 1)
            {
                qualifier = seq[1].ToAsn1Object();
            }
        }
Exemple #19
0
		/**
         * creates a time object from a given date - if the date is between 1950
         * and 2049 a UTCTime object is Generated, otherwise a GeneralizedTime
         * is used.
         */
        public Time(
            DateTime date)
        {
            string d = date.ToString("yyyyMMddHHmmss") + "Z";

			int year = int.Parse(d.Substring(0, 4));

			if (year < 1950 || year > 2049)
            {
                time = new DerGeneralizedTime(d);
            }
            else
            {
                time = new DerUtcTime(d.Substring(2));
            }
        }
		public DerExternal(
			Asn1EncodableVector vector)
		{
			int offset = 0;
			Asn1Object enc = GetObjFromVector(vector, offset);
			if (enc is DerObjectIdentifier)
			{
				directReference = (DerObjectIdentifier)enc;
				offset++;
				enc = GetObjFromVector(vector, offset);
			}
			if (enc is DerInteger)
			{
				indirectReference = (DerInteger) enc;
				offset++;
				enc = GetObjFromVector(vector, offset);
			}
			if (!(enc is DerTaggedObject))
			{
				dataValueDescriptor = (Asn1Object) enc;
				offset++;
				enc = GetObjFromVector(vector, offset);
			}
			if (!(enc is DerTaggedObject))
			{
				throw new InvalidOperationException(
					"No tagged object found in vector. Structure doesn't seem to be of type External");
			}

			if (vector.Count != offset + 1)
				throw new ArgumentException("input vector too large", "vector");

			if (!(enc is DerTaggedObject))
				throw new ArgumentException("No tagged object found in vector. Structure doesn't seem to be of type External", "vector");

			DerTaggedObject obj = (DerTaggedObject)enc;

			// Use property accessor to include check on value
			Encoding = obj.TagNo;

			if (encoding < 0 || encoding > 2)
				throw new InvalidOperationException("invalid encoding value");

			externalContent = obj.GetObject();
		}
 public override void AddObject(
     Asn1Object obj)
 {
     new DerOutputStream(_bOut).WriteObject(obj);
 }
Exemple #22
0
 public abstract void AddObject(Asn1Object obj);
Exemple #23
0
 private static String GetStringFromGeneralName(Asn1Object names) {
     DerTaggedObject taggedObject = (DerTaggedObject) names ;
     return Encoding.GetEncoding(1252).GetString(Asn1OctetString.GetInstance(taggedObject, false).GetOctets());
 }
Exemple #24
0
        /// <summary>
        /// Questo metodo verifica se l'associazione fra marca e file è valida, verifica inoltre la
        /// validità del certificato firmatario della marca e la data di scadenza della marca; infine
        /// restituisce (se le verifiche vanno a buon fine) tutti i dati contenuti nella marca.
        /// </summary>
        /// <param name="tsRes"></param>
        /// <param name="tsReq"></param>
        /// <returns></returns>
        protected OutputResponseMarca checkMarca(TimeStampResponse tsRes, TimeStampRequest tsReq)
        {
            OutputResponseMarca outTSR = new OutputResponseMarca();

            try
            {
                tsRes.Validate(tsReq);
                outTSR.esito             = "OK";
                outTSR.descrizioneErrore = string.Empty;
            }
            catch (TspException e)
            {
                outTSR.esito             = "KO";
                outTSR.descrizioneErrore = "verifica della marca fallita: " + e.Message;
                logger.Debug("verifica della marca fallita: " + e.Message);
                //return outTSR;
            }

            TimeStampToken tsToken = tsRes.TimeStampToken;

            //Verifica data scadenza marca secondo l'ora locale
            Org.BouncyCastle.X509.Store.IX509Store store = tsToken.GetCertificates("Collection");
            Org.BouncyCastle.X509.X509Certificate  cert  = (Org.BouncyCastle.X509.X509Certificate) new ArrayList(store.GetMatches(tsToken.SignerID))[0];
            //se la data attuale è maggiore di quella di scadenza del certificato che ha firmato la marca
            //allora la marca è scaduta!!!
            if (DateTime.Now.CompareTo(cert.NotAfter.ToLocalTime()) > 0)
            {
                //outTSR.esito = "KO";
                outTSR.descrizioneErrore = "marca temporale scaduta";
                logger.Debug("marca temporale scaduta");
                //return outTSR;
            }

            try
            {
                //estrazione delle informazioni dalla marca
                outTSR.dsm            = cert.NotAfter.ToLocalTime().ToString();
                outTSR.sernum         = tsToken.TimeStampInfo.SerialNumber.ToString();
                outTSR.fhash          = byteArrayToHexa(tsToken.TimeStampInfo.TstInfo.MessageImprint.GetHashedMessage());
                outTSR.docm           = tsToken.TimeStampInfo.TstInfo.GenTime.TimeString;
                outTSR.docm_date      = tsToken.TimeStampInfo.GenTime.ToLocalTime().ToString();
                outTSR.marca          = Convert.ToBase64String(tsRes.GetEncoded());
                outTSR.algCertificato = cert.SigAlgName;
                outTSR.fromDate       = cert.NotBefore.ToLocalTime().ToString();
                outTSR.snCertificato  = cert.SerialNumber.ToString();
                //Algoritmo hash utilizzato per l'impronta
                string algHashOid = tsToken.TimeStampInfo.MessageImprintAlgOid;
                if (!string.IsNullOrEmpty(algHashOid))
                {
                    System.Security.Cryptography.Oid oidHash = new System.Security.Cryptography.Oid(algHashOid);
                    outTSR.algHash = oidHash.FriendlyName;
                }

                outTSR.TSA = new TSARFC2253();

                //Con le TSA di test potrebbe non essere valorizzato l'oggetto TSA
                logger.Debug("Controllo TSA : " + tsToken.TimeStampInfo.Tsa);
                try
                {
                    if (tsToken.TimeStampInfo.Tsa != null)
                    {
                        string oid      = string.Empty;
                        string oidValue = string.Empty;
                        logger.Debug("TagNo: " + tsToken.TimeStampInfo.Tsa.TagNo);
                        for (int n = 0; n < tsToken.TimeStampInfo.Tsa.TagNo; n++)
                        {
                            logger.Debug("Tag: " + n);
                            Org.BouncyCastle.Asn1.Asn1Sequence seq = (Org.BouncyCastle.Asn1.Asn1Sequence)tsToken.TimeStampInfo.Tsa.Name.ToAsn1Object();

                            //Obsoleto
                            //Org.BouncyCastle.Asn1.Asn1Object obj = (Org.BouncyCastle.Asn1.Asn1Object)seq.GetObjectAt(n);
                            Org.BouncyCastle.Asn1.Asn1Object obj = (Org.BouncyCastle.Asn1.Asn1Object)seq[n];

                            Org.BouncyCastle.Asn1.Asn1Set set1 = (Org.BouncyCastle.Asn1.Asn1Set)obj.ToAsn1Object();

                            //Obsoleto
                            //seq = (Org.BouncyCastle.Asn1.Asn1Sequence)set1.GetObjectAt(0);
                            //obj = (Org.BouncyCastle.Asn1.Asn1Object)seq.GetObjectAt(0);
                            seq = (Org.BouncyCastle.Asn1.Asn1Sequence)set1[0];
                            obj = (Org.BouncyCastle.Asn1.Asn1Object)seq[0];


                            oid = obj.ToString();

                            //Obsoleto
                            //obj = (Org.BouncyCastle.Asn1.Asn1Object)seq.GetObjectAt(1);
                            obj = (Org.BouncyCastle.Asn1.Asn1Object)seq[1];

                            oidValue = obj.ToString();
                            System.Security.Cryptography.Oid oid_obj = new System.Security.Cryptography.Oid(oid);
                            string friendly = oid_obj.FriendlyName;
                            logger.Debug("oid: " + oid + " friendly: " + friendly);
                            switch (friendly)
                            {
                            case "CN":
                                outTSR.TSA.CN = oidValue;
                                break;

                            case "OU":
                                outTSR.TSA.OU = oidValue;
                                break;

                            case "O":
                                outTSR.TSA.O = oidValue;
                                break;

                            case "C":
                                outTSR.TSA.C = oidValue;
                                break;
                            }
                        }
                        outTSR.TSA.TSARFC2253Name = "CN=" + outTSR.TSA.CN + ",OU=" + outTSR.TSA.OU + ",O=" + outTSR.TSA.O + ",C=" + outTSR.TSA.C;
                    }
                }
                catch (Exception e)
                {
                    logger.Debug("Eccezione controllo TSA : " + e.Message);
                }
                logger.Debug("Fine Controllo TSA");
            }
            catch (Exception eTsp)
            {
                outTSR.esito             = "KO";
                outTSR.descrizioneErrore = "estrazione delle informazioni dalla marca fallita: " + eTsp.Message;
                logger.Debug("estrazione delle informazioni dalla marca fallita: " + eTsp.Message);
                //return outTSR;
            }

            //verifico l'esistenza del documento al quale è associata la marca temporale
            //Commentata perchè l'impronta del documento è ancora calcolata con SHA1 invece che SHA256
            //DocsPaDB.Query_DocsPAWS.Documenti documento = new DocsPaDB.Query_DocsPAWS.Documenti();
            //outTSR.timestampedDoc = documento.GetDocNumberByImpronta(outTSR.fhash);
            //if (string.IsNullOrEmpty(outTSR.timestampedDoc))
            //{
            //    outTSR.timestampedDoc = "Non esiste alcun documento associato alla marca temporale.";
            //}

            //costruisco l'oggetto rappresentante il contenuto in chiaro della marca
            outTSR.DecryptedTSR             = new Marca();
            outTSR.DecryptedTSR.content     = contentMarca(outTSR);
            outTSR.DecryptedTSR.contentType = "text/html"; //"application/x-html";
            outTSR.DecryptedTSR.length      = outTSR.DecryptedTSR.content.Length;

            return(outTSR);
        }
		protected override bool Asn1Equals(
            Asn1Object asn1Object)
        {
            DerVideotexString other = asn1Object as DerVideotexString;

            if (other == null)
				return false;

            return Arrays.AreEqual(mString, other.mString);
        }
		public static ICipherParameters GetCipherParameters(
			string				algorithm,
			ICipherParameters	key,
			Asn1Object			asn1Params)
		{
			if (algorithm == null)
				throw new ArgumentNullException("algorithm");

			string canonical = GetCanonicalAlgorithmName(algorithm);

			if (canonical == null)
				throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised.");

			byte[] iv = null;

			try
			{
				// TODO These algorithms support an IV
				// but JCE doesn't seem to provide an AlgorithmParametersGenerator for them
				// "RIJNDAEL", "SKIPJACK", "TWOFISH"

				int basicIVKeySize = FindBasicIVSize(canonical);
				if (basicIVKeySize != -1
					|| canonical == "RIJNDAEL" || canonical == "SKIPJACK" || canonical == "TWOFISH")
				{
					iv = ((Asn1OctetString) asn1Params).GetOctets();
				}
				else if (canonical == "CAST5")
				{
					iv = Cast5CbcParameters.GetInstance(asn1Params).GetIV();
				}
#if INCLUDE_IDEA
				else if (canonical == "IDEA")
				{
					iv = IdeaCbcPar.GetInstance(asn1Params).GetIV();
				}
#endif
				else if (canonical == "RC2")
				{
					iv = RC2CbcParameter.GetInstance(asn1Params).GetIV();
				}
			}
			catch (Exception e)
			{
				throw new ArgumentException("Could not process ASN.1 parameters", e);
			}

			if (iv != null)
			{
				return new ParametersWithIV(key, iv);
			}

			throw new SecurityUtilityException("Algorithm " + algorithm + " not recognised.");
		}
 protected abstract bool Asn1Equals(Asn1Object asn1Object);
Exemple #28
0
		protected override bool Asn1Equals(
			Asn1Object asn1Object)
        {
			DerBoolean other = asn1Object as DerBoolean;

			if (other == null)
				return false;

			return IsTrue == other.IsTrue;
        }
        protected override bool Asn1Equals(Asn1Object asn1Object)
        {
            DerOctetString derOctetString = asn1Object as DerOctetString;

            return(derOctetString != null && Arrays.AreEqual(this.GetOctets(), derOctetString.GetOctets()));
        }
 internal bool CallAsn1Equals(Asn1Object obj)
 {
     return(Asn1Equals(obj));
 }
Exemple #31
0
 /**
  * Creates a new instance of DerExternal
  * See X.690 for more informations about the meaning of these parameters
  * @param directReference The direct reference or <code>null</code> if not set.
  * @param indirectReference The indirect reference or <code>null</code> if not set.
  * @param dataValueDescriptor The data value descriptor or <code>null</code> if not set.
  * @param externalData The external data in its encoded form.
  */
 public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference, Asn1Object dataValueDescriptor, DerTaggedObject externalData)
     : this(directReference, indirectReference, dataValueDescriptor, externalData.TagNo, externalData.ToAsn1Object())
 {
 }
Exemple #32
0
 /**
  * Creates a new instance of DerExternal.
  * See X.690 for more informations about the meaning of these parameters
  * @param directReference The direct reference or <code>null</code> if not set.
  * @param indirectReference The indirect reference or <code>null</code> if not set.
  * @param dataValueDescriptor The data value descriptor or <code>null</code> if not set.
  * @param encoding The encoding to be used for the external data
  * @param externalData The external data
  */
 public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference, Asn1Object dataValueDescriptor, int encoding, Asn1Object externalData)
 {
     DirectReference     = directReference;
     IndirectReference   = indirectReference;
     DataValueDescriptor = dataValueDescriptor;
     Encoding            = encoding;
     ExternalContent     = externalData.ToAsn1Object();
 }
Exemple #33
0
 protected override bool Asn1Equals(
     Asn1Object asn1Object)
 {
     return(asn1Object is DerNull);
 }
Exemple #34
0
        public Asn1Object ReadObject()
        {
            int tag = ReadByte();

            if (tag == -1)
            {
                if (eofFound)
                {
                    throw new EndOfStreamException("attempt to read past end of file.");
                }

                eofFound = true;

                return(null);
            }

            int tagNo = 0;

            if ((tag & Asn1Tags.Tagged) != 0 || (tag & Asn1Tags.Application) != 0)
            {
                tagNo = ReadTagNumber(tag);
            }

            int length = ReadLength();

            if (length < 0)                // indefinite length method
            {
                switch (tag)
                {
                case Asn1Tags.Null:
                    return(BerNull.Instance);

                case Asn1Tags.Sequence | Asn1Tags.Constructed:
                {
                    Asn1EncodableVector v = BuildEncodableVector(EndOfStream);
                    return(new BerSequence(v));
                }

                case Asn1Tags.Set | Asn1Tags.Constructed:
                {
                    Asn1EncodableVector v = BuildEncodableVector(EndOfStream);
                    return(new BerSet(v, false));
                }

                case Asn1Tags.OctetString | Asn1Tags.Constructed:
                    return(BuildConstructedOctetString(EndOfStream));

                default:
                {
                    //
                    // with tagged object tag number is bottom 5 bits
                    //
                    if ((tag & (int)Asn1Tags.Tagged) != 0)
                    {
                        //
                        // simple type - implicit... return an octet string
                        //
                        if ((tag & (int)Asn1Tags.Constructed) == 0)
                        {
                            byte[] bytes = ReadIndefiniteLengthFully();

                            return(new BerTaggedObject(false, tagNo, new DerOctetString(bytes)));
                        }

                        //
                        // either constructed or explicitly tagged
                        //
                        Asn1Object dObj = ReadObject();

                        if (dObj == EndOfStream)                                     // empty tag!
                        {
                            return(new DerTaggedObject(tagNo));
                        }

                        Asn1Object next = ReadObject();

                        //
                        // explicitly tagged (probably!) - if it isn't we'd have to
                        // tell from the context
                        //
                        if (next == EndOfStream)
                        {
                            return(new BerTaggedObject(tagNo, dObj));
                        }

                        //
                        // another implicit object, we'll create a sequence...
                        //
                        Asn1EncodableVector v = new Asn1EncodableVector(dObj);

                        do
                        {
                            v.Add(next);
                            next = ReadObject();
                        }while (next != EndOfStream);

                        return(new BerTaggedObject(false, tagNo, new BerSequence(v)));
                    }

                    throw new IOException("unknown Ber object encountered");
                }
                }
            }
            else
            {
                if (tag == 0 && length == 0)    // end of contents marker.
                {
                    return(EndOfStream);
                }

                byte[] bytes = new byte[length];

                ReadFully(bytes);

                return(BuildObject(tag, tagNo, bytes));
            }
        }
        protected override bool Asn1Equals(Asn1Object asn1Object)
        {
            DerNumericString derNumericString = asn1Object as DerNumericString;

            return(derNumericString != null && this.str.Equals(derNumericString.str));
        }
Exemple #36
0
 protected override bool Asn1Equals(
     Asn1Object obj)
 {
     return(obj is EosAsn1Object);
 }
Exemple #37
0
 protected override bool Asn1Equals(Asn1Object asn1Object) =>
 (asn1Object is DerNull);
		private static AsymmetricKeyParameter DecryptKey(
			char[]		passPhrase,
			Asn1Object	asn1Object)
		{
			return DecryptKey(passPhrase, EncryptedPrivateKeyInfo.GetInstance(asn1Object));
		}
Exemple #39
0
		public virtual void WriteObject(
			Asn1Object obj)
		{
			if (obj == null)
			{
				WriteNull();
			}
			else
			{
				obj.Encode(this);
			}
		}
		public static ICipherParameters GetCipherParameters(
			DerObjectIdentifier	algOid,
			ICipherParameters	key,
			Asn1Object			asn1Params)
		{
			return GetCipherParameters(algOid.Id, key, asn1Params);
		}
		public RecipientIdentifier(
            Asn1Object id)
        {
            this.id = id;
        }
        protected override bool Asn1Equals(
            Asn1Object asn1Object)
        {
            DerGeneralizedTime other = asn1Object as DerGeneralizedTime;

            if (other == null)
                return false;

            return this.time.Equals(other.time);
        }
 public BerOctetString(
     Asn1Object obj)
     : base(obj)
 {
 }
		public SignerIdentifier(
            Asn1Object id)
        {
            this.id = id;
        }
Exemple #45
0
 public Asn1Object GetObject()
 {
     return(Asn1Object.FromByteArray(GetContents()));
 }