How to solve 'CERTIFICATE_VERIFY_FAILED: self signed certificate in certificate chain(handshake.cc:354)'

486 views
Skip to first unread message

Nicolás Peñaloza

unread,
May 28, 2021, 12:35:55 PM5/28/21
to Flutter Development (flutter-dev)
Hello everyone!

I am trying to connect my application in Flutter with a Websocket, but it has not been possible as the Websocket server uses a self-signed certificate and Flutter considers the certificate to be invalid.

Does anyone know if it is possible to configure Flutter to accept self-signed certificates?

------------------------------------ CODE ----------------------------------------------------

import 'package:flutter/material.dart';
import 'package:web_socket_channel/io.dart';
import 'package:test1/home.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test 1',
      home: HomePage(
        channel: IOWebSocketChannel.connect('wss://10.0.2.2:6868'),
      ),
    );
  }
}

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class HomePage extends StatefulWidget {
  final WebSocketChannel channel;

  HomePage({Key key, @required this.channel}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Map<String, dynamic> request = {
    "id": 1,
    "jsonrpc": "2.0",
    "method": "getCortexInfo"
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Test 1'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Center(
              child: IconButton(
                  icon: Icon(Icons.send),
                  onPressed: () {
                    widget.channel.sink.add(jsonEncode(request));
                  }),
            ),
            StreamBuilder(
                stream: widget.channel.stream,
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  print('Response: ${snapshot.data}');
                  print('Error: ${snapshot.error}');
                  return Padding(
                    padding: const EdgeInsets.only(top: 20.0),
                    child: Text(
                        snapshot.hasData ? '${snapshot.data}' : 'Not response'),
                  );
                }),
          ],
        ));
  }

  @override
  void dispose() {
    widget.channel.sink.close();
    super.dispose();
  }
}

------------------------------------------------------------------------------------------------
ERROR : WebSocketChannelException: WebSocketChannelException: HandshakeException: Handshake error in client (OS Error: CERTIFICATE_VERIFY_FAILED: self signed certificate in certificate chain(handshake.cc:354))

errorflutter.png
Reply all
Reply to author
Forward
0 new messages