libwebsockets
Lightweight C library for HTML5 websockets
lws-jose.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation:
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  *
21  * included from libwebsockets.h
22  */
23 
24 enum lws_jws_jose_hdr_indexes {
25  LJJHI_ALG, /* REQUIRED */
26  LJJHI_JKU, /* Optional: string */
27  LJJHI_JWK, /* Optional: jwk JSON object: public key: */
28  LJJHI_KID, /* Optional: string */
29  LJJHI_X5U, /* Optional: string: url of public key cert / chain */
30  LJJHI_X5C, /* Optional: base64 (NOT -url): actual cert */
31  LJJHI_X5T, /* Optional: base64url: SHA-1 of actual cert */
32  LJJHI_X5T_S256, /* Optional: base64url: SHA-256 of actual cert */
33  LJJHI_TYP, /* Optional: string: media type */
34  LJJHI_CTY, /* Optional: string: content media type */
35  LJJHI_CRIT, /* Optional for send, REQUIRED: array of strings:
36  * mustn't contain standardized strings or null set */
37 
38  LJJHI_RECIPS_HDR,
39  LJJHI_RECIPS_HDR_ALG,
40  LJJHI_RECIPS_HDR_KID,
41  LJJHI_RECIPS_EKEY,
42 
43  LJJHI_ENC, /* JWE only: Optional: string */
44  LJJHI_ZIP, /* JWE only: Optional: string ("DEF" = deflate) */
45 
46  LJJHI_EPK, /* Additional arg for JWE ECDH: ephemeral public key */
47  LJJHI_APU, /* Additional arg for JWE ECDH: base64url */
48  LJJHI_APV, /* Additional arg for JWE ECDH: base64url */
49  LJJHI_IV, /* Additional arg for JWE AES: base64url */
50  LJJHI_TAG, /* Additional arg for JWE AES: base64url */
51  LJJHI_P2S, /* Additional arg for JWE PBES2: base64url: salt */
52  LJJHI_P2C, /* Additional arg for JWE PBES2: integer: count */
53 
54  LWS_COUNT_JOSE_HDR_ELEMENTS
55 };
56 
57 enum lws_jose_algtype {
58  LWS_JOSE_ENCTYPE_NONE,
59 
60  LWS_JOSE_ENCTYPE_RSASSA_PKCS1_1_5,
61  LWS_JOSE_ENCTYPE_RSASSA_PKCS1_OAEP,
62  LWS_JOSE_ENCTYPE_RSASSA_PKCS1_PSS,
63 
64  LWS_JOSE_ENCTYPE_ECDSA,
65  LWS_JOSE_ENCTYPE_ECDHES,
66 
67  LWS_JOSE_ENCTYPE_AES_CBC,
68  LWS_JOSE_ENCTYPE_AES_CFB128,
69  LWS_JOSE_ENCTYPE_AES_CFB8,
70  LWS_JOSE_ENCTYPE_AES_CTR,
71  LWS_JOSE_ENCTYPE_AES_ECB,
72  LWS_JOSE_ENCTYPE_AES_OFB,
73  LWS_JOSE_ENCTYPE_AES_XTS, /* care: requires double-length key */
74  LWS_JOSE_ENCTYPE_AES_GCM,
75 };
76 
77 /* there's a table of these defined in lws-gencrypto-common.c */
78 
80  enum lws_genhash_types hash_type;
81  enum lws_genhmac_types hmac_type;
82  enum lws_jose_algtype algtype_signing; /* the signing cipher */
83  enum lws_jose_algtype algtype_crypto; /* the encryption cipher */
84  const char *alg; /* the JWA enc alg name, eg "ES512" */
85  const char *curve_name; /* NULL, or, eg, "P-256" */
86  unsigned short keybits_min, keybits_fixed;
87  unsigned short ivbits;
88 };
89 
90 /*
91  * For JWS, "JOSE header" is defined to be the union of...
92  *
93  * o JWS Protected Header
94  * o JWS Unprotected Header
95  *
96  * For JWE, the "JOSE header" is the union of...
97  *
98  * o JWE Protected Header
99  * o JWE Shared Unprotected Header
100  * o JWE Per-Recipient Unprotected Header
101  */
102 
103 #define LWS_JWS_MAX_RECIPIENTS 3
104 
106  /*
107  * JOSE per-recipient unprotected header... for JWS this contains
108  * protected / header / signature
109  */
110  struct lws_gencrypto_keyelem unprot[LWS_COUNT_JOSE_HDR_ELEMENTS];
111  struct lws_jwk jwk_ephemeral; /* recipient ephemeral key if any */
112  struct lws_jwk jwk; /* recipient "jwk" key if any */
113 };
114 
115 struct lws_jose {
116  /* JOSE protected and unprotected header elements */
117  struct lws_gencrypto_keyelem e[LWS_COUNT_JOSE_HDR_ELEMENTS];
118 
119  struct lws_jws_recpient recipient[LWS_JWS_MAX_RECIPIENTS];
120 
121  /* information from the protected header part */
122  const struct lws_jose_jwe_alg *alg;
123  const struct lws_jose_jwe_alg *enc_alg;
124 
125  int recipients; /* count of used recipient[] entries */
126 };
127 
133 LWS_VISIBLE LWS_EXTERN void
134 lws_jose_init(struct lws_jose *jose);
135 
141 LWS_VISIBLE LWS_EXTERN void
142 lws_jose_destroy(struct lws_jose *jose);
143 
152 LWS_VISIBLE LWS_EXTERN int
153 lws_gencrypto_jws_alg_to_definition(const char *alg,
154  const struct lws_jose_jwe_alg **jose);
155 
164 LWS_VISIBLE LWS_EXTERN int
165 lws_gencrypto_jwe_alg_to_definition(const char *alg,
166  const struct lws_jose_jwe_alg **jose);
167 
176 LWS_VISIBLE LWS_EXTERN int
177 lws_gencrypto_jwe_enc_to_definition(const char *enc,
178  const struct lws_jose_jwe_alg **jose);
179 
191 LWS_VISIBLE LWS_EXTERN int
192 lws_jws_parse_jose(struct lws_jose *jose,
193  const char *buf, int len, char *temp, int *temp_len);
194 
206 LWS_VISIBLE LWS_EXTERN int
207 lws_jwe_parse_jose(struct lws_jose *jose,
208  const char *buf, int len, char *temp, int *temp_len);
209 
Definition: lws-gencrypto.h:91
Definition: lws-jose.h:79
Definition: lws-jose.h:115
Definition: lws-jwk.h:49
Definition: lws-jose.h:105