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 /* we don't actively use these if given, but may come from COSE */ 63 64 LWS_GENCRYPTO_RSA_KEYEL_OTHER, 65 LWS_GENCRYPTO_RSA_KEYEL_RI, 66 LWS_GENCRYPTO_RSA_KEYEL_DI, 67 LWS_GENCRYPTO_RSA_KEYEL_TI, 68 69 LWS_GENCRYPTO_RSA_KEYEL_COUNT 70 }; 71 72 enum lws_gencrypto_ec_tok { 73 LWS_GENCRYPTO_EC_KEYEL_CRV, 74 LWS_GENCRYPTO_EC_KEYEL_X, 75 /* note... same offset as RSA D */ 76 LWS_GENCRYPTO_EC_KEYEL_D = LWS_GENCRYPTO_RSA_KEYEL_D, 77 LWS_GENCRYPTO_EC_KEYEL_Y, 78 79 LWS_GENCRYPTO_EC_KEYEL_COUNT 80 }; 81 82 enum lws_gencrypto_aes_tok { 83 /* note... same offset as OCT K */ 84 LWS_GENCRYPTO_AES_KEYEL_K = LWS_GENCRYPTO_OCT_KEYEL_K, 85 86 LWS_GENCRYPTO_AES_KEYEL_COUNT 87 }; 88 89 /* largest number of key elements for any algorithm */ 90 #define LWS_GENCRYPTO_MAX_KEYEL_COUNT LWS_GENCRYPTO_RSA_KEYEL_COUNT 91 92 /* this "stretchy" type holds individual key element data in binary form. 93 * It's typcially used in an array with the layout mapping the element index to 94 * the key element meaning defined by the enums above. An array of these of 95 * length LWS_GENCRYPTO_MAX_KEYEL_COUNT can define key elements for any key 96 * type. 97 */ 98 99 typedef struct lws_gencrypto_keyelem { 100 uint8_t *buf; 101 uint32_t len; 102 } lws_gc_elem_t; 103 104 105 /** 106 * lws_gencrypto_bits_to_bytes() - returns rounded up bytes needed for bits 107 * 108 * \param bits 109 * 110 * Returns the number of bytes needed to store the given number of bits. If 111 * a byte is partially used, the byte count is rounded up. 112 */ 113 LWS_VISIBLE LWS_EXTERN int 114 lws_gencrypto_bits_to_bytes(int bits); 115 116 /** 117 * lws_base64_size() - returns estimated size of base64 encoding 118 * 119 * \param bytes 120 * 121 * Returns a slightly oversize estimate of the size of a base64 encoded version 122 * of the given amount of unencoded data. 123 */ 124 LWS_VISIBLE LWS_EXTERN int 125 lws_base64_size(int bytes); 126 127 /** 128 * lws_gencrypto_padded_length() - returns PKCS#5/#7 padded length 129 * 130 * @param blocksize - blocksize to pad to 131 * @param len - Length of input to pad 132 * 133 * Returns the length of a buffer originally of size len after PKCS#5 or PKCS#7 134 * padding has been applied to it. 135 */ 136 LWS_VISIBLE LWS_EXTERN size_t 137 lws_gencrypto_padded_length(size_t block_size, size_t len); 138