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GORo5s9704J+TZySxN/JTH/NdFxwHt6tL\n"
"jPY8unNKxXDHPkaAOe1IGUiFDmvjo1daQKaO6M3dXicyK4u6A0pgqEMbBNcKNGPA\n"
"JeyT\n"
"-----END CERTIFICATE-----\n";

static const uint8_t expected_spki[] = {
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb9, 0x8a, 0xd6, 0xf3, 0x74, 0xad, 0x82, 0x78, 0x39, 0xcb, 0x32, 0xd9, 0xfc, 0xe5, 0x34, 0x9f, 0x86, 0xb1, 0x5d, 0x4b, 0x96, 0x2e, 0x1c, 0x71, 0x45, 0x23, 0x74, 0x16, 0x55, 0x51, 0xfc, 0x56, 0x41, 0xa9, 0xcc, 0x1a, 0x8e, 0x95, 0x17, 0x40, 0x29, 0x9c, 0x74, 0x6c, 0xe6, 0xe2, 0x5b, 0x70, 0x96, 0x8b, 0x5b, 0x64, 0x18, 0x9d, 0xff, 0x6b, 0xc8, 0x3c, 0xd4, 0x49, 0xfd, 0xdb, 0xdc, 0xe7, 0x79, 0x65, 0x66, 0xa1, 0x23, 0xf9, 0x69, 0x8f, 0x87, 0x8e, 0x21, 0x47, 0x0f, 0x4a, 0x72, 0xf0, 0xc7, 0xe1, 0x54, 0x46, 0x33, 0x3f, 0x47, 0xa5, 0x8f, 0x5c, 0x30, 0x2b, 0x3d, 0xb5, 0x8b, 0xe2, 0x8b, 0x63, 0x37, 0x65, 0xba, 0x1a, 0xe5, 0x6b, 0x3e, 0x40, 0x8a, 0x6d, 0x04, 0x56, 0xbe, 0x64, 0xde, 0x88, 0xc1, 0x8a, 0x0b, 0x7c, 0x01, 0x84, 0xb8, 0xae, 0xb2, 0x6f, 0x12, 0xc1, 0x14, 0x70, 0x1c, 0x69, 0xdf, 0x61, 0xe7, 0x3a, 0xaa, 0xc4, 0xb2, 0xee, 0x18, 0xc4, 0x0a, 0x84, 0x1e, 0x04, 0xef, 0x39, 0x19, 0x6a, 0x68, 0xd3, 0xeb, 0xd3, 0xc6, 0x2a, 0x89, 0x61, 0xe6, 0x53, 0xcf, 0xbf, 0x68, 0x0a, 0xfe, 0xc5, 0xf9, 0x56, 0x25, 0xa4, 0xcd, 0x51, 0x96, 0x17, 0x9f, 0x17, 0x0b, 0xc6, 0xa4, 0x82, 0x16, 0x21, 0x29, 0x3e, 0x92, 0xee, 0x16, 0x18, 0x80, 0x91, 0x2d, 0x43, 0xb5, 0x81, 0x74, 0xf9, 0xbb, 0x07, 0xce, 0x99, 0x0e, 0xe9, 0x91, 0xf7, 0x1e, 0x49, 0xbc, 0x1d, 0x48, 0x10, 0x41, 0xc7, 0x6a, 0xb7, 0x52, 0x32, 0x8d, 0x1e, 0x56, 0x35, 0x96, 0x5c, 0x4a, 0xbd, 0x0a, 0x08, 0x22, 0xeb, 0x9b, 0x2c, 0x2c, 0x06, 0x6e, 0x95, 0x4a, 0xe8, 0xea, 0x5b, 0x7a, 0x0c, 0xb7, 0x9e, 0x01, 0x40, 0x4a, 0xf7, 0xd6, 0x82, 0xfe, 0xda, 0x98, 0x12, 0x06, 0x6b, 0xc5, 0xc6, 0x65, 0x3f, 0x02, 0x03, 0x01, 0x00, 0x01
};

int main(int argc, const char **argv)
{
	
	struct lws_x509_cert *x509 = NULL;
	char big[1024];
	union lws_tls_cert_info_results *res = (union lws_tls_cert_info_results *)big;
	int ret = 0;
	size_t big_len = sizeof(big) - sizeof(*res) + sizeof(res->ns.name);

	struct lws_context_creation_info info;
	struct lws_context *context;

	lwsl_user("LWS x509 API test\n");

	lws_context_info_defaults(&info, NULL);
#if defined(LWS_WITH_NETWORK)
	info.port = CONTEXT_PORT_NO_LISTEN;
#endif
	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;

	context = lws_create_context(&info);
	if (!context) {
		lwsl_err("lws init failed\n");
		return 1;
	}

	if (lws_x509_create(&x509)) {
		lwsl_err("lws_x509_create failed\n");
		ret = 1;
		goto bail_ctx;
	}

	if (lws_x509_parse_from_pem(x509, test_cert, strlen(test_cert) + 1)) {
		lwsl_err("lws_x509_parse_from_pem failed\n");
		ret = 1;
		goto bail;
	}

	res->ns.len = 0;
	if (lws_x509_info(x509, LWS_TLS_CERT_INFO_DER_SPKI, res, big_len)) {
		lwsl_err("LWS_TLS_CERT_INFO_DER_SPKI failed (needed %d)\n", res->ns.len);
		ret = 1;
		goto bail;
	}

	if (res->ns.len != sizeof(expected_spki)) {
		lwsl_err("SPKI size mismatch: got %d, expected %d\n", res->ns.len, (int)sizeof(expected_spki));
		ret = 1;
		goto bail;
	}

	size_t name_offset = (size_t)((uint8_t *)&res->ns.name - (uint8_t *)res);
	if (memcmp(big + name_offset, expected_spki, (size_t)res->ns.len)) {
		lwsl_err("SPKI content mismatch\n");
		ret = 1;
		goto bail;
	}

#if defined(LWS_WITH_CLIENT)
	{
		char multi_cert[4096];
		struct lws_vhost *vh;
		struct lws_context_creation_info vinfo;

		lws_snprintf(multi_cert, sizeof(multi_cert), "%s%s", test_cert, test_cert);

		memset(&vinfo, 0, sizeof(vinfo));
		vinfo.vhost_name = "test_multi_ca_mem";
		vinfo.client_ssl_ca_mem = multi_cert;
		vinfo.client_ssl_ca_mem_len = (unsigned int)strlen(multi_cert);

		vh = lws_create_vhost(context, &vinfo);
		if (!vh) {
			lwsl_err("lws_create_vhost failed for multi-cert ca_mem\n");
			ret = 1;
			goto bail;
		}
		lwsl_user("Multi-cert ca_mem vhost creation success\n");
	}
#endif

	lwsl_user("Success\n");

bail:
	lws_x509_destroy(&x509);
bail_ctx:
	lws_context_destroy(context);
	return ret;
}
