WriteObject() публичный Метод

public WriteObject ( Asn1Encodable obj ) : void
obj Asn1Encodable
Результат void
        bool ValidateDEREncoding(Stream stream)
        {
            try
            {
                var asn1Stream  = new Org.BouncyCastle.Asn1.Asn1InputStream(stream);
                var certificate = asn1Stream.ReadObject();

                var derEncodedStream = new MemoryStream();
                var encoder          = new Org.BouncyCastle.Asn1.DerOutputStream(derEncodedStream);
                encoder.WriteObject(certificate);
                encoder.Flush();

                if (stream.Length != derEncodedStream.Length)
                {
                    return(false);
                }

                stream.Seek(0, SeekOrigin.Begin);
                derEncodedStream.Seek(0, SeekOrigin.Begin);

                for (int i = 0; i < stream.Length; i++)
                {
                    if (stream.ReadByte() != derEncodedStream.ReadByte())
                    {
                        return(false);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Пример #2
0
		/// <exception cref="System.IO.IOException"></exception>
		public virtual Stream OpenStream()
		{
            Stream output = new MemoryStream();
			DerOutputStream derOuput = new DerOutputStream(output);
			derOuput.WriteObject(Asn1Object.FromByteArray(signedData.GetEncoded()));
            output.Seek(0, SeekOrigin.Begin);
			return output;
		}
Пример #3
0
		public byte[] GetEncoded(
			string encoding)
		{
			if (encoding.Equals(Der))
			{
				MemoryStream bOut = new MemoryStream();
				DerOutputStream dOut = new DerOutputStream(bOut);

				dOut.WriteObject(this);

				return bOut.ToArray();
			}

			return GetEncoded();
		}
Пример #4
0
        /*
         * A note on the implementation:
         * <p>
         * As Der requires the constructed, definite-length model to
         * be used for structured types, this varies slightly from the
         * ASN.1 descriptions given. Rather than just outputing Sequence,
         * we also have to specify Constructed, and the objects length.
         */
        internal override void Encode(
            DerOutputStream derOut)
        {
            MemoryStream bOut = new MemoryStream();
            DerOutputStream dOut = new DerOutputStream(bOut);

            foreach (object obj in this)
            {
                dOut.WriteObject(obj);
            }

            dOut.Close();

            byte[] bytes = bOut.ToArray();

            derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes);
        }
		/*
		 * A note on the implementation:
		 * <p>
		 * As Der requires the constructed, definite-length model to
		 * be used for structured types, this varies slightly from the
		 * ASN.1 descriptions given. Rather than just outputing Sequence,
		 * we also have to specify Constructed, and the objects length.
		 */
		internal override void Encode(
			DerOutputStream derOut)
		{
			// TODO Intermediate buffer could be avoided if we could calculate expected length
			MemoryStream bOut = new MemoryStream();
			DerOutputStream dOut = new DerOutputStream(bOut);

			foreach (Asn1Encodable obj in this)
			{
				dOut.WriteObject(obj);
			}

			dOut.Dispose();

			byte[] bytes = bOut.ToArray();

			derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes);
		}
Пример #6
0
        /*
         */
        internal override void Encode(
            DerOutputStream derOut)
        {
            if (derOut is Asn1OutputStream || derOut is BerOutputStream)
            {
                derOut.WriteByte((byte)(Asn1Tags.Sequence | Asn1Tags.Constructed));
                derOut.WriteByte(0x80);

                foreach (object o in this)
                {
                    derOut.WriteObject(o);
                }

                derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.Encode(derOut);
            }
        }
Пример #7
0
        internal override void Encode(DerOutputStream derOut)
        {
            if (derOut is Asn1OutputStream || derOut is BerOutputStream)
            {
                derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);

                derOut.WriteByte(0x80);

                //
                // write out the octet array
                //
                foreach (DerOctetString oct in this)
                {
                    derOut.WriteObject(oct);
                }

                derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.Encode(derOut);
            }
        }
        virtual public byte[] GetEncodedRecipient(int index) {
            //Certificate certificate = recipient.GetX509();
            PdfPublicKeyRecipient recipient = recipients[index];
            byte[] cms = recipient.Cms;
            
            if (cms != null) return cms;
            
            X509Certificate certificate  = recipient.Certificate;
            int permission =  recipient.Permission;//PdfWriter.AllowCopy | PdfWriter.AllowPrinting | PdfWriter.AllowScreenReaders | PdfWriter.AllowAssembly;   
            int revision = 3;
            
            permission |= (int)(revision==3 ? (uint)0xfffff0c0 : (uint)0xffffffc0);
            permission &= unchecked((int)0xfffffffc);
            permission += 1;
          
            byte[] pkcs7input = new byte[24];
            
            byte one = (byte)(permission);
            byte two = (byte)(permission >> 8);
            byte three = (byte)(permission >> 16);
            byte four = (byte)(permission >> 24);

            System.Array.Copy(seed, 0, pkcs7input, 0, 20); // put this seed in the pkcs7 input
                                
            pkcs7input[20] = four;
            pkcs7input[21] = three;                
            pkcs7input[22] = two;
            pkcs7input[23] = one;

            Asn1Object obj = CreateDERForRecipient(pkcs7input, certificate);
                
            MemoryStream baos = new MemoryStream();
                
            DerOutputStream k = new DerOutputStream(baos);
                
            k.WriteObject(obj);  
            
            cms = baos.ToArray();

            recipient.Cms = cms;
            
            return cms;    
        }
Пример #9
0
 internal override void Encode(DerOutputStream derOut)
 {
     if ((derOut is Asn1OutputStream) || (derOut is BerOutputStream))
     {
         derOut.WriteTag(160, base.tagNo);
         derOut.WriteByte(0x80);
         if (!base.IsEmpty())
         {
             if (!base.explicitly)
             {
                 IEnumerable enumerable;
                 if (base.obj is Asn1OctetString)
                 {
                     if (base.obj is BerOctetString)
                     {
                         enumerable = (BerOctetString)base.obj;
                     }
                     else
                     {
                         Asn1OctetString str = (Asn1OctetString)base.obj;
                         enumerable = new BerOctetString(str.GetOctets());
                     }
                 }
                 else if (base.obj is Asn1Sequence)
                 {
                     enumerable = (Asn1Sequence)base.obj;
                 }
                 else
                 {
                     if (!(base.obj is Asn1Set))
                     {
                         throw Platform.CreateNotImplementedException(Platform.GetTypeName(base.obj));
                     }
                     enumerable = (Asn1Set)base.obj;
                 }
                 IEnumerator enumerator = enumerable.GetEnumerator();
                 try
                 {
                     while (enumerator.MoveNext())
                     {
                         Asn1Encodable current = (Asn1Encodable)enumerator.Current;
                         derOut.WriteObject(current);
                     }
                 }
                 finally
                 {
                     if (enumerator is IDisposable disposable)
                     {
                         IDisposable disposable;
                         disposable.Dispose();
                     }
                 }
             }
             else
             {
                 derOut.WriteObject(base.obj);
             }
         }
         derOut.WriteByte(0);
         derOut.WriteByte(0);
     }
     else
     {
         base.Encode(derOut);
     }
 }
Пример #10
0
        internal override void Encode(
            DerOutputStream derOut)
        {
            if (derOut is Asn1OutputStream || derOut is BerOutputStream)
            {
                derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);

                derOut.WriteByte(0x80);

                //
                // write out the octet array
                //
                if (octs != null)
                {
                    for (int i = 0; i != octs.Count; i++)
                    {
                        derOut.WriteObject(octs[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < str.Length; i += MaxLength)
                    {
                        int end = System.Math.Min(str.Length, i + MaxLength);

                        byte[] nStr = new byte[end - i];

                        Array.Copy(str, i, nStr, 0, nStr.Length);

                        derOut.WriteObject(new DerOctetString(nStr));
                    }
                }

                derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.Encode(derOut);
            }
        }
Пример #11
0
 internal override void Encode(DerOutputStream derOut)
 {
     if (derOut is Asn1OutputStream || derOut is BerOutputStream)
     {
         derOut.WriteTag(160, tagNo);
         ((Stream)derOut).WriteByte((byte)128);
         if (!IsEmpty())
         {
             if (!explicitly)
             {
                 global::System.Collections.IEnumerable enumerable;
                 if (obj is Asn1OctetString)
                 {
                     if (obj is BerOctetString)
                     {
                         enumerable = (BerOctetString)obj;
                     }
                     else
                     {
                         Asn1OctetString asn1OctetString = (Asn1OctetString)obj;
                         enumerable = new BerOctetString(asn1OctetString.GetOctets());
                     }
                 }
                 else if (obj is Asn1Sequence)
                 {
                     enumerable = (Asn1Sequence)obj;
                 }
                 else
                 {
                     if (!(obj is Asn1Set))
                     {
                         throw Platform.CreateNotImplementedException(Platform.GetTypeName(obj));
                     }
                     enumerable = (Asn1Set)obj;
                 }
                 {
                     global::System.Collections.IEnumerator enumerator = enumerable.GetEnumerator();
                     try
                     {
                         while (enumerator.MoveNext())
                         {
                             Asn1Encodable asn1Encodable = (Asn1Encodable)enumerator.get_Current();
                             derOut.WriteObject(asn1Encodable);
                         }
                     }
                     finally
                     {
                         global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                         if (disposable != null)
                         {
                             disposable.Dispose();
                         }
                     }
                 }
             }
             else
             {
                 derOut.WriteObject(obj);
             }
         }
         ((Stream)derOut).WriteByte((byte)0);
         ((Stream)derOut).WriteByte((byte)0);
     }
     else
     {
         base.Encode(derOut);
     }
 }
Пример #12
0
        //            : base("X.509")
        /**
         * Creates a CertPath of the specified type.
         * This constructor is protected because most users should use
         * a CertificateFactory to create CertPaths.
         *
         * @param type the standard name of the type of Certificatesin this path
         **/
        public PkixCertPath(
			Stream	inStream,
			String	encoding)
        {
            try
            {
                if (encoding.ToUpper().Equals("PkiPath".ToUpper()))
                {
                    Asn1InputStream derInStream = new Asn1InputStream(inStream);
                    Asn1Object derObject = derInStream.ReadObject();
                    if (!(derObject is Asn1Sequence))
                    {
                        throw new CertificateException(
                            "input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath");
                    }
                    IEnumerator e = ((Asn1Sequence)derObject).GetEnumerator();
                    Stream certInStream;
                    MemoryStream outStream;
                    DerOutputStream derOutStream;
                    certificates = new ArrayList();

                    while (e.MoveNext())
                    {
                        outStream = new MemoryStream();
                        derOutStream = new DerOutputStream(outStream);

                        derOutStream.WriteObject((Asn1Encodable)e.Current);
                        derOutStream.Close();

                        certInStream = new MemoryStream(outStream.ToArray(), false);
                        certificates.Insert(0, new X509CertificateParser().ReadCertificate(certInStream));
                    }
                }
                else if (encoding.ToUpper().Equals("PKCS7")
                    || encoding.ToUpper().Equals("PEM"))
                {
                    inStream = new BufferedStream(inStream);
                    certificates = new ArrayList();

                    X509CertificateParser certParser = new X509CertificateParser();
                    X509Certificate cert = null;

                    while ((cert = certParser.ReadCertificate(inStream)) != null)
                    {
                        certificates.Add(cert);
                    }
                }
                else
                {
                    throw new CertificateException("unsupported encoding: " + encoding);
                }
            }
            catch (IOException ex)
            {
                throw new CertificateException(
                    "IOException throw while decoding CertPath:\n"
                    + ex.ToString());
            }

            this.certificates = SortCerts(certificates);
        }
Пример #13
0
        /*
         * A note on the implementation:
         * <p>
         * As Der requires the constructed, definite-length model to
         * be used for structured types, this varies slightly from the
         * ASN.1 descriptions given. Rather than just outputing Set,
         * we also have to specify Constructed, and the objects length.
         */
        internal override void Encode(DerOutputStream derOut)
        {
            // TODO Intermediate buffer could be avoided if we could calculate expected length
            using (var bOut = new MemoryStream())
            {
                using (var dOut = new DerOutputStream(bOut))
                {
                    foreach (Asn1Encodable obj in this)
                    {
                        dOut.WriteObject(obj);
                    }
                }

                var bytes = bOut.ToArray();

                derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, bytes);
            }
        }
        internal override void Encode(
            DerOutputStream derOut)
        {
            if (derOut is Asn1OutputStream || derOut is BerOutputStream)
            {
                derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);

                derOut.WriteByte(0x80);

                //
                // write out the octet array
                //
                foreach (DerOctetString oct in this)
                {
                    derOut.WriteObject(oct);
                }

				derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.Encode(derOut);
            }
        }
Пример #15
0
		internal override void Encode(
			DerOutputStream derOut)
		{
			if (derOut is Asn1OutputStream || derOut is BerOutputStream)
			{
				derOut.WriteTag((byte)(Asn1Tags.Constructed | Asn1Tags.Tagged), tagNo);
				derOut.WriteByte(0x80);

				if (!IsEmpty())
				{
					if (!explicitly)
					{
						IEnumerable eObj;
						if (obj is Asn1OctetString)
						{
							if (obj is BerOctetString)
							{
								eObj = (BerOctetString) obj;
							}
							else
							{
								Asn1OctetString octs = (Asn1OctetString)obj;
								eObj = new BerOctetString(octs.GetOctets());
							}
						}
						else if (obj is Asn1Sequence)
						{
							eObj = (Asn1Sequence) obj;
						}
						else if (obj is Asn1Set)
						{
							eObj = (Asn1Set) obj;
						}
						else
						{
							throw Platform.CreateNotImplementedException(obj.GetType().Name);
						}

						foreach (Asn1Encodable o in eObj)
						{
							derOut.WriteObject(o);
						}
					}
					else
					{
						derOut.WriteObject(obj);
					}
				}

				derOut.WriteByte(0x00);
				derOut.WriteByte(0x00);
			}
			else
			{
				base.Encode(derOut);
			}
		}