tlsv1_cred.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * TLSv1 credentials
  3. * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "base64.h"
  17. #include "crypto.h"
  18. #include "x509v3.h"
  19. #include "tlsv1_cred.h"
  20. struct tlsv1_credentials * tlsv1_cred_alloc(void)
  21. {
  22. struct tlsv1_credentials *cred;
  23. cred = os_zalloc(sizeof(*cred));
  24. return cred;
  25. }
  26. void tlsv1_cred_free(struct tlsv1_credentials *cred)
  27. {
  28. if (cred == NULL)
  29. return;
  30. x509_certificate_chain_free(cred->trusted_certs);
  31. x509_certificate_chain_free(cred->cert);
  32. crypto_private_key_free(cred->key);
  33. os_free(cred->dh_p);
  34. os_free(cred->dh_g);
  35. os_free(cred);
  36. }
  37. static int tlsv1_add_cert_der(struct x509_certificate **chain,
  38. const u8 *buf, size_t len)
  39. {
  40. struct x509_certificate *cert;
  41. char name[128];
  42. cert = x509_certificate_parse(buf, len);
  43. if (cert == NULL) {
  44. wpa_printf(MSG_INFO, "TLSv1: %s - failed to parse certificate",
  45. __func__);
  46. return -1;
  47. }
  48. cert->next = *chain;
  49. *chain = cert;
  50. x509_name_string(&cert->subject, name, sizeof(name));
  51. wpa_printf(MSG_DEBUG, "TLSv1: Added certificate: %s", name);
  52. return 0;
  53. }
  54. static const char *pem_cert_begin = "-----BEGIN CERTIFICATE-----";
  55. static const char *pem_cert_end = "-----END CERTIFICATE-----";
  56. static const u8 * search_tag(const char *tag, const u8 *buf, size_t len)
  57. {
  58. size_t i, plen;
  59. plen = os_strlen(tag);
  60. if (len < plen)
  61. return NULL;
  62. for (i = 0; i < len - plen; i++) {
  63. if (os_memcmp(buf + i, tag, plen) == 0)
  64. return buf + i;
  65. }
  66. return NULL;
  67. }
  68. static int tlsv1_add_cert(struct x509_certificate **chain,
  69. const u8 *buf, size_t len)
  70. {
  71. const u8 *pos, *end;
  72. unsigned char *der;
  73. size_t der_len;
  74. pos = search_tag(pem_cert_begin, buf, len);
  75. if (!pos) {
  76. wpa_printf(MSG_DEBUG, "TLSv1: No PEM certificate tag found - "
  77. "assume DER format");
  78. return tlsv1_add_cert_der(chain, buf, len);
  79. }
  80. wpa_printf(MSG_DEBUG, "TLSv1: Converting PEM format certificate into "
  81. "DER format");
  82. while (pos) {
  83. pos += os_strlen(pem_cert_begin);
  84. end = search_tag(pem_cert_end, pos, buf + len - pos);
  85. if (end == NULL) {
  86. wpa_printf(MSG_INFO, "TLSv1: Could not find PEM "
  87. "certificate end tag (%s)", pem_cert_end);
  88. return -1;
  89. }
  90. der = base64_decode(pos, end - pos, &der_len);
  91. if (der == NULL) {
  92. wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM "
  93. "certificate");
  94. return -1;
  95. }
  96. if (tlsv1_add_cert_der(chain, der, der_len) < 0) {
  97. wpa_printf(MSG_INFO, "TLSv1: Failed to parse PEM "
  98. "certificate after DER conversion");
  99. os_free(der);
  100. return -1;
  101. }
  102. os_free(der);
  103. end += os_strlen(pem_cert_end);
  104. pos = search_tag(pem_cert_begin, end, buf + len - end);
  105. }
  106. return 0;
  107. }
  108. static int tlsv1_set_cert_chain(struct x509_certificate **chain,
  109. const char *cert, const u8 *cert_blob,
  110. size_t cert_blob_len)
  111. {
  112. if (cert_blob)
  113. return tlsv1_add_cert(chain, cert_blob, cert_blob_len);
  114. if (cert) {
  115. u8 *buf;
  116. size_t len;
  117. int ret;
  118. buf = (u8 *) os_readfile(cert, &len);
  119. if (buf == NULL) {
  120. wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
  121. cert);
  122. return -1;
  123. }
  124. ret = tlsv1_add_cert(chain, buf, len);
  125. os_free(buf);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. /**
  131. * tlsv1_set_ca_cert - Set trusted CA certificate(s)
  132. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  133. * @cert: File or reference name for X.509 certificate in PEM or DER format
  134. * @cert_blob: cert as inlined data or %NULL if not used
  135. * @cert_blob_len: ca_cert_blob length
  136. * @path: Path to CA certificates (not yet supported)
  137. * Returns: 0 on success, -1 on failure
  138. */
  139. int tlsv1_set_ca_cert(struct tlsv1_credentials *cred, const char *cert,
  140. const u8 *cert_blob, size_t cert_blob_len,
  141. const char *path)
  142. {
  143. if (tlsv1_set_cert_chain(&cred->trusted_certs, cert,
  144. cert_blob, cert_blob_len) < 0)
  145. return -1;
  146. if (path) {
  147. /* TODO: add support for reading number of certificate files */
  148. wpa_printf(MSG_INFO, "TLSv1: Use of CA certificate directory "
  149. "not yet supported");
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. /**
  155. * tlsv1_set_cert - Set certificate
  156. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  157. * @cert: File or reference name for X.509 certificate in PEM or DER format
  158. * @cert_blob: cert as inlined data or %NULL if not used
  159. * @cert_blob_len: cert_blob length
  160. * Returns: 0 on success, -1 on failure
  161. */
  162. int tlsv1_set_cert(struct tlsv1_credentials *cred, const char *cert,
  163. const u8 *cert_blob, size_t cert_blob_len)
  164. {
  165. return tlsv1_set_cert_chain(&cred->cert, cert,
  166. cert_blob, cert_blob_len);
  167. }
  168. static int tlsv1_set_key(struct tlsv1_credentials *cred,
  169. const u8 *key, size_t len)
  170. {
  171. cred->key = crypto_private_key_import(key, len);
  172. if (cred->key == NULL) {
  173. wpa_printf(MSG_INFO, "TLSv1: Failed to parse private key");
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. /**
  179. * tlsv1_set_private_key - Set private key
  180. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  181. * @private_key: File or reference name for the key in PEM or DER format
  182. * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
  183. * passphrase is used.
  184. * @private_key_blob: private_key as inlined data or %NULL if not used
  185. * @private_key_blob_len: private_key_blob length
  186. * Returns: 0 on success, -1 on failure
  187. */
  188. int tlsv1_set_private_key(struct tlsv1_credentials *cred,
  189. const char *private_key,
  190. const char *private_key_passwd,
  191. const u8 *private_key_blob,
  192. size_t private_key_blob_len)
  193. {
  194. crypto_private_key_free(cred->key);
  195. cred->key = NULL;
  196. if (private_key_blob)
  197. return tlsv1_set_key(cred, private_key_blob,
  198. private_key_blob_len);
  199. if (private_key) {
  200. u8 *buf;
  201. size_t len;
  202. int ret;
  203. buf = (u8 *) os_readfile(private_key, &len);
  204. if (buf == NULL) {
  205. wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
  206. private_key);
  207. return -1;
  208. }
  209. ret = tlsv1_set_key(cred, buf, len);
  210. os_free(buf);
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. static int tlsv1_set_dhparams_der(struct tlsv1_credentials *cred,
  216. const u8 *dh, size_t len)
  217. {
  218. struct asn1_hdr hdr;
  219. const u8 *pos, *end;
  220. pos = dh;
  221. end = dh + len;
  222. /*
  223. * DHParameter ::= SEQUENCE {
  224. * prime INTEGER, -- p
  225. * base INTEGER, -- g
  226. * privateValueLength INTEGER OPTIONAL }
  227. */
  228. /* DHParamer ::= SEQUENCE */
  229. if (asn1_get_next(pos, len, &hdr) < 0 ||
  230. hdr.class != ASN1_CLASS_UNIVERSAL ||
  231. hdr.tag != ASN1_TAG_SEQUENCE) {
  232. wpa_printf(MSG_DEBUG, "DH: DH parameters did not start with a "
  233. "valid SEQUENCE - found class %d tag 0x%x",
  234. hdr.class, hdr.tag);
  235. return -1;
  236. }
  237. pos = hdr.payload;
  238. /* prime INTEGER */
  239. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  240. return -1;
  241. if (hdr.class != ASN1_CLASS_UNIVERSAL ||
  242. hdr.tag != ASN1_TAG_INTEGER) {
  243. wpa_printf(MSG_DEBUG, "DH: No INTEGER tag found for p; "
  244. "class=%d tag=0x%x", hdr.class, hdr.tag);
  245. return -1;
  246. }
  247. wpa_hexdump(MSG_MSGDUMP, "DH: prime (p)", hdr.payload, hdr.length);
  248. if (hdr.length == 0)
  249. return -1;
  250. os_free(cred->dh_p);
  251. cred->dh_p = os_malloc(hdr.length);
  252. if (cred->dh_p == NULL)
  253. return -1;
  254. os_memcpy(cred->dh_p, hdr.payload, hdr.length);
  255. cred->dh_p_len = hdr.length;
  256. pos = hdr.payload + hdr.length;
  257. /* base INTEGER */
  258. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  259. return -1;
  260. if (hdr.class != ASN1_CLASS_UNIVERSAL ||
  261. hdr.tag != ASN1_TAG_INTEGER) {
  262. wpa_printf(MSG_DEBUG, "DH: No INTEGER tag found for g; "
  263. "class=%d tag=0x%x", hdr.class, hdr.tag);
  264. return -1;
  265. }
  266. wpa_hexdump(MSG_MSGDUMP, "DH: base (g)", hdr.payload, hdr.length);
  267. if (hdr.length == 0)
  268. return -1;
  269. os_free(cred->dh_g);
  270. cred->dh_g = os_malloc(hdr.length);
  271. if (cred->dh_g == NULL)
  272. return -1;
  273. os_memcpy(cred->dh_g, hdr.payload, hdr.length);
  274. cred->dh_g_len = hdr.length;
  275. return 0;
  276. }
  277. static const char *pem_dhparams_begin = "-----BEGIN DH PARAMETERS-----";
  278. static const char *pem_dhparams_end = "-----END DH PARAMETERS-----";
  279. static int tlsv1_set_dhparams_blob(struct tlsv1_credentials *cred,
  280. const u8 *buf, size_t len)
  281. {
  282. const u8 *pos, *end;
  283. unsigned char *der;
  284. size_t der_len;
  285. pos = search_tag(pem_dhparams_begin, buf, len);
  286. if (!pos) {
  287. wpa_printf(MSG_DEBUG, "TLSv1: No PEM dhparams tag found - "
  288. "assume DER format");
  289. return tlsv1_set_dhparams_der(cred, buf, len);
  290. }
  291. wpa_printf(MSG_DEBUG, "TLSv1: Converting PEM format dhparams into DER "
  292. "format");
  293. pos += os_strlen(pem_dhparams_begin);
  294. end = search_tag(pem_dhparams_end, pos, buf + len - pos);
  295. if (end == NULL) {
  296. wpa_printf(MSG_INFO, "TLSv1: Could not find PEM dhparams end "
  297. "tag (%s)", pem_dhparams_end);
  298. return -1;
  299. }
  300. der = base64_decode(pos, end - pos, &der_len);
  301. if (der == NULL) {
  302. wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM dhparams");
  303. return -1;
  304. }
  305. if (tlsv1_set_dhparams_der(cred, der, der_len) < 0) {
  306. wpa_printf(MSG_INFO, "TLSv1: Failed to parse PEM dhparams "
  307. "DER conversion");
  308. os_free(der);
  309. return -1;
  310. }
  311. os_free(der);
  312. return 0;
  313. }
  314. /**
  315. * tlsv1_set_dhparams - Set Diffie-Hellman parameters
  316. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  317. * @dh_file: File or reference name for the DH params in PEM or DER format
  318. * @dh_blob: DH params as inlined data or %NULL if not used
  319. * @dh_blob_len: dh_blob length
  320. * Returns: 0 on success, -1 on failure
  321. */
  322. int tlsv1_set_dhparams(struct tlsv1_credentials *cred, const char *dh_file,
  323. const u8 *dh_blob, size_t dh_blob_len)
  324. {
  325. if (dh_blob)
  326. return tlsv1_set_dhparams_blob(cred, dh_blob, dh_blob_len);
  327. if (dh_file) {
  328. u8 *buf;
  329. size_t len;
  330. int ret;
  331. buf = (u8 *) os_readfile(dh_file, &len);
  332. if (buf == NULL) {
  333. wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
  334. dh_file);
  335. return -1;
  336. }
  337. ret = tlsv1_set_dhparams_blob(cred, buf, len);
  338. os_free(buf);
  339. return ret;
  340. }
  341. return 0;
  342. }