summaryrefslogtreecommitdiff
path: root/src/sbearssl/sbearssl_cert_readfile.c
blob: b00a35a6d9b297479ce96eafd5622817b05c6f11 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ISC license. */

#include <string.h>

#include <bearssl.h>

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

#include <s6-networking/sbearssl.h>

int sbearssl_cert_readfile (char const *fn, genalloc *certs, stralloc *sa)
{
  char buf[SBEARSSL_MAXCERTFILESIZE] ;
  size_t certsbase = genalloc_len(sbearssl_cert, certs) ;
  size_t sabase = sa->len ;
  size_t n ;
  int certswasnull = !genalloc_s(sbearssl_cert, certs) ;
  int sawasnull = !sa->s ;
  {
    ssize_t r = openreadnclose(fn, buf, SBEARSSL_MAXCERTFILESIZE) ;
    if (r < 0) return r ;
    n = r ;
  }
  if (sbearssl_isder((unsigned char *)buf, n))
  {
    sbearssl_cert cert = { .data = sa->len, .datalen = n } ;
    if (!stralloc_catb(sa, buf, n)) return -1 ;
    if (!genalloc_append(sbearssl_cert, certs, &cert)) goto fail ;
  }
  else
  {
    genalloc pems = GENALLOC_ZERO ;
    size_t i = 0 ;
    sbearssl_pemobject *p ;
    int r = sbearssl_pem_decode_from_string(buf, n, &pems, sa) ;
    if (r) return r ;
    p = genalloc_s(sbearssl_pemobject, &pems) ;
    n = genalloc_len(sbearssl_pemobject, &pems) ;
    for (; i < n ; i++)
    {
      char const *name = sa->s + p[i].name ;
      if (!strcmp(name, "CERTIFICATE")
       || !strcmp(name, "X509 CERTIFICATE"))
      {
        sbearssl_cert cert = { .data = p[i].data, .datalen = p[i].datalen } ;
        if (!genalloc_append(sbearssl_cert, certs, &cert))
        {
          genalloc_free(sbearssl_pemobject, &pems) ;
          goto fail ;
        }
      }
      genalloc_free(sbearssl_pemobject, &pems) ;
    }
  }
  
  return 0 ;

 fail:
  if (certswasnull) genalloc_free(sbearssl_cert, certs) ;
  else genalloc_setlen(sbearssl_cert, certs, certsbase) ;
  if (sawasnull) stralloc_free(sa) ;
  else sa->len = sabase ;
  return -1 ;
}