• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2020 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 
25 /*
26  * These are gencrypto-level constants... they are used by both JOSE and direct
27  * gencrypto code.  However while JWK relies on these, using gencrypto apis has
28  * no dependency at all on any JOSE type.
29  */
30 
31 enum lws_gencrypto_kty {
32 	LWS_GENCRYPTO_KTY_UNKNOWN,
33 
34 	LWS_GENCRYPTO_KTY_OCT,
35 	LWS_GENCRYPTO_KTY_RSA,
36 	LWS_GENCRYPTO_KTY_EC
37 };
38 
39 /*
40  * Keytypes where the same element name is reused must all agree to put the
41  * same-named element at the same e[] index.  It's because when used with jwk,
42  * we parse and store in incoming key data, but we may not be informed of the
43  * definitive keytype until the end.
44  */
45 
46 enum lws_gencrypto_oct_tok {
47 	LWS_GENCRYPTO_OCT_KEYEL_K, /* note... same offset as AES K */
48 
49 	LWS_GENCRYPTO_OCT_KEYEL_COUNT
50 };
51 
52 enum lws_gencrypto_rsa_tok {
53 	LWS_GENCRYPTO_RSA_KEYEL_E,
54 	LWS_GENCRYPTO_RSA_KEYEL_N,
55 	LWS_GENCRYPTO_RSA_KEYEL_D, /* note... same offset as EC D */
56 	LWS_GENCRYPTO_RSA_KEYEL_P,
57 	LWS_GENCRYPTO_RSA_KEYEL_Q,
58 	LWS_GENCRYPTO_RSA_KEYEL_DP,
59 	LWS_GENCRYPTO_RSA_KEYEL_DQ,
60 	LWS_GENCRYPTO_RSA_KEYEL_QI,
61 
62 	LWS_GENCRYPTO_RSA_KEYEL_COUNT
63 };
64 
65 enum lws_gencrypto_ec_tok {
66 	LWS_GENCRYPTO_EC_KEYEL_CRV,
67 	LWS_GENCRYPTO_EC_KEYEL_X,
68 	/* note... same offset as RSA D */
69 	LWS_GENCRYPTO_EC_KEYEL_D = LWS_GENCRYPTO_RSA_KEYEL_D,
70 	LWS_GENCRYPTO_EC_KEYEL_Y,
71 
72 	LWS_GENCRYPTO_EC_KEYEL_COUNT
73 };
74 
75 enum lws_gencrypto_aes_tok {
76 	/* note... same offset as OCT K */
77 	LWS_GENCRYPTO_AES_KEYEL_K = LWS_GENCRYPTO_OCT_KEYEL_K,
78 
79 	LWS_GENCRYPTO_AES_KEYEL_COUNT
80 };
81 
82 /* largest number of key elements for any algorithm */
83 #define LWS_GENCRYPTO_MAX_KEYEL_COUNT LWS_GENCRYPTO_RSA_KEYEL_COUNT
84 
85 /* this "stretchy" type holds individual key element data in binary form.
86  * It's typcially used in an array with the layout mapping the element index to
87  * the key element meaning defined by the enums above.  An array of these of
88  * length LWS_GENCRYPTO_MAX_KEYEL_COUNT can define key elements for any key
89  * type.
90  */
91 
92 struct lws_gencrypto_keyelem {
93 	uint8_t *buf;
94 	uint32_t len;
95 };
96 
97 
98 /**
99  * lws_gencrypto_bits_to_bytes() - returns rounded up bytes needed for bits
100  *
101  * \param bits
102  *
103  * Returns the number of bytes needed to store the given number of bits.  If
104  * a byte is partially used, the byte count is rounded up.
105  */
106 LWS_VISIBLE LWS_EXTERN int
107 lws_gencrypto_bits_to_bytes(int bits);
108 
109 /**
110  * lws_base64_size() - returns estimated size of base64 encoding
111  *
112  * \param bytes
113  *
114  * Returns a slightly oversize estimate of the size of a base64 encoded version
115  * of the given amount of unencoded data.
116  */
117 LWS_VISIBLE LWS_EXTERN int
118 lws_base64_size(int bytes);
119 
120 /**
121  * lws_gencrypto_padded_length() - returns PKCS#5/#7 padded length
122  *
123  * @param blocksize - blocksize to pad to
124  * @param len - Length of input to pad
125  *
126  * Returns the length of a buffer originally of size len after PKCS#5 or PKCS#7
127  * padding has been applied to it.
128  */
129 LWS_VISIBLE LWS_EXTERN size_t
130 lws_gencrypto_padded_length(size_t block_size, size_t len);
131