1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty *
4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty * the following conditions:
11*0957b409SSimon J. Gerraty *
12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty *
15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty * SOFTWARE.
23*0957b409SSimon J. Gerraty */
24*0957b409SSimon J. Gerraty
25*0957b409SSimon J. Gerraty #include "brssl.h"
26*0957b409SSimon J. Gerraty
27*0957b409SSimon J. Gerraty /* see brssl.h */
28*0957b409SSimon J. Gerraty void *
xmalloc(size_t len)29*0957b409SSimon J. Gerraty xmalloc(size_t len)
30*0957b409SSimon J. Gerraty {
31*0957b409SSimon J. Gerraty void *buf;
32*0957b409SSimon J. Gerraty
33*0957b409SSimon J. Gerraty if (len == 0) {
34*0957b409SSimon J. Gerraty return NULL;
35*0957b409SSimon J. Gerraty }
36*0957b409SSimon J. Gerraty buf = malloc(len);
37*0957b409SSimon J. Gerraty if (buf == NULL) {
38*0957b409SSimon J. Gerraty #ifndef _STANDALONE
39*0957b409SSimon J. Gerraty fprintf(stderr, "ERROR: could not allocate %lu byte(s)\n",
40*0957b409SSimon J. Gerraty (unsigned long)len);
41*0957b409SSimon J. Gerraty exit(EXIT_FAILURE);
42*0957b409SSimon J. Gerraty #else
43*0957b409SSimon J. Gerraty ;
44*0957b409SSimon J. Gerraty #endif
45*0957b409SSimon J. Gerraty }
46*0957b409SSimon J. Gerraty return buf;
47*0957b409SSimon J. Gerraty }
48*0957b409SSimon J. Gerraty
49*0957b409SSimon J. Gerraty /* see brssl.h */
50*0957b409SSimon J. Gerraty void
xfree(void * buf)51*0957b409SSimon J. Gerraty xfree(void *buf)
52*0957b409SSimon J. Gerraty {
53*0957b409SSimon J. Gerraty if (buf != NULL) {
54*0957b409SSimon J. Gerraty free(buf);
55*0957b409SSimon J. Gerraty }
56*0957b409SSimon J. Gerraty }
57*0957b409SSimon J. Gerraty
58*0957b409SSimon J. Gerraty /* see brssl.h */
59*0957b409SSimon J. Gerraty void *
xblobdup(const void * src,size_t len)60*0957b409SSimon J. Gerraty xblobdup(const void *src, size_t len)
61*0957b409SSimon J. Gerraty {
62*0957b409SSimon J. Gerraty void *buf;
63*0957b409SSimon J. Gerraty
64*0957b409SSimon J. Gerraty buf = xmalloc(len);
65*0957b409SSimon J. Gerraty memcpy(buf, src, len);
66*0957b409SSimon J. Gerraty return buf;
67*0957b409SSimon J. Gerraty }
68*0957b409SSimon J. Gerraty
69*0957b409SSimon J. Gerraty /* see brssl.h */
70*0957b409SSimon J. Gerraty char *
xstrdup(const void * src)71*0957b409SSimon J. Gerraty xstrdup(const void *src)
72*0957b409SSimon J. Gerraty {
73*0957b409SSimon J. Gerraty return xblobdup(src, strlen(src) + 1);
74*0957b409SSimon J. Gerraty }
75*0957b409SSimon J. Gerraty
76*0957b409SSimon J. Gerraty /* see brssl.h */
77*0957b409SSimon J. Gerraty br_x509_pkey *
xpkeydup(const br_x509_pkey * pk)78*0957b409SSimon J. Gerraty xpkeydup(const br_x509_pkey *pk)
79*0957b409SSimon J. Gerraty {
80*0957b409SSimon J. Gerraty br_x509_pkey *pk2;
81*0957b409SSimon J. Gerraty
82*0957b409SSimon J. Gerraty pk2 = xmalloc(sizeof *pk2);
83*0957b409SSimon J. Gerraty pk2->key_type = pk->key_type;
84*0957b409SSimon J. Gerraty switch (pk->key_type) {
85*0957b409SSimon J. Gerraty case BR_KEYTYPE_RSA:
86*0957b409SSimon J. Gerraty pk2->key.rsa.n = xblobdup(pk->key.rsa.n, pk->key.rsa.nlen);
87*0957b409SSimon J. Gerraty pk2->key.rsa.nlen = pk->key.rsa.nlen;
88*0957b409SSimon J. Gerraty pk2->key.rsa.e = xblobdup(pk->key.rsa.e, pk->key.rsa.elen);
89*0957b409SSimon J. Gerraty pk2->key.rsa.elen = pk->key.rsa.elen;
90*0957b409SSimon J. Gerraty break;
91*0957b409SSimon J. Gerraty case BR_KEYTYPE_EC:
92*0957b409SSimon J. Gerraty pk2->key.ec.curve = pk->key.ec.curve;
93*0957b409SSimon J. Gerraty pk2->key.ec.q = xblobdup(pk->key.ec.q, pk->key.ec.qlen);
94*0957b409SSimon J. Gerraty pk2->key.ec.qlen = pk->key.ec.qlen;
95*0957b409SSimon J. Gerraty break;
96*0957b409SSimon J. Gerraty default:
97*0957b409SSimon J. Gerraty #ifndef _STANDALONE
98*0957b409SSimon J. Gerraty fprintf(stderr, "Unknown public key type: %u\n",
99*0957b409SSimon J. Gerraty (unsigned)pk->key_type);
100*0957b409SSimon J. Gerraty exit(EXIT_FAILURE);
101*0957b409SSimon J. Gerraty #else
102*0957b409SSimon J. Gerraty ;
103*0957b409SSimon J. Gerraty #endif
104*0957b409SSimon J. Gerraty }
105*0957b409SSimon J. Gerraty return pk2;
106*0957b409SSimon J. Gerraty }
107*0957b409SSimon J. Gerraty
108*0957b409SSimon J. Gerraty /* see brssl.h */
109*0957b409SSimon J. Gerraty void
xfreepkey(br_x509_pkey * pk)110*0957b409SSimon J. Gerraty xfreepkey(br_x509_pkey *pk)
111*0957b409SSimon J. Gerraty {
112*0957b409SSimon J. Gerraty if (pk != NULL) {
113*0957b409SSimon J. Gerraty switch (pk->key_type) {
114*0957b409SSimon J. Gerraty case BR_KEYTYPE_RSA:
115*0957b409SSimon J. Gerraty xfree(pk->key.rsa.n);
116*0957b409SSimon J. Gerraty xfree(pk->key.rsa.e);
117*0957b409SSimon J. Gerraty break;
118*0957b409SSimon J. Gerraty case BR_KEYTYPE_EC:
119*0957b409SSimon J. Gerraty xfree(pk->key.ec.q);
120*0957b409SSimon J. Gerraty break;
121*0957b409SSimon J. Gerraty default:
122*0957b409SSimon J. Gerraty #ifndef _STANDALONE
123*0957b409SSimon J. Gerraty fprintf(stderr, "Unknown public key type: %u\n",
124*0957b409SSimon J. Gerraty (unsigned)pk->key_type);
125*0957b409SSimon J. Gerraty exit(EXIT_FAILURE);
126*0957b409SSimon J. Gerraty #else
127*0957b409SSimon J. Gerraty ;
128*0957b409SSimon J. Gerraty #endif
129*0957b409SSimon J. Gerraty }
130*0957b409SSimon J. Gerraty xfree(pk);
131*0957b409SSimon J. Gerraty }
132*0957b409SSimon J. Gerraty }
133