1 /* 2 * libwebsockets - small server side websockets and web server implementation 3 * 4 * Copyright (C) 2010 - 2019 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 enum lws_tls_cert_info { 26 LWS_TLS_CERT_INFO_VALIDITY_FROM, 27 /**< fills .time with the time_t the cert validity started from */ 28 LWS_TLS_CERT_INFO_VALIDITY_TO, 29 /**< fills .time with the time_t the cert validity ends at */ 30 LWS_TLS_CERT_INFO_COMMON_NAME, 31 /**< fills up to len bytes of .ns.name with the cert common name */ 32 LWS_TLS_CERT_INFO_ISSUER_NAME, 33 /**< fills up to len bytes of .ns.name with the cert issuer name */ 34 LWS_TLS_CERT_INFO_USAGE, 35 /**< fills verified with a bitfield asserting the valid uses */ 36 LWS_TLS_CERT_INFO_VERIFIED, 37 /**< fills .verified with a bool representing peer cert validity, 38 * call returns -1 if no cert */ 39 LWS_TLS_CERT_INFO_OPAQUE_PUBLIC_KEY, 40 /**< the certificate's public key, as an opaque bytestream. These 41 * opaque bytestreams can only be compared with each other using the 42 * same tls backend, ie, OpenSSL or mbedTLS. The different backends 43 * produce different, incompatible representations for the same cert. 44 */ 45 LWS_TLS_CERT_INFO_DER_RAW, 46 /**< the certificate's raw DER representation. If it's too big, 47 * -1 is returned and the size will be returned in buf->ns.len. 48 * If the certificate cannot be found -1 is returned and 0 in 49 * buf->ns.len. */ 50 }; 51 52 union lws_tls_cert_info_results { 53 unsigned int verified; 54 time_t time; 55 unsigned int usage; 56 struct { 57 int len; 58 /* KEEP LAST... notice the [64] is only there because 59 * name[] is not allowed in a union. The actual length of 60 * name[] is arbitrary and is passed into the api using the 61 * len parameter. Eg 62 * 63 * char big[1024]; 64 * union lws_tls_cert_info_results *buf = 65 * (union lws_tls_cert_info_results *)big; 66 * 67 * lws_tls_peer_cert_info(wsi, type, buf, sizeof(big) - 68 * sizeof(*buf) + sizeof(buf->ns.name)); 69 */ 70 char name[64]; 71 } ns; 72 }; 73 74 struct lws_x509_cert; 75 struct lws_jwk; 76 77 /** 78 * lws_x509_create() - Allocate an lws_x509_cert object 79 * 80 * \param x509: pointer to lws_x509_cert pointer to be set to allocated object 81 * 82 * Allocates an lws_x509_cert object and set *x509 to point to it. 83 */ 84 LWS_VISIBLE LWS_EXTERN int 85 lws_x509_create(struct lws_x509_cert **x509); 86 87 /** 88 * lws_x509_parse_from_pem() - Read one or more x509 certs in PEM format from memory 89 * 90 * \param x509: pointer to lws_x509_cert object 91 * \param pem: pointer to PEM format content 92 * \param len: length of PEM format content 93 * 94 * Parses PEM certificates in memory into a native x509 representation for the 95 * TLS library. If there are multiple PEM certs concatenated, they are all 96 * read into the same object and exist as a "chain". 97 * 98 * IMPORTANT for compatibility with mbedtls, the last used byte of \p pem 99 * must be '\0' and the \p len must include it. 100 * 101 * Returns 0 if all went OK. 102 */ 103 LWS_VISIBLE LWS_EXTERN int 104 lws_x509_parse_from_pem(struct lws_x509_cert *x509, const void *pem, size_t len); 105 106 /** 107 * lws_x509_verify() - Validate signing relationship between one or more certs 108 * and a trusted CA cert 109 * 110 * \param x509: pointer to lws_x509_cert object, may contain multiple 111 * \param trusted: a single, trusted cert object that we are checking for 112 * \param common_name: NULL, or required CN (Common Name) of \p x509 113 * 114 * Returns 0 if the cert or certs in \p x509 represent a complete chain that is 115 * ultimately signed by the cert in \p trusted. Returns nonzero if that's not 116 * the case. 117 */ 118 LWS_VISIBLE LWS_EXTERN int 119 lws_x509_verify(struct lws_x509_cert *x509, struct lws_x509_cert *trusted, 120 const char *common_name); 121 122 /** 123 * lws_x509_public_to_jwk() - Copy the public key out of a cert and into a JWK 124 * 125 * \param jwk: pointer to the jwk to initialize and set to the public key 126 * \param x509: pointer to lws_x509_cert object that has the public key 127 * \param curves: NULL to disallow EC, else a comma-separated list of valid 128 * curves using the JWA naming, eg, "P-256,P-384,P-521". 129 * \param rsabits: minimum number of RSA bits required in the cert if RSA 130 * 131 * Returns 0 if JWK was set to the certificate public key correctly and the 132 * curve / the RSA key size was acceptable. Automatically produces an RSA or 133 * EC JWK depending on what the cert had. 134 */ 135 LWS_VISIBLE LWS_EXTERN int 136 lws_x509_public_to_jwk(struct lws_jwk *jwk, struct lws_x509_cert *x509, 137 const char *curves, int rsabits); 138 139 /** 140 * lws_x509_jwk_privkey_pem() - Copy a private key PEM into a jwk that has the 141 * public part already 142 * 143 * \param jwk: pointer to the jwk to initialize and set to the public key 144 * \param pem: pointer to PEM private key in memory 145 * \param len: length of PEM private key in memory 146 * \param passphrase: NULL or passphrase needed to decrypt private key 147 * 148 * IMPORTANT for compatibility with mbedtls, the last used byte of \p pem 149 * must be '\0' and the \p len must include it. 150 * 151 * Returns 0 if the private key was successfully added to the JWK, else 152 * nonzero if failed. 153 * 154 * The PEM image in memory is zeroed down on both successful and failed exits. 155 * The caller should take care to zero down passphrase if used. 156 */ 157 LWS_VISIBLE LWS_EXTERN int 158 lws_x509_jwk_privkey_pem(struct lws_jwk *jwk, void *pem, size_t len, 159 const char *passphrase); 160 161 /** 162 * lws_x509_destroy() - Destroy a previously allocated lws_x509_cert object 163 * 164 * \param x509: pointer to lws_x509_cert pointer 165 * 166 * Deallocates an lws_x509_cert object and sets its pointer to NULL. 167 */ 168 LWS_VISIBLE LWS_EXTERN void 169 lws_x509_destroy(struct lws_x509_cert **x509); 170 171 LWS_VISIBLE LWS_EXTERN int 172 lws_x509_info(struct lws_x509_cert *x509, enum lws_tls_cert_info type, 173 union lws_tls_cert_info_results *buf, size_t len); 174 175 /** 176 * lws_tls_peer_cert_info() - get information from the peer's TLS cert 177 * 178 * \param wsi: the connection to query 179 * \param type: one of LWS_TLS_CERT_INFO_ 180 * \param buf: pointer to union to take result 181 * \param len: when result is a string, the true length of buf->ns.name[] 182 * 183 * lws_tls_peer_cert_info() lets you get hold of information from the peer 184 * certificate. 185 * 186 * Return 0 if there is a result in \p buf, or -1 indicating there was no cert 187 * or another problem. 188 * 189 * This function works the same no matter if the TLS backend is OpenSSL or 190 * mbedTLS. 191 */ 192 LWS_VISIBLE LWS_EXTERN int 193 lws_tls_peer_cert_info(struct lws *wsi, enum lws_tls_cert_info type, 194 union lws_tls_cert_info_results *buf, size_t len); 195 196 /** 197 * lws_tls_vhost_cert_info() - get information from the vhost's own TLS cert 198 * 199 * \param vhost: the vhost to query 200 * \param type: one of LWS_TLS_CERT_INFO_ 201 * \param buf: pointer to union to take result 202 * \param len: when result is a string, the true length of buf->ns.name[] 203 * 204 * lws_tls_vhost_cert_info() lets you get hold of information from the vhost 205 * certificate. 206 * 207 * Return 0 if there is a result in \p buf, or -1 indicating there was no cert 208 * or another problem. 209 * 210 * This function works the same no matter if the TLS backend is OpenSSL or 211 * mbedTLS. 212 */ 213 LWS_VISIBLE LWS_EXTERN int 214 lws_tls_vhost_cert_info(struct lws_vhost *vhost, enum lws_tls_cert_info type, 215 union lws_tls_cert_info_results *buf, size_t len); 216 217 /** 218 * lws_tls_acme_sni_cert_create() - creates a temp selfsigned cert 219 * and attaches to a vhost 220 * 221 * \param vhost: the vhost to acquire the selfsigned cert 222 * \param san_a: SAN written into the certificate 223 * \param san_b: second SAN written into the certificate 224 * 225 * 226 * Returns 0 if created and attached to the vhost. Returns -1 if problems and 227 * frees all allocations before returning. 228 * 229 * On success, any allocations are destroyed at vhost destruction automatically. 230 */ 231 LWS_VISIBLE LWS_EXTERN int 232 lws_tls_acme_sni_cert_create(struct lws_vhost *vhost, const char *san_a, 233 const char *san_b); 234 235 /** 236 * lws_tls_acme_sni_csr_create() - creates a CSR and related private key PEM 237 * 238 * \param context: lws_context used for random 239 * \param elements: array of LWS_TLS_REQ_ELEMENT_COUNT const char * 240 * \param csr: buffer that will get the b64URL(ASN-1 CSR) 241 * \param csr_len: max length of the csr buffer 242 * \param privkey_pem: pointer to pointer allocated to hold the privkey_pem 243 * \param privkey_len: pointer to size_t set to the length of the privkey_pem 244 * 245 * Creates a CSR according to the information in \p elements, and a private 246 * RSA key used to sign the CSR. 247 * 248 * The outputs are the b64URL(ASN-1 CSR) into csr, and the PEM private key into 249 * privkey_pem. 250 * 251 * Notice that \p elements points to an array of const char *s pointing to the 252 * information listed in the enum above. If an entry is NULL or an empty 253 * string, the element is set to "none" in the CSR. 254 * 255 * Returns 0 on success or nonzero for failure. 256 */ 257 LWS_VISIBLE LWS_EXTERN int 258 lws_tls_acme_sni_csr_create(struct lws_context *context, const char *elements[], 259 uint8_t *csr, size_t csr_len, char **privkey_pem, 260 size_t *privkey_len); 261 262 /** 263 * lws_tls_cert_updated() - update every vhost using the given cert path 264 * 265 * \param context: our lws_context 266 * \param certpath: the filepath to the certificate 267 * \param keypath: the filepath to the private key of the certificate 268 * \param mem_cert: copy of the cert in memory 269 * \param len_mem_cert: length of the copy of the cert in memory 270 * \param mem_privkey: copy of the private key in memory 271 * \param len_mem_privkey: length of the copy of the private key in memory 272 * 273 * Checks every vhost to see if it is the using certificate described by the 274 * the given filepaths. If so, it attempts to update the vhost ssl_ctx to use 275 * the new certificate. 276 * 277 * Returns 0 on success or nonzero for failure. 278 */ 279 LWS_VISIBLE LWS_EXTERN int 280 lws_tls_cert_updated(struct lws_context *context, const char *certpath, 281 const char *keypath, 282 const char *mem_cert, size_t len_mem_cert, 283 const char *mem_privkey, size_t len_mem_privkey); 284 285