{"schema":"libjg2-1",
"vpath":"/git/",
"avatar":"/git/avatar/",
"alang":"",
"gen_ut":1745023787,
"reponame":"libwebsockets",
"desc":"libwebsockets lightweight C networking library",
"owner": { "name": "Andy Green", "email": "andy@warmcat.com", "md5": "c50933ca2aa61e0fe2c43d46bb6b59cb" },"url":"https://libwebsockets.org/repo/libwebsockets",
"f":3,
"items": [
{"schema":"libjg2-1",
"cid":"3bba2ebfbc8eccf00dbf73fc8e91d866",
"oid":{ "oid": "de4f989c1e76bcaeceda0ed2b6fd24d0b0aff1b8", "alias": [ "refs/heads/main"]},"blobname": "lib/jose/jwe/jwe-rsa-aesgcm.c", "blob": "/*\n * libwebsockets - small server side websockets and web server implementation\n *\n * Copyright (C) 2010 - 2020 Andy Green \u003candy@warmcat.com\u003e\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \u0022Software\u0022), to\n * deal in the Software without restriction, including without limitation the\n * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or\n * sell copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \u0022AS IS\u0022, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS\n * IN THE SOFTWARE.\n */\n\n#include \u0022private-lib-core.h\u0022\n#include \u0022private-lib-jose-jwe.h\u0022\n\n#define LWS_AESGCM_IV 12\n\n\nint\nlws_jwe_encrypt_rsa_aes_gcm(struct lws_jwe *jwe, char *temp, int *temp_len)\n{\n\tint ekbytes \u003d jwe-\u003ejose.enc_alg-\u003ekeybits_fixed / 8;\n\tstruct lws_genrsa_ctx rsactx;\n\tint n, ret \u003d -1, ot \u003d *temp_len;\n\n\tif (jwe-\u003ejws.jwk-\u003ekty !\u003d LWS_GENCRYPTO_KTY_RSA) {\n\t\tlwsl_err(\u0022%s: wrong kty %d\u005cn\u0022, __func__, jwe-\u003ejws.jwk-\u003ekty);\n\n\t\treturn -1;\n\t}\n\n\t/* create the IV + CEK */\n\n\tif (lws_jws_randomize_element(jwe-\u003ejws.context, \u0026jwe-\u003ejws.map, LJWE_IV,\n\t\t\t\t temp, temp_len,\n\t\t\t\t LWS_AESGCM_IV, 0))\n\t\treturn -1;\n\n\tif (lws_jws_alloc_element(\u0026jwe-\u003ejws.map, LJWE_ATAG,\n\t\t\t\t temp + (ot - *temp_len),\n\t\t\t\t temp_len, LWS_AESGCM_TAG, 0))\n\t\treturn -1;\n\n\t/* create a b64 version of the JOSE header, needed as aad */\n\n\tif (lws_jws_encode_b64_element(\u0026jwe-\u003ejws.map_b64, LJWE_JOSE,\n\t\t\t\t temp + (ot - *temp_len), temp_len,\n\t\t\t\t jwe-\u003ejws.map.buf[LJWE_JOSE],\n\t\t\t\t jwe-\u003ejws.map.len[LJWE_JOSE]))\n\t\treturn -1;\n\n\t/*\n\t * If none already, create a new, random CEK in the JWE (so it can be\n\t * reused for other recipients on same payload). If it already exists,\n\t * just reuse it. It will be cleansed in the JWE destroy.\n\t */\n\tif (!jwe-\u003ecek_valid) {\n\t\tif (lws_get_random(jwe-\u003ejws.context, jwe-\u003ecek, (unsigned int)ekbytes) !\u003d\n\t\t\t\t\t\t\t (size_t)ekbytes) {\n\t\t\tlwsl_err(\u0022%s: Problem getting random\u005cn\u0022, __func__);\n\t\t\treturn -1;\n\t\t}\n\t\tjwe-\u003ecek_valid \u003d 1;\n\t}\n\n\tif (lws_jws_dup_element(\u0026jwe-\u003ejws.map, LJWE_EKEY,\n\t\t\t temp + (ot - *temp_len), temp_len,\n\t\t\t jwe-\u003ecek, (unsigned int)ekbytes, 0))\n\t\treturn -1;\n\n\t/* encrypt the payload */\n\n\tn \u003d lws_jwe_encrypt_gcm(jwe, (uint8_t *)jwe-\u003ejws.map.buf[LJWE_EKEY],\n\t\t\t\t(uint8_t *)jwe-\u003ejws.map_b64.buf[LJWE_JOSE],\n\t\t\t\t(int)jwe-\u003ejws.map_b64.len[LJWE_JOSE]);\n\tif (n \u003c 0) {\n\t\tlwsl_err(\u0022%s: lws_jwe_encrypt_gcm failed\u005cn\u0022,\n\t\t\t __func__);\n\t\tgoto bail;\n\t}\n\n\t/* Encrypt the CEK into EKEY to make the JWE Encrypted Key */\n\n\tif (lws_genrsa_create(\u0026rsactx, jwe-\u003ejws.jwk-\u003ee, jwe-\u003ejws.context,\n\t\t\t!strcmp(jwe-\u003ejose.alg-\u003ealg, \u0022RSA-OAEP\u0022) ?\n\t\t\t\tLGRSAM_PKCS1_OAEP_PSS : LGRSAM_PKCS1_1_5,\n\t\t\tLWS_GENHASH_TYPE_SHA1 /* !!! */)) {\n\t\tlwsl_notice(\u0022%s: lws_genrsa_public_decrypt_create\u005cn\u0022,\n\t\t\t __func__);\n\t\tgoto bail;\n\t}\n\n\tn \u003d lws_genrsa_public_encrypt(\u0026rsactx, jwe-\u003ecek, (unsigned int)ekbytes,\n\t\t\t\t (uint8_t *)jwe-\u003ejws.map.buf[LJWE_EKEY]);\n\tlws_genrsa_destroy(\u0026rsactx);\n\tif (n \u003c 0) {\n\t\tlwsl_err(\u0022%s: encrypt cek fail: \u005cn\u0022, __func__);\n\t\tgoto bail;\n\t}\n\n\t/* set the EKEY length to the actual enciphered length */\n\tjwe-\u003ejws.map.len[LJWE_EKEY] \u003d (unsigned int)n;\n\n\tret \u003d (int32_t)jwe-\u003ejws.map.len[LJWE_CTXT];\n\nbail:\n\n\treturn ret;\n}\n\nint\nlws_jwe_auth_and_decrypt_rsa_aes_gcm(struct lws_jwe *jwe)\n{\n\tint n;\n\tstruct lws_genrsa_ctx rsactx;\n\tuint8_t enc_cek[LWS_JWE_LIMIT_KEY_ELEMENT_BYTES];\n\n\tif (jwe-\u003ejws.jwk-\u003ekty !\u003d LWS_GENCRYPTO_KTY_RSA) {\n\t\tlwsl_err(\u0022%s: unexpected kty %d\u005cn\u0022, __func__, jwe-\u003ejws.jwk-\u003ekty);\n\n\t\treturn -1;\n\t}\n\n\tif (jwe-\u003ejws.map.len[LJWE_EKEY] \u003c 32) {\n\t\tlwsl_err(\u0022%s: EKEY length too short %d\u005cn\u0022, __func__,\n\t\t\t\t(int)jwe-\u003ejws.map.len[LJWE_EKEY]);\n\n\t\treturn -1;\n\t}\n\n\t/* Decrypt the JWE Encrypted Key to get the direct CEK */\n\n\tif (lws_genrsa_create(\u0026rsactx, jwe-\u003ejws.jwk-\u003ee, jwe-\u003ejws.context,\n\t\t\t!strcmp(jwe-\u003ejose.alg-\u003ealg, \u0022RSA-OAEP\u0022) ?\n\t\t\t\tLGRSAM_PKCS1_OAEP_PSS : LGRSAM_PKCS1_1_5,\n\t\t\tLWS_GENHASH_TYPE_SHA1 /* !!! */)) {\n\t\tlwsl_notice(\u0022%s: lws_genrsa_public_decrypt_create\u005cn\u0022,\n\t\t\t __func__);\n\t\treturn -1;\n\t}\n\n\tn \u003d lws_genrsa_private_decrypt(\u0026rsactx,\n\t\t\t\t (uint8_t *)jwe-\u003ejws.map.buf[LJWE_EKEY],\n\t\t\t\t jwe-\u003ejws.map.len[LJWE_EKEY], enc_cek,\n\t\t\t\t sizeof(enc_cek));\n\tlws_genrsa_destroy(\u0026rsactx);\n\tif (n \u003c 0) {\n\t\tlwsl_err(\u0022%s: decrypt cek fail: \u005cn\u0022, __func__);\n\t\treturn -1;\n\t}\n\n\tn \u003d lws_jwe_auth_and_decrypt_gcm(jwe, enc_cek,\n\t\t\t(uint8_t *)jwe-\u003ejws.map_b64.buf[LJWE_JOSE],\n\t\t\t\t(int)jwe-\u003ejws.map_b64.len[LJWE_JOSE]);\n\tif (n \u003c 0) {\n\t\tlwsl_err(\u0022%s: lws_jwe_auth_and_decrypt_gcm_hs failed\u005cn\u0022,\n\t\t\t __func__);\n\t\treturn -1;\n\t}\n\n#if defined(LWS_WITH_MBEDTLS) \u0026\u0026 defined(LWS_PLAT_OPTEE)\n\t/* strip padding */\n\n\tn \u003d jwe-\u003ejws.map.buf[LJWE_CTXT][jwe-\u003ejws.map.len[LJWE_CTXT] - 1];\n\tif (n \u003e 16)\n\t\treturn -1;\n\tjwe-\u003ejws.map.len[LJWE_CTXT] -\u003d n;\n#endif\n\n\treturn (int)jwe-\u003ejws.map.len[LJWE_CTXT];\n}\n","s":{"c":1745023787,"u": 511}}
],"g": 3945,"chitpc": 0,"ehitpc": 0,"indexed":0
,
"ab": 1, "si": 0, "db":0, "di":0, "sat":0, "lfc": "0000"}