summaryrefslogtreecommitdiff
path: root/src/sbearssl/sbearssl_get_keycert.c
blob: b0cccabf249fbf19535a3228f8e3968f717e4b58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* ISC license. */

#include <stdlib.h>

#include <skalibs/strerr.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>

#include <s6-networking/sbearssl.h>
#include "sbearssl-internal.h"

size_t sbearssl_get_keycert (sbearssl_skey *skey, genalloc *certs, stralloc *storage)
{
  size_t chainlen ;
  int r ;
  char const *x = getenv("CERTFILE") ;
  if (!x) strerr_dienotset(100, "CERTFILE") ;
  r = sbearssl_cert_readbigpem(x, certs, storage) ;
  if (r < 0)
    strerr_diefu2sys(111, "read certificate chain in ", x) ;
  else if (r)
    strerr_diefu4sys(96, "read certificate chain in ", x, ": ", sbearssl_error_str(r)) ;
  chainlen = genalloc_len(sbearssl_cert, certs) ;
  if (!chainlen)
    strerr_diefu2x(96, "find a certificate in ", x) ;

  x = getenv("KEYFILE") ;
  if (!x) strerr_dienotset(100, "KEYFILE") ;
  r = sbearssl_skey_readfile(x, skey, storage) ;
  if (r < 0)
    strerr_diefu2sys(111, "read private key in ", x) ;
  else if (r)
    strerr_diefu4x(96, "decode private key in ", x, ": ", sbearssl_error_str(r)) ;

  return chainlen ;
}