ToAsn1Object() public abstract méthode

public abstract ToAsn1Object ( ) : Asn1Object
Résultat Asn1Object
        public virtual void WriteObject(
			Asn1Encodable obj)
        {
            if (obj == null)
            {
                WriteNull();
            }
            else
            {
                obj.ToAsn1Object().Encode(this);
            }
        }
		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();
			}
        }
Exemple #4
0
        public DerApplicationSpecific(bool isExplicit, int tag, Asn1Encodable obj)
        {
            Asn1Object asn1Object = obj.ToAsn1Object();

            byte[] derEncoded = asn1Object.GetDerEncoded();
            this.isConstructed = (isExplicit || asn1Object is Asn1Set || asn1Object is Asn1Sequence);
            this.tag           = tag;
            if (isExplicit)
            {
                this.octets = derEncoded;
                return;
            }
            int lengthOfHeader = this.GetLengthOfHeader(derEncoded);

            byte[] array = new byte[derEncoded.Length - lengthOfHeader];
            Array.Copy(derEncoded, lengthOfHeader, array, 0, array.Length);
            this.octets = array;
        }
Exemple #5
0
        public DerApplicationSpecific(bool isExplicit, int tag, Asn1Encodable obj)
        {
            Asn1Object asn1Object = obj.ToAsn1Object();

            byte[] derEncoded = asn1Object.GetDerEncoded();
            isConstructed = Asn1TaggedObject.IsConstructed(isExplicit, asn1Object);
            this.tag      = tag;
            if (isExplicit)
            {
                octets = derEncoded;
                return;
            }
            int lengthOfHeader = GetLengthOfHeader(derEncoded);

            byte[] array = new byte[derEncoded.Length - lengthOfHeader];
            global::System.Array.Copy((global::System.Array)derEncoded, lengthOfHeader, (global::System.Array)array, 0, array.Length);
            octets = array;
        }
        public DerApplicationSpecific(bool isExplicit, int tag, Asn1Encodable obj)
        {
            Asn1Object obj2 = obj.ToAsn1Object();

            byte[] derEncoded = obj2.GetDerEncoded();
            this.isConstructed = Asn1TaggedObject.IsConstructed(isExplicit, obj2);
            this.tag           = tag;
            if (isExplicit)
            {
                this.octets = derEncoded;
            }
            else
            {
                int    lengthOfHeader   = this.GetLengthOfHeader(derEncoded);
                byte[] destinationArray = new byte[derEncoded.Length - lengthOfHeader];
                Array.Copy(derEncoded, lengthOfHeader, destinationArray, 0, destinationArray.Length);
                this.octets = destinationArray;
            }
        }
Exemple #7
0
        public DerApplicationSpecific(
            bool isExplicit,
            int tag,
            Asn1Encodable obj)
        {
            Asn1Object asn1Obj = obj.ToAsn1Object();

            byte[] data = asn1Obj.GetDerEncoded();

            this.isConstructed = isExplicit || asn1Obj is Asn1Set || asn1Obj is Asn1Sequence;
            this.tag           = tag;

            if (isExplicit)
            {
                this.octets = data;
            }
            else
            {
                int    lenBytes = GetLengthOfHeader(data);
                byte[] tmp      = new byte[data.Length - lenBytes];
                Array.Copy(data, lenBytes, tmp, 0, tmp.Length);
                this.octets = tmp;
            }
        }
		public DerApplicationSpecific(
			bool			isExplicit,
			int				tag,
			Asn1Encodable	obj)
		{
            Asn1Object asn1Obj = obj.ToAsn1Object();

            byte[] data = asn1Obj.GetDerEncoded();

			this.isConstructed = isExplicit || asn1Obj is Asn1Set || asn1Obj is Asn1Sequence;
			this.tag = tag;

			if (isExplicit)
			{
				this.octets = data;
			}
			else
			{
				int lenBytes = GetLengthOfHeader(data);
				byte[] tmp = new byte[data.Length - lenBytes];
				Array.Copy(data, lenBytes, tmp, 0, tmp.Length);
				this.octets = tmp;
			}
		}
Exemple #9
0
 public BerOctetString(
     Asn1Encodable obj)
     : base(obj.ToAsn1Object())
 {
 }
        public static ICipherParameters GenerateCipherParameters(
			string          algorithm,
			char[]          password,
			bool			wrongPkcs12Zero,
			Asn1Encodable   pbeParameters)
        {
            string	mechanism = (string) algorithms[algorithm.ToUpperInvariant()];

            byte[] keyBytes = null;
            byte[] salt = null;
            int iterationCount = 0;

            if (IsPkcs12(mechanism))
            {
                Pkcs12PbeParams pbeParams = Pkcs12PbeParams.GetInstance(pbeParameters);
                salt = pbeParams.GetIV();
                iterationCount = pbeParams.Iterations.IntValue;
                keyBytes = PbeParametersGenerator.Pkcs12PasswordToBytes(password, wrongPkcs12Zero);
            }
            else if (IsPkcs5Scheme2(mechanism))
            {
                // See below
            }
            else
            {
                PbeParameter pbeParams = PbeParameter.GetInstance(pbeParameters);
                salt = pbeParams.GetSalt();
                iterationCount = pbeParams.IterationCount.IntValue;
                keyBytes = PbeParametersGenerator.Pkcs5PasswordToBytes(password);
            }

            ICipherParameters parameters = null;

            if (IsPkcs5Scheme2(mechanism))
            {
                PbeS2Parameters s2p = PbeS2Parameters.GetInstance(pbeParameters.ToAsn1Object());
                AlgorithmIdentifier encScheme = s2p.EncryptionScheme;
                DerObjectIdentifier encOid = encScheme.ObjectID;
                Asn1Object encParams = encScheme.Parameters.ToAsn1Object();

                // TODO What about s2p.KeyDerivationFunc.ObjectID?
                Pbkdf2Params pbeParams = Pbkdf2Params.GetInstance(s2p.KeyDerivationFunc.Parameters.ToAsn1Object());

                byte[] iv;
                if (encOid.Equals(PkcsObjectIdentifiers.RC2Cbc)) // PKCS5.B.2.3
                {
                    RC2CbcParameter rc2Params = RC2CbcParameter.GetInstance(encParams);
                    iv = rc2Params.GetIV();
                }
                else
                {
                    iv = Asn1OctetString.GetInstance(encParams).GetOctets();
                }

                salt = pbeParams.GetSalt();
                iterationCount = pbeParams.IterationCount.IntValue;
                keyBytes = PbeParametersGenerator.Pkcs5PasswordToBytes(password);

                int keyLength = pbeParams.KeyLength != null
                    ?	pbeParams.KeyLength.IntValue * 8
                    :	GeneratorUtilities.GetDefaultKeySize(encOid);

                PbeParametersGenerator gen = MakePbeGenerator(
                    (string)algorithmType[mechanism], null, keyBytes, salt, iterationCount);

                parameters = gen.GenerateDerivedParameters(encOid.Id, keyLength);

                if (iv != null)
                {
                    // FIXME? OpenSSL weirdness with IV of zeros (for ECB keys?)
                    if (Arrays.AreEqual(iv, new byte[iv.Length]))
                    {
                        //System.Diagnostics.Debug.Error.Write("***** IV all 0 (length " + iv.Length + ") *****");
                    }
                    else
                    {
                        parameters = new ParametersWithIV(parameters, iv);
                    }
                }
            }
            else if (mechanism.StartsWith("PBEwithSHA-1"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string) algorithmType[mechanism], new Sha1Digest(), keyBytes, salt, iterationCount);

                if (mechanism.Equals("PBEwithSHA-1and128bitRC4"))
                {
                    parameters = generator.GenerateDerivedParameters("RC4", 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and40bitRC4"))
                {
                    parameters = generator.GenerateDerivedParameters("RC4", 40);
                }
                else if (mechanism.Equals("PBEwithSHA-1and3-keyDESEDE-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DESEDE", 192, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and2-keyDESEDE-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DESEDE", 128, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and128bitRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 128, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and40bitRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 40, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1andDES-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1andRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
                }
                else if (mechanism.Equals("PBEwithSHA-1and128bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and192bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-1and256bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (mechanism.StartsWith("PBEwithSHA-256"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string) algorithmType[mechanism], new Sha256Digest(), keyBytes, salt, iterationCount);

                if (mechanism.Equals("PBEwithSHA-256and128bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-256and192bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (mechanism.Equals("PBEwithSHA-256and256bitAES-CBC-BC"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (mechanism.StartsWith("PBEwithMD5"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], new MD5Digest(), keyBytes, salt, iterationCount);

                if (mechanism.Equals("PBEwithMD5andDES-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (mechanism.Equals("PBEwithMD5andRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
                }
                else if (mechanism.Equals("PBEwithMD5and128bitAES-CBC-OpenSSL"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 128, 128);
                }
                else if (mechanism.Equals("PBEwithMD5and192bitAES-CBC-OpenSSL"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 192, 128);
                }
                else if (mechanism.Equals("PBEwithMD5and256bitAES-CBC-OpenSSL"))
                {
                    parameters = generator.GenerateDerivedParameters("AES", 256, 128);
                }
            }
            else if (mechanism.StartsWith("PBEwithMD2"))
            {
                PbeParametersGenerator generator = MakePbeGenerator(
                    (string)algorithmType[mechanism], new MD2Digest(), keyBytes, salt, iterationCount);
                if (mechanism.Equals("PBEwithMD2andDES-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("DES", 64, 64);
                }
                else if (mechanism.Equals("PBEwithMD2andRC2-CBC"))
                {
                    parameters = generator.GenerateDerivedParameters("RC2", 64, 64);
                }
            }
            else if (mechanism.StartsWith("PBEwithHmac"))
            {
                string digestName = mechanism.Substring("PBEwithHmac".Length);
                IDigest digest = DigestUtilities.GetDigest(digestName);

                PbeParametersGenerator generator = MakePbeGenerator(
                    (string) algorithmType[mechanism], digest, keyBytes, salt, iterationCount);

                int bitLen = digest.GetDigestSize() * 8;
                parameters = generator.GenerateDerivedMacParameters(bitLen);
            }

            Array.Clear(keyBytes, 0, keyBytes.Length);

            return FixDesParity(mechanism, parameters);
        }
Exemple #11
0
		protected internal virtual AlgorithmIdentifier GetAlgorithmIdentifier(
			string					encryptionOid,
			KeyParameter			encKey,
			Asn1Encodable			asn1Params,
			out ICipherParameters	cipherParameters)
		{
			Asn1Object asn1Object;
			if (asn1Params != null)
			{
				asn1Object = asn1Params.ToAsn1Object();
				cipherParameters = ParameterUtilities.GetCipherParameters(
					encryptionOid, encKey, asn1Object);
			}
			else
			{
				asn1Object = DerNull.Instance;
				cipherParameters = encKey;
			}

			return new AlgorithmIdentifier(
				new DerObjectIdentifier(encryptionOid),
				asn1Object);
		}
Exemple #12
0
        public BerOctetString(
			Asn1Encodable obj)
            : base(obj.ToAsn1Object())
        {
        }
		/// <summary>
		/// Don't use this one if you are trying to be RFC 3281 compliant.
		/// Use it for v1 attribute certificates only.
		/// </summary>
		/// <param name="names">Our GeneralNames structure</param>
		public AttCertIssuer(
			GeneralNames names)
		{
			obj = names;
			choiceObj = obj.ToAsn1Object();
		}
        protected Stream Open(
            Stream					outStream,
            string					encryptionOid,
            KeyParameter			encKey,
			Asn1Encodable			asn1Params,
			Asn1EncodableVector		recipientInfos)
        {
            Asn1Object asn1Object;
            ICipherParameters cipherParameters;

            if (asn1Params != null)
            {
                asn1Object = asn1Params.ToAsn1Object();
                cipherParameters = ParameterUtilities.GetCipherParameters(
                    encryptionOid, encKey, asn1Object);
            }
            else
            {
                asn1Object = DerNull.Instance;
                cipherParameters = encKey;
            }

            try
            {
                AlgorithmIdentifier encAlgId = new AlgorithmIdentifier(
                    new DerObjectIdentifier(encryptionOid),
                    asn1Object);

                //
                // ContentInfo
                //
                BerSequenceGenerator cGen = new BerSequenceGenerator(outStream);

                cGen.AddObject(CmsObjectIdentifiers.EnvelopedData);

                //
                // Encrypted Data
                //
                BerSequenceGenerator envGen = new BerSequenceGenerator(
                    cGen.GetRawOutputStream(), 0, true);

                envGen.AddObject(this.Version);

                DerSet derSet = _berEncodeRecipientSet
                    ?	new BerSet(recipientInfos)
                    :	new DerSet(recipientInfos);

                byte[] derSetEncoding = derSet.GetEncoded();

                envGen.GetRawOutputStream().Write(derSetEncoding, 0, derSetEncoding.Length);

                IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid);

                cipher.Init(true, cipherParameters);

                BerSequenceGenerator eiGen = new BerSequenceGenerator(
                    envGen.GetRawOutputStream());

                eiGen.AddObject(PkcsObjectIdentifiers.Data);

                byte[] tmp = encAlgId.GetEncoded();
                eiGen.GetRawOutputStream().Write(tmp, 0, tmp.Length);

                BerOctetStringGenerator octGen = new BerOctetStringGenerator(
                    eiGen.GetRawOutputStream(), 0, false);

                Stream octetOutputStream = _bufferSize != 0
                    ?	octGen.GetOctetOutputStream(new byte[_bufferSize])
                    :	octGen.GetOctetOutputStream();

                CipherStream cOut = new CipherStream(octetOutputStream, null, cipher);

                return new CmsEnvelopedDataOutputStream(cOut, cGen, envGen, eiGen);
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CmsException("key invalid in message.", e);
            }
            catch (IOException e)
            {
                throw new CmsException("exception decoding algorithm parameters.", e);
            }
        }