1  /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  *  lws-gencrypto openssl-specific common code
25  */
26 
27 #include "private-lib-core.h"
28 #include "private-lib-tls-openssl.h"
29 
30 /*
31  * Care: many openssl apis return 1 for success.  These are translated to the
32  * lws convention of 0 for success.
33  */
34 
35 int
lws_gencrypto_openssl_hash_to_NID(enum lws_genhash_types hash_type)36 lws_gencrypto_openssl_hash_to_NID(enum lws_genhash_types hash_type)
37 {
38 	int h = -1;
39 
40 	switch (hash_type) {
41 	case LWS_GENHASH_TYPE_UNKNOWN:
42 		break;
43 	case LWS_GENHASH_TYPE_MD5:
44 		h = NID_md5;
45 		break;
46 	case LWS_GENHASH_TYPE_SHA1:
47 		h = NID_sha1;
48 		break;
49 	case LWS_GENHASH_TYPE_SHA256:
50 		h = NID_sha256;
51 		break;
52 	case LWS_GENHASH_TYPE_SHA384:
53 		h = NID_sha384;
54 		break;
55 	case LWS_GENHASH_TYPE_SHA512:
56 		h = NID_sha512;
57 		break;
58 	}
59 
60 	return h;
61 }
62 
63 const EVP_MD *
lws_gencrypto_openssl_hash_to_EVP_MD(enum lws_genhash_types hash_type)64 lws_gencrypto_openssl_hash_to_EVP_MD(enum lws_genhash_types hash_type)
65 {
66 	const EVP_MD *h = NULL;
67 
68 	switch (hash_type) {
69 	case LWS_GENHASH_TYPE_UNKNOWN:
70 		break;
71 	case LWS_GENHASH_TYPE_MD5:
72 		h = EVP_md5();
73 		break;
74 	case LWS_GENHASH_TYPE_SHA1:
75 		h = EVP_sha1();
76 		break;
77 	case LWS_GENHASH_TYPE_SHA256:
78 		h = EVP_sha256();
79 		break;
80 	case LWS_GENHASH_TYPE_SHA384:
81 		h = EVP_sha384();
82 		break;
83 	case LWS_GENHASH_TYPE_SHA512:
84 		h = EVP_sha512();
85 		break;
86 	}
87 
88 	return h;
89 }
90