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