[flutter] amqps를 위한 tls 설정

Edward Hyun·2022년 2월 12일
0

app&web-dev

목록 보기
79/178
post-custom-banner

flutter dart의 dart_amqp 라이브러리에서 tls 설정할 수 있다고 한다.
https://pub.dev/packages/dart_amqp/example

ConnectionSettings({
    this.host = "127.0.0.1",
    this.port = 5672,
    this.virtualHost = "/",
    this.authProvider = const PlainAuthenticator("guest", "guest"),
    this.maxConnectionAttempts = 1,
    this.reconnectWaitTime = const Duration(milliseconds: 1500),
    TuningSettings? tuningSettings,
    this.tlsContext,
    this.onBadCertificate,
  }) : tuningSettings = tuningSettings ?? TuningSettings();

여기에서 tlsContext 부분이 tls 설정 부분이다.

Security Context 공식 문서 https://api.flutter.dev/flutter/dart-io/SecurityContext-class.html

tls관련
http://gjchoi.github.io/rabbit/rabbit-mq-TLS/
https://www.youtube.com/results?search_query=SSL+in+flutter
https://doc.xuwenliang.com/docs/dart-flutter/1378 [TLS/SSL with Dart]
https://github.com/xoltawn/mtls_example_flutter/blob/main/lib/main.dart [mtls 예제]

SecurityContext sc = SecurityContext(withTrustedRoots: true);
      sc.useCertificateChainBytes(certificateChainBytes);
      sc.usePrivateKeyBytes(keyBytes);
Future<List<int>> getKeyBytes() async {
    return (await rootBundle.load('assets/keys/private.key'))
        .buffer
        .asInt8List();
  }

Future<List<int>> getCertificateChainBytes() async {
    return (await rootBundle.load('assets/keys/certificate.crt'))
        .buffer
        .asInt8List();
  }

Future<SecurityContext> get globalContext async {
    List<int> keyBytes = await getKeyBytes();
    List<int> certificateChainBytes = await getCertificateChainBytes();

    SecurityContext sc = SecurityContext(withTrustedRoots: false);
    sc.usePrivateKeyBytes(keyBytes);
    sc.useCertificateChainBytes(certificateChainBytes);
    return sc;
  }

https://api.flutter.dev/flutter/dart-io/SecurityContext/usePrivateKeyBytes.html
https://api.flutter.dev/flutter/dart-io/SecurityContext/useCertificateChainBytes.html

Future<SecurityContext> get globalContext async {
    final List<int> keyBytes =
        (await rootBundle.load(‘assets/resources/key.key’))
        .buffer
        .asInt8List();
    final List<int> certificateChainBytes =
        (await rootBundle.load(‘assets/resources/key_crt.crt’))
        .buffer
        .asInt8List();
    SecurityContext sc = SecurityContext(withTrustedRoots: false);
    sc.usePrivateKeyBytes(keyBytes);
    sc.useCertificateChainBytes(certificateChainBytes);
    return sc;
}

https://xoltawn.medium.com/how-to-use-mtls-in-flutter-da698e5700ef
https://github.com/xoltawn/mtls_example_flutter/blob/main/lib/main.dart

SSL pinning
https://medium.com/surfstudio/ssl-pinning-in-flutter-apps-254e01e57965

[중요]
client 에서 tls관련 설정은 아래와 같다.

ByteData data = await rootBundle.load('assets/raw/certificate.crt');
SecurityContext context = SecurityContext.defaultContext;
context.setTrustedCertificatesBytes(data.buffer.asUint8List());
client = HttpClient(context: context);

참고 :: https://stackoverflow.com/questions/54104685/flutter-add-self-signed-certificate-from-asset-folder

profile
앱&웹개발(flutter, vuejs, typescript, react), 인공지능(nlp, asr, rl), 백엔드(nodejs, flask, golang, grpc, webrtc, aws, msa, nft, spring cloud, nest.js), 함수형 프로그래밍(scala, erlang)을 공부하며 정리합니다.
post-custom-banner

0개의 댓글