summaryrefslogtreecommitdiff
path: root/src/sbearssl/sbearssl_ta_readfile.c
blob: bb518877b028a1f115a9d5f255a63b1b9ab0d09b (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
37
38
39
40
/* ISC license. */

#include <sys/types.h>
#include <errno.h>

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

#include <s6-networking/sbearssl.h>

int sbearssl_ta_readfile (char const *file, genalloc *taga, stralloc *tasa)
{
  stralloc certsa = STRALLOC_ZERO ;
  genalloc certga = GENALLOC_ZERO ;
  size_t tasabase = tasa->len ;
  size_t tagabase = genalloc_len(sbearssl_ta, taga) ;
  int tasawasnull = !tasa->s ;
  int tagawasnull = !genalloc_s(sbearssl_ta, taga) ;
  int r = sbearssl_cert_readbigpem(file, &certga, &certsa) ;
  if (r) return r ;
  r = sbearssl_ta_certs(taga, tasa, genalloc_s(sbearssl_cert, &certga), genalloc_len(sbearssl_cert, &certga), certsa.s) ;
  if (r) goto fail ;

  genalloc_free(sbearssl_ta, &certga) ;
  stralloc_free(&certsa) ;
  return 0 ;

 fail:
  {
    int e = errno ;
    genalloc_free(sbearssl_cert, &certga) ;
    stralloc_free(&certsa) ;
    if (tagawasnull) genalloc_free(sbearssl_ta, taga) ;
    else genalloc_setlen(sbearssl_ta, taga, tagabase) ;
    if (tasawasnull) stralloc_free(tasa) ;
    else tasa->len = tasabase ;
    errno = e ;
  }
  return r ;
}