Change the DRBG HASH implementation to lookup all allowed algorithm names
[openssl.git] / crypto / o_str.c
1 /*
2  * Copyright 2003-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "e_os.h"
11 #include <limits.h>
12 #include <openssl/crypto.h>
13 #include "internal/cryptlib.h"
14
15 char *CRYPTO_strdup(const char *str, const char* file, int line)
16 {
17     char *ret;
18
19     if (str == NULL)
20         return NULL;
21     ret = CRYPTO_malloc(strlen(str) + 1, file, line);
22     if (ret != NULL)
23         strcpy(ret, str);
24     return ret;
25 }
26
27 char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
28 {
29     size_t maxlen;
30     char *ret;
31
32     if (str == NULL)
33         return NULL;
34
35     maxlen = OPENSSL_strnlen(str, s);
36
37     ret = CRYPTO_malloc(maxlen + 1, file, line);
38     if (ret) {
39         memcpy(ret, str, maxlen);
40         ret[maxlen] = '\0';
41     }
42     return ret;
43 }
44
45 void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
46 {
47     void *ret;
48
49     if (data == NULL || siz >= INT_MAX)
50         return NULL;
51
52     ret = CRYPTO_malloc(siz, file, line);
53     if (ret == NULL) {
54         CRYPTOerr(CRYPTO_F_CRYPTO_MEMDUP, ERR_R_MALLOC_FAILURE);
55         return NULL;
56     }
57     return memcpy(ret, data, siz);
58 }
59
60 size_t OPENSSL_strnlen(const char *str, size_t maxlen)
61 {
62     const char *p;
63
64     for (p = str; maxlen-- != 0 && *p != '\0'; ++p) ;
65
66     return p - str;
67 }
68
69 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
70 {
71     size_t l = 0;
72     for (; size > 1 && *src; size--) {
73         *dst++ = *src++;
74         l++;
75     }
76     if (size)
77         *dst = '\0';
78     return l + strlen(src);
79 }
80
81 size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
82 {
83     size_t l = 0;
84     for (; size > 0 && *dst; size--, dst++)
85         l++;
86     return l + OPENSSL_strlcpy(dst, src, size);
87 }
88
89 int OPENSSL_hexchar2int(unsigned char c)
90 {
91 #ifdef CHARSET_EBCDIC
92     c = os_toebcdic[c];
93 #endif
94
95     switch (c) {
96     case '0':
97         return 0;
98     case '1':
99         return 1;
100     case '2':
101         return 2;
102     case '3':
103         return 3;
104     case '4':
105           return 4;
106     case '5':
107           return 5;
108     case '6':
109           return 6;
110     case '7':
111           return 7;
112     case '8':
113           return 8;
114     case '9':
115           return 9;
116     case 'a': case 'A':
117           return 0x0A;
118     case 'b': case 'B':
119           return 0x0B;
120     case 'c': case 'C':
121           return 0x0C;
122     case 'd': case 'D':
123           return 0x0D;
124     case 'e': case 'E':
125           return 0x0E;
126     case 'f': case 'F':
127           return 0x0F;
128     }
129     return -1;
130 }
131
132 /*
133  * Give a string of hex digits convert to a buffer
134  */
135 unsigned char *OPENSSL_hexstr2buf(const char *str, long *len)
136 {
137     unsigned char *hexbuf, *q;
138     unsigned char ch, cl;
139     int chi, cli;
140     const unsigned char *p;
141     size_t s;
142
143     s = strlen(str);
144     if ((hexbuf = OPENSSL_malloc(s >> 1)) == NULL) {
145         CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, ERR_R_MALLOC_FAILURE);
146         return NULL;
147     }
148     for (p = (const unsigned char *)str, q = hexbuf; *p; ) {
149         ch = *p++;
150         if (ch == ':')
151             continue;
152         cl = *p++;
153         if (!cl) {
154             CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF,
155                       CRYPTO_R_ODD_NUMBER_OF_DIGITS);
156             OPENSSL_free(hexbuf);
157             return NULL;
158         }
159         cli = OPENSSL_hexchar2int(cl);
160         chi = OPENSSL_hexchar2int(ch);
161         if (cli < 0 || chi < 0) {
162             OPENSSL_free(hexbuf);
163             CRYPTOerr(CRYPTO_F_OPENSSL_HEXSTR2BUF, CRYPTO_R_ILLEGAL_HEX_DIGIT);
164             return NULL;
165         }
166         *q++ = (unsigned char)((chi << 4) | cli);
167     }
168
169     if (len)
170         *len = q - hexbuf;
171     return hexbuf;
172 }
173
174 /*
175  * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
176  * hex representation @@@ (Contents of buffer are always kept in ASCII, also
177  * on EBCDIC machines)
178  */
179 char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
180 {
181     static const char hexdig[] = "0123456789ABCDEF";
182     char *tmp, *q;
183     const unsigned char *p;
184     int i;
185
186     if (len == 0)
187     {
188         return OPENSSL_zalloc(1);
189     }
190
191     if ((tmp = OPENSSL_malloc(len * 3)) == NULL) {
192         CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
193         return NULL;
194     }
195     q = tmp;
196     for (i = 0, p = buffer; i < len; i++, p++) {
197         *q++ = hexdig[(*p >> 4) & 0xf];
198         *q++ = hexdig[*p & 0xf];
199         *q++ = ':';
200     }
201     q[-1] = 0;
202 #ifdef CHARSET_EBCDIC
203     ebcdic2ascii(tmp, tmp, q - tmp - 1);
204 #endif
205
206     return tmp;
207 }
208
209 int openssl_strerror_r(int errnum, char *buf, size_t buflen)
210 {
211 #if defined(_MSC_VER) && _MSC_VER>=1400
212     return !strerror_s(buf, buflen, errnum);
213 #elif defined(_GNU_SOURCE)
214     char *err;
215
216     /*
217      * GNU strerror_r may not actually set buf.
218      * It can return a pointer to some (immutable) static string in which case
219      * buf is left unused.
220      */
221     err = strerror_r(errnum, buf, buflen);
222     if (err == NULL || buflen == 0)
223         return 0;
224     /*
225      * If err is statically allocated, err != buf and we need to copy the data.
226      * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
227      * since src and dest are not annotated with __restrict and the function
228      * reads src byte for byte and writes to dest.
229      * If err == buf we do not have to copy anything.
230      */
231     if (err != buf)
232         OPENSSL_strlcpy(buf, err, buflen);
233     return 1;
234 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
235       (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
236     /*
237      * We can use "real" strerror_r. The OpenSSL version differs in that it
238      * gives 1 on success and 0 on failure for consistency with other OpenSSL
239      * functions. Real strerror_r does it the other way around
240      */
241     return !strerror_r(errnum, buf, buflen);
242 #else
243     char *err;
244
245     /* Fall back to non-thread safe strerror()...its all we can do */
246     if (buflen < 2)
247         return 0;
248     err = strerror(errnum);
249     /* Can this ever happen? */
250     if (err == NULL)
251         return 0;
252     OPENSSL_strlcpy(buf, err, buflen);
253     return 1;
254 #endif
255 }