Exemple #1
0
 public object _wrap_socket(CodeContext context, PythonSocket.socket sock = null, bool server_side = false, string server_hostname = null, object ssl_sock = null)
 {
     return(new PythonSocket.ssl(context, sock, server_side, null, _cafile, verify_mode, protocol | options, null, _cert_store)
     {
         _serverHostName = server_hostname
     });
 }
Exemple #2
0
 public object _wrap_socket(CodeContext context, [DefaultParameterValue(null)] PythonSocket.socket sock, [DefaultParameterValue(false)] bool server_side, [DefaultParameterValue(null)] string server_hostname, [DefaultParameterValue(null)] object ssl_sock)
 {
     return(new PythonSocket.ssl(context, sock, server_side, null, _cafile, verify_mode, protocol | options, null, _cert_store)
     {
         _serverHostName = server_hostname
     });
 }
Exemple #3
0
        /// <summary>
        /// Return the System.Net.Sockets.Socket object that corresponds to the passed-in
        /// object. obj can be a System.Net.Sockets.Socket, a PythonSocket.SocketObj, a
        /// long integer (representing a socket handle), or a Python object with a fileno()
        /// method (whose result is used to look up an existing PythonSocket.SocketObj,
        /// which is in turn converted to a Socket.
        /// </summary>
        private static Socket ObjectToSocket(CodeContext context, object obj)
        {
            Socket socket;

            PythonSocket.socket pythonSocket = obj as PythonSocket.socket;
            if (pythonSocket != null)
            {
                return(pythonSocket._socket);
            }

            Int64 handle;

            if (!Converter.TryConvertToInt64(obj, out handle))
            {
                object userSocket     = obj;
                object filenoCallable = PythonOps.GetBoundAttr(context, userSocket, "fileno");
                object fileno         = PythonCalls.Call(context, filenoCallable);
                handle = Converter.ConvertToInt64(fileno);
            }
            if (handle < 0)
            {
                throw PythonOps.ValueError("file descriptor cannot be a negative number ({0})", handle);
            }
            socket = PythonSocket.socket.HandleToSocket(handle);
            if (socket == null)
            {
                SocketException e = new SocketException((int)SocketError.NotSocket);
                throw PythonExceptions.CreateThrowable((PythonType)PythonContext.GetContext(context).GetModuleState("selecterror"), PythonTuple.MakeTuple(e.ErrorCode, e.Message));
            }
            return(socket);
        }
Exemple #4
0
 public _SSLSocket(CodeContext context, PythonSocket.socket sock, string keyfile = null, string certfile = null, X509Certificate2Collection certs = null)
 {
     _context        = context;
     _sslStream      = new SslStream(new NetworkStream(sock._socket, false), true, CertValidationCallback);
     _socket         = sock;
     _protocol       = PythonSsl.PROTOCOL_SSLv23 | PythonSsl.OP_NO_SSLv2 | PythonSsl.OP_NO_SSLv3;
     _validate       = false;
     _certCollection = certs ?? new X509Certificate2Collection();
 }
Exemple #5
0
 public static PythonSocket.ssl sslwrap(
     CodeContext context,
     PythonSocket.socket socket,
     bool server_side,
     [DefaultParameterValue(null)] string keyfile,
     [DefaultParameterValue(null)] string certfile,
     [DefaultParameterValue(PythonSsl.CERT_NONE)] int certs_mode,
     [DefaultParameterValue(-1)] int protocol,
     [DefaultParameterValue(null)] string cacertsfile)
 {
     return(new PythonSocket.ssl(
                context,
                socket,
                server_side,
                keyfile,
                certfile,
                certs_mode,
                protocol,
                cacertsfile
                ));
 }
Exemple #6
0
 public static PythonSocket.ssl sslwrap(
     CodeContext context,
     PythonSocket.socket socket,
     bool server_side,
     [DefaultParameterValue(null)] string keyfile,
     [DefaultParameterValue(null)] string certfile,
     [DefaultParameterValue(PythonSsl.CERT_NONE)] int certs_mode,
     [DefaultParameterValue(PythonSsl.PROTOCOL_SSLv23 | PythonSsl.OP_NO_SSLv2 | PythonSsl.OP_NO_SSLv3)] int protocol,
     [DefaultParameterValue(null)] string cacertsfile,
     [DefaultParameterValue(null)] object ciphers)
 {
     return(new PythonSocket.ssl(
                context,
                socket,
                server_side,
                keyfile,
                certfile,
                certs_mode,
                protocol,
                cacertsfile
                ));
 }
Exemple #7
0
 public static PythonSocket.ssl sslwrap(
     CodeContext context,
     PythonSocket.socket socket, 
     bool server_side, 
     string keyfile=null, 
     string certfile=null,
     int certs_mode=PythonSsl.CERT_NONE,
     int protocol= (PythonSsl.PROTOCOL_SSLv23 | PythonSsl.OP_NO_SSLv2 | PythonSsl.OP_NO_SSLv3),
     string cacertsfile=null,
     object ciphers=null) {
     return new PythonSocket.ssl(
         context,
         socket,
         server_side,
         keyfile,
         certfile,
         certs_mode,
         protocol,
         cacertsfile,
         null
     );
 }
Exemple #8
0
            internal _SSLSocket(CodeContext context,
                                PythonSocket.socket sock,
                                bool server_side,
                                string keyfile     = null,
                                string certfile    = null,
                                int certs_mode     = PythonSsl.CERT_NONE,
                                int protocol       = (PythonSsl.PROTOCOL_SSLv23 | PythonSsl.OP_NO_SSLv2 | PythonSsl.OP_NO_SSLv3),
                                string cacertsfile = null,
                                X509Certificate2Collection certs = null)
            {
                if (sock == null)
                {
                    throw PythonOps.TypeError("expected socket object, got None");
                }

                _serverSide = server_side;
                bool validate;

                _certsMode = certs_mode;

                RemoteCertificateValidationCallback callback;

                switch (certs_mode)
                {
                case PythonSsl.CERT_NONE:
                    validate = false;
                    callback = CertValidationCallback;
                    break;

                case PythonSsl.CERT_OPTIONAL:
                    validate = true;
                    callback = CertValidationCallbackOptional;
                    break;

                case PythonSsl.CERT_REQUIRED:
                    validate = true;
                    callback = CertValidationCallbackRequired;
                    break;

                default:
                    throw new InvalidOperationException(String.Format("bad certs_mode: {0}", certs_mode));
                }

                _callback = callback;

                if (certs != null)
                {
                    _certCollection = certs;
                }

                if (certfile != null)
                {
                    _cert = PythonSsl.ReadCertificate(context, certfile);
                }

                if (cacertsfile != null)
                {
                    _certCollection = new X509Certificate2Collection(new[] { PythonSsl.ReadCertificate(context, cacertsfile) });
                }

                _socket = sock;

                EnsureSslStream(false);

                _protocol = protocol;
                _validate = validate;
                _context  = context;
            }