tlsv1_cred.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * TLSv1 credentials
  3. * Copyright (c) 2006-2015, 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 "crypto/sha1.h"
  13. #include "pkcs5.h"
  14. #include "pkcs8.h"
  15. #include "x509v3.h"
  16. #include "tlsv1_cred.h"
  17. struct tlsv1_credentials * tlsv1_cred_alloc(void)
  18. {
  19. struct tlsv1_credentials *cred;
  20. cred = os_zalloc(sizeof(*cred));
  21. return cred;
  22. }
  23. void tlsv1_cred_free(struct tlsv1_credentials *cred)
  24. {
  25. if (cred == NULL)
  26. return;
  27. x509_certificate_chain_free(cred->trusted_certs);
  28. x509_certificate_chain_free(cred->cert);
  29. crypto_private_key_free(cred->key);
  30. os_free(cred->dh_p);
  31. os_free(cred->dh_g);
  32. os_free(cred);
  33. }
  34. static int tlsv1_add_cert_der(struct x509_certificate **chain,
  35. const u8 *buf, size_t len)
  36. {
  37. struct x509_certificate *cert, *p;
  38. char name[128];
  39. cert = x509_certificate_parse(buf, len);
  40. if (cert == NULL) {
  41. wpa_printf(MSG_INFO, "TLSv1: %s - failed to parse certificate",
  42. __func__);
  43. return -1;
  44. }
  45. p = *chain;
  46. while (p && p->next)
  47. p = p->next;
  48. if (p && x509_name_compare(&cert->subject, &p->issuer) == 0) {
  49. /*
  50. * The new certificate is the issuer of the last certificate in
  51. * the chain - add the new certificate to the end.
  52. */
  53. p->next = cert;
  54. } else {
  55. /* Add to the beginning of the chain */
  56. cert->next = *chain;
  57. *chain = cert;
  58. }
  59. x509_name_string(&cert->subject, name, sizeof(name));
  60. wpa_printf(MSG_DEBUG, "TLSv1: Added certificate: %s", name);
  61. return 0;
  62. }
  63. static const char *pem_cert_begin = "-----BEGIN CERTIFICATE-----";
  64. static const char *pem_cert_end = "-----END CERTIFICATE-----";
  65. static const char *pem_key_begin = "-----BEGIN RSA PRIVATE KEY-----";
  66. static const char *pem_key_end = "-----END RSA PRIVATE KEY-----";
  67. static const char *pem_key2_begin = "-----BEGIN PRIVATE KEY-----";
  68. static const char *pem_key2_end = "-----END PRIVATE KEY-----";
  69. static const char *pem_key_enc_begin = "-----BEGIN ENCRYPTED PRIVATE KEY-----";
  70. static const char *pem_key_enc_end = "-----END ENCRYPTED PRIVATE KEY-----";
  71. static const u8 * search_tag(const char *tag, const u8 *buf, size_t len)
  72. {
  73. size_t i, plen;
  74. plen = os_strlen(tag);
  75. if (len < plen)
  76. return NULL;
  77. for (i = 0; i < len - plen; i++) {
  78. if (os_memcmp(buf + i, tag, plen) == 0)
  79. return buf + i;
  80. }
  81. return NULL;
  82. }
  83. static int tlsv1_add_cert(struct x509_certificate **chain,
  84. const u8 *buf, size_t len)
  85. {
  86. const u8 *pos, *end;
  87. unsigned char *der;
  88. size_t der_len;
  89. pos = search_tag(pem_cert_begin, buf, len);
  90. if (!pos) {
  91. wpa_printf(MSG_DEBUG, "TLSv1: No PEM certificate tag found - "
  92. "assume DER format");
  93. return tlsv1_add_cert_der(chain, buf, len);
  94. }
  95. wpa_printf(MSG_DEBUG, "TLSv1: Converting PEM format certificate into "
  96. "DER format");
  97. while (pos) {
  98. pos += os_strlen(pem_cert_begin);
  99. end = search_tag(pem_cert_end, pos, buf + len - pos);
  100. if (end == NULL) {
  101. wpa_printf(MSG_INFO, "TLSv1: Could not find PEM "
  102. "certificate end tag (%s)", pem_cert_end);
  103. return -1;
  104. }
  105. der = base64_decode(pos, end - pos, &der_len);
  106. if (der == NULL) {
  107. wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM "
  108. "certificate");
  109. return -1;
  110. }
  111. if (tlsv1_add_cert_der(chain, der, der_len) < 0) {
  112. wpa_printf(MSG_INFO, "TLSv1: Failed to parse PEM "
  113. "certificate after DER conversion");
  114. os_free(der);
  115. return -1;
  116. }
  117. os_free(der);
  118. end += os_strlen(pem_cert_end);
  119. pos = search_tag(pem_cert_begin, end, buf + len - end);
  120. }
  121. return 0;
  122. }
  123. static int tlsv1_set_cert_chain(struct x509_certificate **chain,
  124. const char *cert, const u8 *cert_blob,
  125. size_t cert_blob_len)
  126. {
  127. if (cert_blob)
  128. return tlsv1_add_cert(chain, cert_blob, cert_blob_len);
  129. if (cert) {
  130. u8 *buf;
  131. size_t len;
  132. int ret;
  133. buf = (u8 *) os_readfile(cert, &len);
  134. if (buf == NULL) {
  135. wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
  136. cert);
  137. return -1;
  138. }
  139. ret = tlsv1_add_cert(chain, buf, len);
  140. os_free(buf);
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. /**
  146. * tlsv1_set_ca_cert - Set trusted CA certificate(s)
  147. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  148. * @cert: File or reference name for X.509 certificate in PEM or DER format
  149. * @cert_blob: cert as inlined data or %NULL if not used
  150. * @cert_blob_len: ca_cert_blob length
  151. * @path: Path to CA certificates (not yet supported)
  152. * Returns: 0 on success, -1 on failure
  153. */
  154. int tlsv1_set_ca_cert(struct tlsv1_credentials *cred, const char *cert,
  155. const u8 *cert_blob, size_t cert_blob_len,
  156. const char *path)
  157. {
  158. if (cert && os_strncmp(cert, "hash://", 7) == 0) {
  159. const char *pos = cert + 7;
  160. if (os_strncmp(pos, "server/sha256/", 14) != 0) {
  161. wpa_printf(MSG_DEBUG,
  162. "TLSv1: Unsupported ca_cert hash value '%s'",
  163. cert);
  164. return -1;
  165. }
  166. pos += 14;
  167. if (os_strlen(pos) != 32 * 2) {
  168. wpa_printf(MSG_DEBUG,
  169. "TLSv1: Unexpected SHA256 hash length in ca_cert '%s'",
  170. cert);
  171. return -1;
  172. }
  173. if (hexstr2bin(pos, cred->srv_cert_hash, 32) < 0) {
  174. wpa_printf(MSG_DEBUG,
  175. "TLSv1: Invalid SHA256 hash value in ca_cert '%s'",
  176. cert);
  177. return -1;
  178. }
  179. cred->server_cert_only = 1;
  180. cred->ca_cert_verify = 0;
  181. wpa_printf(MSG_DEBUG,
  182. "TLSv1: Checking only server certificate match");
  183. return 0;
  184. }
  185. if (cert && os_strncmp(cert, "probe://", 8) == 0) {
  186. cred->cert_probe = 1;
  187. cred->ca_cert_verify = 0;
  188. wpa_printf(MSG_DEBUG, "TLSv1: Only probe server certificate");
  189. return 0;
  190. }
  191. cred->ca_cert_verify = cert || cert_blob || path;
  192. if (tlsv1_set_cert_chain(&cred->trusted_certs, cert,
  193. cert_blob, cert_blob_len) < 0)
  194. return -1;
  195. if (path) {
  196. /* TODO: add support for reading number of certificate files */
  197. wpa_printf(MSG_INFO, "TLSv1: Use of CA certificate directory "
  198. "not yet supported");
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. /**
  204. * tlsv1_set_cert - Set certificate
  205. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  206. * @cert: File or reference name for X.509 certificate in PEM or DER format
  207. * @cert_blob: cert as inlined data or %NULL if not used
  208. * @cert_blob_len: cert_blob length
  209. * Returns: 0 on success, -1 on failure
  210. */
  211. int tlsv1_set_cert(struct tlsv1_credentials *cred, const char *cert,
  212. const u8 *cert_blob, size_t cert_blob_len)
  213. {
  214. return tlsv1_set_cert_chain(&cred->cert, cert,
  215. cert_blob, cert_blob_len);
  216. }
  217. static struct crypto_private_key * tlsv1_set_key_pem(const u8 *key, size_t len)
  218. {
  219. const u8 *pos, *end;
  220. unsigned char *der;
  221. size_t der_len;
  222. struct crypto_private_key *pkey;
  223. pos = search_tag(pem_key_begin, key, len);
  224. if (!pos) {
  225. pos = search_tag(pem_key2_begin, key, len);
  226. if (!pos)
  227. return NULL;
  228. pos += os_strlen(pem_key2_begin);
  229. end = search_tag(pem_key2_end, pos, key + len - pos);
  230. if (!end)
  231. return NULL;
  232. } else {
  233. const u8 *pos2;
  234. pos += os_strlen(pem_key_begin);
  235. end = search_tag(pem_key_end, pos, key + len - pos);
  236. if (!end)
  237. return NULL;
  238. pos2 = search_tag("Proc-Type: 4,ENCRYPTED", pos, end - pos);
  239. if (pos2) {
  240. wpa_printf(MSG_DEBUG, "TLSv1: Unsupported private key "
  241. "format (Proc-Type/DEK-Info)");
  242. return NULL;
  243. }
  244. }
  245. der = base64_decode(pos, end - pos, &der_len);
  246. if (!der)
  247. return NULL;
  248. pkey = crypto_private_key_import(der, der_len, NULL);
  249. os_free(der);
  250. return pkey;
  251. }
  252. static struct crypto_private_key * tlsv1_set_key_enc_pem(const u8 *key,
  253. size_t len,
  254. const char *passwd)
  255. {
  256. const u8 *pos, *end;
  257. unsigned char *der;
  258. size_t der_len;
  259. struct crypto_private_key *pkey;
  260. if (passwd == NULL)
  261. return NULL;
  262. pos = search_tag(pem_key_enc_begin, key, len);
  263. if (!pos)
  264. return NULL;
  265. pos += os_strlen(pem_key_enc_begin);
  266. end = search_tag(pem_key_enc_end, pos, key + len - pos);
  267. if (!end)
  268. return NULL;
  269. der = base64_decode(pos, end - pos, &der_len);
  270. if (!der)
  271. return NULL;
  272. pkey = crypto_private_key_import(der, der_len, passwd);
  273. os_free(der);
  274. return pkey;
  275. }
  276. #ifdef PKCS12_FUNCS
  277. static int oid_is_rsadsi(struct asn1_oid *oid)
  278. {
  279. return oid->len >= 4 &&
  280. oid->oid[0] == 1 /* iso */ &&
  281. oid->oid[1] == 2 /* member-body */ &&
  282. oid->oid[2] == 840 /* us */ &&
  283. oid->oid[3] == 113549 /* rsadsi */;
  284. }
  285. static int pkcs12_is_bagtype_oid(struct asn1_oid *oid, unsigned long type)
  286. {
  287. return oid->len == 9 &&
  288. oid_is_rsadsi(oid) &&
  289. oid->oid[4] == 1 /* pkcs */ &&
  290. oid->oid[5] == 12 /* pkcs-12 */ &&
  291. oid->oid[6] == 10 &&
  292. oid->oid[7] == 1 /* bagtypes */ &&
  293. oid->oid[8] == type;
  294. }
  295. static int is_oid_pkcs7(struct asn1_oid *oid)
  296. {
  297. return oid->len == 7 &&
  298. oid->oid[0] == 1 /* iso */ &&
  299. oid->oid[1] == 2 /* member-body */ &&
  300. oid->oid[2] == 840 /* us */ &&
  301. oid->oid[3] == 113549 /* rsadsi */ &&
  302. oid->oid[4] == 1 /* pkcs */ &&
  303. oid->oid[5] == 7 /* pkcs-7 */;
  304. }
  305. static int is_oid_pkcs7_data(struct asn1_oid *oid)
  306. {
  307. return is_oid_pkcs7(oid) && oid->oid[6] == 1 /* data */;
  308. }
  309. static int is_oid_pkcs7_enc_data(struct asn1_oid *oid)
  310. {
  311. return is_oid_pkcs7(oid) && oid->oid[6] == 6 /* encryptedData */;
  312. }
  313. static int is_oid_pkcs9(struct asn1_oid *oid)
  314. {
  315. return oid->len >= 6 &&
  316. oid->oid[0] == 1 /* iso */ &&
  317. oid->oid[1] == 2 /* member-body */ &&
  318. oid->oid[2] == 840 /* us */ &&
  319. oid->oid[3] == 113549 /* rsadsi */ &&
  320. oid->oid[4] == 1 /* pkcs */ &&
  321. oid->oid[5] == 9 /* pkcs-9 */;
  322. }
  323. static int is_oid_pkcs9_friendly_name(struct asn1_oid *oid)
  324. {
  325. return oid->len == 7 && is_oid_pkcs9(oid) &&
  326. oid->oid[6] == 20;
  327. }
  328. static int is_oid_pkcs9_local_key_id(struct asn1_oid *oid)
  329. {
  330. return oid->len == 7 && is_oid_pkcs9(oid) &&
  331. oid->oid[6] == 21;
  332. }
  333. static int is_oid_pkcs9_x509_cert(struct asn1_oid *oid)
  334. {
  335. return oid->len == 8 && is_oid_pkcs9(oid) &&
  336. oid->oid[6] == 22 /* certTypes */ &&
  337. oid->oid[7] == 1 /* x509Certificate */;
  338. }
  339. static int pkcs12_keybag(struct tlsv1_credentials *cred,
  340. const u8 *buf, size_t len)
  341. {
  342. /* TODO */
  343. return 0;
  344. }
  345. static int pkcs12_pkcs8_keybag(struct tlsv1_credentials *cred,
  346. const u8 *buf, size_t len,
  347. const char *passwd)
  348. {
  349. struct crypto_private_key *key;
  350. /* PKCS8ShroudedKeyBag ::= EncryptedPrivateKeyInfo */
  351. key = pkcs8_enc_key_import(buf, len, passwd);
  352. if (!key)
  353. return -1;
  354. wpa_printf(MSG_DEBUG,
  355. "PKCS #12: Successfully decrypted PKCS8ShroudedKeyBag");
  356. crypto_private_key_free(cred->key);
  357. cred->key = key;
  358. return 0;
  359. }
  360. static int pkcs12_certbag(struct tlsv1_credentials *cred,
  361. const u8 *buf, size_t len)
  362. {
  363. struct asn1_hdr hdr;
  364. struct asn1_oid oid;
  365. char obuf[80];
  366. const u8 *pos, *end;
  367. /*
  368. * CertBag ::= SEQUENCE {
  369. * certId BAG-TYPE.&id ({CertTypes}),
  370. * certValue [0] EXPLICIT BAG-TYPE.&Type ({CertTypes}{@certId})
  371. * }
  372. */
  373. if (asn1_get_next(buf, len, &hdr) < 0 ||
  374. hdr.class != ASN1_CLASS_UNIVERSAL ||
  375. hdr.tag != ASN1_TAG_SEQUENCE) {
  376. wpa_printf(MSG_DEBUG,
  377. "PKCS #12: Expected SEQUENCE (CertBag) - found class %d tag 0x%x",
  378. hdr.class, hdr.tag);
  379. return -1;
  380. }
  381. pos = hdr.payload;
  382. end = hdr.payload + hdr.length;
  383. if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
  384. wpa_printf(MSG_DEBUG,
  385. "PKCS #12: Failed to parse OID (certId)");
  386. return -1;
  387. }
  388. asn1_oid_to_str(&oid, obuf, sizeof(obuf));
  389. wpa_printf(MSG_DEBUG, "PKCS #12: certId %s", obuf);
  390. if (!is_oid_pkcs9_x509_cert(&oid)) {
  391. wpa_printf(MSG_DEBUG,
  392. "PKCS #12: Ignored unsupported certificate type (certId %s)",
  393. obuf);
  394. }
  395. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  396. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC ||
  397. hdr.tag != 0) {
  398. wpa_printf(MSG_DEBUG,
  399. "PKCS #12: Expected [0] EXPLICIT (certValue) - found class %d tag 0x%x",
  400. hdr.class, hdr.tag);
  401. return -1;
  402. }
  403. if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
  404. hdr.class != ASN1_CLASS_UNIVERSAL ||
  405. hdr.tag != ASN1_TAG_OCTETSTRING) {
  406. wpa_printf(MSG_DEBUG,
  407. "PKCS #12: Expected OCTET STRING (x509Certificate) - found class %d tag 0x%x",
  408. hdr.class, hdr.tag);
  409. return -1;
  410. }
  411. wpa_hexdump(MSG_DEBUG, "PKCS #12: x509Certificate",
  412. hdr.payload, hdr.length);
  413. if (cred->cert) {
  414. struct x509_certificate *cert;
  415. wpa_printf(MSG_DEBUG, "PKCS #12: Ignore extra certificate");
  416. cert = x509_certificate_parse(hdr.payload, hdr.length);
  417. if (!cert) {
  418. wpa_printf(MSG_DEBUG,
  419. "PKCS #12: Failed to parse x509Certificate");
  420. return 0;
  421. }
  422. x509_certificate_chain_free(cert);
  423. return 0;
  424. }
  425. return tlsv1_set_cert(cred, NULL, hdr.payload, hdr.length);
  426. }
  427. static int pkcs12_parse_attr_friendly_name(const u8 *pos, const u8 *end)
  428. {
  429. struct asn1_hdr hdr;
  430. /*
  431. * RFC 2985, 5.5.1:
  432. * friendlyName ATTRIBUTE ::= {
  433. * WITH SYNTAX BMPString (SIZE(1..pkcs-9-ub-friendlyName))
  434. * EQUALITY MATCHING RULE caseIgnoreMatch
  435. * SINGLE VALUE TRUE
  436. * ID pkcs-9-at-friendlyName
  437. * }
  438. */
  439. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  440. hdr.class != ASN1_CLASS_UNIVERSAL ||
  441. hdr.tag != ASN1_TAG_BMPSTRING) {
  442. wpa_printf(MSG_DEBUG,
  443. "PKCS #12: Expected BMPSTRING (friendlyName) - found class %d tag 0x%x",
  444. hdr.class, hdr.tag);
  445. return 0;
  446. }
  447. wpa_hexdump_ascii(MSG_DEBUG, "PKCS #12: friendlyName",
  448. hdr.payload, hdr.length);
  449. return 0;
  450. }
  451. static int pkcs12_parse_attr_local_key_id(const u8 *pos, const u8 *end)
  452. {
  453. struct asn1_hdr hdr;
  454. /*
  455. * RFC 2985, 5.5.2:
  456. * localKeyId ATTRIBUTE ::= {
  457. * WITH SYNTAX OCTET STRING
  458. * EQUALITY MATCHING RULE octetStringMatch
  459. * SINGLE VALUE TRUE
  460. * ID pkcs-9-at-localKeyId
  461. * }
  462. */
  463. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  464. hdr.class != ASN1_CLASS_UNIVERSAL ||
  465. hdr.tag != ASN1_TAG_OCTETSTRING) {
  466. wpa_printf(MSG_DEBUG,
  467. "PKCS #12: Expected OCTET STRING (localKeyID) - found class %d tag 0x%x",
  468. hdr.class, hdr.tag);
  469. return -1;
  470. }
  471. wpa_hexdump_key(MSG_DEBUG, "PKCS #12: localKeyID",
  472. hdr.payload, hdr.length);
  473. return 0;
  474. }
  475. static int pkcs12_parse_attr(const u8 *pos, size_t len)
  476. {
  477. const u8 *end = pos + len;
  478. struct asn1_hdr hdr;
  479. struct asn1_oid a_oid;
  480. char obuf[80];
  481. /*
  482. * PKCS12Attribute ::= SEQUENCE {
  483. * attrId ATTRIBUTE.&id ({PKCS12AttrSet}),
  484. * attrValues SET OF ATTRIBUTE.&Type ({PKCS12AttrSet}{@attrId})
  485. * }
  486. */
  487. if (asn1_get_oid(pos, end - pos, &a_oid, &pos)) {
  488. wpa_printf(MSG_DEBUG, "PKCS #12: Failed to parse OID (attrId)");
  489. return -1;
  490. }
  491. asn1_oid_to_str(&a_oid, obuf, sizeof(obuf));
  492. wpa_printf(MSG_DEBUG, "PKCS #12: attrId %s", obuf);
  493. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  494. hdr.class != ASN1_CLASS_UNIVERSAL ||
  495. hdr.tag != ASN1_TAG_SET) {
  496. wpa_printf(MSG_DEBUG,
  497. "PKCS #12: Expected SET (attrValues) - found class %d tag 0x%x",
  498. hdr.class, hdr.tag);
  499. return -1;
  500. }
  501. wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: attrValues",
  502. hdr.payload, hdr.length);
  503. pos = hdr.payload;
  504. end = hdr.payload + hdr.length;
  505. if (is_oid_pkcs9_friendly_name(&a_oid))
  506. return pkcs12_parse_attr_friendly_name(pos, end);
  507. if (is_oid_pkcs9_local_key_id(&a_oid))
  508. return pkcs12_parse_attr_local_key_id(pos, end);
  509. wpa_printf(MSG_DEBUG, "PKCS #12: Ignore unknown attribute");
  510. return 0;
  511. }
  512. static int pkcs12_safebag(struct tlsv1_credentials *cred,
  513. const u8 *buf, size_t len, const char *passwd)
  514. {
  515. struct asn1_hdr hdr;
  516. struct asn1_oid oid;
  517. char obuf[80];
  518. const u8 *pos = buf, *end = buf + len;
  519. const u8 *value;
  520. size_t value_len;
  521. wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: SafeBag", buf, len);
  522. /* BAG-TYPE ::= TYPE-IDENTIFIER */
  523. if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
  524. wpa_printf(MSG_DEBUG,
  525. "PKCS #12: Failed to parse OID (BAG-TYPE)");
  526. return -1;
  527. }
  528. asn1_oid_to_str(&oid, obuf, sizeof(obuf));
  529. wpa_printf(MSG_DEBUG, "PKCS #12: BAG-TYPE %s", obuf);
  530. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  531. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC ||
  532. hdr.tag != 0) {
  533. wpa_printf(MSG_DEBUG,
  534. "PKCS #12: Expected [0] EXPLICIT (bagValue) - found class %d tag 0x%x",
  535. hdr.class, hdr.tag);
  536. return 0;
  537. }
  538. value = hdr.payload;
  539. value_len = hdr.length;
  540. wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: bagValue", value, value_len);
  541. pos = hdr.payload + hdr.length;
  542. if (pos < end) {
  543. /* bagAttributes SET OF PKCS12Attribute OPTIONAL */
  544. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  545. hdr.class != ASN1_CLASS_UNIVERSAL ||
  546. hdr.tag != ASN1_TAG_SET) {
  547. wpa_printf(MSG_DEBUG,
  548. "PKCS #12: Expected SET (bagAttributes) - found class %d tag 0x%x",
  549. hdr.class, hdr.tag);
  550. return -1;
  551. }
  552. wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: bagAttributes",
  553. hdr.payload, hdr.length);
  554. pos = hdr.payload;
  555. end = hdr.payload + hdr.length;
  556. while (pos < end) {
  557. /* PKCS12Attribute ::= SEQUENCE */
  558. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  559. hdr.class != ASN1_CLASS_UNIVERSAL ||
  560. hdr.tag != ASN1_TAG_SEQUENCE) {
  561. wpa_printf(MSG_DEBUG,
  562. "PKCS #12: Expected SEQUENCE (PKCS12Attribute) - found class %d tag 0x%x",
  563. hdr.class, hdr.tag);
  564. return -1;
  565. }
  566. if (pkcs12_parse_attr(hdr.payload, hdr.length) < 0)
  567. return -1;
  568. pos = hdr.payload + hdr.length;
  569. }
  570. }
  571. if (pkcs12_is_bagtype_oid(&oid, 1))
  572. return pkcs12_keybag(cred, value, value_len);
  573. if (pkcs12_is_bagtype_oid(&oid, 2))
  574. return pkcs12_pkcs8_keybag(cred, value, value_len, passwd);
  575. if (pkcs12_is_bagtype_oid(&oid, 3))
  576. return pkcs12_certbag(cred, value, value_len);
  577. wpa_printf(MSG_DEBUG, "PKCS #12: Ignore unsupported BAG-TYPE");
  578. return 0;
  579. }
  580. static int pkcs12_safecontents(struct tlsv1_credentials *cred,
  581. const u8 *buf, size_t len,
  582. const char *passwd)
  583. {
  584. struct asn1_hdr hdr;
  585. const u8 *pos, *end;
  586. /* SafeContents ::= SEQUENCE OF SafeBag */
  587. if (asn1_get_next(buf, len, &hdr) < 0 ||
  588. hdr.class != ASN1_CLASS_UNIVERSAL ||
  589. hdr.tag != ASN1_TAG_SEQUENCE) {
  590. wpa_printf(MSG_DEBUG,
  591. "PKCS #12: Expected SEQUENCE (SafeContents) - found class %d tag 0x%x",
  592. hdr.class, hdr.tag);
  593. return -1;
  594. }
  595. pos = hdr.payload;
  596. end = hdr.payload + hdr.length;
  597. /*
  598. * SafeBag ::= SEQUENCE {
  599. * bagId BAG-TYPE.&id ({PKCS12BagSet})
  600. * bagValue [0] EXPLICIT BAG-TYPE.&Type({PKCS12BagSet}{@bagId}),
  601. * bagAttributes SET OF PKCS12Attribute OPTIONAL
  602. * }
  603. */
  604. while (pos < end) {
  605. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  606. hdr.class != ASN1_CLASS_UNIVERSAL ||
  607. hdr.tag != ASN1_TAG_SEQUENCE) {
  608. wpa_printf(MSG_DEBUG,
  609. "PKCS #12: Expected SEQUENCE (SafeBag) - found class %d tag 0x%x",
  610. hdr.class, hdr.tag);
  611. return -1;
  612. }
  613. if (pkcs12_safebag(cred, hdr.payload, hdr.length, passwd) < 0)
  614. return -1;
  615. pos = hdr.payload + hdr.length;
  616. }
  617. return 0;
  618. }
  619. static int pkcs12_parse_content_data(struct tlsv1_credentials *cred,
  620. const u8 *pos, const u8 *end,
  621. const char *passwd)
  622. {
  623. struct asn1_hdr hdr;
  624. /* Data ::= OCTET STRING */
  625. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  626. hdr.class != ASN1_CLASS_UNIVERSAL ||
  627. hdr.tag != ASN1_TAG_OCTETSTRING) {
  628. wpa_printf(MSG_DEBUG,
  629. "PKCS #12: Expected OCTET STRING (Data) - found class %d tag 0x%x",
  630. hdr.class, hdr.tag);
  631. return -1;
  632. }
  633. wpa_hexdump(MSG_MSGDUMP, "PKCS #12: Data", hdr.payload, hdr.length);
  634. return pkcs12_safecontents(cred, hdr.payload, hdr.length, passwd);
  635. }
  636. static int pkcs12_parse_content_enc_data(struct tlsv1_credentials *cred,
  637. const u8 *pos, const u8 *end,
  638. const char *passwd)
  639. {
  640. struct asn1_hdr hdr;
  641. struct asn1_oid oid;
  642. char buf[80];
  643. const u8 *enc_alg;
  644. u8 *data;
  645. size_t enc_alg_len, data_len;
  646. int res = -1;
  647. /*
  648. * EncryptedData ::= SEQUENCE {
  649. * version Version,
  650. * encryptedContentInfo EncryptedContentInfo }
  651. */
  652. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  653. hdr.class != ASN1_CLASS_UNIVERSAL ||
  654. hdr.tag != ASN1_TAG_SEQUENCE) {
  655. wpa_printf(MSG_DEBUG,
  656. "PKCS #12: Expected SEQUENCE (EncryptedData) - found class %d tag 0x%x",
  657. hdr.class, hdr.tag);
  658. return 0;
  659. }
  660. pos = hdr.payload;
  661. /* Version ::= INTEGER */
  662. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  663. hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
  664. wpa_printf(MSG_DEBUG,
  665. "PKCS #12: No INTEGER tag found for version; class=%d tag=0x%x",
  666. hdr.class, hdr.tag);
  667. return -1;
  668. }
  669. if (hdr.length != 1 || hdr.payload[0] != 0) {
  670. wpa_printf(MSG_DEBUG, "PKCS #12: Unrecognized PKCS #7 version");
  671. return -1;
  672. }
  673. pos = hdr.payload + hdr.length;
  674. wpa_hexdump(MSG_MSGDUMP, "PKCS #12: EncryptedContentInfo",
  675. pos, end - pos);
  676. /*
  677. * EncryptedContentInfo ::= SEQUENCE {
  678. * contentType ContentType,
  679. * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
  680. * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL }
  681. */
  682. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  683. hdr.class != ASN1_CLASS_UNIVERSAL ||
  684. hdr.tag != ASN1_TAG_SEQUENCE) {
  685. wpa_printf(MSG_DEBUG,
  686. "PKCS #12: Expected SEQUENCE (EncryptedContentInfo) - found class %d tag 0x%x",
  687. hdr.class, hdr.tag);
  688. return -1;
  689. }
  690. pos = hdr.payload;
  691. end = pos + hdr.length;
  692. /* ContentType ::= OBJECT IDENTIFIER */
  693. if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
  694. wpa_printf(MSG_DEBUG,
  695. "PKCS #12: Could not find OBJECT IDENTIFIER (contentType)");
  696. return -1;
  697. }
  698. asn1_oid_to_str(&oid, buf, sizeof(buf));
  699. wpa_printf(MSG_DEBUG, "PKCS #12: EncryptedContentInfo::contentType %s",
  700. buf);
  701. if (!is_oid_pkcs7_data(&oid)) {
  702. wpa_printf(MSG_DEBUG,
  703. "PKCS #12: Unsupported EncryptedContentInfo::contentType %s",
  704. buf);
  705. return 0;
  706. }
  707. /* ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier */
  708. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  709. hdr.class != ASN1_CLASS_UNIVERSAL ||
  710. hdr.tag != ASN1_TAG_SEQUENCE) {
  711. wpa_printf(MSG_DEBUG, "PKCS #12: Expected SEQUENCE (ContentEncryptionAlgorithmIdentifier) - found class %d tag 0x%x",
  712. hdr.class, hdr.tag);
  713. return -1;
  714. }
  715. enc_alg = hdr.payload;
  716. enc_alg_len = hdr.length;
  717. pos = hdr.payload + hdr.length;
  718. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  719. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC ||
  720. hdr.tag != 0) {
  721. wpa_printf(MSG_DEBUG,
  722. "PKCS #12: Expected [0] IMPLICIT (encryptedContent) - found class %d tag 0x%x",
  723. hdr.class, hdr.tag);
  724. return -1;
  725. }
  726. /* EncryptedContent ::= OCTET STRING */
  727. data = pkcs5_decrypt(enc_alg, enc_alg_len, hdr.payload, hdr.length,
  728. passwd, &data_len);
  729. if (data) {
  730. wpa_hexdump_key(MSG_MSGDUMP,
  731. "PKCS #12: Decrypted encryptedContent",
  732. data, data_len);
  733. res = pkcs12_safecontents(cred, data, data_len, passwd);
  734. os_free(data);
  735. }
  736. return res;
  737. }
  738. static int pkcs12_parse_content(struct tlsv1_credentials *cred,
  739. const u8 *buf, size_t len,
  740. const char *passwd)
  741. {
  742. const u8 *pos = buf;
  743. const u8 *end = buf + len;
  744. struct asn1_oid oid;
  745. char txt[80];
  746. struct asn1_hdr hdr;
  747. wpa_hexdump(MSG_MSGDUMP, "PKCS #12: ContentInfo", buf, len);
  748. if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
  749. wpa_printf(MSG_DEBUG,
  750. "PKCS #12: Could not find OBJECT IDENTIFIER (contentType)");
  751. return 0;
  752. }
  753. asn1_oid_to_str(&oid, txt, sizeof(txt));
  754. wpa_printf(MSG_DEBUG, "PKCS #12: contentType %s", txt);
  755. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  756. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC ||
  757. hdr.tag != 0) {
  758. wpa_printf(MSG_DEBUG,
  759. "PKCS #12: Expected [0] EXPLICIT (content) - found class %d tag 0x%x",
  760. hdr.class, hdr.tag);
  761. return 0;
  762. }
  763. pos = hdr.payload;
  764. if (is_oid_pkcs7_data(&oid))
  765. return pkcs12_parse_content_data(cred, pos, end, passwd);
  766. if (is_oid_pkcs7_enc_data(&oid))
  767. return pkcs12_parse_content_enc_data(cred, pos, end, passwd);
  768. wpa_printf(MSG_DEBUG, "PKCS #12: Ignored unsupported contentType %s",
  769. txt);
  770. return 0;
  771. }
  772. static int pkcs12_parse(struct tlsv1_credentials *cred,
  773. const u8 *key, size_t len, const char *passwd)
  774. {
  775. struct asn1_hdr hdr;
  776. const u8 *pos, *end;
  777. struct asn1_oid oid;
  778. char buf[80];
  779. /*
  780. * PFX ::= SEQUENCE {
  781. * version INTEGER {v3(3)}(v3,...),
  782. * authSafe ContentInfo,
  783. * macData MacData OPTIONAL
  784. * }
  785. */
  786. if (asn1_get_next(key, len, &hdr) < 0 ||
  787. hdr.class != ASN1_CLASS_UNIVERSAL ||
  788. hdr.tag != ASN1_TAG_SEQUENCE) {
  789. wpa_printf(MSG_DEBUG,
  790. "PKCS #12: Expected SEQUENCE (PFX) - found class %d tag 0x%x; assume PKCS #12 not used",
  791. hdr.class, hdr.tag);
  792. return -1;
  793. }
  794. pos = hdr.payload;
  795. end = pos + hdr.length;
  796. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  797. hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
  798. wpa_printf(MSG_DEBUG,
  799. "PKCS #12: No INTEGER tag found for version; class=%d tag=0x%x",
  800. hdr.class, hdr.tag);
  801. return -1;
  802. }
  803. if (hdr.length != 1 || hdr.payload[0] != 3) {
  804. wpa_printf(MSG_DEBUG, "PKCS #12: Unrecognized version");
  805. return -1;
  806. }
  807. pos = hdr.payload + hdr.length;
  808. /*
  809. * ContentInfo ::= SEQUENCE {
  810. * contentType ContentType,
  811. * content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
  812. */
  813. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  814. hdr.class != ASN1_CLASS_UNIVERSAL ||
  815. hdr.tag != ASN1_TAG_SEQUENCE) {
  816. wpa_printf(MSG_DEBUG,
  817. "PKCS #12: Expected SEQUENCE (authSafe) - found class %d tag 0x%x; assume PKCS #12 not used",
  818. hdr.class, hdr.tag);
  819. return -1;
  820. }
  821. pos = hdr.payload;
  822. end = pos + hdr.length;
  823. /* ContentType ::= OBJECT IDENTIFIER */
  824. if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
  825. wpa_printf(MSG_DEBUG,
  826. "PKCS #12: Could not find OBJECT IDENTIFIER (contentType); assume PKCS #12 not used");
  827. return -1;
  828. }
  829. asn1_oid_to_str(&oid, buf, sizeof(buf));
  830. wpa_printf(MSG_DEBUG, "PKCS #12: contentType %s", buf);
  831. if (!is_oid_pkcs7_data(&oid)) {
  832. wpa_printf(MSG_DEBUG, "PKCS #12: Unsupported contentType %s",
  833. buf);
  834. return -1;
  835. }
  836. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  837. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC ||
  838. hdr.tag != 0) {
  839. wpa_printf(MSG_DEBUG,
  840. "PKCS #12: Expected [0] EXPLICIT (content) - found class %d tag 0x%x; assume PKCS #12 not used",
  841. hdr.class, hdr.tag);
  842. return -1;
  843. }
  844. pos = hdr.payload;
  845. /* Data ::= OCTET STRING */
  846. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  847. hdr.class != ASN1_CLASS_UNIVERSAL ||
  848. hdr.tag != ASN1_TAG_OCTETSTRING) {
  849. wpa_printf(MSG_DEBUG,
  850. "PKCS #12: Expected OCTET STRING (Data) - found class %d tag 0x%x; assume PKCS #12 not used",
  851. hdr.class, hdr.tag);
  852. return -1;
  853. }
  854. /*
  855. * AuthenticatedSafe ::= SEQUENCE OF ContentInfo
  856. * -- Data if unencrypted
  857. * -- EncryptedData if password-encrypted
  858. * -- EnvelopedData if public key-encrypted
  859. */
  860. wpa_hexdump(MSG_MSGDUMP, "PKCS #12: Data content",
  861. hdr.payload, hdr.length);
  862. if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
  863. hdr.class != ASN1_CLASS_UNIVERSAL ||
  864. hdr.tag != ASN1_TAG_SEQUENCE) {
  865. wpa_printf(MSG_DEBUG,
  866. "PKCS #12: Expected SEQUENCE within Data content - found class %d tag 0x%x; assume PKCS #12 not used",
  867. hdr.class, hdr.tag);
  868. return -1;
  869. }
  870. pos = hdr.payload;
  871. end = pos + hdr.length;
  872. while (end > pos) {
  873. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  874. hdr.class != ASN1_CLASS_UNIVERSAL ||
  875. hdr.tag != ASN1_TAG_SEQUENCE) {
  876. wpa_printf(MSG_DEBUG,
  877. "PKCS #12: Expected SEQUENCE (ContentInfo) - found class %d tag 0x%x; assume PKCS #12 not used",
  878. hdr.class, hdr.tag);
  879. return -1;
  880. }
  881. if (pkcs12_parse_content(cred, hdr.payload, hdr.length,
  882. passwd) < 0)
  883. return -1;
  884. pos = hdr.payload + hdr.length;
  885. }
  886. return 0;
  887. }
  888. #endif /* PKCS12_FUNCS */
  889. static int tlsv1_set_key(struct tlsv1_credentials *cred,
  890. const u8 *key, size_t len, const char *passwd)
  891. {
  892. cred->key = crypto_private_key_import(key, len, passwd);
  893. if (cred->key == NULL)
  894. cred->key = tlsv1_set_key_pem(key, len);
  895. if (cred->key == NULL)
  896. cred->key = tlsv1_set_key_enc_pem(key, len, passwd);
  897. #ifdef PKCS12_FUNCS
  898. if (!cred->key)
  899. pkcs12_parse(cred, key, len, passwd);
  900. #endif /* PKCS12_FUNCS */
  901. if (cred->key == NULL) {
  902. wpa_printf(MSG_INFO, "TLSv1: Failed to parse private key");
  903. return -1;
  904. }
  905. return 0;
  906. }
  907. /**
  908. * tlsv1_set_private_key - Set private key
  909. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  910. * @private_key: File or reference name for the key in PEM or DER format
  911. * @private_key_passwd: Passphrase for decrypted private key, %NULL if no
  912. * passphrase is used.
  913. * @private_key_blob: private_key as inlined data or %NULL if not used
  914. * @private_key_blob_len: private_key_blob length
  915. * Returns: 0 on success, -1 on failure
  916. */
  917. int tlsv1_set_private_key(struct tlsv1_credentials *cred,
  918. const char *private_key,
  919. const char *private_key_passwd,
  920. const u8 *private_key_blob,
  921. size_t private_key_blob_len)
  922. {
  923. crypto_private_key_free(cred->key);
  924. cred->key = NULL;
  925. if (private_key_blob)
  926. return tlsv1_set_key(cred, private_key_blob,
  927. private_key_blob_len,
  928. private_key_passwd);
  929. if (private_key) {
  930. u8 *buf;
  931. size_t len;
  932. int ret;
  933. buf = (u8 *) os_readfile(private_key, &len);
  934. if (buf == NULL) {
  935. wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
  936. private_key);
  937. return -1;
  938. }
  939. ret = tlsv1_set_key(cred, buf, len, private_key_passwd);
  940. os_free(buf);
  941. return ret;
  942. }
  943. return 0;
  944. }
  945. static int tlsv1_set_dhparams_der(struct tlsv1_credentials *cred,
  946. const u8 *dh, size_t len)
  947. {
  948. struct asn1_hdr hdr;
  949. const u8 *pos, *end;
  950. pos = dh;
  951. end = dh + len;
  952. /*
  953. * DHParameter ::= SEQUENCE {
  954. * prime INTEGER, -- p
  955. * base INTEGER, -- g
  956. * privateValueLength INTEGER OPTIONAL }
  957. */
  958. /* DHParamer ::= SEQUENCE */
  959. if (asn1_get_next(pos, len, &hdr) < 0 ||
  960. hdr.class != ASN1_CLASS_UNIVERSAL ||
  961. hdr.tag != ASN1_TAG_SEQUENCE) {
  962. wpa_printf(MSG_DEBUG, "DH: DH parameters did not start with a "
  963. "valid SEQUENCE - found class %d tag 0x%x",
  964. hdr.class, hdr.tag);
  965. return -1;
  966. }
  967. pos = hdr.payload;
  968. /* prime INTEGER */
  969. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  970. return -1;
  971. if (hdr.class != ASN1_CLASS_UNIVERSAL ||
  972. hdr.tag != ASN1_TAG_INTEGER) {
  973. wpa_printf(MSG_DEBUG, "DH: No INTEGER tag found for p; "
  974. "class=%d tag=0x%x", hdr.class, hdr.tag);
  975. return -1;
  976. }
  977. wpa_hexdump(MSG_MSGDUMP, "DH: prime (p)", hdr.payload, hdr.length);
  978. if (hdr.length == 0)
  979. return -1;
  980. os_free(cred->dh_p);
  981. cred->dh_p = os_malloc(hdr.length);
  982. if (cred->dh_p == NULL)
  983. return -1;
  984. os_memcpy(cred->dh_p, hdr.payload, hdr.length);
  985. cred->dh_p_len = hdr.length;
  986. pos = hdr.payload + hdr.length;
  987. /* base INTEGER */
  988. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  989. return -1;
  990. if (hdr.class != ASN1_CLASS_UNIVERSAL ||
  991. hdr.tag != ASN1_TAG_INTEGER) {
  992. wpa_printf(MSG_DEBUG, "DH: No INTEGER tag found for g; "
  993. "class=%d tag=0x%x", hdr.class, hdr.tag);
  994. return -1;
  995. }
  996. wpa_hexdump(MSG_MSGDUMP, "DH: base (g)", hdr.payload, hdr.length);
  997. if (hdr.length == 0)
  998. return -1;
  999. os_free(cred->dh_g);
  1000. cred->dh_g = os_malloc(hdr.length);
  1001. if (cred->dh_g == NULL)
  1002. return -1;
  1003. os_memcpy(cred->dh_g, hdr.payload, hdr.length);
  1004. cred->dh_g_len = hdr.length;
  1005. return 0;
  1006. }
  1007. static const char *pem_dhparams_begin = "-----BEGIN DH PARAMETERS-----";
  1008. static const char *pem_dhparams_end = "-----END DH PARAMETERS-----";
  1009. static int tlsv1_set_dhparams_blob(struct tlsv1_credentials *cred,
  1010. const u8 *buf, size_t len)
  1011. {
  1012. const u8 *pos, *end;
  1013. unsigned char *der;
  1014. size_t der_len;
  1015. pos = search_tag(pem_dhparams_begin, buf, len);
  1016. if (!pos) {
  1017. wpa_printf(MSG_DEBUG, "TLSv1: No PEM dhparams tag found - "
  1018. "assume DER format");
  1019. return tlsv1_set_dhparams_der(cred, buf, len);
  1020. }
  1021. wpa_printf(MSG_DEBUG, "TLSv1: Converting PEM format dhparams into DER "
  1022. "format");
  1023. pos += os_strlen(pem_dhparams_begin);
  1024. end = search_tag(pem_dhparams_end, pos, buf + len - pos);
  1025. if (end == NULL) {
  1026. wpa_printf(MSG_INFO, "TLSv1: Could not find PEM dhparams end "
  1027. "tag (%s)", pem_dhparams_end);
  1028. return -1;
  1029. }
  1030. der = base64_decode(pos, end - pos, &der_len);
  1031. if (der == NULL) {
  1032. wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM dhparams");
  1033. return -1;
  1034. }
  1035. if (tlsv1_set_dhparams_der(cred, der, der_len) < 0) {
  1036. wpa_printf(MSG_INFO, "TLSv1: Failed to parse PEM dhparams "
  1037. "DER conversion");
  1038. os_free(der);
  1039. return -1;
  1040. }
  1041. os_free(der);
  1042. return 0;
  1043. }
  1044. /**
  1045. * tlsv1_set_dhparams - Set Diffie-Hellman parameters
  1046. * @cred: TLSv1 credentials from tlsv1_cred_alloc()
  1047. * @dh_file: File or reference name for the DH params in PEM or DER format
  1048. * @dh_blob: DH params as inlined data or %NULL if not used
  1049. * @dh_blob_len: dh_blob length
  1050. * Returns: 0 on success, -1 on failure
  1051. */
  1052. int tlsv1_set_dhparams(struct tlsv1_credentials *cred, const char *dh_file,
  1053. const u8 *dh_blob, size_t dh_blob_len)
  1054. {
  1055. if (dh_blob)
  1056. return tlsv1_set_dhparams_blob(cred, dh_blob, dh_blob_len);
  1057. if (dh_file) {
  1058. u8 *buf;
  1059. size_t len;
  1060. int ret;
  1061. buf = (u8 *) os_readfile(dh_file, &len);
  1062. if (buf == NULL) {
  1063. wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
  1064. dh_file);
  1065. return -1;
  1066. }
  1067. ret = tlsv1_set_dhparams_blob(cred, buf, len);
  1068. os_free(buf);
  1069. return ret;
  1070. }
  1071. return 0;
  1072. }