Esempio n. 1
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     base.Encode(bcpgOut);
     bcpgOut.WriteByte(3);
     bcpgOut.WriteByte(reserved);
     bcpgOut.WriteByte((byte)hashFunctionId);
     bcpgOut.WriteByte((byte)symAlgorithmId);
 }
    public override void Encode(BcpgOutputStream bcpgOut)
    {
        MemoryStream     memoryStream     = new MemoryStream();
        BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream);

        bcpgOutputStream.WriteByte((byte)version);
        bcpgOutputStream.WriteLong(keyId);
        bcpgOutputStream.WriteByte((byte)algorithm);
        for (int i = 0; i < data.Length; i++)
        {
            bcpgOutputStream.Write(data[i]);
        }
        Platform.Dispose(bcpgOutputStream);
        bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession, memoryStream.ToArray(), oldFormat: true);
    }
Esempio n. 3
0
    public virtual byte[] GetEncodedContents()
    {
        MemoryStream     memoryStream     = new MemoryStream();
        BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream);

        bcpgOutputStream.WriteByte((byte)version);
        bcpgOutputStream.WriteInt((int)time);
        if (version <= 3)
        {
            bcpgOutputStream.WriteShort((short)validDays);
        }
        bcpgOutputStream.WriteByte((byte)algorithm);
        bcpgOutputStream.WriteObject((BcpgObject)key);
        return(memoryStream.ToArray());
    }
Esempio n. 4
0
        private void DoOpen()
        {
            _pkOut.WriteByte((byte)_algorithm);

            switch (_algorithm)
            {
            case CompressionAlgorithmTag.Uncompressed:
                _dOut = _pkOut;
                break;

            case CompressionAlgorithmTag.Zip:
                _dOut = new SafeZOutputStream(_pkOut, _compression, true);
                break;

            case CompressionAlgorithmTag.ZLib:
                _dOut = new SafeZOutputStream(_pkOut, _compression, false);
                break;

            case CompressionAlgorithmTag.BZip2:
                _dOut = new SafeCBZip2OutputStream(_pkOut);
                break;

            default:
                // Constructor should guard against this possibility
                throw new InvalidOperationException();
            }
        }
Esempio n. 5
0
    private void doOpen()
    {
        pkOut.WriteByte((byte)algorithm);
        switch (algorithm)
        {
        case CompressionAlgorithmTag.Uncompressed:
            dOut = pkOut;
            break;

        case CompressionAlgorithmTag.Zip:
            dOut = new SafeZOutputStream(pkOut, compression, nowrap: true);
            break;

        case CompressionAlgorithmTag.ZLib:
            dOut = new SafeZOutputStream(pkOut, compression, nowrap: false);
            break;

        case CompressionAlgorithmTag.BZip2:
            dOut = new SafeCBZip2OutputStream(pkOut);
            break;

        default:
            throw new InvalidOperationException();
        }
    }
Esempio n. 6
0
    public override void Encode(BcpgOutputStream bcpgOut)
    {
        MemoryStream     memoryStream     = new MemoryStream();
        BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream);

        bcpgOutputStream.WriteByte((byte)version);
        if (version == 3 || version == 2)
        {
            bcpgOutputStream.Write(5, (byte)signatureType);
            bcpgOutputStream.WriteInt((int)(creationTime / 1000));
            bcpgOutputStream.WriteLong(keyId);
            bcpgOutputStream.Write((byte)keyAlgorithm, (byte)hashAlgorithm);
        }
        else
        {
            if (version != 4)
            {
                throw new IOException("unknown version: " + version);
            }
            bcpgOutputStream.Write((byte)signatureType, (byte)keyAlgorithm, (byte)hashAlgorithm);
            EncodeLengthAndData(bcpgOutputStream, GetEncodedSubpackets(hashedData));
            EncodeLengthAndData(bcpgOutputStream, GetEncodedSubpackets(unhashedData));
        }
        bcpgOutputStream.Write(fingerprint);
        if (signature != null)
        {
            bcpgOutputStream.WriteObjects(signature);
        }
        else
        {
            bcpgOutputStream.Write(signatureEncoding);
        }
        bcpgOut.WritePacket(PacketTag.Signature, memoryStream.ToArray(), oldFormat: true);
    }
Esempio n. 7
0
        private void doOpen()
        {
            pkOut.WriteByte((byte)algorithm);

            switch (algorithm)
            {
            case CompressionAlgorithmTag.Uncompressed:
                dOut = pkOut;
                break;

            case CompressionAlgorithmTag.Zip:
                dOut = new ZDeflaterOutputStream(pkOut, compression, true);
                break;

            case CompressionAlgorithmTag.ZLib:
                dOut = new ZDeflaterOutputStream(pkOut, compression, false);
                break;

            case CompressionAlgorithmTag.BZip2:
                dOut = new CBZip2OutputStream(pkOut);
                break;

            default:
                // Constructor should guard against this possibility
                throw new InvalidOperationException();
            }
        }
Esempio n. 8
0
    public override void Encode(BcpgOutputStream bcpgOut)
    {
        MemoryStream     memoryStream     = new MemoryStream();
        BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream);

        bcpgOutputStream.Write((byte)version, (byte)sigType, (byte)hashAlgorithm, (byte)keyAlgorithm);
        bcpgOutputStream.WriteLong(keyId);
        bcpgOutputStream.WriteByte((byte)nested);
        bcpgOut.WritePacket(PacketTag.OnePassSignature, memoryStream.ToArray(), oldFormat: true);
    }
Esempio n. 9
0
    public byte[] GetEncodedContents()
    {
        MemoryStream     memoryStream     = new MemoryStream();
        BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream);

        bcpgOutputStream.Write(pubKeyPacket.GetEncodedContents());
        bcpgOutputStream.WriteByte((byte)s2kUsage);
        if (s2kUsage == 255 || s2kUsage == 254)
        {
            bcpgOutputStream.WriteByte((byte)encAlgorithm);
            bcpgOutputStream.WriteObject(s2k);
        }
        if (iv != null)
        {
            bcpgOutputStream.Write(iv);
        }
        if (secKeyData != null && secKeyData.Length > 0)
        {
            bcpgOutputStream.Write(secKeyData);
        }
        return(memoryStream.ToArray());
    }
Esempio n. 10
0
        private void doOpen()
        {
            pkOut.WriteByte((byte)algorithm);

            switch (algorithm)
            {
            case CompressionAlgorithmTag.Uncompressed:
                dOut = pkOut;
                break;

            //case CompressionAlgorithmTag.Zip:
            //    dOut = new DeflaterOutputStream(pkOut, new Deflater(compression, true));
            //    break;
            //case CompressionAlgorithmTag.ZLib:
            //    dOut = new DeflaterOutputStream(pkOut, new Deflater(compression, false));
            //    break;
            //case CompressionAlgorithmTag.BZip2:
            //    dOut = new BZip2OutputStream(pkOut);
            //    break;
            default:
                // Constructor should guard against this possibility
                throw new ExecutionEngineException();
            }
        }
Esempio n. 11
0
 public override void Encode(BcpgOutputStream bcpgOut)
 {
     bcpgOut.WriteByte((byte)type);
     bcpgOut.WriteByte((byte)algorithm);
     if (type != 101)
     {
         if (type != 0)
         {
             bcpgOut.Write(iv);
         }
         if (type == 3)
         {
             bcpgOut.WriteByte((byte)itCount);
         }
     }
     else
     {
         bcpgOut.WriteByte(71);
         bcpgOut.WriteByte(78);
         bcpgOut.WriteByte(85);
         bcpgOut.WriteByte((byte)protectionMode);
     }
 }
        /// <summary>
        ///     <p>
        ///         If buffer is non null stream assumed to be partial, otherwise the length will be used
        ///         to output a fixed length packet.
        ///     </p>
        ///     <p>
        ///         The stream created can be closed off by either calling Close()
        ///         on the stream or Close() on the generator. Closing the returned
        ///         stream does not close off the Stream parameter <c>outStr</c>.
        ///     </p>
        /// </summary>
        private Stream Open(Stream outStr, long length, byte[] buffer)
        {
            if (_cOut != null)
            {
                throw new InvalidOperationException("generator already in open state");
            }
            if (_methods.Count == 0)
            {
                throw new InvalidOperationException("No encryption methods specified");
            }
            if (outStr == null)
            {
                throw new ArgumentNullException("outStr");
            }

            _pOut = new BcpgOutputStream(outStr);

            KeyParameter key;

            if (_methods.Count == 1)
            {
                var pbeMethod = _methods[0] as PbeMethod;
                if (pbeMethod != null)
                {
                    key = pbeMethod.GetKey();
                }
                else
                {
                    var pubMethod = (PubMethod)_methods[0];

                    key = PgpUtilities.MakeRandomKey(_defAlgorithm, _rand);

                    var sessionInfo = CreateSessionInfo(_defAlgorithm, key);


                    try
                    {
                        pubMethod.AddSessionInfo(sessionInfo, _rand);
                    }
                    catch (Exception e)
                    {
                        throw new PgpException("exception encrypting session key", e);
                    }
                }

                _pOut.WritePacket((ContainedPacket)_methods[0]);
            }
            else // multiple methods
            {
                key = PgpUtilities.MakeRandomKey(_defAlgorithm, _rand);
                var sessionInfo = CreateSessionInfo(_defAlgorithm, key);

                for (var i = 0; i != _methods.Count; i++)
                {
                    var m = (EncMethod)_methods[i];

                    try
                    {
                        m.AddSessionInfo(sessionInfo, _rand);
                    }
                    catch (Exception e)
                    {
                        throw new PgpException("exception encrypting session key", e);
                    }

                    _pOut.WritePacket(m);
                }
            }

            var cName = PgpUtilities.GetSymmetricCipherName(_defAlgorithm);

            if (cName == null)
            {
                throw new PgpException("null cipher specified");
            }

            try
            {
                if (_withIntegrityPacket)
                {
                    cName += "/CFB/NoPadding";
                }
                else
                {
                    cName += "/OpenPGPCFB/NoPadding";
                }

                _c = CipherUtilities.GetCipher(cName);

                // TODO Confirm the IV should be all zero bytes (not inLineIv - see below)
                var iv = new byte[_c.GetBlockSize()];
                _c.Init(true, new ParametersWithRandom(new ParametersWithIV(key, iv), _rand));

                if (buffer == null)
                {
                    //
                    // we have to Add block size + 2 for the Generated IV and + 1 + 22 if integrity protected
                    //
                    if (_withIntegrityPacket)
                    {
                        _pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricEncryptedIntegrityProtected,
                                                     length + _c.GetBlockSize() + 2 + 1 + 22);
                        _pOut.WriteByte(1); // version number
                    }
                    else
                    {
                        _pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricKeyEncrypted,
                                                     length + _c.GetBlockSize() + 2, _oldFormat);
                    }
                }
                else
                {
                    if (_withIntegrityPacket)
                    {
                        _pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricEncryptedIntegrityProtected, buffer);
                        _pOut.WriteByte(1); // version number
                    }
                    else
                    {
                        _pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricKeyEncrypted, buffer);
                    }
                }

                var blockSize = _c.GetBlockSize();
                var inLineIv  = new byte[blockSize + 2];
                _rand.NextBytes(inLineIv, 0, blockSize);
                Array.Copy(inLineIv, inLineIv.Length - 4, inLineIv, inLineIv.Length - 2, 2);

                Stream myOut = _cOut = new CipherStream(_pOut, null, _c);

                if (_withIntegrityPacket)
                {
                    var digestName = PgpUtilities.GetDigestName(HashAlgorithmTag.Sha1);
                    var digest     = DigestUtilities.GetDigest(digestName);
                    myOut = _digestOut = new DigestStream(myOut, null, digest);
                }

                myOut.Write(inLineIv, 0, inLineIv.Length);

                return(new WrappedGeneratorStream(this, myOut));
            }
            catch (Exception e)
            {
                throw new PgpException("Exception creating cipher", e);
            }
        }
		/// <summary>
		/// <p>
		/// If buffer is non null stream assumed to be partial, otherwise the length will be used
		/// to output a fixed length packet.
		/// </p>
		/// <p>
		/// The stream created can be closed off by either calling Close()
		/// on the stream or Close() on the generator. Closing the returned
		/// stream does not close off the Stream parameter <c>outStr</c>.
		/// </p>
		/// </summary>
        private Stream Open(
            Stream	outStr,
            long	length,
            byte[]	buffer)
        {
			if (cOut != null)
				throw new InvalidOperationException("generator already in open state");
			if (methods.Count == 0)
				throw new InvalidOperationException("No encryption methods specified");
			if (outStr == null)
				throw new ArgumentNullException("outStr");

			pOut = new BcpgOutputStream(outStr);

			KeyParameter key;

			if (methods.Count == 1)
            {
                if (methods[0] is PbeMethod)
                {
                    PbeMethod m = (PbeMethod)methods[0];

					key = m.GetKey();
                }
                else
                {
                    key = PgpUtilities.MakeRandomKey(defAlgorithm, rand);

					byte[] sessionInfo = CreateSessionInfo(defAlgorithm, key);
                    PubMethod m = (PubMethod)methods[0];

                    try
                    {
                        m.AddSessionInfo(sessionInfo, rand);
                    }
                    catch (Exception e)
                    {
                        throw new PgpException("exception encrypting session key", e);
                    }
                }

				pOut.WritePacket((ContainedPacket)methods[0]);
            }
            else // multiple methods
            {
                key = PgpUtilities.MakeRandomKey(defAlgorithm, rand);
				byte[] sessionInfo = CreateSessionInfo(defAlgorithm, key);

				for (int i = 0; i != methods.Count; i++)
                {
                    EncMethod m = (EncMethod)methods[i];

                    try
                    {
                        m.AddSessionInfo(sessionInfo, rand);
                    }
                    catch (Exception e)
                    {
                        throw new PgpException("exception encrypting session key", e);
                    }

                    pOut.WritePacket(m);
                }
            }

            string cName = PgpUtilities.GetSymmetricCipherName(defAlgorithm);
			if (cName == null)
            {
                throw new PgpException("null cipher specified");
            }

			try
            {
                if (withIntegrityPacket)
                {
                    cName += "/CFB/NoPadding";
                }
                else
                {
                    cName += "/OpenPGPCFB/NoPadding";
                }

                c = CipherUtilities.GetCipher(cName);

				// TODO Confirm the IV should be all zero bytes (not inLineIv - see below)
				byte[] iv = new byte[c.GetBlockSize()];
                c.Init(true, new ParametersWithRandom(new ParametersWithIV(key, iv), rand));

                if (buffer == null)
                {
                    //
                    // we have to Add block size + 2 for the Generated IV and + 1 + 22 if integrity protected
                    //
                    if (withIntegrityPacket)
                    {
                        pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricEncryptedIntegrityProtected, length + c.GetBlockSize() + 2 + 1 + 22);
                        pOut.WriteByte(1);        // version number
                    }
                    else
                    {
                        pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricKeyEncrypted, length + c.GetBlockSize() + 2, oldFormat);
                    }
                }
                else
                {
                    if (withIntegrityPacket)
                    {
                        pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricEncryptedIntegrityProtected, buffer);
                        pOut.WriteByte(1);        // version number
                    }
                    else
                    {
                        pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricKeyEncrypted, buffer);
                    }
                }

				int blockSize = c.GetBlockSize();
				byte[] inLineIv = new byte[blockSize + 2];
                rand.NextBytes(inLineIv, 0, blockSize);
				Array.Copy(inLineIv, inLineIv.Length - 4, inLineIv, inLineIv.Length - 2, 2);

				Stream myOut = cOut = new CipherStream(pOut, null, c);

				if (withIntegrityPacket)
                {
					string digestName = PgpUtilities.GetDigestName(HashAlgorithmTag.Sha1);
					IDigest digest = DigestUtilities.GetDigest(digestName);
					myOut = digestOut = new DigestStream(myOut, null, digest);
                }

				myOut.Write(inLineIv, 0, inLineIv.Length);

				return new WrappedGeneratorStream(this, myOut);
            }
            catch (Exception e)
            {
                throw new PgpException("Exception creating cipher", e);
            }
        }
Esempio n. 14
0
    private Stream Open(Stream outStr, long length, byte[] buffer)
    {
        if (cOut != null)
        {
            throw new InvalidOperationException("generator already in open state");
        }
        if (methods.Count == 0)
        {
            throw new InvalidOperationException("No encryption methods specified");
        }
        if (outStr == null)
        {
            throw new ArgumentNullException("outStr");
        }
        pOut = new BcpgOutputStream(outStr);
        KeyParameter keyParameter;

        if (methods.Count == 1)
        {
            if (methods[0] is PbeMethod)
            {
                PbeMethod pbeMethod = (PbeMethod)methods[0];
                keyParameter = pbeMethod.GetKey();
            }
            else
            {
                keyParameter = PgpUtilities.MakeRandomKey(defAlgorithm, rand);
                byte[]    si        = CreateSessionInfo(defAlgorithm, keyParameter);
                PubMethod pubMethod = (PubMethod)methods[0];
                try
                {
                    pubMethod.AddSessionInfo(si, rand);
                }
                catch (Exception exception)
                {
                    throw new PgpException("exception encrypting session key", exception);
                }
            }
            pOut.WritePacket((ContainedPacket)methods[0]);
        }
        else
        {
            keyParameter = PgpUtilities.MakeRandomKey(defAlgorithm, rand);
            byte[] si2 = CreateSessionInfo(defAlgorithm, keyParameter);
            for (int i = 0; i != methods.Count; i++)
            {
                EncMethod encMethod = (EncMethod)methods[i];
                try
                {
                    encMethod.AddSessionInfo(si2, rand);
                }
                catch (Exception exception2)
                {
                    throw new PgpException("exception encrypting session key", exception2);
                }
                pOut.WritePacket(encMethod);
            }
        }
        string symmetricCipherName = PgpUtilities.GetSymmetricCipherName(defAlgorithm);

        if (symmetricCipherName == null)
        {
            throw new PgpException("null cipher specified");
        }
        try
        {
            symmetricCipherName = ((!withIntegrityPacket) ? (symmetricCipherName + "/OpenPGPCFB/NoPadding") : (symmetricCipherName + "/CFB/NoPadding"));
            c = CipherUtilities.GetCipher(symmetricCipherName);
            byte[] iv = new byte[c.GetBlockSize()];
            c.Init(forEncryption: true, new ParametersWithRandom(new ParametersWithIV(keyParameter, iv), rand));
            if (buffer == null)
            {
                if (withIntegrityPacket)
                {
                    pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricEncryptedIntegrityProtected, length + c.GetBlockSize() + 2 + 1 + 22);
                    pOut.WriteByte(1);
                }
                else
                {
                    pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricKeyEncrypted, length + c.GetBlockSize() + 2, oldFormat);
                }
            }
            else if (withIntegrityPacket)
            {
                pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricEncryptedIntegrityProtected, buffer);
                pOut.WriteByte(1);
            }
            else
            {
                pOut = new BcpgOutputStream(outStr, PacketTag.SymmetricKeyEncrypted, buffer);
            }
            int    blockSize = c.GetBlockSize();
            byte[] array     = new byte[blockSize + 2];
            rand.NextBytes(array, 0, blockSize);
            Array.Copy(array, array.Length - 4, array, array.Length - 2, 2);
            Stream stream = cOut = new CipherStream(pOut, null, c);
            if (withIntegrityPacket)
            {
                string  digestName = PgpUtilities.GetDigestName(HashAlgorithmTag.Sha1);
                IDigest digest     = DigestUtilities.GetDigest(digestName);
                stream = (digestOut = new DigestStream(stream, null, digest));
            }
            stream.Write(array, 0, array.Length);
            return(new WrappedGeneratorStream(this, stream));
        }
        catch (Exception exception3)
        {
            throw new PgpException("Exception creating cipher", exception3);
        }
    }