1 /* 2 * libwebsockets - small server side websockets and web server implementation 3 * 4 * Copyright (C) 2010 - 2021 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 #define VERBOSE 26 27 #define MAX_BLOBBED_PARAMS 96 /* largest bstr-encoded params */ 28 29 enum { 30 ST_UNKNOWN, 31 32 ST_OUTER_PROTECTED, 33 ST_OUTER_UNPROTECTED, 34 ST_OUTER_PAYLOAD, 35 ST_OUTER_SIGN1_SIGNATURE, 36 37 ST_OUTER_SIGN_SIGARRAY, 38 39 ST_OUTER_MACTAG, 40 41 ST_INNER_PROTECTED, 42 ST_INNER_UNPROTECTED, 43 ST_INNER_SIGNATURE, 44 45 ST_INNER_EXCESS, 46 }; 47 48 typedef struct lws_cose_sig_alg { 49 lws_dll2_t list; 50 uint8_t rhash[512]; 51 const lws_cose_key_t *cose_key; 52 struct lws_genhash_ctx hash_ctx; 53 union { 54 struct lws_genec_ctx ecdsactx; 55 struct lws_genrsa_ctx rsactx; 56 struct lws_genhmac_ctx hmacctx; 57 } u; 58 cose_param_t cose_alg; 59 int keybits; 60 int rhash_len; 61 62 char failed; 63 char completed; 64 } lws_cose_sig_alg_t; 65 66 typedef struct lws_cose_validate_param_stack { 67 uint8_t ph[4][MAX_BLOBBED_PARAMS]; 68 int ph_pos[4]; 69 struct lws_gencrypto_keyelem kid; 70 cose_param_t alg; 71 } lws_cose_validate_param_stack_t; 72 73 struct lws_cose_validate_context { 74 lws_cose_validate_create_info_t info; 75 uint8_t mac[LWS_GENHASH_LARGEST]; 76 uint8_t sig_agg[512]; 77 lws_cose_validate_param_stack_t st[3]; 78 lws_dll2_owner_t algs; 79 lws_dll2_owner_t results; 80 uint8_t *payload_stash; 81 struct lwsac *ac; 82 struct lecp_ctx ctx; 83 void *user; 84 85 size_t payload_pos; 86 size_t payload_stash_size; 87 88 int seen; 89 int depth; 90 91 int outer; 92 size_t mac_pos; 93 size_t sig_agg_pos; 94 95 cose_param_t map_key; /* parsing temp before val */ 96 97 int tli; /* toplevel item */ 98 int sp; 99 100 uint8_t sub; 101 }; 102 103 struct lws_cose_sign_context { 104 lws_cose_sign_create_info_t info; 105 106 lws_dll2_owner_t algs; 107 lws_cose_sig_alg_t *alg; 108 109 size_t rem_pay; 110 enum lws_cose_sig_types type; /* computed */ 111 int flags; 112 113 size_t along; 114 115 int tli; 116 117 char subsequent; 118 }; 119 120 extern const uint8_t *sig_mctx[]; 121 extern uint8_t sig_mctx_len[]; 122 extern const char *cose_sections[]; 123 124 lws_cose_sig_alg_t * 125 lws_cose_val_alg_create(struct lws_context *cx, lws_cose_key_t *ck, 126 cose_param_t cose_alg, int op); 127 128 int 129 lws_cose_val_alg_hash(lws_cose_sig_alg_t *alg, const uint8_t *in, size_t in_len); 130 131 void 132 lws_cose_val_alg_destroy(struct lws_cose_validate_context *cps, 133 lws_cose_sig_alg_t **_alg, const uint8_t *against, 134 size_t against_len); 135 136 lws_cose_sig_alg_t * 137 lws_cose_sign_alg_create(struct lws_context *cx, const lws_cose_key_t *ck, 138 cose_param_t cose_alg, int op); 139 140 int 141 lws_cose_sign_alg_hash(lws_cose_sig_alg_t *alg, const uint8_t *in, size_t in_len); 142 143 void 144 lws_cose_sign_alg_complete(lws_cose_sig_alg_t *alg); 145 146 void 147 lws_cose_sign_alg_destroy(lws_cose_sig_alg_t **_alg); 148 149