Project homepage Mailing List  Warmcat.com  API Docs  Github Mirror 
{"schema":"libjg2-1", "vpath":"/git/", "avatar":"/git/avatar/", "alang":"en-US,en;q\u003d0.5", "gen_ut":1580278115, "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":"bbc37fa0331d8bfdf4e26471745d2731", "oid":{ "oid": "00d206d2b27969cf83df45059058e34d37a47b49", "alias": [ "refs/heads/master"]},"blobname": "include/libwebsockets/lws-jws.h", "blob": "/*\n * libwebsockets - small server side websockets and web server implementation\n *\n * Copyright (C) 2010 - 2019 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/*! \u005cdefgroup jws JSON Web Signature\n * ## JSON Web Signature API\n *\n * Lws provides an API to check and create RFC7515 JSON Web Signatures\n *\n * SHA256/384/512 HMAC, and RSA 256/384/512 are supported.\n *\n * The API uses your TLS library crypto, but works exactly the same no matter\n * what your TLS backend is.\n */\n///@{\n\n/*\n * The maps are built to work with both JWS (LJWS_) and JWE (LJWE_), and are\n * sized to the slightly larger JWE case.\n */\n\nenum enum_jws_sig_elements {\n\n\t/* JWS block namespace */\n\tLJWS_JOSE,\n\tLJWS_PYLD,\n\tLJWS_SIG,\n\tLJWS_UHDR,\n\n\t/* JWE block namespace */\n\tLJWE_JOSE \u003d 0,\n\tLJWE_EKEY,\n\tLJWE_IV,\n\tLJWE_CTXT,\n\tLJWE_ATAG,\n\tLJWE_AAD,\n\n\tLWS_JWS_MAX_COMPACT_BLOCKS\n};\n\nstruct lws_jws_map {\n\tconst char *buf[LWS_JWS_MAX_COMPACT_BLOCKS];\n\tuint32_t len[LWS_JWS_MAX_COMPACT_BLOCKS];\n};\n\n#define LWS_JWS_MAX_SIGS 3\n\nstruct lws_jws {\n\tstruct lws_jwk *jwk; /* the struct lws_jwk containing the signing key */\n\tstruct lws_context *context; /* the lws context (used to get random) */\n\tstruct lws_jws_map map, map_b64;\n};\n\n/* jws EC signatures do not have ASN.1 in them, meaning they're incompatible\n * with generic signatures.\n */\n\n/**\n * lws_jws_init() - initialize a jws for use\n *\n * \u005cparam jws: pointer to the jws to initialize\n * \u005cparam jwk: the jwk to use with this jws\n * \u005cparam context: the lws_context to use\n */\nLWS_VISIBLE LWS_EXTERN void\nlws_jws_init(struct lws_jws *jws, struct lws_jwk *jwk,\n\t struct lws_context *context);\n\n/**\n * lws_jws_destroy() - scrub a jws\n *\n * \u005cparam jws: pointer to the jws to destroy\n *\n * Call before the jws goes out of scope.\n *\n * Elements defined in the jws are zeroed.\n */\nLWS_VISIBLE LWS_EXTERN void\nlws_jws_destroy(struct lws_jws *jws);\n\n/**\n * lws_jws_sig_confirm_compact() - check signature\n *\n * \u005cparam map: pointers and lengths for each of the unencoded JWS elements\n * \u005cparam jwk: public key\n * \u005cparam context: lws_context\n * \u005cparam temp: scratchpad\n * \u005cparam temp_len: length of scratchpad\n *\n * Confirms the signature on a JWS. Use if you have non-b64 plain JWS elements\n * in a map... it'll make a temp b64 version needed for comparison. See below\n * for other variants.\n *\n * Returns 0 on match.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_sig_confirm_compact(struct lws_jws_map *map, struct lws_jwk *jwk,\n\t\t\t struct lws_context *context,\n\t\t\t char *temp, int *temp_len);\n\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_sig_confirm_compact_b64_map(struct lws_jws_map *map_b64,\n\t\t\t\t struct lws_jwk *jwk,\n\t\t\t struct lws_context *context,\n\t\t\t char *temp, int *temp_len);\n\n/**\n * lws_jws_sig_confirm_compact_b64() - check signature on b64 compact JWS\n *\n * \u005cparam in: pointer to b64 jose.payload[.hdr].sig\n * \u005cparam len: bytes available at \u005cp in\n * \u005cparam map: map to take decoded non-b64 content\n * \u005cparam jwk: public key\n * \u005cparam context: lws_context\n * \u005cparam temp: scratchpad\n * \u005cparam temp_len: size of scratchpad\n *\n * Confirms the signature on a JWS. Use if you have you have b64 compact layout\n * (jose.payload.hdr.sig) as an aggregated string... it'll make a temp plain\n * version needed for comparison.\n *\n * Returns 0 on match.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_sig_confirm_compact_b64(const char *in, size_t len,\n\t\t\t\tstruct lws_jws_map *map,\n\t\t\t\tstruct lws_jwk *jwk,\n\t\t\t\tstruct lws_context *context,\n\t\t\t\tchar *temp, int *temp_len);\n\n/**\n * lws_jws_sig_confirm() - check signature on plain + b64 JWS elements\n *\n * \u005cparam map_b64: pointers and lengths for each of the b64-encoded JWS elements\n * \u005cparam map: pointers and lengths for each of the unencoded JWS elements\n * \u005cparam jwk: public key\n * \u005cparam context: lws_context\n *\n * Confirms the signature on a JWS. Use if you have you already have both b64\n * compact layout (jose.payload.hdr.sig) and decoded JWS elements in maps.\n *\n * If you had the b64 string and called lws_jws_compact_decode() on it, you\n * will end up with both maps, and can use this api version, saving needlessly\n * regenerating any temp map.\n *\n * Returns 0 on match.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_sig_confirm(struct lws_jws_map *map_b64, /* b64-encoded */\n\t\t struct lws_jws_map *map,\t/* non-b64 */\n\t\t struct lws_jwk *jwk, struct lws_context *context);\n\n/**\n * lws_jws_sign_from_b64() - add b64 sig to b64 hdr + payload\n *\n * \u005cparam jose: jose header information\n * \u005cparam jws: information to include in the signature\n * \u005cparam b64_sig: output buffer for b64 signature\n * \u005cparam sig_len: size of \u005cp b64_sig output buffer\n *\n * This adds a b64-coded JWS signature of the b64-encoded protected header\n * and b64-encoded payload, at \u005cp b64_sig. The signature will be as large\n * as the N element of the RSA key when the RSA key is used, eg, 512 bytes for\n * a 4096-bit key, and then b64-encoding on top.\n *\n * In some special cases, there is only payload to sign and no header, in that\n * case \u005cp b64_hdr may be NULL, and only the payload will be hashed before\n * signing.\n *\n * Returns the length of the encoded signature written to \u005cp b64_sig, or -1.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_sign_from_b64(struct lws_jose *jose, struct lws_jws *jws, char *b64_sig,\n\t\t\tsize_t sig_len);\n\n/**\n * lws_jws_compact_decode() - converts and maps compact serialization b64 sections\n *\n * \u005cparam in: the incoming compact serialized b64\n * \u005cparam len: the length of the incoming compact serialized b64\n * \u005cparam map: pointer to the results structure\n * \u005cparam map_b64: NULL, or pointer to a second results structure taking block\n *\t\t information about the undecoded b64\n * \u005cparam out: buffer to hold decoded results\n * \u005cparam out_len: size of out in bytes\n *\n * Returns number of sections (2 if \u0022none\u0022, else 3), or -1 if illegal.\n *\n * map is set to point to the start and hold the length of each decoded block.\n * If map_b64 is non-NULL, then it's set with information about the input b64\n * blocks.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_compact_decode(const char *in, int len, struct lws_jws_map *map,\n\t\tstruct lws_jws_map *map_b64, char *out, int *out_len);\n\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_compact_encode(struct lws_jws_map *map_b64, /* b64-encoded */\n\t\t const struct lws_jws_map *map,\t/* non-b64 */\n\t\t char *buf, int *out_len);\n\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_sig_confirm_json(const char *in, size_t len,\n\t\t\t struct lws_jws *jws, struct lws_jwk *jwk,\n\t\t\t struct lws_context *context,\n\t\t\t char *temp, int *temp_len);\n\n/**\n * lws_jws_write_flattened_json() - create flattened JSON sig\n *\n * \u005cparam jws: information to include in the signature\n * \u005cparam flattened: output buffer for JSON\n * \u005cparam len: size of \u005cp flattened output buffer\n *\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_write_flattened_json(struct lws_jws *jws, char *flattened, size_t len);\n\n/**\n * lws_jws_write_compact() - create flattened JSON sig\n *\n * \u005cparam jws: information to include in the signature\n * \u005cparam compact: output buffer for compact format\n * \u005cparam len: size of \u005cp flattened output buffer\n *\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_write_compact(struct lws_jws *jws, char *compact, size_t len);\n\n\n\n/*\n * below apis are not normally needed if dealing with whole JWS... they're\n * useful for creating from scratch\n */\n\n\n/**\n * lws_jws_dup_element() - allocate space for an element and copy data into it\n *\n * \u005cparam map: map to create the element in\n * \u005cparam idx: index of element in the map to create\n * \u005cparam temp: space to allocate in\n * \u005cparam temp_len: available space at temp\n * \u005cparam in: data to duplicate into element\n * \u005cparam in_len: length of data to duplicate\n * \u005cparam actual_alloc: 0 for same as in_len, else actual allocation size\n *\n * Copies in_len from in to temp, if temp_len is sufficient.\n *\n * Returns 0 or -1 if not enough space in temp / temp_len.\n *\n * Over-allocation can be acheived by setting actual_alloc to the real\n * allocation desired... in_len will be copied into it.\n *\n * *temp_len is reduced by actual_alloc if successful.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_dup_element(struct lws_jws_map *map, int idx,\n\t\t char *temp, int *temp_len, const void *in, size_t in_len,\n\t\t size_t actual_alloc);\n\n/**\n * lws_jws_randomize_element() - create an element and fill with random\n *\n * \u005cparam context: lws_context used for random\n * \u005cparam map: map to create the element in\n * \u005cparam idx: index of element in the map to create\n * \u005cparam temp: space to allocate in\n * \u005cparam temp_len: available space at temp\n * \u005cparam random_len: length of data to fill with random\n * \u005cparam actual_alloc: 0 for same as random_len, else actual allocation size\n *\n * Randomize random_len bytes at temp, if temp_len is sufficient.\n *\n * Returns 0 or -1 if not enough space in temp / temp_len.\n *\n * Over-allocation can be acheived by setting actual_alloc to the real\n * allocation desired... the first random_len will be filled with random.\n *\n * *temp_len is reduced by actual_alloc if successful.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_randomize_element(struct lws_context *context,\n\t\t\t struct lws_jws_map *map,\n\t\t\t int idx, char *temp, int *temp_len, size_t random_len,\n\t\t\t size_t actual_alloc);\n\n/**\n * lws_jws_alloc_element() - create an element and reserve space for content\n *\n * \u005cparam map: map to create the element in\n * \u005cparam idx: index of element in the map to create\n * \u005cparam temp: space to allocate in\n * \u005cparam temp_len: available space at temp\n * \u005cparam len: logical length of element\n * \u005cparam actual_alloc: 0 for same as len, else actual allocation size\n *\n * Allocate len bytes at temp, if temp_len is sufficient.\n *\n * Returns 0 or -1 if not enough space in temp / temp_len.\n *\n * Over-allocation can be acheived by setting actual_alloc to the real\n * allocation desired... the element logical length will be set to len.\n *\n * *temp_len is reduced by actual_alloc if successful.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_alloc_element(struct lws_jws_map *map, int idx, char *temp,\n\t\t int *temp_len, size_t len, size_t actual_alloc);\n\n/**\n * lws_jws_encode_b64_element() - create an b64-encoded element\n *\n * \u005cparam map: map to create the element in\n * \u005cparam idx: index of element in the map to create\n * \u005cparam temp: space to allocate in\n * \u005cparam temp_len: available space at temp\n * \u005cparam in: pointer to unencoded input\n * \u005cparam in_len: length of unencoded input\n *\n * Allocate len bytes at temp, if temp_len is sufficient.\n *\n * Returns 0 or -1 if not enough space in temp / temp_len.\n *\n * Over-allocation can be acheived by setting actual_alloc to the real\n * allocation desired... the element logical length will be set to len.\n *\n * *temp_len is reduced by actual_alloc if successful.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_encode_b64_element(struct lws_jws_map *map, int idx,\n\t\t\t char *temp, int *temp_len, const void *in,\n\t\t\t size_t in_len);\n\n\n/**\n * lws_jws_b64_compact_map() - find block starts and lengths in compact b64\n *\n * \u005cparam in: pointer to b64 jose.payload[.hdr].sig\n * \u005cparam len: bytes available at \u005cp in\n * \u005cparam map: output struct with pointers and lengths for each JWS element\n *\n * Scans a jose.payload[.hdr].sig b64 string and notes where the blocks start\n * and their length into \u005cp map.\n *\n * Returns number of blocks if OK. May return \u003c0 if malformed.\n * May not fill all map entries.\n */\n\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_b64_compact_map(const char *in, int len, struct lws_jws_map *map);\n\n\n/**\n * lws_jws_base64_enc() - encode input data into b64url data\n *\n * \u005cparam in: the incoming plaintext\n * \u005cparam in_len: the length of the incoming plaintext in bytes\n * \u005cparam out: the buffer to store the b64url encoded data to\n * \u005cparam out_max: the length of \u005cp out in bytes\n *\n * Returns either -1 if problems, or the number of bytes written to \u005cp out.\n */\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_base64_enc(const char *in, size_t in_len, char *out, size_t out_max);\n\n/**\n * lws_jws_encode_section() - encode input data into b64url data,\n *\t\t\t\tprepending . if not first\n *\n * \u005cparam in: the incoming plaintext\n * \u005cparam in_len: the length of the incoming plaintext in bytes\n * \u005cparam first: nonzero if the first section\n * \u005cparam p: the buffer to store the b64url encoded data to\n * \u005cparam end: just past the end of p\n *\n * Returns either -1 if problems, or the number of bytes written to \u005cp out.\n * If the section is not the first one, '.' is prepended.\n */\n\nLWS_VISIBLE LWS_EXTERN int\nlws_jws_encode_section(const char *in, size_t in_len, int first, char **p,\n\t\t char *end);\n///@}\n","s":{"c":1580278115,"u": 613}} ],"g": 3293,"chitpc": 0,"ehitpc": 0, "indexed":0 }