tlsv1_cred.c 12 KB

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