Repository: flutter
Issue: SecurityContext.setTrustedCertificatesBytes fails with BAD_PKCS12_DATA
Labels: engine,dependency: dart,P2,team-engine,triaged-engine

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill our the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

Hi! I'm trying to use SecurityContext.setTrustedCertificateBytes with a DER encoded x509 certificate and my app keeps blowing up and telling me that it's an invalid PKCS12 format. However, the docs do not say that the x509 certificate needs to be encoded in PKCS12 (all it says is that it needs to be a DER encoding of the x509 cert). This occurs on both Android and iOS simulators. I'm wondering if I'm just missing something? I believe my code to convert the pem text certificate to the .DER binary encoding is correct, but I can post it if necessary. Of note: I am already obtaining a client private key and a client cert, converting them to PKCS12 format and setting the security context with them before I am setting the trusted certificate bytes, and I am not receiving a format error there on either platform.

## Steps to Reproduce

<!--
     Please tell us exactly how to reproduce the problem you are running into.

     Please attach a small application (ideally just one main.dart file) that
     reproduces the problem. You could use https://gist.github.com/ for this.

     If the problem is with your application's rendering, then please attach
     a screenshot and explain what the problem is.
-->

1. Obtain x509 certificate PEM via network
2. Convert x509 PEM to DER programmatically using OPENSSL in C via Platform Channel method call:
    1. Write x509 PEM to bio using BIO_write_ex
    2. Get X509 struct using PEM_read_bio_X509
    3. Create new bio for output
    4. Write x509 to BIO in DER format using i2d_X509_bio
    5. calloc() C array and call BIO_read_ex to fill it with the i2d'd bytes
    6. return DER converted to the appropriate type to get Uint8List back from method call
        * byte[] on Android
        * FlutterStandardTypedData: TypedDataWithBytes on iOS
3. Call SecurityContext.setTrustedCertificateBytes(der.toList()) (.toList() to convert to List<int> from Uint8List)

## Logs

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue.
-->

```
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: TlsException: Failure trusting builtin roots (OS Error: 
	BAD_PKCS12_DATA(pkcs8_x509.c:618), errno = 0)
#0      _SecurityContext.setTrustedCertificatesBytes (dart:io-patch/secure_socket_patch.dart:172:59)
#1      User.generateSecurityContext (package:seabrook_mobile/models/user.dart:128:21)
<asynchronous suspension>
#2      _LoginPageState.build.<anonymous closure>.<anonymous closure> (package:seabrook_mobile/screens/login_page.dart:124:50)
<asynchronous suspension>
#3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511:14)
#4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:566:30)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
#6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240:9)
#7      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:177:9)
#8      PrimaryPointe<…>
```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing seabrook_mobile...                                            

   info • Unused import: 'package:flutter_secure_storage/flutter_secure_storage.dart' • lib/main.dart:5:8 • unused_import
   info • Unused import: 'dart:ui' • lib/models/enclave.dart:1:8 • unused_import
   info • Avoid wrapping fields in getters and setters just to be "safe" • lib/models/user.dart:45:14 •
          unnecessary_getters_setters
   info • Avoid wrapping fields in getters and setters just to be "safe" • lib/models/user.dart:47:7 • unnecessary_getters_setters
   info • Unused import: 'dart:convert' • lib/sbk_crypto.dart:2:8 • unused_import
   info • Unused import: 'dart:io' • lib/sbk_crypto.dart:3:8 • unused_import
   info • Unused import: 'dart:async' • lib/screens/enclave_info_page.dart:1:8 • unused_import
   info • Unused import: 'dart:convert' • lib/screens/enclave_info_page.dart:2:8 • unused_import
   info • Unused import: 'dart:io' • lib/screens/enclave_info_page.dart:3:8 • unused_import
   info • Unused import: 'package:http/http.dart' • lib/screens/enclave_info_page.dart:7:8 • unused_import
   info • Unused import: 'package:seabrook_mobile/server_utils.dart' • lib/screens/enclave_info_page.dart:13:8 • unused_import
   info • Unused import: 'dart:convert' • lib/screens/key_test_page.dart:2:8 • unused_import
   info • Unused import: 'dart:io' • lib/screens/key_test_page.dart:3:8 • unused_import
   info • Unused import: 'package:provider/provider.dart' • lib/screens/key_test_page.dart:7:8 • unused_import
   info • Unused import: 'package:seabrook_mobile/screens/routes.dart' • lib/screens/key_test_page.dart:10:8 • unused_import
   info • Unused import: 'package:seabrook_mobile/server_utils.dart' • lib/screens/key_test_page.dart:11:8 • unused_import
   info • Unused import: 'package:seabrook_mobile/models/models.dart' • lib/screens/key_test_page.dart:12:8 • unused_import
   info • Unused import: 'dart:async' • lib/screens/login_page.dart:1:8 • unused_import
   info • Unused import: 'package:seabrook_mobile/screens/key_test_page.dart' • lib/screens/login_page.dart:10:8 • unused_import
   info • Unused import: 'package:seabrook_mobile/models/models.dart' • lib/screens/router.dart:2:8 • unused_import
   info • Unused import: 'dart:io' • lib/screens/signup_page.dart:3:8 • unused_import
   info • Unused import: 'package:provider/provider.dart' • lib/screens/signup_page.dart:9:8 • unused_import
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.6 18G87, locale en-US)
    • Flutter version 1.5.4-hotfix.2 at /Users/mike/development/flutter
    • Framework revision 7a4c33425d (4 months ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/mike/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.3, Build version 10G8
    • ios-deploy 1.9.4
    • CocoaPods version 1.6.1

[✓] Android Studio (version 3.4)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 37.0.1
    • Dart plugin version 183.6270
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[✓] IntelliJ IDEA Community Edition (version 2019.1.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 37.0.2
    • Dart plugin version 191.7830

[✓] Connected device (1 available)
    • iPhone Xʀ • 45696439-655A-4C5C-8CB9-6B3BE0782582 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-4 (simulator)

• No issues found!

```