title: Dart Add SSL certificate and key to HttpClient
body: <p>I'm trying to secure a REST API using TLS/SSL, to do so I needed to update my client to use the public key and certificate.
  The client is written in dart and here's how I implemented the SecurityContext : </p>
  
  <pre><code>SecurityContext clientContext = SecurityContext.defaultContext;
  
  var certificate = (await rootBundle.load("assets/ssl/coastr.crt")).buffer.asInt8List();
  print(certificate.toString());
  clientContext.setTrustedCertificatesBytes(certificate);
  
  /*var authorities = (await rootBundle.load('assets/ssl/coastr.ca-bundle')).buffer.asUint8List();
  print(authorities.toString());
  clientContext.setClientAuthoritiesBytes(authorities);*/
  
  var key = (await rootBundle.load("assets/ssl/coastr_public.key")).buffer.asInt8List();
  print(key.toString());
  clientContext.usePrivateKeyBytes(key);
  
  HttpClient client = HttpClient(context: clientContext);
  
  HttpClientRequest request = await client.getUrl(Uri.parse(url));
  
  HttpClientResponse response = await request.close();
  </code></pre>
  
  <p>The certificate (.crt file) is added without issue to the clientContext but adding the key to it returns me this error : </p>
  
  <pre><code>[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception:
  TlsException: Failure in usePrivateKeyBytes (OS Error: 
  BAD_PKCS12_DATA(pkcs8_x509.c:606)
  passed a null parameter(ssl_privkey.cc:375), errno = 0)
  </code></pre>
  
  <p>The files I'm using are :</p>
  
  <ul>
  <li><p>coastr.crt with this as a header : -----BEGIN CERTIFICATE-----</p></li>
  <li><p>coastr_public.key with header : -----BEGIN PUBLIC KEY-----</p></li>
  </ul>
  
  <p>I have no idea if I'm providing the wrong files to the client or if the error comes from elsewhere.
  The files where generated using openssl.</p>
  
  <p>Thank you for your help.</p>