Exemplo n.º 1
0
        public TlsStream(Stream innerStream, string certificatePath, string password, IEnumerable <string> protocols)
        {
            _innerStream     = innerStream;
            _protocols       = ToWireFormat(protocols);
            _protocolsHandle = GCHandle.Alloc(_protocols);

            _ctx = OpenSsl.SSL_CTX_new(OpenSsl.TLSv1_2_method());

            if (_ctx == IntPtr.Zero)
            {
                throw new Exception("Unable to create SSL context.");
            }

            if (OpenSsl.SSL_CTX_Set_Pfx(_ctx, certificatePath, password) != 1)
            {
                throw new InvalidOperationException("Unable to load PFX");
            }

            OpenSsl.SSL_CTX_set_ecdh_auto(_ctx, 1);

            OpenSsl.SSL_CTX_set_alpn_select_cb(_ctx, _alpnSelectCallback, GCHandle.ToIntPtr(_protocolsHandle));

            _ssl = OpenSsl.SSL_new(_ctx);

            _inputBio = OpenSsl.BIO_new(OpenSsl.BIO_s_mem());
            OpenSsl.BIO_set_mem_eof_return(_inputBio, -1);

            _outputBio = OpenSsl.BIO_new(OpenSsl.BIO_s_mem());
            OpenSsl.BIO_set_mem_eof_return(_outputBio, -1);

            OpenSsl.SSL_set_bio(_ssl, _inputBio, _outputBio);
        }
Exemplo n.º 2
0
 static TlsStream()
 {
     OpenSsl.SSL_library_init();
     OpenSsl.SSL_load_error_strings();
     OpenSsl.ERR_load_BIO_strings();
     OpenSsl.OpenSSL_add_all_algorithms();
 }