PageRenderTime 78ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/usr/src/lib/libkmf/plugins/kmf_openssl/common/openssl_spi.c

https://bitbucket.org/a3217055/illumos-gate
C | 5579 lines | 4336 code | 798 blank | 445 comment | 1725 complexity | 87d81b820a778af910d92a235ab80a7a MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, LGPL-2.0, 0BSD, AGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, AGPL-1.0
  1. /*
  2. * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * Use is subject to license terms.
  5. */
  6. /*
  7. * Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  8. * project 2000.
  9. */
  10. /*
  11. * ====================================================================
  12. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * licensing@OpenSSL.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include <stdlib.h>
  65. #include <kmfapiP.h>
  66. #include <ber_der.h>
  67. #include <fcntl.h>
  68. #include <sys/stat.h>
  69. #include <dirent.h>
  70. #include <cryptoutil.h>
  71. #include <synch.h>
  72. #include <thread.h>
  73. /* OPENSSL related headers */
  74. #include <openssl/bio.h>
  75. #include <openssl/bn.h>
  76. #include <openssl/asn1.h>
  77. #include <openssl/err.h>
  78. #include <openssl/bn.h>
  79. #include <openssl/x509.h>
  80. #include <openssl/rsa.h>
  81. #include <openssl/dsa.h>
  82. #include <openssl/x509v3.h>
  83. #include <openssl/objects.h>
  84. #include <openssl/pem.h>
  85. #include <openssl/pkcs12.h>
  86. #include <openssl/ocsp.h>
  87. #include <openssl/des.h>
  88. #include <openssl/rand.h>
  89. #define PRINT_ANY_EXTENSION (\
  90. KMF_X509_EXT_KEY_USAGE |\
  91. KMF_X509_EXT_CERT_POLICIES |\
  92. KMF_X509_EXT_SUBJALTNAME |\
  93. KMF_X509_EXT_BASIC_CONSTRAINTS |\
  94. KMF_X509_EXT_NAME_CONSTRAINTS |\
  95. KMF_X509_EXT_POLICY_CONSTRAINTS |\
  96. KMF_X509_EXT_EXT_KEY_USAGE |\
  97. KMF_X509_EXT_INHIBIT_ANY_POLICY |\
  98. KMF_X509_EXT_AUTH_KEY_ID |\
  99. KMF_X509_EXT_SUBJ_KEY_ID |\
  100. KMF_X509_EXT_POLICY_MAPPING)
  101. static uchar_t P[] = { 0x00, 0x8d, 0xf2, 0xa4, 0x94, 0x49, 0x22, 0x76,
  102. 0xaa, 0x3d, 0x25, 0x75, 0x9b, 0xb0, 0x68, 0x69,
  103. 0xcb, 0xea, 0xc0, 0xd8, 0x3a, 0xfb, 0x8d, 0x0c,
  104. 0xf7, 0xcb, 0xb8, 0x32, 0x4f, 0x0d, 0x78, 0x82,
  105. 0xe5, 0xd0, 0x76, 0x2f, 0xc5, 0xb7, 0x21, 0x0e,
  106. 0xaf, 0xc2, 0xe9, 0xad, 0xac, 0x32, 0xab, 0x7a,
  107. 0xac, 0x49, 0x69, 0x3d, 0xfb, 0xf8, 0x37, 0x24,
  108. 0xc2, 0xec, 0x07, 0x36, 0xee, 0x31, 0xc8, 0x02,
  109. 0x91 };
  110. static uchar_t Q[] = { 0x00, 0xc7, 0x73, 0x21, 0x8c, 0x73, 0x7e, 0xc8,
  111. 0xee, 0x99, 0x3b, 0x4f, 0x2d, 0xed, 0x30, 0xf4,
  112. 0x8e, 0xda, 0xce, 0x91, 0x5f };
  113. static uchar_t G[] = { 0x00, 0x62, 0x6d, 0x02, 0x78, 0x39, 0xea, 0x0a,
  114. 0x13, 0x41, 0x31, 0x63, 0xa5, 0x5b, 0x4c, 0xb5,
  115. 0x00, 0x29, 0x9d, 0x55, 0x22, 0x95, 0x6c, 0xef,
  116. 0xcb, 0x3b, 0xff, 0x10, 0xf3, 0x99, 0xce, 0x2c,
  117. 0x2e, 0x71, 0xcb, 0x9d, 0xe5, 0xfa, 0x24, 0xba,
  118. 0xbf, 0x58, 0xe5, 0xb7, 0x95, 0x21, 0x92, 0x5c,
  119. 0x9c, 0xc4, 0x2e, 0x9f, 0x6f, 0x46, 0x4b, 0x08,
  120. 0x8c, 0xc5, 0x72, 0xaf, 0x53, 0xe6, 0xd7, 0x88,
  121. 0x02 };
  122. #define SET_ERROR(h, c) h->lasterr.kstype = KMF_KEYSTORE_OPENSSL; \
  123. h->lasterr.errcode = c;
  124. #define SET_SYS_ERROR(h, c) h->lasterr.kstype = -1; h->lasterr.errcode = c;
  125. /*
  126. * Declare some new macros for managing stacks of EVP_PKEYS, similar to
  127. * what wanboot did.
  128. */
  129. DECLARE_STACK_OF(EVP_PKEY)
  130. #define sk_EVP_PKEY_new_null() SKM_sk_new_null(EVP_PKEY)
  131. #define sk_EVP_PKEY_free(st) SKM_sk_free(EVP_PKEY, (st))
  132. #define sk_EVP_PKEY_num(st) SKM_sk_num(EVP_PKEY, (st))
  133. #define sk_EVP_PKEY_value(st, i) SKM_sk_value(EVP_PKEY, (st), (i))
  134. #define sk_EVP_PKEY_push(st, val) SKM_sk_push(EVP_PKEY, (st), (val))
  135. #define sk_EVP_PKEY_pop_free(st, free_func) SKM_sk_pop_free(EVP_PKEY, (st), \
  136. (free_func))
  137. mutex_t init_lock = DEFAULTMUTEX;
  138. static int ssl_initialized = 0;
  139. static BIO *bio_err = NULL;
  140. static int
  141. test_for_file(char *, mode_t);
  142. static KMF_RETURN
  143. openssl_parse_bag(PKCS12_SAFEBAG *, char *, int,
  144. STACK_OF(EVP_PKEY) *, STACK_OF(X509) *);
  145. static KMF_RETURN
  146. local_export_pk12(KMF_HANDLE_T, KMF_CREDENTIAL *, int, KMF_X509_DER_CERT *,
  147. int, KMF_KEY_HANDLE *, char *);
  148. static KMF_RETURN set_pkey_attrib(EVP_PKEY *, ASN1_TYPE *, int);
  149. static KMF_RETURN
  150. extract_pem(KMF_HANDLE *, char *, char *, KMF_BIGINT *, char *,
  151. CK_UTF8CHAR *, CK_ULONG, EVP_PKEY **, KMF_DATA **, int *);
  152. static KMF_RETURN
  153. kmf_load_cert(KMF_HANDLE *, char *, char *, KMF_BIGINT *, KMF_CERT_VALIDITY,
  154. char *, KMF_DATA *);
  155. static KMF_RETURN
  156. load_certs(KMF_HANDLE *, char *, char *, KMF_BIGINT *, KMF_CERT_VALIDITY,
  157. char *, KMF_DATA **, uint32_t *);
  158. static KMF_RETURN
  159. sslBN2KMFBN(BIGNUM *, KMF_BIGINT *);
  160. static EVP_PKEY *
  161. ImportRawRSAKey(KMF_RAW_RSA_KEY *);
  162. static KMF_RETURN
  163. convertToRawKey(EVP_PKEY *, KMF_RAW_KEY_DATA *);
  164. KMF_RETURN
  165. OpenSSL_FindCert(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  166. void
  167. OpenSSL_FreeKMFCert(KMF_HANDLE_T, KMF_X509_DER_CERT *);
  168. KMF_RETURN
  169. OpenSSL_StoreCert(KMF_HANDLE_T handle, int, KMF_ATTRIBUTE *);
  170. KMF_RETURN
  171. OpenSSL_DeleteCert(KMF_HANDLE_T handle, int, KMF_ATTRIBUTE *);
  172. KMF_RETURN
  173. OpenSSL_CreateKeypair(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  174. KMF_RETURN
  175. OpenSSL_StoreKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  176. KMF_RETURN
  177. OpenSSL_EncodePubKeyData(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_DATA *);
  178. KMF_RETURN
  179. OpenSSL_SignData(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_OID *,
  180. KMF_DATA *, KMF_DATA *);
  181. KMF_RETURN
  182. OpenSSL_DeleteKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  183. KMF_RETURN
  184. OpenSSL_ImportCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  185. KMF_RETURN
  186. OpenSSL_DeleteCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  187. KMF_RETURN
  188. OpenSSL_ListCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  189. KMF_RETURN
  190. OpenSSL_FindCertInCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  191. KMF_RETURN
  192. OpenSSL_CertGetPrintable(KMF_HANDLE_T, const KMF_DATA *,
  193. KMF_PRINTABLE_ITEM, char *);
  194. KMF_RETURN
  195. OpenSSL_GetErrorString(KMF_HANDLE_T, char **);
  196. KMF_RETURN
  197. OpenSSL_FindPrikeyByCert(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  198. KMF_RETURN
  199. OpenSSL_DecryptData(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_OID *,
  200. KMF_DATA *, KMF_DATA *);
  201. KMF_RETURN
  202. OpenSSL_CreateOCSPRequest(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  203. KMF_RETURN
  204. OpenSSL_GetOCSPStatusForCert(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  205. KMF_RETURN
  206. OpenSSL_FindKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  207. KMF_RETURN
  208. OpenSSL_ExportPK12(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  209. KMF_RETURN
  210. OpenSSL_CreateSymKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  211. KMF_RETURN
  212. OpenSSL_GetSymKeyValue(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_RAW_SYM_KEY *);
  213. KMF_RETURN
  214. OpenSSL_VerifyCRLFile(KMF_HANDLE_T, char *, KMF_DATA *);
  215. KMF_RETURN
  216. OpenSSL_CheckCRLDate(KMF_HANDLE_T, char *);
  217. static
  218. KMF_PLUGIN_FUNCLIST openssl_plugin_table =
  219. {
  220. 1, /* Version */
  221. NULL, /* ConfigureKeystore */
  222. OpenSSL_FindCert,
  223. OpenSSL_FreeKMFCert,
  224. OpenSSL_StoreCert,
  225. NULL, /* ImportCert */
  226. OpenSSL_ImportCRL,
  227. OpenSSL_DeleteCert,
  228. OpenSSL_DeleteCRL,
  229. OpenSSL_CreateKeypair,
  230. OpenSSL_FindKey,
  231. OpenSSL_EncodePubKeyData,
  232. OpenSSL_SignData,
  233. OpenSSL_DeleteKey,
  234. OpenSSL_ListCRL,
  235. NULL, /* FindCRL */
  236. OpenSSL_FindCertInCRL,
  237. OpenSSL_GetErrorString,
  238. OpenSSL_FindPrikeyByCert,
  239. OpenSSL_DecryptData,
  240. OpenSSL_ExportPK12,
  241. OpenSSL_CreateSymKey,
  242. OpenSSL_GetSymKeyValue,
  243. NULL, /* SetTokenPin */
  244. OpenSSL_StoreKey,
  245. NULL /* Finalize */
  246. };
  247. static mutex_t *lock_cs;
  248. static long *lock_count;
  249. static void
  250. /* ARGSUSED1 */
  251. locking_cb(int mode, int type, char *file, int line)
  252. {
  253. if (mode & CRYPTO_LOCK) {
  254. (void) mutex_lock(&(lock_cs[type]));
  255. lock_count[type]++;
  256. } else {
  257. (void) mutex_unlock(&(lock_cs[type]));
  258. }
  259. }
  260. static unsigned long
  261. thread_id()
  262. {
  263. return ((unsigned long)thr_self());
  264. }
  265. KMF_PLUGIN_FUNCLIST *
  266. KMF_Plugin_Initialize()
  267. {
  268. int i;
  269. (void) mutex_lock(&init_lock);
  270. if (!ssl_initialized) {
  271. /*
  272. * Add support for extension OIDs that are not yet in the
  273. * openssl default set.
  274. */
  275. (void) OBJ_create("2.5.29.30", "nameConstraints",
  276. "X509v3 Name Constraints");
  277. (void) OBJ_create("2.5.29.33", "policyMappings",
  278. "X509v3 Policy Mappings");
  279. (void) OBJ_create("2.5.29.36", "policyConstraints",
  280. "X509v3 Policy Constraints");
  281. (void) OBJ_create("2.5.29.46", "freshestCRL",
  282. "X509v3 Freshest CRL");
  283. (void) OBJ_create("2.5.29.54", "inhibitAnyPolicy",
  284. "X509v3 Inhibit Any-Policy");
  285. /*
  286. * Set up for thread-safe operation.
  287. */
  288. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof (mutex_t));
  289. if (lock_cs == NULL) {
  290. (void) mutex_unlock(&init_lock);
  291. return (NULL);
  292. }
  293. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof (long));
  294. if (lock_count == NULL) {
  295. OPENSSL_free(lock_cs);
  296. (void) mutex_unlock(&init_lock);
  297. return (NULL);
  298. }
  299. for (i = 0; i < CRYPTO_num_locks(); i++) {
  300. lock_count[i] = 0;
  301. (void) mutex_init(&lock_cs[i], USYNC_THREAD, NULL);
  302. }
  303. CRYPTO_set_id_callback((unsigned long (*)())thread_id);
  304. if (CRYPTO_get_locking_callback() == NULL)
  305. CRYPTO_set_locking_callback((void (*)())locking_cb);
  306. OpenSSL_add_all_algorithms();
  307. /* Enable error strings for reporting */
  308. ERR_load_crypto_strings();
  309. ssl_initialized = 1;
  310. }
  311. (void) mutex_unlock(&init_lock);
  312. return (&openssl_plugin_table);
  313. }
  314. /*
  315. * Convert an SSL DN to a KMF DN.
  316. */
  317. static KMF_RETURN
  318. get_x509_dn(X509_NAME *sslDN, KMF_X509_NAME *kmfDN)
  319. {
  320. KMF_DATA derdata;
  321. KMF_RETURN rv = KMF_OK;
  322. uchar_t *tmp;
  323. /* Convert to raw DER format */
  324. derdata.Length = i2d_X509_NAME(sslDN, NULL);
  325. if ((tmp = derdata.Data = (uchar_t *)OPENSSL_malloc(derdata.Length))
  326. == NULL) {
  327. return (KMF_ERR_MEMORY);
  328. }
  329. (void) i2d_X509_NAME(sslDN, &tmp);
  330. /* Decode to KMF format */
  331. rv = DerDecodeName(&derdata, kmfDN);
  332. if (rv != KMF_OK) {
  333. rv = KMF_ERR_BAD_CERT_FORMAT;
  334. }
  335. OPENSSL_free(derdata.Data);
  336. return (rv);
  337. }
  338. int
  339. isdir(char *path)
  340. {
  341. struct stat s;
  342. if (stat(path, &s) == -1)
  343. return (0);
  344. return ((s.st_mode & S_IFMT) == S_IFDIR);
  345. }
  346. static KMF_RETURN
  347. ssl_cert2KMFDATA(KMF_HANDLE *kmfh, X509 *x509cert, KMF_DATA *cert)
  348. {
  349. KMF_RETURN rv = KMF_OK;
  350. unsigned char *buf = NULL, *p;
  351. int len;
  352. /*
  353. * Convert the X509 internal struct to DER encoded data
  354. */
  355. if ((len = i2d_X509(x509cert, NULL)) < 0) {
  356. SET_ERROR(kmfh, ERR_get_error());
  357. rv = KMF_ERR_BAD_CERT_FORMAT;
  358. goto cleanup;
  359. }
  360. if ((buf = malloc(len)) == NULL) {
  361. SET_SYS_ERROR(kmfh, errno);
  362. rv = KMF_ERR_MEMORY;
  363. goto cleanup;
  364. }
  365. /*
  366. * i2d_X509 will increment the buf pointer so that we need to
  367. * save it.
  368. */
  369. p = buf;
  370. if ((len = i2d_X509(x509cert, &p)) < 0) {
  371. SET_ERROR(kmfh, ERR_get_error());
  372. free(buf);
  373. rv = KMF_ERR_BAD_CERT_FORMAT;
  374. goto cleanup;
  375. }
  376. /* caller's responsibility to free it */
  377. cert->Data = buf;
  378. cert->Length = len;
  379. cleanup:
  380. if (rv != KMF_OK) {
  381. if (buf)
  382. free(buf);
  383. cert->Data = NULL;
  384. cert->Length = 0;
  385. }
  386. return (rv);
  387. }
  388. static KMF_RETURN
  389. check_cert(X509 *xcert, char *issuer, char *subject, KMF_BIGINT *serial,
  390. boolean_t *match)
  391. {
  392. KMF_RETURN rv = KMF_OK;
  393. boolean_t findIssuer = FALSE;
  394. boolean_t findSubject = FALSE;
  395. boolean_t findSerial = FALSE;
  396. KMF_X509_NAME issuerDN, subjectDN;
  397. KMF_X509_NAME certIssuerDN, certSubjectDN;
  398. *match = FALSE;
  399. if (xcert == NULL) {
  400. return (KMF_ERR_BAD_PARAMETER);
  401. }
  402. (void) memset(&issuerDN, 0, sizeof (KMF_X509_NAME));
  403. (void) memset(&subjectDN, 0, sizeof (KMF_X509_NAME));
  404. (void) memset(&certIssuerDN, 0, sizeof (KMF_X509_NAME));
  405. (void) memset(&certSubjectDN, 0, sizeof (KMF_X509_NAME));
  406. if (issuer != NULL && strlen(issuer)) {
  407. rv = kmf_dn_parser(issuer, &issuerDN);
  408. if (rv != KMF_OK)
  409. return (KMF_ERR_BAD_PARAMETER);
  410. rv = get_x509_dn(xcert->cert_info->issuer, &certIssuerDN);
  411. if (rv != KMF_OK) {
  412. kmf_free_dn(&issuerDN);
  413. return (KMF_ERR_BAD_PARAMETER);
  414. }
  415. findIssuer = TRUE;
  416. }
  417. if (subject != NULL && strlen(subject)) {
  418. rv = kmf_dn_parser(subject, &subjectDN);
  419. if (rv != KMF_OK) {
  420. rv = KMF_ERR_BAD_PARAMETER;
  421. goto cleanup;
  422. }
  423. rv = get_x509_dn(xcert->cert_info->subject, &certSubjectDN);
  424. if (rv != KMF_OK) {
  425. rv = KMF_ERR_BAD_PARAMETER;
  426. goto cleanup;
  427. }
  428. findSubject = TRUE;
  429. }
  430. if (serial != NULL && serial->val != NULL)
  431. findSerial = TRUE;
  432. if (findSerial) {
  433. BIGNUM *bn;
  434. /* Comparing BIGNUMs is a pain! */
  435. bn = ASN1_INTEGER_to_BN(xcert->cert_info->serialNumber, NULL);
  436. if (bn != NULL) {
  437. int bnlen = BN_num_bytes(bn);
  438. if (bnlen == serial->len) {
  439. uchar_t *a = malloc(bnlen);
  440. if (a == NULL) {
  441. rv = KMF_ERR_MEMORY;
  442. BN_free(bn);
  443. goto cleanup;
  444. }
  445. bnlen = BN_bn2bin(bn, a);
  446. *match = (memcmp(a, serial->val, serial->len) ==
  447. 0);
  448. rv = KMF_OK;
  449. free(a);
  450. }
  451. BN_free(bn);
  452. if (!(*match))
  453. goto cleanup;
  454. } else {
  455. rv = KMF_OK;
  456. goto cleanup;
  457. }
  458. }
  459. if (findIssuer) {
  460. *match = (kmf_compare_rdns(&issuerDN, &certIssuerDN) == 0);
  461. if ((*match) == B_FALSE) {
  462. /* stop checking and bail */
  463. rv = KMF_OK;
  464. goto cleanup;
  465. }
  466. }
  467. if (findSubject) {
  468. *match = (kmf_compare_rdns(&subjectDN, &certSubjectDN) == 0);
  469. if ((*match) == B_FALSE) {
  470. /* stop checking and bail */
  471. rv = KMF_OK;
  472. goto cleanup;
  473. }
  474. }
  475. *match = TRUE;
  476. cleanup:
  477. if (findIssuer) {
  478. kmf_free_dn(&issuerDN);
  479. kmf_free_dn(&certIssuerDN);
  480. }
  481. if (findSubject) {
  482. kmf_free_dn(&subjectDN);
  483. kmf_free_dn(&certSubjectDN);
  484. }
  485. return (rv);
  486. }
  487. /*
  488. * This function loads a certificate file into an X509 data structure, and
  489. * checks if its issuer, subject or the serial number matches with those
  490. * values. If it matches, then return the X509 data structure.
  491. */
  492. static KMF_RETURN
  493. load_X509cert(KMF_HANDLE *kmfh,
  494. char *issuer, char *subject, KMF_BIGINT *serial,
  495. char *pathname, X509 **outcert)
  496. {
  497. KMF_RETURN rv = KMF_OK;
  498. X509 *xcert = NULL;
  499. BIO *bcert = NULL;
  500. boolean_t match = FALSE;
  501. KMF_ENCODE_FORMAT format;
  502. /*
  503. * auto-detect the file format, regardless of what
  504. * the 'format' parameters in the params say.
  505. */
  506. rv = kmf_get_file_format(pathname, &format);
  507. if (rv != KMF_OK) {
  508. if (rv == KMF_ERR_OPEN_FILE)
  509. rv = KMF_ERR_CERT_NOT_FOUND;
  510. return (rv);
  511. }
  512. /* Not ASN1(DER) format */
  513. if ((bcert = BIO_new_file(pathname, "rb")) == NULL) {
  514. SET_ERROR(kmfh, ERR_get_error());
  515. rv = KMF_ERR_OPEN_FILE;
  516. goto cleanup;
  517. }
  518. if (format == KMF_FORMAT_PEM)
  519. xcert = PEM_read_bio_X509_AUX(bcert, NULL, NULL, NULL);
  520. else if (format == KMF_FORMAT_ASN1)
  521. xcert = d2i_X509_bio(bcert, NULL);
  522. else if (format == KMF_FORMAT_PKCS12) {
  523. PKCS12 *p12 = d2i_PKCS12_bio(bcert, NULL);
  524. if (p12 != NULL) {
  525. (void) PKCS12_parse(p12, NULL, NULL, &xcert, NULL);
  526. PKCS12_free(p12);
  527. p12 = NULL;
  528. } else {
  529. SET_ERROR(kmfh, ERR_get_error());
  530. rv = KMF_ERR_BAD_CERT_FORMAT;
  531. }
  532. } else {
  533. rv = KMF_ERR_BAD_PARAMETER;
  534. goto cleanup;
  535. }
  536. if (xcert == NULL) {
  537. SET_ERROR(kmfh, ERR_get_error());
  538. rv = KMF_ERR_BAD_CERT_FORMAT;
  539. goto cleanup;
  540. }
  541. if (check_cert(xcert, issuer, subject, serial, &match) != KMF_OK ||
  542. match == FALSE) {
  543. rv = KMF_ERR_CERT_NOT_FOUND;
  544. goto cleanup;
  545. }
  546. if (outcert != NULL) {
  547. *outcert = xcert;
  548. }
  549. cleanup:
  550. if (bcert != NULL) (void) BIO_free(bcert);
  551. if (rv != KMF_OK && xcert != NULL)
  552. X509_free(xcert);
  553. return (rv);
  554. }
  555. static int
  556. datacmp(const void *a, const void *b)
  557. {
  558. KMF_DATA *adata = (KMF_DATA *)a;
  559. KMF_DATA *bdata = (KMF_DATA *)b;
  560. if (adata->Length > bdata->Length)
  561. return (-1);
  562. if (adata->Length < bdata->Length)
  563. return (1);
  564. return (0);
  565. }
  566. static KMF_RETURN
  567. load_certs(KMF_HANDLE *kmfh, char *issuer, char *subject, KMF_BIGINT *serial,
  568. KMF_CERT_VALIDITY validity, char *pathname,
  569. KMF_DATA **certlist, uint32_t *numcerts)
  570. {
  571. KMF_RETURN rv = KMF_OK;
  572. int i;
  573. KMF_DATA *certs = NULL;
  574. int nc = 0;
  575. int hits = 0;
  576. KMF_ENCODE_FORMAT format;
  577. rv = kmf_get_file_format(pathname, &format);
  578. if (rv != KMF_OK) {
  579. if (rv == KMF_ERR_OPEN_FILE)
  580. rv = KMF_ERR_CERT_NOT_FOUND;
  581. return (rv);
  582. }
  583. if (format == KMF_FORMAT_ASN1) {
  584. /* load a single certificate */
  585. certs = (KMF_DATA *)malloc(sizeof (KMF_DATA));
  586. if (certs == NULL)
  587. return (KMF_ERR_MEMORY);
  588. certs->Data = NULL;
  589. certs->Length = 0;
  590. rv = kmf_load_cert(kmfh, issuer, subject, serial, validity,
  591. pathname, certs);
  592. if (rv == KMF_OK) {
  593. *certlist = certs;
  594. *numcerts = 1;
  595. } else {
  596. kmf_free_data(certs);
  597. free(certs);
  598. certs = NULL;
  599. }
  600. return (rv);
  601. } else if (format == KMF_FORMAT_PKCS12) {
  602. /* We need a credential to access a PKCS#12 file */
  603. rv = KMF_ERR_BAD_CERT_FORMAT;
  604. } else if (format == KMF_FORMAT_PEM ||
  605. format != KMF_FORMAT_PEM_KEYPAIR) {
  606. /* This function only works on PEM files */
  607. rv = extract_pem(kmfh, issuer, subject, serial, pathname,
  608. (uchar_t *)NULL, 0, NULL, &certs, &nc);
  609. } else {
  610. return (KMF_ERR_ENCODING);
  611. }
  612. if (rv != KMF_OK)
  613. return (rv);
  614. for (i = 0; i < nc; i++) {
  615. if (validity == KMF_NONEXPIRED_CERTS) {
  616. rv = kmf_check_cert_date(kmfh, &certs[i]);
  617. } else if (validity == KMF_EXPIRED_CERTS) {
  618. rv = kmf_check_cert_date(kmfh, &certs[i]);
  619. if (rv == KMF_OK)
  620. rv = KMF_ERR_CERT_NOT_FOUND;
  621. if (rv == KMF_ERR_VALIDITY_PERIOD)
  622. rv = KMF_OK;
  623. }
  624. if (rv != KMF_OK) {
  625. /* Remove this cert from the list by clearing it. */
  626. kmf_free_data(&certs[i]);
  627. } else {
  628. hits++; /* count valid certs found */
  629. }
  630. rv = KMF_OK;
  631. }
  632. if (rv == KMF_OK && hits > 0) {
  633. /*
  634. * Sort the list of certs by length to put the cleared ones
  635. * at the end so they don't get accessed by the caller.
  636. */
  637. qsort((void *)certs, nc, sizeof (KMF_DATA), datacmp);
  638. *certlist = certs;
  639. /* since we sorted the list, just return the number of hits */
  640. *numcerts = hits;
  641. } else {
  642. if (rv == KMF_OK && hits == 0)
  643. rv = KMF_ERR_CERT_NOT_FOUND;
  644. if (certs != NULL) {
  645. free(certs);
  646. certs = NULL;
  647. }
  648. }
  649. return (rv);
  650. }
  651. static KMF_RETURN
  652. kmf_load_cert(KMF_HANDLE *kmfh,
  653. char *issuer, char *subject, KMF_BIGINT *serial,
  654. KMF_CERT_VALIDITY validity,
  655. char *pathname,
  656. KMF_DATA *cert)
  657. {
  658. KMF_RETURN rv = KMF_OK;
  659. X509 *x509cert = NULL;
  660. rv = load_X509cert(kmfh, issuer, subject, serial, pathname, &x509cert);
  661. if (rv == KMF_OK && x509cert != NULL && cert != NULL) {
  662. rv = ssl_cert2KMFDATA(kmfh, x509cert, cert);
  663. if (rv != KMF_OK) {
  664. goto cleanup;
  665. }
  666. if (validity == KMF_NONEXPIRED_CERTS) {
  667. rv = kmf_check_cert_date(kmfh, cert);
  668. } else if (validity == KMF_EXPIRED_CERTS) {
  669. rv = kmf_check_cert_date(kmfh, cert);
  670. if (rv == KMF_OK) {
  671. /*
  672. * This is a valid cert so skip it.
  673. */
  674. rv = KMF_ERR_CERT_NOT_FOUND;
  675. }
  676. if (rv == KMF_ERR_VALIDITY_PERIOD) {
  677. /*
  678. * We want to return success when we
  679. * find an invalid cert.
  680. */
  681. rv = KMF_OK;
  682. goto cleanup;
  683. }
  684. }
  685. }
  686. cleanup:
  687. if (x509cert != NULL)
  688. X509_free(x509cert);
  689. return (rv);
  690. }
  691. static KMF_RETURN
  692. readAltFormatPrivateKey(KMF_DATA *filedata, EVP_PKEY **pkey)
  693. {
  694. KMF_RETURN ret = KMF_OK;
  695. KMF_RAW_RSA_KEY rsa;
  696. BerElement *asn1 = NULL;
  697. BerValue filebuf;
  698. BerValue OID = { NULL, 0 };
  699. BerValue *Mod = NULL, *PubExp = NULL;
  700. BerValue *PriExp = NULL, *Prime1 = NULL, *Prime2 = NULL;
  701. BerValue *Coef = NULL;
  702. BIGNUM *D = NULL, *P = NULL, *Q = NULL, *COEF = NULL;
  703. BIGNUM *Exp1 = NULL, *Exp2 = NULL, *pminus1 = NULL;
  704. BIGNUM *qminus1 = NULL;
  705. BN_CTX *ctx = NULL;
  706. *pkey = NULL;
  707. filebuf.bv_val = (char *)filedata->Data;
  708. filebuf.bv_len = filedata->Length;
  709. asn1 = kmfder_init(&filebuf);
  710. if (asn1 == NULL) {
  711. ret = KMF_ERR_MEMORY;
  712. goto out;
  713. }
  714. if (kmfber_scanf(asn1, "{{Dn{IIIIII}}}",
  715. &OID, &Mod, &PubExp, &PriExp, &Prime1,
  716. &Prime2, &Coef) == -1) {
  717. ret = KMF_ERR_ENCODING;
  718. goto out;
  719. }
  720. /*
  721. * We have to derive the 2 Exponents using Bignumber math.
  722. * Exp1 = PriExp mod (Prime1 - 1)
  723. * Exp2 = PriExp mod (Prime2 - 1)
  724. */
  725. /* D = PrivateExponent */
  726. D = BN_bin2bn((const uchar_t *)PriExp->bv_val, PriExp->bv_len, D);
  727. if (D == NULL) {
  728. ret = KMF_ERR_MEMORY;
  729. goto out;
  730. }
  731. /* P = Prime1 (first prime factor of Modulus) */
  732. P = BN_bin2bn((const uchar_t *)Prime1->bv_val, Prime1->bv_len, P);
  733. if (D == NULL) {
  734. ret = KMF_ERR_MEMORY;
  735. goto out;
  736. }
  737. /* Q = Prime2 (second prime factor of Modulus) */
  738. Q = BN_bin2bn((const uchar_t *)Prime2->bv_val, Prime2->bv_len, Q);
  739. if ((ctx = BN_CTX_new()) == NULL) {
  740. ret = KMF_ERR_MEMORY;
  741. goto out;
  742. }
  743. /* Compute (P - 1) */
  744. pminus1 = BN_new();
  745. (void) BN_sub(pminus1, P, BN_value_one());
  746. /* Exponent1 = D mod (P - 1) */
  747. Exp1 = BN_new();
  748. (void) BN_mod(Exp1, D, pminus1, ctx);
  749. /* Compute (Q - 1) */
  750. qminus1 = BN_new();
  751. (void) BN_sub(qminus1, Q, BN_value_one());
  752. /* Exponent2 = D mod (Q - 1) */
  753. Exp2 = BN_new();
  754. (void) BN_mod(Exp2, D, qminus1, ctx);
  755. /* Coef = (Inverse Q) mod P */
  756. COEF = BN_new();
  757. (void) BN_mod_inverse(COEF, Q, P, ctx);
  758. /* Convert back to KMF format */
  759. (void) memset(&rsa, 0, sizeof (rsa));
  760. if ((ret = sslBN2KMFBN(Exp1, &rsa.exp1)) != KMF_OK)
  761. goto out;
  762. if ((ret = sslBN2KMFBN(Exp2, &rsa.exp2)) != KMF_OK)
  763. goto out;
  764. if ((ret = sslBN2KMFBN(COEF, &rsa.coef)) != KMF_OK)
  765. goto out;
  766. rsa.mod.val = (uchar_t *)Mod->bv_val;
  767. rsa.mod.len = Mod->bv_len;
  768. rsa.pubexp.val = (uchar_t *)PubExp->bv_val;
  769. rsa.pubexp.len = PubExp->bv_len;
  770. rsa.priexp.val = (uchar_t *)PriExp->bv_val;
  771. rsa.priexp.len = PriExp->bv_len;
  772. rsa.prime1.val = (uchar_t *)Prime1->bv_val;
  773. rsa.prime1.len = Prime1->bv_len;
  774. rsa.prime2.val = (uchar_t *)Prime2->bv_val;
  775. rsa.prime2.len = Prime2->bv_len;
  776. *pkey = ImportRawRSAKey(&rsa);
  777. out:
  778. if (asn1 != NULL)
  779. kmfber_free(asn1, 1);
  780. if (OID.bv_val) {
  781. free(OID.bv_val);
  782. }
  783. if (PriExp)
  784. free(PriExp);
  785. if (Mod)
  786. free(Mod);
  787. if (PubExp)
  788. free(PubExp);
  789. if (Coef) {
  790. (void) memset(Coef->bv_val, 0, Coef->bv_len);
  791. free(Coef->bv_val);
  792. free(Coef);
  793. }
  794. if (Prime1)
  795. free(Prime1);
  796. if (Prime2)
  797. free(Prime2);
  798. if (ctx != NULL)
  799. BN_CTX_free(ctx);
  800. if (D)
  801. BN_clear_free(D);
  802. if (P)
  803. BN_clear_free(P);
  804. if (Q)
  805. BN_clear_free(Q);
  806. if (pminus1)
  807. BN_clear_free(pminus1);
  808. if (qminus1)
  809. BN_clear_free(qminus1);
  810. if (Exp1)
  811. BN_clear_free(Exp1);
  812. if (Exp2)
  813. BN_clear_free(Exp2);
  814. return (ret);
  815. }
  816. static EVP_PKEY *
  817. openssl_load_key(KMF_HANDLE_T handle, const char *file)
  818. {
  819. BIO *keyfile = NULL;
  820. EVP_PKEY *pkey = NULL;
  821. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  822. KMF_ENCODE_FORMAT format;
  823. KMF_RETURN rv;
  824. KMF_DATA filedata;
  825. if (file == NULL) {
  826. return (NULL);
  827. }
  828. if (kmf_get_file_format((char *)file, &format) != KMF_OK)
  829. return (NULL);
  830. keyfile = BIO_new_file(file, "rb");
  831. if (keyfile == NULL) {
  832. goto end;
  833. }
  834. if (format == KMF_FORMAT_ASN1) {
  835. pkey = d2i_PrivateKey_bio(keyfile, NULL);
  836. if (pkey == NULL) {
  837. (void) BIO_free(keyfile);
  838. keyfile = NULL;
  839. /* Try odd ASN.1 variations */
  840. rv = kmf_read_input_file(kmfh, (char *)file,
  841. &filedata);
  842. if (rv == KMF_OK) {
  843. (void) readAltFormatPrivateKey(&filedata,
  844. &pkey);
  845. kmf_free_data(&filedata);
  846. }
  847. }
  848. } else if (format == KMF_FORMAT_PEM ||
  849. format == KMF_FORMAT_PEM_KEYPAIR) {
  850. pkey = PEM_read_bio_PrivateKey(keyfile, NULL, NULL, NULL);
  851. if (pkey == NULL) {
  852. KMF_DATA derdata;
  853. /*
  854. * Check if this is the alt. format
  855. * RSA private key file.
  856. */
  857. rv = kmf_read_input_file(kmfh, (char *)file,
  858. &filedata);
  859. if (rv == KMF_OK) {
  860. uchar_t *d = NULL;
  861. int len;
  862. rv = kmf_pem_to_der(filedata.Data,
  863. filedata.Length, &d, &len);
  864. if (rv == KMF_OK && d != NULL) {
  865. derdata.Data = d;
  866. derdata.Length = (size_t)len;
  867. (void) readAltFormatPrivateKey(
  868. &derdata, &pkey);
  869. free(d);
  870. }
  871. kmf_free_data(&filedata);
  872. }
  873. }
  874. }
  875. end:
  876. if (pkey == NULL)
  877. SET_ERROR(kmfh, ERR_get_error());
  878. if (keyfile != NULL)
  879. (void) BIO_free(keyfile);
  880. return (pkey);
  881. }
  882. KMF_RETURN
  883. OpenSSL_FindCert(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  884. {
  885. KMF_RETURN rv = KMF_OK;
  886. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  887. int i, n;
  888. uint32_t maxcerts = 0;
  889. uint32_t *num_certs;
  890. KMF_X509_DER_CERT *kmf_cert = NULL;
  891. char *dirpath = NULL;
  892. char *filename = NULL;
  893. char *fullpath = NULL;
  894. char *issuer = NULL;
  895. char *subject = NULL;
  896. KMF_BIGINT *serial = NULL;
  897. KMF_CERT_VALIDITY validity;
  898. num_certs = kmf_get_attr_ptr(KMF_COUNT_ATTR, attrlist, numattr);
  899. if (num_certs == NULL)
  900. return (KMF_ERR_BAD_PARAMETER);
  901. /* num_certs should reference the size of kmf_cert */
  902. maxcerts = *num_certs;
  903. if (maxcerts == 0)
  904. maxcerts = 0xFFFFFFFF;
  905. *num_certs = 0;
  906. /* Get the optional returned certificate list */
  907. kmf_cert = kmf_get_attr_ptr(KMF_X509_DER_CERT_ATTR, attrlist,
  908. numattr);
  909. /*
  910. * The dirpath attribute and the filename attribute can not be NULL
  911. * at the same time.
  912. */
  913. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  914. filename = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist,
  915. numattr);
  916. fullpath = get_fullpath(dirpath, filename);
  917. if (fullpath == NULL)
  918. return (KMF_ERR_BAD_PARAMETER);
  919. /* Get optional search criteria attributes */
  920. issuer = kmf_get_attr_ptr(KMF_ISSUER_NAME_ATTR, attrlist, numattr);
  921. subject = kmf_get_attr_ptr(KMF_SUBJECT_NAME_ATTR, attrlist, numattr);
  922. serial = kmf_get_attr_ptr(KMF_BIGINT_ATTR, attrlist, numattr);
  923. rv = kmf_get_attr(KMF_CERT_VALIDITY_ATTR, attrlist, numattr,
  924. &validity, NULL);
  925. if (rv != KMF_OK) {
  926. validity = KMF_ALL_CERTS;
  927. rv = KMF_OK;
  928. }
  929. if (isdir(fullpath)) {
  930. DIR *dirp;
  931. struct dirent *dp;
  932. n = 0;
  933. /* open all files in the directory and attempt to read them */
  934. if ((dirp = opendir(fullpath)) == NULL) {
  935. return (KMF_ERR_BAD_PARAMETER);
  936. }
  937. while ((dp = readdir(dirp)) != NULL) {
  938. char *fname;
  939. KMF_DATA *certlist = NULL;
  940. uint32_t loaded_certs = 0;
  941. if (strcmp(dp->d_name, ".") == 0 ||
  942. strcmp(dp->d_name, "..") == 0)
  943. continue;
  944. fname = get_fullpath(fullpath, (char *)&dp->d_name);
  945. rv = load_certs(kmfh, issuer, subject, serial,
  946. validity, fname, &certlist, &loaded_certs);
  947. if (rv != KMF_OK) {
  948. free(fname);
  949. if (certlist != NULL) {
  950. for (i = 0; i < loaded_certs; i++)
  951. kmf_free_data(&certlist[i]);
  952. free(certlist);
  953. }
  954. continue;
  955. }
  956. /* If load succeeds, add certdata to the list */
  957. if (kmf_cert != NULL) {
  958. for (i = 0; i < loaded_certs &&
  959. n < maxcerts; i++) {
  960. kmf_cert[n].certificate.Data =
  961. certlist[i].Data;
  962. kmf_cert[n].certificate.Length =
  963. certlist[i].Length;
  964. kmf_cert[n].kmf_private.keystore_type =
  965. KMF_KEYSTORE_OPENSSL;
  966. kmf_cert[n].kmf_private.flags =
  967. KMF_FLAG_CERT_VALID;
  968. kmf_cert[n].kmf_private.label =
  969. strdup(fname);
  970. n++;
  971. }
  972. /*
  973. * If maxcerts < loaded_certs, clean up the
  974. * certs that were not used.
  975. */
  976. for (; i < loaded_certs; i++)
  977. kmf_free_data(&certlist[i]);
  978. } else {
  979. for (i = 0; i < loaded_certs; i++)
  980. kmf_free_data(&certlist[i]);
  981. n += loaded_certs;
  982. }
  983. free(certlist);
  984. free(fname);
  985. }
  986. (*num_certs) = n;
  987. if (*num_certs == 0)
  988. rv = KMF_ERR_CERT_NOT_FOUND;
  989. if (*num_certs > 0)
  990. rv = KMF_OK;
  991. exit:
  992. (void) closedir(dirp);
  993. } else {
  994. KMF_DATA *certlist = NULL;
  995. uint32_t loaded_certs = 0;
  996. rv = load_certs(kmfh, issuer, subject, serial, validity,
  997. fullpath, &certlist, &loaded_certs);
  998. if (rv != KMF_OK) {
  999. free(fullpath);
  1000. return (rv);
  1001. }
  1002. n = 0;
  1003. if (kmf_cert != NULL && certlist != NULL) {
  1004. for (i = 0; i < loaded_certs && i < maxcerts; i++) {
  1005. kmf_cert[n].certificate.Data =
  1006. certlist[i].Data;
  1007. kmf_cert[n].certificate.Length =
  1008. certlist[i].Length;
  1009. kmf_cert[n].kmf_private.keystore_type =
  1010. KMF_KEYSTORE_OPENSSL;
  1011. kmf_cert[n].kmf_private.flags =
  1012. KMF_FLAG_CERT_VALID;
  1013. kmf_cert[n].kmf_private.label =
  1014. strdup(fullpath);
  1015. n++;
  1016. }
  1017. /* If maxcerts < loaded_certs, clean up */
  1018. for (; i < loaded_certs; i++)
  1019. kmf_free_data(&certlist[i]);
  1020. } else if (certlist != NULL) {
  1021. for (i = 0; i < loaded_certs; i++)
  1022. kmf_free_data(&certlist[i]);
  1023. n = loaded_certs;
  1024. }
  1025. if (certlist != NULL)
  1026. free(certlist);
  1027. *num_certs = n;
  1028. }
  1029. free(fullpath);
  1030. return (rv);
  1031. }
  1032. void
  1033. /*ARGSUSED*/
  1034. OpenSSL_FreeKMFCert(KMF_HANDLE_T handle,
  1035. KMF_X509_DER_CERT *kmf_cert)
  1036. {
  1037. if (kmf_cert != NULL) {
  1038. if (kmf_cert->certificate.Data != NULL) {
  1039. kmf_free_data(&kmf_cert->certificate);
  1040. }
  1041. if (kmf_cert->kmf_private.label)
  1042. free(kmf_cert->kmf_private.label);
  1043. }
  1044. }
  1045. /*ARGSUSED*/
  1046. KMF_RETURN
  1047. OpenSSL_StoreCert(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  1048. {
  1049. KMF_RETURN ret = KMF_OK;
  1050. KMF_DATA *cert = NULL;
  1051. char *outfilename = NULL;
  1052. char *dirpath = NULL;
  1053. char *fullpath = NULL;
  1054. KMF_ENCODE_FORMAT format;
  1055. /* Get the cert data */
  1056. cert = kmf_get_attr_ptr(KMF_CERT_DATA_ATTR, attrlist, numattr);
  1057. if (cert == NULL || cert->Data == NULL)
  1058. return (KMF_ERR_BAD_PARAMETER);
  1059. /* Check the output filename and directory attributes. */
  1060. outfilename = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist,
  1061. numattr);
  1062. if (outfilename == NULL)
  1063. return (KMF_ERR_BAD_PARAMETER);
  1064. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1065. fullpath = get_fullpath(dirpath, outfilename);
  1066. if (fullpath == NULL)
  1067. return (KMF_ERR_BAD_CERTFILE);
  1068. /* Check the optional format attribute */
  1069. ret = kmf_get_attr(KMF_ENCODE_FORMAT_ATTR, attrlist, numattr,
  1070. &format, NULL);
  1071. if (ret != KMF_OK) {
  1072. /* If there is no format attribute, then default to PEM */
  1073. format = KMF_FORMAT_PEM;
  1074. ret = KMF_OK;
  1075. } else if (format != KMF_FORMAT_ASN1 && format != KMF_FORMAT_PEM) {
  1076. ret = KMF_ERR_BAD_CERT_FORMAT;
  1077. goto out;
  1078. }
  1079. /* Store the certificate in the file with the specified format */
  1080. ret = kmf_create_cert_file(cert, format, fullpath);
  1081. out:
  1082. if (fullpath != NULL)
  1083. free(fullpath);
  1084. return (ret);
  1085. }
  1086. KMF_RETURN
  1087. OpenSSL_DeleteCert(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  1088. {
  1089. KMF_RETURN rv;
  1090. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1091. KMF_DATA certdata = {NULL, 0};
  1092. char *dirpath = NULL;
  1093. char *filename = NULL;
  1094. char *fullpath = NULL;
  1095. char *issuer = NULL;
  1096. char *subject = NULL;
  1097. KMF_BIGINT *serial = NULL;
  1098. KMF_CERT_VALIDITY validity;
  1099. /*
  1100. * Get the DIRPATH and CERT_FILENAME attributes. They can not be
  1101. * NULL at the same time.
  1102. */
  1103. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1104. filename = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist,
  1105. numattr);
  1106. fullpath = get_fullpath(dirpath, filename);
  1107. if (fullpath == NULL)
  1108. return (KMF_ERR_BAD_PARAMETER);
  1109. /* Get optional search criteria attributes */
  1110. issuer = kmf_get_attr_ptr(KMF_ISSUER_NAME_ATTR, attrlist, numattr);
  1111. subject = kmf_get_attr_ptr(KMF_SUBJECT_NAME_ATTR, attrlist, numattr);
  1112. serial = kmf_get_attr_ptr(KMF_BIGINT_ATTR, attrlist, numattr);
  1113. rv = kmf_get_attr(KMF_CERT_VALIDITY_ATTR, attrlist, numattr,
  1114. &validity, NULL);
  1115. if (rv != KMF_OK) {
  1116. validity = KMF_ALL_CERTS;
  1117. rv = KMF_OK;
  1118. }
  1119. if (isdir(fullpath)) {
  1120. DIR *dirp;
  1121. struct dirent *dp;
  1122. /* open all files in the directory and attempt to read them */
  1123. if ((dirp = opendir(fullpath)) == NULL) {
  1124. return (KMF_ERR_BAD_PARAMETER);
  1125. }
  1126. while ((dp = readdir(dirp)) != NULL) {
  1127. if (strcmp(dp->d_name, ".") != 0 &&
  1128. strcmp(dp->d_name, "..") != 0) {
  1129. char *fname;
  1130. fname = get_fullpath(fullpath,
  1131. (char *)&dp->d_name);
  1132. if (fname == NULL) {
  1133. rv = KMF_ERR_MEMORY;
  1134. break;
  1135. }
  1136. rv = kmf_load_cert(kmfh, issuer, subject,
  1137. serial, validity, fname, &certdata);
  1138. if (rv == KMF_ERR_CERT_NOT_FOUND) {
  1139. free(fname);
  1140. kmf_free_data(&certdata);
  1141. rv = KMF_OK;
  1142. continue;
  1143. } else if (rv != KMF_OK) {
  1144. free(fname);
  1145. break;
  1146. }
  1147. if (unlink(fname) != 0) {
  1148. SET_SYS_ERROR(kmfh, errno);
  1149. rv = KMF_ERR_INTERNAL;
  1150. free(fname);
  1151. break;
  1152. }
  1153. free(fname);
  1154. kmf_free_data(&certdata);
  1155. }
  1156. }
  1157. (void) closedir(dirp);
  1158. } else {
  1159. /* Just try to load a single certificate */
  1160. rv = kmf_load_cert(kmfh, issuer, subject, serial, validity,
  1161. fullpath, &certdata);
  1162. if (rv == KMF_OK) {
  1163. if (unlink(fullpath) != 0) {
  1164. SET_SYS_ERROR(kmfh, errno);
  1165. rv = KMF_ERR_INTERNAL;
  1166. }
  1167. }
  1168. }
  1169. out:
  1170. if (fullpath != NULL)
  1171. free(fullpath);
  1172. kmf_free_data(&certdata);
  1173. return (rv);
  1174. }
  1175. KMF_RETURN
  1176. OpenSSL_EncodePubKeyData(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key,
  1177. KMF_DATA *keydata)
  1178. {
  1179. KMF_RETURN rv = KMF_OK;
  1180. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1181. int n;
  1182. if (key == NULL || keydata == NULL ||
  1183. key->keyp == NULL)
  1184. return (KMF_ERR_BAD_PARAMETER);
  1185. if (key->keyalg == KMF_RSA) {
  1186. RSA *pubkey = EVP_PKEY_get1_RSA(key->keyp);
  1187. if (!(n = i2d_RSA_PUBKEY(pubkey, &keydata->Data))) {
  1188. SET_ERROR(kmfh, ERR_get_error());
  1189. return (KMF_ERR_ENCODING);
  1190. }
  1191. RSA_free(pubkey);
  1192. } else if (key->keyalg == KMF_DSA) {
  1193. DSA *pubkey = EVP_PKEY_get1_DSA(key->keyp);
  1194. if (!(n = i2d_DSA_PUBKEY(pubkey, &keydata->Data))) {
  1195. SET_ERROR(kmfh, ERR_get_error());
  1196. return (KMF_ERR_ENCODING);
  1197. }
  1198. DSA_free(pubkey);
  1199. } else {
  1200. return (KMF_ERR_BAD_PARAMETER);
  1201. }
  1202. keydata->Length = n;
  1203. cleanup:
  1204. if (rv != KMF_OK) {
  1205. if (keydata->Data)
  1206. free(keydata->Data);
  1207. keydata->Data = NULL;
  1208. keydata->Length = 0;
  1209. }
  1210. return (rv);
  1211. }
  1212. static KMF_RETURN
  1213. ssl_write_key(KMF_HANDLE *kmfh, KMF_ENCODE_FORMAT format, BIO *out,
  1214. KMF_CREDENTIAL *cred, EVP_PKEY *pkey, boolean_t private)
  1215. {
  1216. int rv = 0;
  1217. RSA *rsa;
  1218. DSA *dsa;
  1219. if (pkey == NULL || out == NULL)
  1220. return (KMF_ERR_BAD_PARAMETER);
  1221. switch (format) {
  1222. case KMF_FORMAT_RAWKEY:
  1223. /* same as ASN.1 */
  1224. case KMF_FORMAT_ASN1:
  1225. if (pkey->type == EVP_PKEY_RSA) {
  1226. rsa = EVP_PKEY_get1_RSA(pkey);
  1227. if (private)
  1228. rv = i2d_RSAPrivateKey_bio(out, rsa);
  1229. else
  1230. rv = i2d_RSAPublicKey_bio(out, rsa);
  1231. RSA_free(rsa);
  1232. } else if (pkey->type == EVP_PKEY_DSA) {
  1233. dsa = EVP_PKEY_get1_DSA(pkey);
  1234. rv = i2d_DSAPrivateKey_bio(out, dsa);
  1235. DSA_free(dsa);
  1236. }
  1237. if (rv == 1) {
  1238. rv = KMF_OK;
  1239. } else {
  1240. SET_ERROR(kmfh, rv);
  1241. }
  1242. break;
  1243. case KMF_FORMAT_PEM:
  1244. if (pkey->type == EVP_PKEY_RSA) {
  1245. rsa = EVP_PKEY_get1_RSA(pkey);
  1246. if (private)
  1247. rv = PEM_write_bio_RSAPrivateKey(out,
  1248. rsa, NULL, NULL, 0, NULL,
  1249. (cred != NULL ? cred->cred : NULL));
  1250. else
  1251. rv = PEM_write_bio_RSAPublicKey(out,
  1252. rsa);
  1253. RSA_free(rsa);
  1254. } else if (pkey->type == EVP_PKEY_DSA) {
  1255. dsa = EVP_PKEY_get1_DSA(pkey);
  1256. rv = PEM_write_bio_DSAPrivateKey(out,
  1257. dsa, NULL, NULL, 0, NULL,
  1258. (cred != NULL ? cred->cred : NULL));
  1259. DSA_free(dsa);
  1260. }
  1261. if (rv == 1) {
  1262. rv = KMF_OK;
  1263. } else {
  1264. SET_ERROR(kmfh, rv);
  1265. }
  1266. break;
  1267. default:
  1268. rv = KMF_ERR_BAD_PARAMETER;
  1269. }
  1270. return (rv);
  1271. }
  1272. KMF_RETURN
  1273. OpenSSL_CreateKeypair(KMF_HANDLE_T handle, int numattr,
  1274. KMF_ATTRIBUTE *attrlist)
  1275. {
  1276. KMF_RETURN rv = KMF_OK;
  1277. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1278. uint32_t eValue = 0x010001;
  1279. RSA *sslPrivKey = NULL;
  1280. DSA *sslDSAKey = NULL;
  1281. EVP_PKEY *eprikey = NULL;
  1282. EVP_PKEY *epubkey = NULL;
  1283. BIO *out = NULL;
  1284. KMF_KEY_HANDLE *pubkey = NULL, *privkey = NULL;
  1285. uint32_t keylen = 1024;
  1286. uint32_t keylen_size = sizeof (uint32_t);
  1287. boolean_t storekey = TRUE;
  1288. KMF_KEY_ALG keytype = KMF_RSA;
  1289. rv = kmf_get_attr(KMF_STOREKEY_BOOL_ATTR, attrlist, numattr,
  1290. &storekey, NULL);
  1291. if (rv != KMF_OK) {
  1292. /* "storekey" is optional. Default is TRUE */
  1293. rv = KMF_OK;
  1294. }
  1295. rv = kmf_get_attr(KMF_KEYALG_ATTR, attrlist, numattr,
  1296. (void *)&keytype, NULL);
  1297. if (rv != KMF_OK)
  1298. /* keytype is optional. KMF_RSA is default */
  1299. rv = KMF_OK;
  1300. pubkey = kmf_get_attr_ptr(KMF_PUBKEY_HANDLE_ATTR, attrlist, numattr);
  1301. if (pubkey == NULL)
  1302. return (KMF_ERR_BAD_PARAMETER);
  1303. privkey = kmf_get_attr_ptr(KMF_PRIVKEY_HANDLE_ATTR, attrlist, numattr);
  1304. if (privkey == NULL)
  1305. return (KMF_ERR_BAD_PARAMETER);
  1306. (void) memset(pubkey, 0, sizeof (KMF_KEY_HANDLE));
  1307. (void) memset(privkey, 0, sizeof (KMF_KEY_HANDLE));
  1308. eprikey = EVP_PKEY_new();
  1309. if (eprikey == NULL) {
  1310. SET_ERROR(kmfh, ERR_get_error());
  1311. rv = KMF_ERR_KEYGEN_FAILED;
  1312. goto cleanup;
  1313. }
  1314. epubkey = EVP_PKEY_new();
  1315. if (epubkey == NULL) {
  1316. SET_ERROR(kmfh, ERR_get_error());
  1317. rv = KMF_ERR_KEYGEN_FAILED;
  1318. goto cleanup;
  1319. }
  1320. if (keytype == KMF_RSA) {
  1321. KMF_BIGINT *rsaexp = NULL;
  1322. rsaexp = kmf_get_attr_ptr(KMF_RSAEXP_ATTR, attrlist, numattr);
  1323. if (rsaexp != NULL) {
  1324. if (rsaexp->len > 0 &&
  1325. rsaexp->len <= sizeof (eValue) &&
  1326. rsaexp->val != NULL) {
  1327. /* LINTED E_BAD_PTR_CAST_ALIGN */
  1328. eValue = *(uint32_t *)rsaexp->val;
  1329. } else {
  1330. rv = KMF_ERR_BAD_PARAMETER;
  1331. goto cleanup;
  1332. }
  1333. } else {
  1334. /* RSA Exponent is optional. Default is 0x10001 */
  1335. rv = KMF_OK;
  1336. }
  1337. rv = kmf_get_attr(KMF_KEYLENGTH_ATTR, attrlist, numattr,
  1338. &keylen, &keylen_size);
  1339. if (rv == KMF_ERR_ATTR_NOT_FOUND)
  1340. /* keylen is optional, default is 1024 */
  1341. rv = KMF_OK;
  1342. if (rv != KMF_OK) {
  1343. rv = KMF_ERR_BAD_PARAMETER;
  1344. goto cleanup;
  1345. }
  1346. sslPrivKey = RSA_generate_key(keylen, eValue, NULL, NULL);
  1347. if (sslPrivKey == NULL) {
  1348. SET_ERROR(kmfh, ERR_get_error());
  1349. rv = KMF_ERR_KEYGEN_FAILED;
  1350. } else {
  1351. (void) EVP_PKEY_set1_RSA(eprikey, sslPrivKey);
  1352. privkey->kstype = KMF_KEYSTORE_OPENSSL;
  1353. privkey->keyalg = KMF_RSA;
  1354. privkey->keyclass = KMF_ASYM_PRI;
  1355. privkey->israw = FALSE;
  1356. privkey->keyp = (void *)eprikey;
  1357. /* OpenSSL derives the public key from the private */
  1358. (void) EVP_PKEY_set1_RSA(epubkey, sslPrivKey);
  1359. pubkey->kstype = KMF_KEYSTORE_OPENSSL;
  1360. pubkey->keyalg = KMF_RSA;
  1361. pubkey->israw = FALSE;
  1362. pubkey->keyclass = KMF_ASYM_PUB;
  1363. pubkey->keyp = (void *)epubkey;
  1364. }
  1365. } else if (keytype == KMF_DSA) {
  1366. DSA *dp;
  1367. sslDSAKey = DSA_new();
  1368. if (sslDSAKey == NULL) {
  1369. SET_ERROR(kmfh, ERR_get_error());
  1370. return (KMF_ERR_MEMORY);
  1371. }
  1372. if ((sslDSAKey->p = BN_bin2bn(P, sizeof (P), sslDSAKey->p)) ==
  1373. NULL) {
  1374. SET_ERROR(kmfh, ERR_get_error());
  1375. rv = KMF_ERR_KEYGEN_FAILED;
  1376. goto cleanup;
  1377. }
  1378. if ((sslDSAKey->q = BN_bin2bn(Q, sizeof (Q), sslDSAKey->q)) ==
  1379. NULL) {
  1380. SET_ERROR(kmfh, ERR_get_error());
  1381. rv = KMF_ERR_KEYGEN_FAILED;
  1382. goto cleanup;
  1383. }
  1384. if ((sslDSAKey->g = BN_bin2bn(G, sizeof (G), sslDSAKey->g)) ==
  1385. NULL) {
  1386. SET_ERROR(kmfh, ERR_get_error());
  1387. rv = KMF_ERR_KEYGEN_FAILED;
  1388. goto cleanup;
  1389. }
  1390. if (!DSA_generate_key(sslDSAKey)) {
  1391. SET_ERROR(kmfh, ERR_get_error());
  1392. rv = KMF_ERR_KEYGEN_FAILED;
  1393. goto cleanup;
  1394. }
  1395. privkey->kstype = KMF_KEYSTORE_OPENSSL;
  1396. privkey->keyalg = KMF_DSA;
  1397. privkey->keyclass = KMF_ASYM_PRI;
  1398. privkey->israw = FALSE;
  1399. if (EVP_PKEY_set1_DSA(eprikey, sslDSAKey)) {
  1400. privkey->keyp = (void *)eprikey;
  1401. } else {
  1402. SET_ERROR(kmfh, ERR_get_error());
  1403. rv = KMF_ERR_KEYGEN_FAILED;
  1404. goto cleanup;
  1405. }
  1406. dp = DSA_new();
  1407. /* Make a copy for the public key */
  1408. if (dp != NULL) {
  1409. if ((dp->p = BN_new()) == NULL) {
  1410. SET_ERROR(kmfh, ERR_get_error());
  1411. rv = KMF_ERR_MEMORY;
  1412. DSA_free(dp);
  1413. goto cleanup;
  1414. }
  1415. if ((dp->q = BN_new()) == NULL) {
  1416. SET_ERROR(kmfh, ERR_get_error());
  1417. rv = KMF_ERR_MEMORY;
  1418. BN_free(dp->p);
  1419. DSA_free(dp);
  1420. goto cleanup;
  1421. }
  1422. if ((dp->g = BN_new()) == NULL) {
  1423. SET_ERROR(kmfh, ERR_get_error());
  1424. rv = KMF_ERR_MEMORY;
  1425. BN_free(dp->q);
  1426. BN_free(dp->p);
  1427. DSA_free(dp);
  1428. goto cleanup;
  1429. }
  1430. if ((dp->pub_key = BN_new()) == NULL) {
  1431. SET_ERROR(kmfh, ERR_get_error());
  1432. rv = KMF_ERR_MEMORY;
  1433. BN_free(dp->q);
  1434. BN_free(dp->p);
  1435. BN_free(dp->g);
  1436. DSA_free(dp);
  1437. goto cleanup;
  1438. }
  1439. (void) BN_copy(dp->p, sslDSAKey->p);
  1440. (void) BN_copy(dp->q, sslDSAKey->q);
  1441. (void) BN_copy(dp->g, sslDSAKey->g);
  1442. (void) BN_copy(dp->pub_key, sslDSAKey->pub_key);
  1443. pubkey->kstype = KMF_KEYSTORE_OPENSSL;
  1444. pubkey->keyalg = KMF_DSA;
  1445. pubkey->keyclass = KMF_ASYM_PUB;
  1446. pubkey->israw = FALSE;
  1447. if (EVP_PKEY_set1_DSA(epubkey, sslDSAKey)) {
  1448. pubkey->keyp = (void *)epubkey;
  1449. } else {
  1450. SET_ERROR(kmfh, ERR_get_error());
  1451. rv = KMF_ERR_KEYGEN_FAILED;
  1452. goto cleanup;
  1453. }
  1454. }
  1455. }
  1456. if (rv != KMF_OK) {
  1457. goto cleanup;
  1458. }
  1459. if (storekey) {
  1460. KMF_ATTRIBUTE storeattrs[4]; /* max. 4 attributes needed */
  1461. int i = 0;
  1462. char *keyfile = NULL, *dirpath = NULL;
  1463. KMF_ENCODE_FORMAT format;
  1464. /*
  1465. * Construct a new attribute arrray and call openssl_store_key
  1466. */
  1467. kmf_set_attr_at_index(storeattrs, i, KMF_PRIVKEY_HANDLE_ATTR,
  1468. privkey, sizeof (privkey));
  1469. i++;
  1470. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1471. if (dirpath != NULL) {
  1472. storeattrs[i].type = KMF_DIRPATH_ATTR;
  1473. storeattrs[i].pValue = dirpath;
  1474. storeattrs[i].valueLen = strlen(dirpath);
  1475. i++;
  1476. } else {
  1477. rv = KMF_OK; /* DIRPATH is optional */
  1478. }
  1479. keyfile = kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR,
  1480. attrlist, numattr);
  1481. if (keyfile != NULL) {
  1482. storeattrs[i].type = KMF_KEY_FILENAME_ATTR;
  1483. storeattrs[i].pValue = keyfile;
  1484. storeattrs[i].valueLen = strlen(keyfile);
  1485. i++;
  1486. } else {
  1487. goto cleanup; /* KEYFILE is required */
  1488. }
  1489. rv = kmf_get_attr(KMF_ENCODE_FORMAT_ATTR, attrlist, numattr,
  1490. (void *)&format, NULL);
  1491. if (rv == KMF_OK) {
  1492. storeattrs[i].type = KMF_ENCODE_FORMAT_ATTR;
  1493. storeattrs[i].pValue = &format;
  1494. storeattrs[i].valueLen = sizeof (format);
  1495. i++;
  1496. }
  1497. rv = OpenSSL_StoreKey(handle, i, storeattrs);
  1498. }
  1499. cleanup:
  1500. if (rv != KMF_OK) {
  1501. if (eprikey != NULL)
  1502. EVP_PKEY_free(eprikey);
  1503. if (epubkey != NULL)
  1504. EVP_PKEY_free(epubkey);
  1505. if (pubkey->keylabel) {
  1506. free(pubkey->keylabel);
  1507. pubkey->keylabel = NULL;
  1508. }
  1509. if (privkey->keylabel) {
  1510. free(privkey->keylabel);
  1511. privkey->keylabel = NULL;
  1512. }
  1513. pubkey->keyp = NULL;
  1514. privkey->keyp = NULL;
  1515. }
  1516. if (sslPrivKey)
  1517. RSA_free(sslPrivKey);
  1518. if (sslDSAKey)
  1519. DSA_free(sslDSAKey);
  1520. if (out != NULL)
  1521. (void) BIO_free(out);
  1522. return (rv);
  1523. }
  1524. /*
  1525. * Make sure the BN conversion is properly padded with 0x00
  1526. * bytes. If not, signature verification for DSA signatures
  1527. * may fail in the case where the bignum value does not use
  1528. * all of the bits.
  1529. */
  1530. static int
  1531. fixbnlen(BIGNUM *bn, unsigned char *buf, int len) {
  1532. int bytes = len - BN_num_bytes(bn);
  1533. /* prepend with leading 0x00 if necessary */
  1534. while (bytes-- > 0)
  1535. *buf++ = 0;
  1536. (void) BN_bn2bin(bn, buf);
  1537. /*
  1538. * Return the desired length since we prepended it
  1539. * with the necessary 0x00 padding.
  1540. */
  1541. return (len);
  1542. }
  1543. KMF_RETURN
  1544. OpenSSL_SignData(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key,
  1545. KMF_OID *AlgOID, KMF_DATA *tobesigned, KMF_DATA *output)
  1546. {
  1547. KMF_RETURN ret = KMF_OK;
  1548. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1549. KMF_ALGORITHM_INDEX AlgId;
  1550. EVP_MD_CTX ctx;
  1551. const EVP_MD *md;
  1552. if (key == NULL || AlgOID == NULL ||
  1553. tobesigned == NULL || output == NULL ||
  1554. tobesigned->Data == NULL ||
  1555. output->Data == NULL)
  1556. return (KMF_ERR_BAD_PARAMETER);
  1557. /* Map the OID to an OpenSSL algorithm */
  1558. AlgId = x509_algoid_to_algid(AlgOID);
  1559. if (AlgId == KMF_ALGID_NONE)
  1560. return (KMF_ERR_BAD_ALGORITHM);
  1561. if (key->keyalg == KMF_RSA) {
  1562. EVP_PKEY *pkey = (EVP_PKEY *)key->keyp;
  1563. uchar_t *p;
  1564. int len;
  1565. if (AlgId == KMF_ALGID_MD5WithRSA)
  1566. md = EVP_md5();
  1567. else if (AlgId == KMF_ALGID_MD2WithRSA)
  1568. md = EVP_md2();
  1569. else if (AlgId == KMF_ALGID_SHA1WithRSA)
  1570. md = EVP_sha1();
  1571. else if (AlgId == KMF_ALGID_SHA256WithRSA)
  1572. md = EVP_sha256();
  1573. else if (AlgId == KMF_ALGID_SHA384WithRSA)
  1574. md = EVP_sha384();
  1575. else if (AlgId == KMF_ALGID_SHA512WithRSA)
  1576. md = EVP_sha512();
  1577. else if (AlgId == KMF_ALGID_RSA)
  1578. md = NULL;
  1579. else
  1580. return (KMF_ERR_BAD_ALGORITHM);
  1581. if ((md == NULL) && (AlgId == KMF_ALGID_RSA)) {
  1582. RSA *rsa = EVP_PKEY_get1_RSA((EVP_PKEY *)pkey);
  1583. p = output->Data;
  1584. if ((len = RSA_private_encrypt(tobesigned->Length,
  1585. tobesigned->Data, p, rsa,
  1586. RSA_PKCS1_PADDING)) <= 0) {
  1587. SET_ERROR(kmfh, ERR_get_error());
  1588. ret = KMF_ERR_INTERNAL;
  1589. }
  1590. output->Length = len;
  1591. } else {
  1592. (void) EVP_MD_CTX_init(&ctx);
  1593. (void) EVP_SignInit_ex(&ctx, md, NULL);
  1594. (void) EVP_SignUpdate(&ctx, tobesigned->Data,
  1595. (uint32_t)tobesigned->Length);
  1596. len = (uint32_t)output->Length;
  1597. p = output->Data;
  1598. if (!EVP_SignFinal(&ctx, p, (uint32_t *)&len, pkey)) {
  1599. SET_ERROR(kmfh, ERR_get_error());
  1600. len = 0;
  1601. ret = KMF_ERR_INTERNAL;
  1602. }
  1603. output->Length = len;
  1604. (void) EVP_MD_CTX_cleanup(&ctx);
  1605. }
  1606. } else if (key->keyalg == KMF_DSA) {
  1607. DSA *dsa = EVP_PKEY_get1_DSA(key->keyp);
  1608. uchar_t hash[EVP_MAX_MD_SIZE];
  1609. uint32_t hashlen;
  1610. DSA_SIG *dsasig;
  1611. if (AlgId == KMF_ALGID_DSA ||
  1612. AlgId == KMF_ALGID_SHA1WithDSA)
  1613. md = EVP_sha1();
  1614. else if (AlgId == KMF_ALGID_SHA256WithDSA)
  1615. md = EVP_sha256();
  1616. else /* Bad algorithm */
  1617. return (KMF_ERR_BAD_ALGORITHM);
  1618. /*
  1619. * OpenSSL EVP_Sign operation automatically converts to
  1620. * ASN.1 output so we do the operations separately so we
  1621. * are assured of NOT getting ASN.1 output returned.
  1622. * KMF does not want ASN.1 encoded results because
  1623. * not all mechanisms return ASN.1 encodings (PKCS#11
  1624. * and NSS return raw signature data).
  1625. */
  1626. EVP_MD_CTX_init(&ctx);
  1627. (void) EVP_DigestInit_ex(&ctx, md, NULL);
  1628. (void) EVP_DigestUpdate(&ctx, tobesigned->Data,
  1629. tobesigned->Length);
  1630. (void) EVP_DigestFinal_ex(&ctx, hash, &hashlen);
  1631. /* Only sign first 20 bytes for SHA2 */
  1632. if (AlgId == KMF_ALGID_SHA256WithDSA)
  1633. hashlen = 20;
  1634. dsasig = DSA_do_sign(hash, hashlen, dsa);
  1635. if (dsasig != NULL) {
  1636. int i;
  1637. output->Length = i = fixbnlen(dsasig->r, output->Data,
  1638. hashlen);
  1639. output->Length += fixbnlen(dsasig->s, &output->Data[i],
  1640. hashlen);
  1641. DSA_SIG_free(dsasig);
  1642. } else {
  1643. SET_ERROR(kmfh, ERR_get_error());
  1644. }
  1645. (void) EVP_MD_CTX_cleanup(&ctx);
  1646. } else {
  1647. return (KMF_ERR_BAD_PARAMETER);
  1648. }
  1649. cleanup:
  1650. return (ret);
  1651. }
  1652. KMF_RETURN
  1653. /*ARGSUSED*/
  1654. OpenSSL_DeleteKey(KMF_HANDLE_T handle,
  1655. int numattr, KMF_ATTRIBUTE *attrlist)
  1656. {
  1657. KMF_RETURN rv = KMF_OK;
  1658. KMF_KEY_HANDLE *key;
  1659. boolean_t destroy = B_TRUE;
  1660. key = kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR, attrlist, numattr);
  1661. if (key == NULL || key->keyp == NULL)
  1662. return (KMF_ERR_BAD_PARAMETER);
  1663. rv = kmf_get_attr(KMF_DESTROY_BOOL_ATTR, attrlist, numattr,
  1664. (void *)&destroy, NULL);
  1665. if (rv != KMF_OK) {
  1666. /* "destroy" is optional. Default is TRUE */
  1667. rv = KMF_OK;
  1668. }
  1669. if (key->keyclass != KMF_ASYM_PUB &&
  1670. key->keyclass != KMF_ASYM_PRI &&
  1671. key->keyclass != KMF_SYMMETRIC)
  1672. return (KMF_ERR_BAD_KEY_CLASS);
  1673. if (key->keyclass == KMF_SYMMETRIC) {
  1674. kmf_free_raw_sym_key((KMF_RAW_SYM_KEY *)key->keyp);
  1675. key->keyp = NULL;
  1676. } else {
  1677. if (key->keyp != NULL) {
  1678. EVP_PKEY_free(key->keyp);
  1679. key->keyp = NULL;
  1680. }
  1681. }
  1682. if (key->keylabel != NULL) {
  1683. EVP_PKEY *pkey = NULL;
  1684. /* If the file exists, make sure it is a proper key. */
  1685. pkey = openssl_load_key(handle, key->keylabel);
  1686. if (pkey == NULL) {
  1687. if (key->keylabel != NULL) {
  1688. free(key->keylabel);
  1689. key->keylabel = NULL;
  1690. }
  1691. return (KMF_ERR_KEY_NOT_FOUND);
  1692. }
  1693. EVP_PKEY_free(pkey);
  1694. if (destroy) {
  1695. if (unlink(key->keylabel) != 0) {
  1696. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1697. SET_SYS_ERROR(kmfh, errno);
  1698. rv = KMF_ERR_INTERNAL;
  1699. }
  1700. }
  1701. if (key->keylabel != NULL) {
  1702. free(key->keylabel);
  1703. key->keylabel = NULL;
  1704. }
  1705. }
  1706. return (rv);
  1707. }
  1708. KMF_RETURN
  1709. OpenSSL_GetErrorString(KMF_HANDLE_T handle, char **msgstr)
  1710. {
  1711. KMF_RETURN ret = KMF_OK;
  1712. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1713. char str[256]; /* OpenSSL needs at least 120 byte buffer */
  1714. ERR_error_string_n(kmfh->lasterr.errcode, str, sizeof (str));
  1715. if (strlen(str)) {
  1716. *msgstr = (char *)strdup(str);
  1717. if ((*msgstr) == NULL)
  1718. ret = KMF_ERR_MEMORY;
  1719. } else {
  1720. *msgstr = NULL;
  1721. }
  1722. return (ret);
  1723. }
  1724. static int
  1725. ext2NID(int kmfext)
  1726. {
  1727. switch (kmfext) {
  1728. case KMF_X509_EXT_KEY_USAGE:
  1729. return (NID_key_usage);
  1730. case KMF_X509_EXT_PRIV_KEY_USAGE_PERIOD:
  1731. return (NID_private_key_usage_period);
  1732. case KMF_X509_EXT_CERT_POLICIES:
  1733. return (NID_certificate_policies);
  1734. case KMF_X509_EXT_SUBJ_ALTNAME:
  1735. return (NID_subject_alt_name);
  1736. case KMF_X509_EXT_ISSUER_ALTNAME:
  1737. return (NID_issuer_alt_name);
  1738. case KMF_X509_EXT_BASIC_CONSTRAINTS:
  1739. return (NID_basic_constraints);
  1740. case KMF_X509_EXT_EXT_KEY_USAGE:
  1741. return (NID_ext_key_usage);
  1742. case KMF_X509_EXT_AUTH_KEY_ID:
  1743. return (NID_authority_key_identifier);
  1744. case KMF_X509_EXT_CRL_DIST_POINTS:
  1745. return (NID_crl_distribution_points);
  1746. case KMF_X509_EXT_SUBJ_KEY_ID:
  1747. return (NID_subject_key_identifier);
  1748. case KMF_X509_EXT_POLICY_MAPPINGS:
  1749. return (OBJ_sn2nid("policyMappings"));
  1750. case KMF_X509_EXT_NAME_CONSTRAINTS:
  1751. return (OBJ_sn2nid("nameConstraints"));
  1752. case KMF_X509_EXT_POLICY_CONSTRAINTS:
  1753. return (OBJ_sn2nid("policyConstraints"));
  1754. case KMF_X509_EXT_INHIBIT_ANY_POLICY:
  1755. return (OBJ_sn2nid("inhibitAnyPolicy"));
  1756. case KMF_X509_EXT_FRESHEST_CRL:
  1757. return (OBJ_sn2nid("freshestCRL"));
  1758. default:
  1759. return (NID_undef);
  1760. }
  1761. }
  1762. KMF_RETURN
  1763. OpenSSL_CertGetPrintable(KMF_HANDLE_T handle, const KMF_DATA *pcert,
  1764. KMF_PRINTABLE_ITEM flag, char *resultStr)
  1765. {
  1766. KMF_RETURN ret = KMF_OK;
  1767. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1768. X509 *xcert = NULL;
  1769. unsigned char *outbuf = NULL;
  1770. unsigned char *outbuf_p;
  1771. char *tmpstr = NULL;
  1772. int j;
  1773. int ext_index, nid, len;
  1774. BIO *mem = NULL;
  1775. STACK *emlst = NULL;
  1776. X509_EXTENSION *ex;
  1777. X509_CINF *ci;
  1778. if (pcert == NULL || pcert->Data == NULL || pcert->Length == 0) {
  1779. return (KMF_ERR_BAD_PARAMETER);
  1780. }
  1781. /* copy cert data to outbuf */
  1782. outbuf = malloc(pcert->Length);
  1783. if (outbuf == NULL) {
  1784. return (KMF_ERR_MEMORY);
  1785. }
  1786. (void) memcpy(outbuf, pcert->Data, pcert->Length);
  1787. outbuf_p = outbuf; /* use a temp pointer; required by openssl */
  1788. xcert = d2i_X509(NULL, (const uchar_t **)&outbuf_p, pcert->Length);
  1789. if (xcert == NULL) {
  1790. SET_ERROR(kmfh, ERR_get_error());
  1791. ret = KMF_ERR_ENCODING;
  1792. goto out;
  1793. }
  1794. mem = BIO_new(BIO_s_mem());
  1795. if (mem == NULL) {
  1796. SET_ERROR(kmfh, ERR_get_error());
  1797. ret = KMF_ERR_MEMORY;
  1798. goto out;
  1799. }
  1800. switch (flag) {
  1801. case KMF_CERT_ISSUER:
  1802. (void) X509_NAME_print_ex(mem, X509_get_issuer_name(xcert), 0,
  1803. XN_FLAG_SEP_CPLUS_SPC);
  1804. len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1805. break;
  1806. case KMF_CERT_SUBJECT:
  1807. (void) X509_NAME_print_ex(mem, X509_get_subject_name(xcert), 0,
  1808. XN_FLAG_SEP_CPLUS_SPC);
  1809. len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1810. break;
  1811. case KMF_CERT_VERSION:
  1812. tmpstr = i2s_ASN1_INTEGER(NULL, xcert->cert_info->version);
  1813. (void) strncpy(resultStr, tmpstr, KMF_CERT_PRINTABLE_LEN);
  1814. OPENSSL_free(tmpstr);
  1815. len = strlen(resultStr);
  1816. break;
  1817. case KMF_CERT_SERIALNUM:
  1818. if (i2a_ASN1_INTEGER(mem, X509_get_serialNumber(xcert)) > 0) {
  1819. (void) strcpy(resultStr, "0x");
  1820. len = BIO_gets(mem, &resultStr[2],
  1821. KMF_CERT_PRINTABLE_LEN - 2);
  1822. }
  1823. break;
  1824. case KMF_CERT_NOTBEFORE:
  1825. (void) ASN1_TIME_print(mem, X509_get_notBefore(xcert));
  1826. len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1827. break;
  1828. case KMF_CERT_NOTAFTER:
  1829. (void) ASN1_TIME_print(mem, X509_get_notAfter(xcert));
  1830. len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1831. break;
  1832. case KMF_CERT_PUBKEY_DATA:
  1833. {
  1834. EVP_PKEY *pkey = X509_get_pubkey(xcert);
  1835. if (pkey == NULL) {
  1836. SET_ERROR(kmfh, ERR_get_error());
  1837. ret = KMF_ERR_ENCODING;
  1838. goto out;
  1839. }
  1840. if (pkey->type == EVP_PKEY_RSA) {
  1841. (void) BIO_printf(mem,
  1842. "RSA Public Key: (%d bit)\n",
  1843. BN_num_bits(pkey->pkey.rsa->n));
  1844. (void) RSA_print(mem, pkey->pkey.rsa, 0);
  1845. } else if (pkey->type == EVP_PKEY_DSA) {
  1846. (void) BIO_printf(mem,
  1847. "%12sDSA Public Key:\n", "");
  1848. (void) DSA_print(mem, pkey->pkey.dsa, 0);
  1849. } else {
  1850. (void) BIO_printf(mem,
  1851. "%12sUnknown Public Key:\n", "");
  1852. }
  1853. (void) BIO_printf(mem, "\n");
  1854. EVP_PKEY_free(pkey);
  1855. }
  1856. len = BIO_read(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1857. break;
  1858. case KMF_CERT_SIGNATURE_ALG:
  1859. case KMF_CERT_PUBKEY_ALG:
  1860. if (flag == KMF_CERT_SIGNATURE_ALG) {
  1861. len = i2a_ASN1_OBJECT(mem,
  1862. xcert->sig_alg->algorithm);
  1863. } else {
  1864. len = i2a_ASN1_OBJECT(mem,
  1865. xcert->cert_info->key->algor->algorithm);
  1866. }
  1867. if (len > 0) {
  1868. len = BIO_read(mem, resultStr,
  1869. KMF_CERT_PRINTABLE_LEN);
  1870. }
  1871. break;
  1872. case KMF_CERT_EMAIL:
  1873. emlst = X509_get1_email(xcert);
  1874. for (j = 0; j < sk_num(emlst); j++)
  1875. (void) BIO_printf(mem, "%s\n", sk_value(emlst, j));
  1876. len = BIO_gets(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1877. X509_email_free(emlst);
  1878. break;
  1879. case KMF_X509_EXT_ISSUER_ALTNAME:
  1880. case KMF_X509_EXT_SUBJ_ALTNAME:
  1881. case KMF_X509_EXT_KEY_USAGE:
  1882. case KMF_X509_EXT_PRIV_KEY_USAGE_PERIOD:
  1883. case KMF_X509_EXT_CERT_POLICIES:
  1884. case KMF_X509_EXT_BASIC_CONSTRAINTS:
  1885. case KMF_X509_EXT_NAME_CONSTRAINTS:
  1886. case KMF_X509_EXT_POLICY_CONSTRAINTS:
  1887. case KMF_X509_EXT_EXT_KEY_USAGE:
  1888. case KMF_X509_EXT_INHIBIT_ANY_POLICY:
  1889. case KMF_X509_EXT_AUTH_KEY_ID:
  1890. case KMF_X509_EXT_SUBJ_KEY_ID:
  1891. case KMF_X509_EXT_POLICY_MAPPINGS:
  1892. case KMF_X509_EXT_CRL_DIST_POINTS:
  1893. case KMF_X509_EXT_FRESHEST_CRL:
  1894. nid = ext2NID(flag);
  1895. if (nid == NID_undef) {
  1896. ret = KMF_ERR_EXTENSION_NOT_FOUND;
  1897. goto out;
  1898. }
  1899. ci = xcert->cert_info;
  1900. ext_index = X509v3_get_ext_by_NID(ci->extensions, nid, -1);
  1901. if (ext_index == -1) {
  1902. SET_ERROR(kmfh, ERR_get_error());
  1903. ret = KMF_ERR_EXTENSION_NOT_FOUND;
  1904. goto out;
  1905. }
  1906. ex = X509v3_get_ext(ci->extensions, ext_index);
  1907. (void) i2a_ASN1_OBJECT(mem, X509_EXTENSION_get_object(ex));
  1908. if (BIO_printf(mem, ": %s\n",
  1909. X509_EXTENSION_get_critical(ex) ? "critical" : "") <= 0) {
  1910. SET_ERROR(kmfh, ERR_get_error());
  1911. ret = KMF_ERR_ENCODING;
  1912. goto out;
  1913. }
  1914. if (!X509V3_EXT_print(mem, ex, X509V3_EXT_DUMP_UNKNOWN, 4)) {
  1915. (void) BIO_printf(mem, "%*s", 4, "");
  1916. (void) M_ASN1_OCTET_STRING_print(mem, ex->value);
  1917. }
  1918. if (BIO_write(mem, "\n", 1) <= 0) {
  1919. SET_ERROR(kmfh, ERR_get_error());
  1920. ret = KMF_ERR_ENCODING;
  1921. goto out;
  1922. }
  1923. len = BIO_read(mem, resultStr, KMF_CERT_PRINTABLE_LEN);
  1924. }
  1925. if (len <= 0) {
  1926. SET_ERROR(kmfh, ERR_get_error());
  1927. ret = KMF_ERR_ENCODING;
  1928. }
  1929. out:
  1930. if (outbuf != NULL) {
  1931. free(outbuf);
  1932. }
  1933. if (xcert != NULL) {
  1934. X509_free(xcert);
  1935. }
  1936. if (mem != NULL) {
  1937. (void) BIO_free(mem);
  1938. }
  1939. return (ret);
  1940. }
  1941. KMF_RETURN
  1942. /*ARGSUSED*/
  1943. OpenSSL_FindPrikeyByCert(KMF_HANDLE_T handle, int numattr,
  1944. KMF_ATTRIBUTE *attrlist)
  1945. {
  1946. KMF_RETURN rv = KMF_OK;
  1947. KMF_KEYSTORE_TYPE kstype = KMF_KEYSTORE_OPENSSL;
  1948. KMF_KEY_CLASS keyclass = KMF_ASYM_PRI;
  1949. KMF_KEY_HANDLE *key = NULL;
  1950. uint32_t numkeys = 1; /* 1 key only */
  1951. char *dirpath = NULL;
  1952. char *keyfile = NULL;
  1953. KMF_ATTRIBUTE new_attrlist[16];
  1954. int i = 0;
  1955. /*
  1956. * This is really just a FindKey operation, reuse the
  1957. * FindKey function.
  1958. */
  1959. kmf_set_attr_at_index(new_attrlist, i,
  1960. KMF_KEYSTORE_TYPE_ATTR, &kstype, sizeof (kstype));
  1961. i++;
  1962. kmf_set_attr_at_index(new_attrlist, i,
  1963. KMF_COUNT_ATTR, &numkeys, sizeof (uint32_t));
  1964. i++;
  1965. kmf_set_attr_at_index(new_attrlist, i,
  1966. KMF_KEYCLASS_ATTR, &keyclass, sizeof (keyclass));
  1967. i++;
  1968. key = kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR, attrlist, numattr);
  1969. if (key == NULL) {
  1970. return (KMF_ERR_BAD_PARAMETER);
  1971. } else {
  1972. kmf_set_attr_at_index(new_attrlist, i,
  1973. KMF_KEY_HANDLE_ATTR, key, sizeof (KMF_KEY_HANDLE));
  1974. i++;
  1975. }
  1976. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1977. if (dirpath != NULL) {
  1978. kmf_set_attr_at_index(new_attrlist, i,
  1979. KMF_DIRPATH_ATTR, dirpath, strlen(dirpath));
  1980. i++;
  1981. }
  1982. keyfile = kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR, attrlist, numattr);
  1983. if (keyfile == NULL)
  1984. return (KMF_ERR_BAD_PARAMETER);
  1985. else {
  1986. kmf_set_attr_at_index(new_attrlist, i,
  1987. KMF_KEY_FILENAME_ATTR, keyfile, strlen(keyfile));
  1988. i++;
  1989. }
  1990. rv = OpenSSL_FindKey(handle, i, new_attrlist);
  1991. return (rv);
  1992. }
  1993. KMF_RETURN
  1994. /*ARGSUSED*/
  1995. OpenSSL_DecryptData(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key,
  1996. KMF_OID *AlgOID, KMF_DATA *ciphertext,
  1997. KMF_DATA *output)
  1998. {
  1999. KMF_RETURN ret = KMF_OK;
  2000. RSA *rsa = NULL;
  2001. unsigned int in_len = 0, out_len = 0;
  2002. unsigned int total_decrypted = 0, modulus_len = 0;
  2003. uint8_t *in_data, *out_data;
  2004. int i, blocks;
  2005. if (key == NULL || AlgOID == NULL ||
  2006. ciphertext == NULL || output == NULL ||
  2007. ciphertext->Data == NULL ||
  2008. output->Data == NULL)
  2009. return (KMF_ERR_BAD_PARAMETER);
  2010. if (key->keyalg == KMF_RSA) {
  2011. rsa = EVP_PKEY_get1_RSA((EVP_PKEY *)key->keyp);
  2012. modulus_len = RSA_size(rsa);
  2013. } else {
  2014. return (KMF_ERR_BAD_PARAMETER);
  2015. }
  2016. blocks = ciphertext->Length/modulus_len;
  2017. out_data = output->Data;
  2018. in_data = ciphertext->Data;
  2019. out_len = modulus_len - 11;
  2020. in_len = modulus_len;
  2021. for (i = 0; i < blocks; i++) {
  2022. out_len = RSA_private_decrypt(in_len,
  2023. in_data, out_data, rsa, RSA_PKCS1_PADDING);
  2024. if (out_len == 0) {
  2025. ret = KMF_ERR_INTERNAL;
  2026. goto cleanup;
  2027. }
  2028. out_data += out_len;
  2029. total_decrypted += out_len;
  2030. in_data += in_len;
  2031. }
  2032. output->Length = total_decrypted;
  2033. cleanup:
  2034. RSA_free(rsa);
  2035. if (ret != KMF_OK)
  2036. output->Length = 0;
  2037. return (ret);
  2038. }
  2039. /*
  2040. * This function will create a certid from issuer_cert and user_cert.
  2041. * The caller should use OCSP_CERTID_free(OCSP_CERTID *) to deallocate
  2042. * certid memory after use.
  2043. */
  2044. static KMF_RETURN
  2045. create_certid(KMF_HANDLE_T handle, const KMF_DATA *issuer_cert,
  2046. const KMF_DATA *user_cert, OCSP_CERTID **certid)
  2047. {
  2048. KMF_RETURN ret = KMF_OK;
  2049. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  2050. X509 *issuer = NULL;
  2051. X509 *cert = NULL;
  2052. unsigned char *ptmp;
  2053. if (issuer_cert == NULL || user_cert == NULL) {
  2054. return (KMF_ERR_BAD_PARAMETER);
  2055. }
  2056. /* convert the DER-encoded issuer cert to an internal X509 */
  2057. ptmp = issuer_cert->Data;
  2058. issuer = d2i_X509(NULL, (const uchar_t **)&ptmp,
  2059. issuer_cert->Length);
  2060. if (issuer == NULL) {
  2061. SET_ERROR(kmfh, ERR_get_error());
  2062. ret = KMF_ERR_OCSP_BAD_ISSUER;
  2063. goto end;
  2064. }
  2065. /* convert the DER-encoded user cert to an internal X509 */
  2066. ptmp = user_cert->Data;
  2067. cert = d2i_X509(NULL, (const uchar_t **)&ptmp,
  2068. user_cert->Length);
  2069. if (cert == NULL) {
  2070. SET_ERROR(kmfh, ERR_get_error());
  2071. ret = KMF_ERR_OCSP_BAD_CERT;
  2072. goto end;
  2073. }
  2074. /* create a CERTID */
  2075. *certid = OCSP_cert_to_id(NULL, cert, issuer);
  2076. if (*certid == NULL) {
  2077. SET_ERROR(kmfh, ERR_get_error());
  2078. ret = KMF_ERR_OCSP_CERTID;
  2079. goto end;
  2080. }
  2081. end:
  2082. if (issuer != NULL) {
  2083. X509_free(issuer);
  2084. }
  2085. if (cert != NULL) {
  2086. X509_free(cert);
  2087. }
  2088. return (ret);
  2089. }
  2090. KMF_RETURN
  2091. OpenSSL_CreateOCSPRequest(KMF_HANDLE_T handle,
  2092. int numattr, KMF_ATTRIBUTE *attrlist)
  2093. {
  2094. KMF_RETURN ret = KMF_OK;
  2095. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  2096. OCSP_CERTID *id = NULL;
  2097. OCSP_REQUEST *req = NULL;
  2098. BIO *derbio = NULL;
  2099. char *reqfile;
  2100. KMF_DATA *issuer_cert;
  2101. KMF_DATA *user_cert;
  2102. user_cert = kmf_get_attr_ptr(KMF_USER_CERT_DATA_ATTR,
  2103. attrlist, numattr);
  2104. if (user_cert == NULL)
  2105. return (KMF_ERR_BAD_PARAMETER);
  2106. issuer_cert = kmf_get_attr_ptr(KMF_ISSUER_CERT_DATA_ATTR,
  2107. attrlist, numattr);
  2108. if (issuer_cert == NULL)
  2109. return (KMF_ERR_BAD_PARAMETER);
  2110. reqfile = kmf_get_attr_ptr(KMF_OCSP_REQUEST_FILENAME_ATTR,
  2111. attrlist, numattr);
  2112. if (reqfile == NULL)
  2113. return (KMF_ERR_BAD_PARAMETER);
  2114. ret = create_certid(handle, issuer_cert, user_cert, &id);
  2115. if (ret != KMF_OK) {
  2116. return (ret);
  2117. }
  2118. /* Create an OCSP request */
  2119. req = OCSP_REQUEST_new();
  2120. if (req == NULL) {
  2121. SET_ERROR(kmfh, ERR_get_error());
  2122. ret = KMF_ERR_OCSP_CREATE_REQUEST;
  2123. goto end;
  2124. }
  2125. if (!OCSP_request_add0_id(req, id)) {
  2126. ret = KMF_ERR_OCSP_CREATE_REQUEST;
  2127. goto end;
  2128. }
  2129. /* Write the request to the output file with DER encoding */
  2130. derbio = BIO_new_file(reqfile, "wb");
  2131. if (!derbio) {
  2132. SET_ERROR(kmfh, ERR_get_error());
  2133. ret = KMF_ERR_OPEN_FILE;
  2134. goto end;
  2135. }
  2136. if (i2d_OCSP_REQUEST_bio(derbio, req) <= 0) {
  2137. ret = KMF_ERR_ENCODING;
  2138. }
  2139. end:
  2140. /*
  2141. * We don't need to free "id" explicitely, because OCSP_REQUEST_free()
  2142. * will also deallocate certid's space.
  2143. */
  2144. if (req != NULL) {
  2145. OCSP_REQUEST_free(req);
  2146. }
  2147. if (derbio != NULL) {
  2148. (void) BIO_free(derbio);
  2149. }
  2150. return (ret);
  2151. }
  2152. /* ocsp_find_signer_sk() is copied from openssl source */
  2153. static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
  2154. {
  2155. int i;
  2156. unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
  2157. /* Easy if lookup by name */
  2158. if (id->type == V_OCSP_RESPID_NAME)
  2159. return (X509_find_by_subject(certs, id->value.byName));
  2160. /* Lookup by key hash */
  2161. /* If key hash isn't SHA1 length then forget it */
  2162. if (id->value.byKey->length != SHA_DIGEST_LENGTH)
  2163. return (NULL);
  2164. keyhash = id->value.byKey->data;
  2165. /* Calculate hash of each key and compare */
  2166. for (i = 0; i < sk_X509_num(certs); i++) {
  2167. /* LINTED E_BAD_PTR_CAST_ALIGN */
  2168. X509 *x = sk_X509_value(certs, i);
  2169. /* Use pubkey_digest to get the key ID value */
  2170. (void) X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL);
  2171. if (!memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH))
  2172. return (x);
  2173. }
  2174. return (NULL);
  2175. }
  2176. /* ocsp_find_signer() is copied from openssl source */
  2177. /* ARGSUSED2 */
  2178. static int
  2179. ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
  2180. X509_STORE *st, unsigned long flags)
  2181. {
  2182. X509 *signer;
  2183. OCSP_RESPID *rid = bs->tbsResponseData->responderId;
  2184. if ((signer = ocsp_find_signer_sk(certs, rid))) {
  2185. *psigner = signer;
  2186. return (2);
  2187. }
  2188. if (!(flags & OCSP_NOINTERN) &&
  2189. (signer = ocsp_find_signer_sk(bs->certs, rid))) {
  2190. *psigner = signer;
  2191. return (1);
  2192. }
  2193. /* Maybe lookup from store if by subject name */
  2194. *psigner = NULL;
  2195. return (0);
  2196. }
  2197. /*
  2198. * This function will verify the signature of a basic response, using
  2199. * the public key from the OCSP responder certificate.
  2200. */
  2201. static KMF_RETURN
  2202. check_response_signature(KMF_HANDLE_T handle, OCSP_BASICRESP *bs,
  2203. KMF_DATA *signer_cert, KMF_DATA *issuer_cert)
  2204. {
  2205. KMF_RETURN ret = KMF_OK;
  2206. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  2207. STACK_OF(X509) *cert_stack = NULL;
  2208. X509 *signer = NULL;
  2209. X509 *issuer = NULL;
  2210. EVP_PKEY *skey = NULL;
  2211. unsigned char *ptmp;
  2212. if (bs == NULL || issuer_cert == NULL)
  2213. return (KMF_ERR_BAD_PARAMETER);
  2214. /*
  2215. * Find the certificate that signed the basic response.
  2216. *
  2217. * If signer_cert is not NULL, we will use that as the signer cert.
  2218. * Otherwise, we will check if the issuer cert is actually the signer.
  2219. * If we still do not find a signer, we will look for it from the
  2220. * certificate list came with the response file.
  2221. */
  2222. if (signer_cert != NULL) {
  2223. ptmp = signer_cert->Data;
  2224. signer = d2i_X509(NULL, (const uchar_t **)&ptmp,
  2225. signer_cert->Length);
  2226. if (signer == NULL) {
  2227. SET_ERROR(kmfh, ERR_get_error());
  2228. ret = KMF_ERR_OCSP_BAD_SIGNER;
  2229. goto end;
  2230. }
  2231. } else {
  2232. /*
  2233. * Convert the issuer cert into X509 and push it into a
  2234. * stack to be used by ocsp_find_signer().
  2235. */
  2236. ptmp = issuer_cert->Data;
  2237. issuer = d2i_X509(NULL, (const uchar_t **)&ptmp,
  2238. issuer_cert->Length);
  2239. if (issuer == NULL) {
  2240. SET_ERROR(kmfh, ERR_get_error());
  2241. ret = KMF_ERR_OCSP_BAD_ISSUER;
  2242. goto end;
  2243. }
  2244. if ((cert_stack = sk_X509_new_null()) == NULL) {
  2245. ret = KMF_ERR_INTERNAL;
  2246. goto end;
  2247. }
  2248. if (sk_X509_push(cert_stack, issuer) == NULL) {
  2249. ret = KMF_ERR_INTERNAL;
  2250. goto end;
  2251. }
  2252. ret = ocsp_find_signer(&signer, bs, cert_stack, NULL, 0);
  2253. if (!ret) {
  2254. /* can not find the signer */
  2255. ret = KMF_ERR_OCSP_BAD_SIGNER;
  2256. goto end;
  2257. }
  2258. }
  2259. /* Verify the signature of the response */
  2260. skey = X509_get_pubkey(signer);
  2261. if (skey == NULL) {
  2262. ret = KMF_ERR_OCSP_BAD_SIGNER;
  2263. goto end;
  2264. }
  2265. ret = OCSP_BASICRESP_verify(bs, skey, 0);
  2266. if (ret == 0) {
  2267. ret = KMF_ERR_OCSP_RESPONSE_SIGNATURE;
  2268. goto end;
  2269. }
  2270. end:
  2271. if (issuer != NULL) {
  2272. X509_free(issuer);
  2273. }
  2274. if (signer != NULL) {
  2275. X509_free(signer);
  2276. }
  2277. if (skey != NULL) {
  2278. EVP_PKEY_free(skey);
  2279. }
  2280. if (cert_stack != NULL) {
  2281. sk_X509_free(cert_stack);
  2282. }
  2283. return (ret);
  2284. }
  2285. KMF_RETURN
  2286. OpenSSL_GetOCSPStatusForCert(KMF_HANDLE_T handle,
  2287. int numattr, KMF_ATTRIBUTE *attrlist)
  2288. {
  2289. KMF_RETURN ret = KMF_OK;
  2290. BIO *derbio = NULL;
  2291. OCSP_RESPONSE *resp = NULL;
  2292. OCSP_BASICRESP *bs = NULL;
  2293. OCSP_CERTID *id = NULL;
  2294. OCSP_SINGLERESP *single = NULL;
  2295. ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
  2296. int index, status, reason;
  2297. KMF_DATA *issuer_cert;
  2298. KMF_DATA *user_cert;
  2299. KMF_DATA *signer_cert;
  2300. KMF_DATA *response;
  2301. int *response_reason, *response_status, *cert_status;
  2302. boolean_t ignore_response_sign = B_FALSE; /* default is FALSE */
  2303. uint32_t response_lifetime;
  2304. issuer_cert = kmf_get_attr_ptr(KMF_ISSUER_CERT_DATA_ATTR,
  2305. attrlist, numattr);
  2306. if (issuer_cert == NULL)
  2307. return (KMF_ERR_BAD_PARAMETER);
  2308. user_cert = kmf_get_attr_ptr(KMF_USER_CERT_DATA_ATTR,
  2309. attrlist, numattr);
  2310. if (user_cert == NULL)
  2311. return (KMF_ERR_BAD_PARAMETER);
  2312. response = kmf_get_attr_ptr(KMF_OCSP_RESPONSE_DATA_ATTR,
  2313. attrlist, numattr);
  2314. if (response == NULL)
  2315. return (KMF_ERR_BAD_PARAMETER);
  2316. response_status = kmf_get_attr_ptr(KMF_OCSP_RESPONSE_STATUS_ATTR,
  2317. attrlist, numattr);
  2318. if (response_status == NULL)
  2319. return (KMF_ERR_BAD_PARAMETER);
  2320. response_reason = kmf_get_attr_ptr(KMF_OCSP_RESPONSE_REASON_ATTR,
  2321. attrlist, numattr);
  2322. if (response_reason == NULL)
  2323. return (KMF_ERR_BAD_PARAMETER);
  2324. cert_status = kmf_get_attr_ptr(KMF_OCSP_RESPONSE_CERT_STATUS_ATTR,
  2325. attrlist, numattr);
  2326. if (cert_status == NULL)
  2327. return (KMF_ERR_BAD_PARAMETER);
  2328. /* Read in the response */
  2329. derbio = BIO_new_mem_buf(response->Data, response->Length);
  2330. if (!derbio) {
  2331. ret = KMF_ERR_MEMORY;
  2332. return (ret);
  2333. }
  2334. resp = d2i_OCSP_RESPONSE_bio(derbio, NULL);
  2335. if (resp == NULL) {
  2336. ret = KMF_ERR_OCSP_MALFORMED_RESPONSE;
  2337. goto end;
  2338. }
  2339. /* Check the response status */
  2340. status = OCSP_response_status(resp);
  2341. *response_status = status;
  2342. if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
  2343. ret = KMF_ERR_OCSP_RESPONSE_STATUS;
  2344. goto end;
  2345. }
  2346. #ifdef DEBUG
  2347. printf("Successfully checked the response file status.\n");
  2348. #endif /* DEBUG */
  2349. /* Extract basic response */
  2350. bs = OCSP_response_get1_basic(resp);
  2351. if (bs == NULL) {
  2352. ret = KMF_ERR_OCSP_NO_BASIC_RESPONSE;
  2353. goto end;
  2354. }
  2355. #ifdef DEBUG
  2356. printf("Successfully retrieved the basic response.\n");
  2357. #endif /* DEBUG */
  2358. /* Check the basic response signature if required */
  2359. ret = kmf_get_attr(KMF_IGNORE_RESPONSE_SIGN_ATTR, attrlist, numattr,
  2360. (void *)&ignore_response_sign, NULL);
  2361. if (ret != KMF_OK)
  2362. ret = KMF_OK;
  2363. signer_cert = kmf_get_attr_ptr(KMF_SIGNER_CERT_DATA_ATTR,
  2364. attrlist, numattr);
  2365. if (ignore_response_sign == B_FALSE) {
  2366. ret = check_response_signature(handle, bs,
  2367. signer_cert, issuer_cert);
  2368. if (ret != KMF_OK)
  2369. goto end;
  2370. }
  2371. #ifdef DEBUG
  2372. printf("Successfully verified the response signature.\n");
  2373. #endif /* DEBUG */
  2374. /* Create a certid for the certificate in question */
  2375. ret = create_certid(handle, issuer_cert, user_cert, &id);
  2376. if (ret != KMF_OK) {
  2377. ret = KMF_ERR_OCSP_CERTID;
  2378. goto end;
  2379. }
  2380. #ifdef DEBUG
  2381. printf("successfully created a certid for the cert.\n");
  2382. #endif /* DEBUG */
  2383. /* Find the index of the single response for the certid */
  2384. index = OCSP_resp_find(bs, id, -1);
  2385. if (index < 0) {
  2386. /* cound not find this certificate in the response */
  2387. ret = KMF_ERR_OCSP_UNKNOWN_CERT;
  2388. goto end;
  2389. }
  2390. #ifdef DEBUG
  2391. printf("Successfully found the single response index for the cert.\n");
  2392. #endif /* DEBUG */
  2393. /* Retrieve the single response and get the cert status */
  2394. single = OCSP_resp_get0(bs, index);
  2395. status = OCSP_single_get0_status(single, &reason, &rev, &thisupd,
  2396. &nextupd);
  2397. if (status == V_OCSP_CERTSTATUS_GOOD) {
  2398. *cert_status = OCSP_GOOD;
  2399. } else if (status == V_OCSP_CERTSTATUS_UNKNOWN) {
  2400. *cert_status = OCSP_UNKNOWN;
  2401. } else { /* revoked */
  2402. *cert_status = OCSP_REVOKED;
  2403. *response_reason = reason;
  2404. }
  2405. ret = KMF_OK;
  2406. /* resp. time is optional, so we don't care about the return code. */
  2407. (void) kmf_get_attr(KMF_RESPONSE_LIFETIME_ATTR, attrlist, numattr,
  2408. (void *)&response_lifetime, NULL);
  2409. if (!OCSP_check_validity(thisupd, nextupd, 300,
  2410. response_lifetime)) {
  2411. ret = KMF_ERR_OCSP_STATUS_TIME_INVALID;
  2412. goto end;
  2413. }
  2414. #ifdef DEBUG
  2415. printf("Successfully verify the time.\n");
  2416. #endif /* DEBUG */
  2417. end:
  2418. if (derbio != NULL)
  2419. (void) BIO_free(derbio);
  2420. if (resp != NULL)
  2421. OCSP_RESPONSE_free(resp);
  2422. if (bs != NULL)
  2423. OCSP_BASICRESP_free(bs);
  2424. if (id != NULL)
  2425. OCSP_CERTID_free(id);
  2426. return (ret);
  2427. }
  2428. static KMF_RETURN
  2429. fetch_key(KMF_HANDLE_T handle, char *path,
  2430. KMF_KEY_CLASS keyclass, KMF_KEY_HANDLE *key)
  2431. {
  2432. KMF_RETURN rv = KMF_OK;
  2433. EVP_PKEY *pkey = NULL;
  2434. KMF_RAW_SYM_KEY *rkey = NULL;
  2435. if (keyclass == KMF_ASYM_PRI ||
  2436. keyclass == KMF_ASYM_PUB) {
  2437. pkey = openssl_load_key(handle, path);
  2438. if (pkey == NULL) {
  2439. return (KMF_ERR_KEY_NOT_FOUND);
  2440. }
  2441. if (key != NULL) {
  2442. if (pkey->type == EVP_PKEY_RSA)
  2443. key->keyalg = KMF_RSA;
  2444. else if (pkey->type == EVP_PKEY_DSA)
  2445. key->keyalg = KMF_DSA;
  2446. key->kstype = KMF_KEYSTORE_OPENSSL;
  2447. key->keyclass = keyclass;
  2448. key->keyp = (void *)pkey;
  2449. key->israw = FALSE;
  2450. if (path != NULL &&
  2451. ((key->keylabel = strdup(path)) == NULL)) {
  2452. EVP_PKEY_free(pkey);
  2453. return (KMF_ERR_MEMORY);
  2454. }
  2455. } else {
  2456. EVP_PKEY_free(pkey);
  2457. pkey = NULL;
  2458. }
  2459. } else if (keyclass == KMF_SYMMETRIC) {
  2460. KMF_ENCODE_FORMAT fmt;
  2461. /*
  2462. * If the file is a recognized format,
  2463. * then it is NOT a symmetric key.
  2464. */
  2465. rv = kmf_get_file_format(path, &fmt);
  2466. if (rv == KMF_OK || fmt != 0) {
  2467. return (KMF_ERR_KEY_NOT_FOUND);
  2468. } else if (rv == KMF_ERR_ENCODING) {
  2469. /*
  2470. * If we don't know the encoding,
  2471. * it is probably a symmetric key.
  2472. */
  2473. rv = KMF_OK;
  2474. } else if (rv == KMF_ERR_OPEN_FILE) {
  2475. return (KMF_ERR_KEY_NOT_FOUND);
  2476. }
  2477. if (key != NULL) {
  2478. KMF_DATA keyvalue;
  2479. rkey = malloc(sizeof (KMF_RAW_SYM_KEY));
  2480. if (rkey == NULL) {
  2481. rv = KMF_ERR_MEMORY;
  2482. goto out;
  2483. }
  2484. (void) memset(rkey, 0, sizeof (KMF_RAW_SYM_KEY));
  2485. rv = kmf_read_input_file(handle, path, &keyvalue);
  2486. if (rv != KMF_OK)
  2487. goto out;
  2488. rkey->keydata.len = keyvalue.Length;
  2489. rkey->keydata.val = keyvalue.Data;
  2490. key->kstype = KMF_KEYSTORE_OPENSSL;
  2491. key->keyclass = keyclass;
  2492. key->israw = TRUE;
  2493. key->keyp = (void *)rkey;
  2494. if (path != NULL &&
  2495. ((key->keylabel = strdup(path)) == NULL)) {
  2496. rv = KMF_ERR_MEMORY;
  2497. }
  2498. }
  2499. }
  2500. out:
  2501. if (rv != KMF_OK) {
  2502. if (rkey != NULL) {
  2503. kmf_free_raw_sym_key(rkey);
  2504. }
  2505. if (pkey != NULL)
  2506. EVP_PKEY_free(pkey);
  2507. if (key != NULL) {
  2508. key->keyalg = KMF_KEYALG_NONE;
  2509. key->keyclass = KMF_KEYCLASS_NONE;
  2510. key->keyp = NULL;
  2511. }
  2512. }
  2513. return (rv);
  2514. }
  2515. KMF_RETURN
  2516. OpenSSL_FindKey(KMF_HANDLE_T handle,
  2517. int numattr, KMF_ATTRIBUTE *attrlist)
  2518. {
  2519. KMF_RETURN rv = KMF_OK;
  2520. char *fullpath = NULL;
  2521. uint32_t maxkeys;
  2522. KMF_KEY_HANDLE *key;
  2523. uint32_t *numkeys;
  2524. KMF_KEY_CLASS keyclass;
  2525. KMF_RAW_KEY_DATA *rawkey;
  2526. char *dirpath;
  2527. char *keyfile;
  2528. if (handle == NULL)
  2529. return (KMF_ERR_BAD_PARAMETER);
  2530. numkeys = kmf_get_attr_ptr(KMF_COUNT_ATTR, attrlist, numattr);
  2531. if (numkeys == NULL)
  2532. return (KMF_ERR_BAD_PARAMETER);
  2533. rv = kmf_get_attr(KMF_KEYCLASS_ATTR, attrlist, numattr,
  2534. (void *)&keyclass, NULL);
  2535. if (rv != KMF_OK)
  2536. return (KMF_ERR_BAD_PARAMETER);
  2537. if (keyclass != KMF_ASYM_PUB &&
  2538. keyclass != KMF_ASYM_PRI &&
  2539. keyclass != KMF_SYMMETRIC)
  2540. return (KMF_ERR_BAD_KEY_CLASS);
  2541. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  2542. keyfile = kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR, attrlist, numattr);
  2543. fullpath = get_fullpath(dirpath, keyfile);
  2544. if (fullpath == NULL)
  2545. return (KMF_ERR_BAD_PARAMETER);
  2546. maxkeys = *numkeys;
  2547. if (maxkeys == 0)
  2548. maxkeys = 0xFFFFFFFF;
  2549. *numkeys = 0;
  2550. key = kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR, attrlist, numattr);
  2551. /* it is okay to have "keys" contains NULL */
  2552. /*
  2553. * The caller may want a list of the raw key data as well.
  2554. * Useful for importing keys from a file into other keystores.
  2555. */
  2556. rawkey = kmf_get_attr_ptr(KMF_RAW_KEY_ATTR, attrlist, numattr);
  2557. if (isdir(fullpath)) {
  2558. DIR *dirp;
  2559. struct dirent *dp;
  2560. int n = 0;
  2561. /* open all files in the directory and attempt to read them */
  2562. if ((dirp = opendir(fullpath)) == NULL) {
  2563. return (KMF_ERR_BAD_PARAMETER);
  2564. }
  2565. rewinddir(dirp);
  2566. while ((dp = readdir(dirp)) != NULL && n < maxkeys) {
  2567. if (strcmp(dp->d_name, ".") &&
  2568. strcmp(dp->d_name, "..")) {
  2569. char *fname;
  2570. fname = get_fullpath(fullpath,
  2571. (char *)&dp->d_name);
  2572. rv = fetch_key(handle, fname,
  2573. keyclass, key ? &key[n] : NULL);
  2574. if (rv == KMF_OK) {
  2575. if (key != NULL && rawkey != NULL)
  2576. rv = convertToRawKey(
  2577. key[n].keyp, &rawkey[n]);
  2578. n++;
  2579. }
  2580. if (rv != KMF_OK || key == NULL)
  2581. free(fname);
  2582. }
  2583. }
  2584. (void) closedir(dirp);
  2585. free(fullpath);
  2586. (*numkeys) = n;
  2587. } else {
  2588. rv = fetch_key(handle, fullpath, keyclass, key);
  2589. if (rv == KMF_OK)
  2590. (*numkeys) = 1;
  2591. if (rv != KMF_OK || key == NULL)
  2592. free(fullpath);
  2593. if (rv == KMF_OK && key != NULL && rawkey != NULL) {
  2594. rv = convertToRawKey(key->keyp, rawkey);
  2595. }
  2596. }
  2597. if (rv == KMF_OK && (*numkeys) == 0)
  2598. rv = KMF_ERR_KEY_NOT_FOUND;
  2599. else if (rv == KMF_ERR_KEY_NOT_FOUND && (*numkeys) > 0)
  2600. rv = KMF_OK;
  2601. return (rv);
  2602. }
  2603. #define HANDLE_PK12_ERROR { \
  2604. SET_ERROR(kmfh, ERR_get_error()); \
  2605. rv = KMF_ERR_ENCODING; \
  2606. goto out; \
  2607. }
  2608. static int
  2609. add_alias_to_bag(PKCS12_SAFEBAG *bag, X509 *xcert)
  2610. {
  2611. if (xcert != NULL && xcert->aux != NULL &&
  2612. xcert->aux->alias != NULL) {
  2613. if (PKCS12_add_friendlyname_asc(bag,
  2614. (const char *)xcert->aux->alias->data,
  2615. xcert->aux->alias->length) == 0)
  2616. return (0);
  2617. }
  2618. return (1);
  2619. }
  2620. static PKCS7 *
  2621. add_cert_to_safe(X509 *sslcert, KMF_CREDENTIAL *cred,
  2622. uchar_t *keyid, unsigned int keyidlen)
  2623. {
  2624. PKCS12_SAFEBAG *bag = NULL;
  2625. PKCS7 *cert_authsafe = NULL;
  2626. STACK_OF(PKCS12_SAFEBAG) *bag_stack;
  2627. bag_stack = sk_PKCS12_SAFEBAG_new_null();
  2628. if (bag_stack == NULL)
  2629. return (NULL);
  2630. /* Convert cert from X509 struct to PKCS#12 bag */
  2631. bag = PKCS12_x5092certbag(sslcert);
  2632. if (bag == NULL) {
  2633. goto out;
  2634. }
  2635. /* Add the key id to the certificate bag. */
  2636. if (keyidlen > 0 && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) {
  2637. goto out;
  2638. }
  2639. if (!add_alias_to_bag(bag, sslcert))
  2640. goto out;
  2641. /* Pile it on the bag_stack. */
  2642. if (!sk_PKCS12_SAFEBAG_push(bag_stack, bag)) {
  2643. goto out;
  2644. }
  2645. /* Turn bag_stack of certs into encrypted authsafe. */
  2646. cert_authsafe = PKCS12_pack_p7encdata(
  2647. NID_pbe_WithSHA1And40BitRC2_CBC,
  2648. cred->cred, cred->credlen, NULL, 0,
  2649. PKCS12_DEFAULT_ITER, bag_stack);
  2650. out:
  2651. if (bag_stack != NULL)
  2652. sk_PKCS12_SAFEBAG_pop_free(bag_stack, PKCS12_SAFEBAG_free);
  2653. return (cert_authsafe);
  2654. }
  2655. static PKCS7 *
  2656. add_key_to_safe(EVP_PKEY *pkey, KMF_CREDENTIAL *cred,
  2657. uchar_t *keyid, unsigned int keyidlen,
  2658. char *label, int label_len)
  2659. {
  2660. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  2661. STACK_OF(PKCS12_SAFEBAG) *bag_stack = NULL;
  2662. PKCS12_SAFEBAG *bag = NULL;
  2663. PKCS7 *key_authsafe = NULL;
  2664. p8 = EVP_PKEY2PKCS8(pkey);
  2665. if (p8 == NULL) {
  2666. return (NULL);
  2667. }
  2668. /* Put the shrouded key into a PKCS#12 bag. */
  2669. bag = PKCS12_MAKE_SHKEYBAG(
  2670. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  2671. cred->cred, cred->credlen,
  2672. NULL, 0, PKCS12_DEFAULT_ITER, p8);
  2673. /* Clean up the PKCS#8 shrouded key, don't need it now. */
  2674. PKCS8_PRIV_KEY_INFO_free(p8);
  2675. p8 = NULL;
  2676. if (bag == NULL) {
  2677. return (NULL);
  2678. }
  2679. if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
  2680. goto out;
  2681. if (label != NULL && !PKCS12_add_friendlyname(bag, label, label_len))
  2682. goto out;
  2683. /* Start a PKCS#12 safebag container for the private key. */
  2684. bag_stack = sk_PKCS12_SAFEBAG_new_null();
  2685. if (bag_stack == NULL)
  2686. goto out;
  2687. /* Pile on the private key on the bag_stack. */
  2688. if (!sk_PKCS12_SAFEBAG_push(bag_stack, bag))
  2689. goto out;
  2690. key_authsafe = PKCS12_pack_p7data(bag_stack);
  2691. out:
  2692. if (bag_stack != NULL)
  2693. sk_PKCS12_SAFEBAG_pop_free(bag_stack, PKCS12_SAFEBAG_free);
  2694. bag_stack = NULL;
  2695. return (key_authsafe);
  2696. }
  2697. static EVP_PKEY *
  2698. ImportRawRSAKey(KMF_RAW_RSA_KEY *key)
  2699. {
  2700. RSA *rsa = NULL;
  2701. EVP_PKEY *newkey = NULL;
  2702. if ((rsa = RSA_new()) == NULL)
  2703. return (NULL);
  2704. if ((rsa->n = BN_bin2bn(key->mod.val, key->mod.len, rsa->n)) == NULL)
  2705. return (NULL);
  2706. if ((rsa->e = BN_bin2bn(key->pubexp.val, key->pubexp.len, rsa->e)) ==
  2707. NULL)
  2708. return (NULL);
  2709. if (key->priexp.val != NULL)
  2710. if ((rsa->d = BN_bin2bn(key->priexp.val, key->priexp.len,
  2711. rsa->d)) == NULL)
  2712. return (NULL);
  2713. if (key->prime1.val != NULL)
  2714. if ((rsa->p = BN_bin2bn(key->prime1.val, key->prime1.len,
  2715. rsa->p)) == NULL)
  2716. return (NULL);
  2717. if (key->prime2.val != NULL)
  2718. if ((rsa->q = BN_bin2bn(key->prime2.val, key->prime2.len,
  2719. rsa->q)) == NULL)
  2720. return (NULL);
  2721. if (key->exp1.val != NULL)
  2722. if ((rsa->dmp1 = BN_bin2bn(key->exp1.val, key->exp1.len,
  2723. rsa->dmp1)) == NULL)
  2724. return (NULL);
  2725. if (key->exp2.val != NULL)
  2726. if ((rsa->dmq1 = BN_bin2bn(key->exp2.val, key->exp2.len,
  2727. rsa->dmq1)) == NULL)
  2728. return (NULL);
  2729. if (key->coef.val != NULL)
  2730. if ((rsa->iqmp = BN_bin2bn(key->coef.val, key->coef.len,
  2731. rsa->iqmp)) == NULL)
  2732. return (NULL);
  2733. if ((newkey = EVP_PKEY_new()) == NULL)
  2734. return (NULL);
  2735. (void) EVP_PKEY_set1_RSA(newkey, rsa);
  2736. /* The original key must be freed once here or it leaks memory */
  2737. RSA_free(rsa);
  2738. return (newkey);
  2739. }
  2740. static EVP_PKEY *
  2741. ImportRawDSAKey(KMF_RAW_DSA_KEY *key)
  2742. {
  2743. DSA *dsa = NULL;
  2744. EVP_PKEY *newkey = NULL;
  2745. if ((dsa = DSA_new()) == NULL)
  2746. return (NULL);
  2747. if ((dsa->p = BN_bin2bn(key->prime.val, key->prime.len,
  2748. dsa->p)) == NULL)
  2749. return (NULL);
  2750. if ((dsa->q = BN_bin2bn(key->subprime.val, key->subprime.len,
  2751. dsa->q)) == NULL)
  2752. return (NULL);
  2753. if ((dsa->g = BN_bin2bn(key->base.val, key->base.len,
  2754. dsa->g)) == NULL)
  2755. return (NULL);
  2756. if ((dsa->priv_key = BN_bin2bn(key->value.val, key->value.len,
  2757. dsa->priv_key)) == NULL)
  2758. return (NULL);
  2759. if (key->pubvalue.val != NULL) {
  2760. if ((dsa->pub_key = BN_bin2bn(key->pubvalue.val,
  2761. key->pubvalue.len, dsa->pub_key)) == NULL)
  2762. return (NULL);
  2763. }
  2764. if ((newkey = EVP_PKEY_new()) == NULL)
  2765. return (NULL);
  2766. (void) EVP_PKEY_set1_DSA(newkey, dsa);
  2767. /* The original key must be freed once here or it leaks memory */
  2768. DSA_free(dsa);
  2769. return (newkey);
  2770. }
  2771. static EVP_PKEY *
  2772. raw_key_to_pkey(KMF_KEY_HANDLE *key)
  2773. {
  2774. EVP_PKEY *pkey = NULL;
  2775. KMF_RAW_KEY_DATA *rawkey;
  2776. ASN1_TYPE *attr = NULL;
  2777. KMF_RETURN ret;
  2778. if (key == NULL || !key->israw)
  2779. return (NULL);
  2780. rawkey = (KMF_RAW_KEY_DATA *)key->keyp;
  2781. if (rawkey->keytype == KMF_RSA) {
  2782. pkey = ImportRawRSAKey(&rawkey->rawdata.rsa);
  2783. } else if (rawkey->keytype == KMF_DSA) {
  2784. pkey = ImportRawDSAKey(&rawkey->rawdata.dsa);
  2785. } else if (rawkey->keytype == KMF_ECDSA) {
  2786. /*
  2787. * OpenSSL in Solaris does not support EC for
  2788. * legal reasons
  2789. */
  2790. return (NULL);
  2791. } else {
  2792. /* wrong kind of key */
  2793. return (NULL);
  2794. }
  2795. if (rawkey->label != NULL) {
  2796. if ((attr = ASN1_TYPE_new()) == NULL) {
  2797. EVP_PKEY_free(pkey);
  2798. return (NULL);
  2799. }
  2800. attr->value.bmpstring = ASN1_STRING_type_new(V_ASN1_BMPSTRING);
  2801. (void) ASN1_STRING_set(attr->value.bmpstring, rawkey->label,
  2802. strlen(rawkey->label));
  2803. attr->type = V_ASN1_BMPSTRING;
  2804. attr->value.ptr = (char *)attr->value.bmpstring;
  2805. ret = set_pkey_attrib(pkey, attr, NID_friendlyName);
  2806. if (ret != KMF_OK) {
  2807. EVP_PKEY_free(pkey);
  2808. ASN1_TYPE_free(attr);
  2809. return (NULL);
  2810. }
  2811. }
  2812. if (rawkey->id.Data != NULL) {
  2813. if ((attr = ASN1_TYPE_new()) == NULL) {
  2814. EVP_PKEY_free(pkey);
  2815. return (NULL);
  2816. }
  2817. attr->value.octet_string =
  2818. ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  2819. attr->type = V_ASN1_OCTET_STRING;
  2820. (void) ASN1_STRING_set(attr->value.octet_string,
  2821. rawkey->id.Data, rawkey->id.Length);
  2822. attr->value.ptr = (char *)attr->value.octet_string;
  2823. ret = set_pkey_attrib(pkey, attr, NID_localKeyID);
  2824. if (ret != KMF_OK) {
  2825. EVP_PKEY_free(pkey);
  2826. ASN1_TYPE_free(attr);
  2827. return (NULL);
  2828. }
  2829. }
  2830. return (pkey);
  2831. }
  2832. /*
  2833. * Search a list of private keys to find one that goes with the certificate.
  2834. */
  2835. static EVP_PKEY *
  2836. find_matching_key(X509 *xcert, int numkeys, KMF_KEY_HANDLE *keylist)
  2837. {
  2838. int i;
  2839. EVP_PKEY *pkey = NULL;
  2840. if (numkeys == 0 || keylist == NULL || xcert == NULL)
  2841. return (NULL);
  2842. for (i = 0; i < numkeys; i++) {
  2843. if (keylist[i].israw)
  2844. pkey = raw_key_to_pkey(&keylist[i]);
  2845. else
  2846. pkey = (EVP_PKEY *)keylist[i].keyp;
  2847. if (pkey != NULL) {
  2848. if (X509_check_private_key(xcert, pkey)) {
  2849. return (pkey);
  2850. } else {
  2851. EVP_PKEY_free(pkey);
  2852. pkey = NULL;
  2853. }
  2854. }
  2855. }
  2856. return (pkey);
  2857. }
  2858. static KMF_RETURN
  2859. local_export_pk12(KMF_HANDLE_T handle,
  2860. KMF_CREDENTIAL *cred,
  2861. int numcerts, KMF_X509_DER_CERT *certlist,
  2862. int numkeys, KMF_KEY_HANDLE *keylist,
  2863. char *filename)
  2864. {
  2865. KMF_RETURN rv = KMF_OK;
  2866. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  2867. BIO *bio = NULL;
  2868. PKCS7 *cert_authsafe = NULL;
  2869. PKCS7 *key_authsafe = NULL;
  2870. STACK_OF(PKCS7) *authsafe_stack = NULL;
  2871. PKCS12 *p12_elem = NULL;
  2872. int i;
  2873. if (numcerts == 0 && numkeys == 0)
  2874. return (KMF_ERR_BAD_PARAMETER);
  2875. /*
  2876. * Open the output file.
  2877. */
  2878. if ((bio = BIO_new_file(filename, "wb")) == NULL) {
  2879. SET_ERROR(kmfh, ERR_get_error());
  2880. rv = KMF_ERR_OPEN_FILE;
  2881. goto cleanup;
  2882. }
  2883. /* Start a PKCS#7 stack. */
  2884. authsafe_stack = sk_PKCS7_new_null();
  2885. if (authsafe_stack == NULL) {
  2886. rv = KMF_ERR_MEMORY;
  2887. goto cleanup;
  2888. }
  2889. if (numcerts > 0) {
  2890. for (i = 0; rv == KMF_OK && i < numcerts; i++) {
  2891. const uchar_t *p = certlist[i].certificate.Data;
  2892. long len = certlist[i].certificate.Length;
  2893. X509 *xcert = NULL;
  2894. EVP_PKEY *pkey = NULL;
  2895. unsigned char keyid[EVP_MAX_MD_SIZE];
  2896. unsigned int keyidlen = 0;
  2897. xcert = d2i_X509(NULL, &p, len);
  2898. if (xcert == NULL) {
  2899. SET_ERROR(kmfh, ERR_get_error());
  2900. rv = KMF_ERR_ENCODING;
  2901. }
  2902. if (certlist[i].kmf_private.label != NULL) {
  2903. /* Set alias attribute */
  2904. (void) X509_alias_set1(xcert,
  2905. (uchar_t *)certlist[i].kmf_private.label,
  2906. strlen(certlist[i].kmf_private.label));
  2907. }
  2908. /* Check if there is a key corresponding to this cert */
  2909. pkey = find_matching_key(xcert, numkeys, keylist);
  2910. /*
  2911. * If key is found, get fingerprint and create a
  2912. * safebag.
  2913. */
  2914. if (pkey != NULL) {
  2915. (void) X509_digest(xcert, EVP_sha1(),
  2916. keyid, &keyidlen);
  2917. key_authsafe = add_key_to_safe(pkey, cred,
  2918. keyid, keyidlen,
  2919. certlist[i].kmf_private.label,
  2920. (certlist[i].kmf_private.label ?
  2921. strlen(certlist[i].kmf_private.label) : 0));
  2922. if (key_authsafe == NULL) {
  2923. X509_free(xcert);
  2924. EVP_PKEY_free(pkey);
  2925. goto cleanup;
  2926. }
  2927. /* Put the key safe into the Auth Safe */
  2928. if (!sk_PKCS7_push(authsafe_stack,
  2929. key_authsafe)) {
  2930. X509_free(xcert);
  2931. EVP_PKEY_free(pkey);
  2932. goto cleanup;
  2933. }
  2934. }
  2935. /* create a certificate safebag */
  2936. cert_authsafe = add_cert_to_safe(xcert, cred, keyid,
  2937. keyidlen);
  2938. if (cert_authsafe == NULL) {
  2939. X509_free(xcert);
  2940. EVP_PKEY_free(pkey);
  2941. goto cleanup;
  2942. }
  2943. if (!sk_PKCS7_push(authsafe_stack, cert_authsafe)) {
  2944. X509_free(xcert);
  2945. EVP_PKEY_free(pkey);
  2946. goto cleanup;
  2947. }
  2948. X509_free(xcert);
  2949. if (pkey)
  2950. EVP_PKEY_free(pkey);
  2951. }
  2952. } else if (numcerts == 0 && numkeys > 0) {
  2953. /*
  2954. * If only adding keys to the file.
  2955. */
  2956. for (i = 0; i < numkeys; i++) {
  2957. EVP_PKEY *pkey = NULL;
  2958. if (keylist[i].israw)
  2959. pkey = raw_key_to_pkey(&keylist[i]);
  2960. else
  2961. pkey = (EVP_PKEY *)keylist[i].keyp;
  2962. if (pkey == NULL)
  2963. continue;
  2964. key_authsafe = add_key_to_safe(pkey, cred,
  2965. NULL, 0, NULL, 0);
  2966. if (key_authsafe == NULL) {
  2967. EVP_PKEY_free(pkey);
  2968. goto cleanup;
  2969. }
  2970. if (!sk_PKCS7_push(authsafe_stack, key_authsafe)) {
  2971. EVP_PKEY_free(pkey);
  2972. goto cleanup;
  2973. }
  2974. }
  2975. }
  2976. p12_elem = PKCS12_init(NID_pkcs7_data);
  2977. if (p12_elem == NULL) {
  2978. goto cleanup;
  2979. }
  2980. /* Put the PKCS#7 stack into the PKCS#12 element. */
  2981. if (!PKCS12_pack_authsafes(p12_elem, authsafe_stack)) {
  2982. goto cleanup;
  2983. }
  2984. /* Set the integrity MAC on the PKCS#12 element. */
  2985. if (!PKCS12_set_mac(p12_elem, cred->cred, cred->credlen,
  2986. NULL, 0, PKCS12_DEFAULT_ITER, NULL)) {
  2987. goto cleanup;
  2988. }
  2989. /* Write the PKCS#12 element to the export file. */
  2990. if (!i2d_PKCS12_bio(bio, p12_elem)) {
  2991. goto cleanup;
  2992. }
  2993. PKCS12_free(p12_elem);
  2994. cleanup:
  2995. /* Clear away the PKCS#7 stack, we're done with it. */
  2996. if (authsafe_stack)
  2997. sk_PKCS7_pop_free(authsafe_stack, PKCS7_free);
  2998. if (bio != NULL)
  2999. (void) BIO_free_all(bio);
  3000. return (rv);
  3001. }
  3002. KMF_RETURN
  3003. openssl_build_pk12(KMF_HANDLE_T handle, int numcerts,
  3004. KMF_X509_DER_CERT *certlist, int numkeys, KMF_KEY_HANDLE *keylist,
  3005. KMF_CREDENTIAL *p12cred, char *filename)
  3006. {
  3007. KMF_RETURN rv;
  3008. if (certlist == NULL && keylist == NULL)
  3009. return (KMF_ERR_BAD_PARAMETER);
  3010. rv = local_export_pk12(handle, p12cred, numcerts, certlist,
  3011. numkeys, keylist, filename);
  3012. return (rv);
  3013. }
  3014. KMF_RETURN
  3015. OpenSSL_ExportPK12(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  3016. {
  3017. KMF_RETURN rv;
  3018. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  3019. char *fullpath = NULL;
  3020. char *dirpath = NULL;
  3021. char *certfile = NULL;
  3022. char *keyfile = NULL;
  3023. char *filename = NULL;
  3024. KMF_CREDENTIAL *p12cred = NULL;
  3025. KMF_X509_DER_CERT certdata;
  3026. KMF_KEY_HANDLE key;
  3027. int gotkey = 0;
  3028. int gotcert = 0;
  3029. if (handle == NULL)
  3030. return (KMF_ERR_BAD_PARAMETER);
  3031. /*
  3032. * First, find the certificate.
  3033. */
  3034. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  3035. certfile = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist, numattr);
  3036. if (certfile != NULL) {
  3037. fullpath = get_fullpath(dirpath, certfile);
  3038. if (fullpath == NULL)
  3039. return (KMF_ERR_BAD_PARAMETER);
  3040. if (isdir(fullpath)) {
  3041. free(fullpath);
  3042. return (KMF_ERR_AMBIGUOUS_PATHNAME);
  3043. }
  3044. (void) memset(&certdata, 0, sizeof (certdata));
  3045. rv = kmf_load_cert(kmfh, NULL, NULL, NULL, NULL,
  3046. fullpath, &certdata.certificate);
  3047. if (rv != KMF_OK)
  3048. goto end;
  3049. gotcert++;
  3050. certdata.kmf_private.keystore_type = KMF_KEYSTORE_OPENSSL;
  3051. free(fullpath);
  3052. }
  3053. /*
  3054. * Now find the private key.
  3055. */
  3056. keyfile = kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR, attrlist, numattr);
  3057. if (keyfile != NULL) {
  3058. fullpath = get_fullpath(dirpath, keyfile);
  3059. if (fullpath == NULL)
  3060. return (KMF_ERR_BAD_PARAMETER);
  3061. if (isdir(fullpath)) {
  3062. free(fullpath);
  3063. return (KMF_ERR_AMBIGUOUS_PATHNAME);
  3064. }
  3065. (void) memset(&key, 0, sizeof (KMF_KEY_HANDLE));
  3066. rv = fetch_key(handle, fullpath, KMF_ASYM_PRI, &key);
  3067. if (rv != KMF_OK)
  3068. goto end;
  3069. gotkey++;
  3070. }
  3071. /*
  3072. * Open the output file.
  3073. */
  3074. filename = kmf_get_attr_ptr(KMF_OUTPUT_FILENAME_ATTR, attrlist,
  3075. numattr);
  3076. if (filename == NULL) {
  3077. rv = KMF_ERR_BAD_PARAMETER;
  3078. goto end;
  3079. }
  3080. /* Stick the key and the cert into a PKCS#12 file */
  3081. p12cred = kmf_get_attr_ptr(KMF_PK12CRED_ATTR, attrlist, numattr);
  3082. if (p12cred == NULL) {
  3083. rv = KMF_ERR_BAD_PARAMETER;
  3084. goto end;
  3085. }
  3086. rv = local_export_pk12(handle, p12cred, 1, &certdata,
  3087. 1, &key, filename);
  3088. end:
  3089. if (fullpath)
  3090. free(fullpath);
  3091. if (gotcert)
  3092. kmf_free_kmf_cert(handle, &certdata);
  3093. if (gotkey)
  3094. kmf_free_kmf_key(handle, &key);
  3095. return (rv);
  3096. }
  3097. /*
  3098. * Helper function to extract keys and certificates from
  3099. * a single PEM file. Typically the file should contain a
  3100. * private key and an associated public key wrapped in an x509 cert.
  3101. * However, the file may be just a list of X509 certs with no keys.
  3102. */
  3103. static KMF_RETURN
  3104. extract_pem(KMF_HANDLE *kmfh,
  3105. char *issuer, char *subject, KMF_BIGINT *serial,
  3106. char *filename, CK_UTF8CHAR *pin,
  3107. CK_ULONG pinlen, EVP_PKEY **priv_key, KMF_DATA **certs,
  3108. int *numcerts)
  3109. /* ARGSUSED6 */
  3110. {
  3111. KMF_RETURN rv = KMF_OK;
  3112. FILE *fp;
  3113. STACK_OF(X509_INFO) *x509_info_stack = NULL;
  3114. int i, ncerts = 0, matchcerts = 0;
  3115. EVP_PKEY *pkey = NULL;
  3116. X509_INFO *info;
  3117. X509 *x;
  3118. X509_INFO **cert_infos = NULL;
  3119. KMF_DATA *certlist = NULL;
  3120. if (priv_key)
  3121. *priv_key = NULL;
  3122. if (certs)
  3123. *certs = NULL;
  3124. fp = fopen(filename, "r");
  3125. if (fp == NULL)
  3126. return (KMF_ERR_OPEN_FILE);
  3127. x509_info_stack = PEM_X509_INFO_read(fp, NULL, NULL, pin);
  3128. if (x509_info_stack == NULL) {
  3129. (void) fclose(fp);
  3130. return (KMF_ERR_ENCODING);
  3131. }
  3132. cert_infos = (X509_INFO **)malloc(sk_X509_INFO_num(x509_info_stack) *
  3133. sizeof (X509_INFO *));
  3134. if (cert_infos == NULL) {
  3135. (void) fclose(fp);
  3136. rv = KMF_ERR_MEMORY;
  3137. goto err;
  3138. }
  3139. for (i = 0; i < sk_X509_INFO_num(x509_info_stack); i++) {
  3140. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3141. cert_infos[ncerts] = sk_X509_INFO_value(x509_info_stack, i);
  3142. ncerts++;
  3143. }
  3144. if (ncerts == 0) {
  3145. (void) fclose(fp);
  3146. rv = KMF_ERR_CERT_NOT_FOUND;
  3147. goto err;
  3148. }
  3149. if (priv_key != NULL) {
  3150. rewind(fp);
  3151. pkey = PEM_read_PrivateKey(fp, NULL, NULL, pin);
  3152. }
  3153. (void) fclose(fp);
  3154. x = cert_infos[ncerts - 1]->x509;
  3155. /*
  3156. * Make sure the private key matchs the last cert in the file.
  3157. */
  3158. if (pkey != NULL && !X509_check_private_key(x, pkey)) {
  3159. EVP_PKEY_free(pkey);
  3160. rv = KMF_ERR_KEY_MISMATCH;
  3161. goto err;
  3162. }
  3163. certlist = (KMF_DATA *)calloc(ncerts, sizeof (KMF_DATA));
  3164. if (certlist == NULL) {
  3165. if (pkey != NULL)
  3166. EVP_PKEY_free(pkey);
  3167. rv = KMF_ERR_MEMORY;
  3168. goto err;
  3169. }
  3170. /*
  3171. * Convert all of the certs to DER format.
  3172. */
  3173. matchcerts = 0;
  3174. for (i = 0; rv == KMF_OK && certs != NULL && i < ncerts; i++) {
  3175. boolean_t match = FALSE;
  3176. info = cert_infos[ncerts - 1 - i];
  3177. rv = check_cert(info->x509, issuer, subject, serial, &match);
  3178. if (rv != KMF_OK || match != TRUE) {
  3179. rv = KMF_OK;
  3180. continue;
  3181. }
  3182. rv = ssl_cert2KMFDATA(kmfh, info->x509,
  3183. &certlist[matchcerts++]);
  3184. if (rv != KMF_OK) {
  3185. int j;
  3186. for (j = 0; j < matchcerts; j++)
  3187. kmf_free_data(&certlist[j]);
  3188. free(certlist);
  3189. certlist = NULL;
  3190. ncerts = matchcerts = 0;
  3191. }
  3192. }
  3193. if (numcerts != NULL)
  3194. *numcerts = matchcerts;
  3195. if (certs != NULL)
  3196. *certs = certlist;
  3197. else if (certlist != NULL) {
  3198. for (i = 0; i < ncerts; i++)
  3199. kmf_free_data(&certlist[i]);
  3200. free(certlist);
  3201. certlist = NULL;
  3202. }
  3203. if (priv_key == NULL && pkey != NULL)
  3204. EVP_PKEY_free(pkey);
  3205. else if (priv_key != NULL && pkey != NULL)
  3206. *priv_key = pkey;
  3207. err:
  3208. /* Cleanup the stack of X509 info records */
  3209. for (i = 0; i < sk_X509_INFO_num(x509_info_stack); i++) {
  3210. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3211. info = (X509_INFO *)sk_X509_INFO_value(x509_info_stack, i);
  3212. X509_INFO_free(info);
  3213. }
  3214. if (x509_info_stack)
  3215. sk_X509_INFO_free(x509_info_stack);
  3216. if (cert_infos != NULL)
  3217. free(cert_infos);
  3218. return (rv);
  3219. }
  3220. static KMF_RETURN
  3221. openssl_parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, char *pin,
  3222. STACK_OF(EVP_PKEY) *keys, STACK_OF(X509) *certs)
  3223. {
  3224. KMF_RETURN ret;
  3225. int i;
  3226. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  3227. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3228. PKCS12_SAFEBAG *bag = sk_PKCS12_SAFEBAG_value(bags, i);
  3229. ret = openssl_parse_bag(bag, pin, (pin ? strlen(pin) : 0),
  3230. keys, certs);
  3231. if (ret != KMF_OK)
  3232. return (ret);
  3233. }
  3234. return (ret);
  3235. }
  3236. static KMF_RETURN
  3237. set_pkey_attrib(EVP_PKEY *pkey, ASN1_TYPE *attrib, int nid)
  3238. {
  3239. X509_ATTRIBUTE *attr = NULL;
  3240. if (pkey == NULL || attrib == NULL)
  3241. return (KMF_ERR_BAD_PARAMETER);
  3242. if (pkey->attributes == NULL) {
  3243. pkey->attributes = sk_X509_ATTRIBUTE_new_null();
  3244. if (pkey->attributes == NULL)
  3245. return (KMF_ERR_MEMORY);
  3246. }
  3247. attr = X509_ATTRIBUTE_create(nid, attrib->type, attrib->value.ptr);
  3248. if (attr != NULL) {
  3249. int i;
  3250. X509_ATTRIBUTE *a;
  3251. for (i = 0;
  3252. i < sk_X509_ATTRIBUTE_num(pkey->attributes); i++) {
  3253. /* LINTED E_BAD_PTR_CASE_ALIGN */
  3254. a = sk_X509_ATTRIBUTE_value(pkey->attributes, i);
  3255. if (OBJ_obj2nid(a->object) == nid) {
  3256. X509_ATTRIBUTE_free(a);
  3257. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3258. sk_X509_ATTRIBUTE_set(pkey->attributes,
  3259. i, attr);
  3260. return (KMF_OK);
  3261. }
  3262. }
  3263. if (sk_X509_ATTRIBUTE_push(pkey->attributes, attr) == NULL) {
  3264. X509_ATTRIBUTE_free(attr);
  3265. return (KMF_ERR_MEMORY);
  3266. }
  3267. } else {
  3268. return (KMF_ERR_MEMORY);
  3269. }
  3270. return (KMF_OK);
  3271. }
  3272. static KMF_RETURN
  3273. openssl_parse_bag(PKCS12_SAFEBAG *bag, char *pass, int passlen,
  3274. STACK_OF(EVP_PKEY) *keylist, STACK_OF(X509) *certlist)
  3275. {
  3276. KMF_RETURN ret = KMF_OK;
  3277. PKCS8_PRIV_KEY_INFO *p8 = NULL;
  3278. EVP_PKEY *pkey = NULL;
  3279. X509 *xcert = NULL;
  3280. ASN1_TYPE *keyid = NULL;
  3281. ASN1_TYPE *fname = NULL;
  3282. uchar_t *data = NULL;
  3283. keyid = PKCS12_get_attr(bag, NID_localKeyID);
  3284. fname = PKCS12_get_attr(bag, NID_friendlyName);
  3285. switch (M_PKCS12_bag_type(bag)) {
  3286. case NID_keyBag:
  3287. if (keylist == NULL)
  3288. goto end;
  3289. pkey = EVP_PKCS82PKEY(bag->value.keybag);
  3290. if (pkey == NULL)
  3291. ret = KMF_ERR_PKCS12_FORMAT;
  3292. break;
  3293. case NID_pkcs8ShroudedKeyBag:
  3294. if (keylist == NULL)
  3295. goto end;
  3296. p8 = M_PKCS12_decrypt_skey(bag, pass, passlen);
  3297. if (p8 == NULL)
  3298. return (KMF_ERR_AUTH_FAILED);
  3299. pkey = EVP_PKCS82PKEY(p8);
  3300. PKCS8_PRIV_KEY_INFO_free(p8);
  3301. if (pkey == NULL)
  3302. ret = KMF_ERR_PKCS12_FORMAT;
  3303. break;
  3304. case NID_certBag:
  3305. if (certlist == NULL)
  3306. goto end;
  3307. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  3308. return (KMF_ERR_PKCS12_FORMAT);
  3309. xcert = M_PKCS12_certbag2x509(bag);
  3310. if (xcert == NULL) {
  3311. ret = KMF_ERR_PKCS12_FORMAT;
  3312. goto end;
  3313. }
  3314. if (keyid != NULL) {
  3315. if (X509_keyid_set1(xcert,
  3316. keyid->value.octet_string->data,
  3317. keyid->value.octet_string->length) == 0) {
  3318. ret = KMF_ERR_PKCS12_FORMAT;
  3319. goto end;
  3320. }
  3321. }
  3322. if (fname != NULL) {
  3323. int len, r;
  3324. len = ASN1_STRING_to_UTF8(&data,
  3325. fname->value.asn1_string);
  3326. if (len > 0 && data != NULL) {
  3327. r = X509_alias_set1(xcert, data, len);
  3328. if (r == NULL) {
  3329. ret = KMF_ERR_PKCS12_FORMAT;
  3330. goto end;
  3331. }
  3332. } else {
  3333. ret = KMF_ERR_PKCS12_FORMAT;
  3334. goto end;
  3335. }
  3336. }
  3337. if (sk_X509_push(certlist, xcert) == 0)
  3338. ret = KMF_ERR_MEMORY;
  3339. else
  3340. xcert = NULL;
  3341. break;
  3342. case NID_safeContentsBag:
  3343. return (openssl_parse_bags(bag->value.safes, pass,
  3344. keylist, certlist));
  3345. default:
  3346. ret = KMF_ERR_PKCS12_FORMAT;
  3347. break;
  3348. }
  3349. /*
  3350. * Set the ID and/or FriendlyName attributes on the key.
  3351. * If converting to PKCS11 objects, these can translate to CKA_ID
  3352. * and CKA_LABEL values.
  3353. */
  3354. if (pkey != NULL && ret == KMF_OK) {
  3355. ASN1_TYPE *attr = NULL;
  3356. if (keyid != NULL && keyid->type == V_ASN1_OCTET_STRING) {
  3357. if ((attr = ASN1_TYPE_new()) == NULL)
  3358. return (KMF_ERR_MEMORY);
  3359. attr->value.octet_string =
  3360. ASN1_STRING_dup(keyid->value.octet_string);
  3361. attr->type = V_ASN1_OCTET_STRING;
  3362. attr->value.ptr = (char *)attr->value.octet_string;
  3363. ret = set_pkey_attrib(pkey, attr, NID_localKeyID);
  3364. OPENSSL_free(attr);
  3365. }
  3366. if (ret == KMF_OK && fname != NULL &&
  3367. fname->type == V_ASN1_BMPSTRING) {
  3368. if ((attr = ASN1_TYPE_new()) == NULL)
  3369. return (KMF_ERR_MEMORY);
  3370. attr->value.bmpstring =
  3371. ASN1_STRING_dup(fname->value.bmpstring);
  3372. attr->type = V_ASN1_BMPSTRING;
  3373. attr->value.ptr = (char *)attr->value.bmpstring;
  3374. ret = set_pkey_attrib(pkey, attr, NID_friendlyName);
  3375. OPENSSL_free(attr);
  3376. }
  3377. if (ret == KMF_OK && keylist != NULL &&
  3378. sk_EVP_PKEY_push(keylist, pkey) == 0)
  3379. ret = KMF_ERR_MEMORY;
  3380. }
  3381. if (ret == KMF_OK && keylist != NULL)
  3382. pkey = NULL;
  3383. end:
  3384. if (pkey != NULL)
  3385. EVP_PKEY_free(pkey);
  3386. if (xcert != NULL)
  3387. X509_free(xcert);
  3388. if (data != NULL)
  3389. OPENSSL_free(data);
  3390. return (ret);
  3391. }
  3392. static KMF_RETURN
  3393. openssl_pkcs12_parse(PKCS12 *p12, char *pin,
  3394. STACK_OF(EVP_PKEY) *keys,
  3395. STACK_OF(X509) *certs,
  3396. STACK_OF(X509) *ca)
  3397. /* ARGSUSED3 */
  3398. {
  3399. KMF_RETURN ret = KMF_OK;
  3400. STACK_OF(PKCS7) *asafes = NULL;
  3401. STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
  3402. int i, bagnid;
  3403. PKCS7 *p7;
  3404. if (p12 == NULL || (keys == NULL && certs == NULL))
  3405. return (KMF_ERR_BAD_PARAMETER);
  3406. if (pin == NULL || *pin == NULL) {
  3407. if (PKCS12_verify_mac(p12, NULL, 0)) {
  3408. pin = NULL;
  3409. } else if (PKCS12_verify_mac(p12, "", 0)) {
  3410. pin = "";
  3411. } else {
  3412. return (KMF_ERR_AUTH_FAILED);
  3413. }
  3414. } else if (!PKCS12_verify_mac(p12, pin, -1)) {
  3415. return (KMF_ERR_AUTH_FAILED);
  3416. }
  3417. if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL)
  3418. return (KMF_ERR_PKCS12_FORMAT);
  3419. for (i = 0; ret == KMF_OK && i < sk_PKCS7_num(asafes); i++) {
  3420. bags = NULL;
  3421. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3422. p7 = sk_PKCS7_value(asafes, i);
  3423. bagnid = OBJ_obj2nid(p7->type);
  3424. if (bagnid == NID_pkcs7_data) {
  3425. bags = PKCS12_unpack_p7data(p7);
  3426. } else if (bagnid == NID_pkcs7_encrypted) {
  3427. bags = PKCS12_unpack_p7encdata(p7, pin,
  3428. (pin ? strlen(pin) : 0));
  3429. } else {
  3430. continue;
  3431. }
  3432. if (bags == NULL) {
  3433. ret = KMF_ERR_PKCS12_FORMAT;
  3434. goto out;
  3435. }
  3436. if (openssl_parse_bags(bags, pin, keys, certs) != KMF_OK)
  3437. ret = KMF_ERR_PKCS12_FORMAT;
  3438. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  3439. }
  3440. out:
  3441. if (asafes != NULL)
  3442. sk_PKCS7_pop_free(asafes, PKCS7_free);
  3443. return (ret);
  3444. }
  3445. /*
  3446. * Helper function to decrypt and parse PKCS#12 import file.
  3447. */
  3448. static KMF_RETURN
  3449. extract_pkcs12(BIO *fbio, CK_UTF8CHAR *pin, CK_ULONG pinlen,
  3450. STACK_OF(EVP_PKEY) **priv_key, STACK_OF(X509) **certs,
  3451. STACK_OF(X509) **ca)
  3452. /* ARGSUSED2 */
  3453. {
  3454. PKCS12 *pk12, *pk12_tmp;
  3455. STACK_OF(EVP_PKEY) *pkeylist = NULL;
  3456. STACK_OF(X509) *xcertlist = NULL;
  3457. STACK_OF(X509) *cacertlist = NULL;
  3458. if ((pk12 = PKCS12_new()) == NULL) {
  3459. return (KMF_ERR_MEMORY);
  3460. }
  3461. if ((pk12_tmp = d2i_PKCS12_bio(fbio, &pk12)) == NULL) {
  3462. /* This is ok; it seems to mean there is no more to read. */
  3463. if (ERR_GET_LIB(ERR_peek_error()) == ERR_LIB_ASN1 &&
  3464. ERR_GET_REASON(ERR_peek_error()) == ASN1_R_HEADER_TOO_LONG)
  3465. goto end_extract_pkcs12;
  3466. PKCS12_free(pk12);
  3467. return (KMF_ERR_PKCS12_FORMAT);
  3468. }
  3469. pk12 = pk12_tmp;
  3470. xcertlist = sk_X509_new_null();
  3471. if (xcertlist == NULL) {
  3472. PKCS12_free(pk12);
  3473. return (KMF_ERR_MEMORY);
  3474. }
  3475. pkeylist = sk_EVP_PKEY_new_null();
  3476. if (pkeylist == NULL) {
  3477. sk_X509_pop_free(xcertlist, X509_free);
  3478. PKCS12_free(pk12);
  3479. return (KMF_ERR_MEMORY);
  3480. }
  3481. if (openssl_pkcs12_parse(pk12, (char *)pin, pkeylist, xcertlist,
  3482. cacertlist) != KMF_OK) {
  3483. sk_X509_pop_free(xcertlist, X509_free);
  3484. sk_EVP_PKEY_pop_free(pkeylist, EVP_PKEY_free);
  3485. PKCS12_free(pk12);
  3486. return (KMF_ERR_PKCS12_FORMAT);
  3487. }
  3488. if (priv_key && pkeylist)
  3489. *priv_key = pkeylist;
  3490. else if (pkeylist)
  3491. sk_EVP_PKEY_pop_free(pkeylist, EVP_PKEY_free);
  3492. if (certs && xcertlist)
  3493. *certs = xcertlist;
  3494. else if (xcertlist)
  3495. sk_X509_pop_free(xcertlist, X509_free);
  3496. if (ca && cacertlist)
  3497. *ca = cacertlist;
  3498. else if (cacertlist)
  3499. sk_X509_pop_free(cacertlist, X509_free);
  3500. end_extract_pkcs12:
  3501. PKCS12_free(pk12);
  3502. return (KMF_OK);
  3503. }
  3504. static KMF_RETURN
  3505. sslBN2KMFBN(BIGNUM *from, KMF_BIGINT *to)
  3506. {
  3507. KMF_RETURN rv = KMF_OK;
  3508. uint32_t sz;
  3509. sz = BN_num_bytes(from);
  3510. to->val = (uchar_t *)malloc(sz);
  3511. if (to->val == NULL)
  3512. return (KMF_ERR_MEMORY);
  3513. if ((to->len = BN_bn2bin(from, to->val)) != sz) {
  3514. free(to->val);
  3515. to->val = NULL;
  3516. to->len = 0;
  3517. rv = KMF_ERR_MEMORY;
  3518. }
  3519. return (rv);
  3520. }
  3521. static KMF_RETURN
  3522. exportRawRSAKey(RSA *rsa, KMF_RAW_KEY_DATA *key)
  3523. {
  3524. KMF_RETURN rv;
  3525. KMF_RAW_RSA_KEY *kmfkey = &key->rawdata.rsa;
  3526. (void) memset(kmfkey, 0, sizeof (KMF_RAW_RSA_KEY));
  3527. if ((rv = sslBN2KMFBN(rsa->n, &kmfkey->mod)) != KMF_OK)
  3528. goto cleanup;
  3529. if ((rv = sslBN2KMFBN(rsa->e, &kmfkey->pubexp)) != KMF_OK)
  3530. goto cleanup;
  3531. if (rsa->d != NULL)
  3532. if ((rv = sslBN2KMFBN(rsa->d, &kmfkey->priexp)) != KMF_OK)
  3533. goto cleanup;
  3534. if (rsa->p != NULL)
  3535. if ((rv = sslBN2KMFBN(rsa->p, &kmfkey->prime1)) != KMF_OK)
  3536. goto cleanup;
  3537. if (rsa->q != NULL)
  3538. if ((rv = sslBN2KMFBN(rsa->q, &kmfkey->prime2)) != KMF_OK)
  3539. goto cleanup;
  3540. if (rsa->dmp1 != NULL)
  3541. if ((rv = sslBN2KMFBN(rsa->dmp1, &kmfkey->exp1)) != KMF_OK)
  3542. goto cleanup;
  3543. if (rsa->dmq1 != NULL)
  3544. if ((rv = sslBN2KMFBN(rsa->dmq1, &kmfkey->exp2)) != KMF_OK)
  3545. goto cleanup;
  3546. if (rsa->iqmp != NULL)
  3547. if ((rv = sslBN2KMFBN(rsa->iqmp, &kmfkey->coef)) != KMF_OK)
  3548. goto cleanup;
  3549. cleanup:
  3550. if (rv != KMF_OK)
  3551. kmf_free_raw_key(key);
  3552. else
  3553. key->keytype = KMF_RSA;
  3554. /*
  3555. * Free the reference to this key, SSL will not actually free
  3556. * the memory until the refcount == 0, so this is safe.
  3557. */
  3558. RSA_free(rsa);
  3559. return (rv);
  3560. }
  3561. static KMF_RETURN
  3562. exportRawDSAKey(DSA *dsa, KMF_RAW_KEY_DATA *key)
  3563. {
  3564. KMF_RETURN rv;
  3565. KMF_RAW_DSA_KEY *kmfkey = &key->rawdata.dsa;
  3566. (void) memset(kmfkey, 0, sizeof (KMF_RAW_DSA_KEY));
  3567. if ((rv = sslBN2KMFBN(dsa->p, &kmfkey->prime)) != KMF_OK)
  3568. goto cleanup;
  3569. if ((rv = sslBN2KMFBN(dsa->q, &kmfkey->subprime)) != KMF_OK)
  3570. goto cleanup;
  3571. if ((rv = sslBN2KMFBN(dsa->g, &kmfkey->base)) != KMF_OK)
  3572. goto cleanup;
  3573. if ((rv = sslBN2KMFBN(dsa->priv_key, &kmfkey->value)) != KMF_OK)
  3574. goto cleanup;
  3575. cleanup:
  3576. if (rv != KMF_OK)
  3577. kmf_free_raw_key(key);
  3578. else
  3579. key->keytype = KMF_DSA;
  3580. /*
  3581. * Free the reference to this key, SSL will not actually free
  3582. * the memory until the refcount == 0, so this is safe.
  3583. */
  3584. DSA_free(dsa);
  3585. return (rv);
  3586. }
  3587. static KMF_RETURN
  3588. add_cert_to_list(KMF_HANDLE *kmfh, X509 *sslcert,
  3589. KMF_X509_DER_CERT **certlist, int *ncerts)
  3590. {
  3591. KMF_RETURN rv = KMF_OK;
  3592. KMF_X509_DER_CERT *list = (*certlist);
  3593. KMF_X509_DER_CERT cert;
  3594. int n = (*ncerts);
  3595. if (list == NULL) {
  3596. list = (KMF_X509_DER_CERT *)malloc(sizeof (KMF_X509_DER_CERT));
  3597. } else {
  3598. list = (KMF_X509_DER_CERT *)realloc(list,
  3599. sizeof (KMF_X509_DER_CERT) * (n + 1));
  3600. }
  3601. if (list == NULL)
  3602. return (KMF_ERR_MEMORY);
  3603. (void) memset(&cert, 0, sizeof (cert));
  3604. rv = ssl_cert2KMFDATA(kmfh, sslcert, &cert.certificate);
  3605. if (rv == KMF_OK) {
  3606. int len = 0;
  3607. /* Get the alias name for the cert if there is one */
  3608. char *a = (char *)X509_alias_get0(sslcert, &len);
  3609. if (a != NULL)
  3610. cert.kmf_private.label = strdup(a);
  3611. cert.kmf_private.keystore_type = KMF_KEYSTORE_OPENSSL;
  3612. list[n] = cert;
  3613. (*ncerts) = n + 1;
  3614. *certlist = list;
  3615. } else {
  3616. free(list);
  3617. }
  3618. return (rv);
  3619. }
  3620. static KMF_RETURN
  3621. add_key_to_list(KMF_RAW_KEY_DATA **keylist,
  3622. KMF_RAW_KEY_DATA *newkey, int *nkeys)
  3623. {
  3624. KMF_RAW_KEY_DATA *list = (*keylist);
  3625. int n = (*nkeys);
  3626. if (list == NULL) {
  3627. list = (KMF_RAW_KEY_DATA *)malloc(sizeof (KMF_RAW_KEY_DATA));
  3628. } else {
  3629. list = (KMF_RAW_KEY_DATA *)realloc(list,
  3630. sizeof (KMF_RAW_KEY_DATA) * (n + 1));
  3631. }
  3632. if (list == NULL)
  3633. return (KMF_ERR_MEMORY);
  3634. list[n] = *newkey;
  3635. (*nkeys) = n + 1;
  3636. *keylist = list;
  3637. return (KMF_OK);
  3638. }
  3639. static X509_ATTRIBUTE *
  3640. find_attr(STACK_OF(X509_ATTRIBUTE) *attrs, int nid)
  3641. {
  3642. X509_ATTRIBUTE *a;
  3643. int i;
  3644. if (attrs == NULL)
  3645. return (NULL);
  3646. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrs); i++) {
  3647. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3648. a = sk_X509_ATTRIBUTE_value(attrs, i);
  3649. if (OBJ_obj2nid(a->object) == nid)
  3650. return (a);
  3651. }
  3652. return (NULL);
  3653. }
  3654. static KMF_RETURN
  3655. convertToRawKey(EVP_PKEY *pkey, KMF_RAW_KEY_DATA *key)
  3656. {
  3657. KMF_RETURN rv = KMF_OK;
  3658. X509_ATTRIBUTE *attr;
  3659. if (pkey == NULL || key == NULL)
  3660. return (KMF_ERR_BAD_PARAMETER);
  3661. /* Convert SSL key to raw key */
  3662. switch (pkey->type) {
  3663. case EVP_PKEY_RSA:
  3664. rv = exportRawRSAKey(EVP_PKEY_get1_RSA(pkey),
  3665. key);
  3666. if (rv != KMF_OK)
  3667. return (rv);
  3668. break;
  3669. case EVP_PKEY_DSA:
  3670. rv = exportRawDSAKey(EVP_PKEY_get1_DSA(pkey),
  3671. key);
  3672. if (rv != KMF_OK)
  3673. return (rv);
  3674. break;
  3675. default:
  3676. return (KMF_ERR_BAD_PARAMETER);
  3677. }
  3678. /*
  3679. * If friendlyName, add it to record.
  3680. */
  3681. attr = find_attr(pkey->attributes, NID_friendlyName);
  3682. if (attr != NULL) {
  3683. ASN1_TYPE *ty = NULL;
  3684. int numattr = sk_ASN1_TYPE_num(attr->value.set);
  3685. if (attr->single == 0 && numattr > 0) {
  3686. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3687. ty = sk_ASN1_TYPE_value(attr->value.set, 0);
  3688. }
  3689. if (ty != NULL) {
  3690. key->label = uni2asc(ty->value.bmpstring->data,
  3691. ty->value.bmpstring->length);
  3692. }
  3693. } else {
  3694. key->label = NULL;
  3695. }
  3696. /*
  3697. * If KeyID, add it to record as a KMF_DATA object.
  3698. */
  3699. attr = find_attr(pkey->attributes, NID_localKeyID);
  3700. if (attr != NULL) {
  3701. ASN1_TYPE *ty = NULL;
  3702. int numattr = sk_ASN1_TYPE_num(attr->value.set);
  3703. if (attr->single == 0 && numattr > 0) {
  3704. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3705. ty = sk_ASN1_TYPE_value(attr->value.set, 0);
  3706. }
  3707. key->id.Data = (uchar_t *)malloc(
  3708. ty->value.octet_string->length);
  3709. if (key->id.Data == NULL)
  3710. return (KMF_ERR_MEMORY);
  3711. (void) memcpy(key->id.Data, ty->value.octet_string->data,
  3712. ty->value.octet_string->length);
  3713. key->id.Length = ty->value.octet_string->length;
  3714. } else {
  3715. (void) memset(&key->id, 0, sizeof (KMF_DATA));
  3716. }
  3717. return (rv);
  3718. }
  3719. static KMF_RETURN
  3720. convertPK12Objects(
  3721. KMF_HANDLE *kmfh,
  3722. STACK_OF(EVP_PKEY) *sslkeys,
  3723. STACK_OF(X509) *sslcert,
  3724. STACK_OF(X509) *sslcacerts,
  3725. KMF_RAW_KEY_DATA **keylist, int *nkeys,
  3726. KMF_X509_DER_CERT **certlist, int *ncerts)
  3727. {
  3728. KMF_RETURN rv = KMF_OK;
  3729. KMF_RAW_KEY_DATA key;
  3730. int i;
  3731. for (i = 0; sslkeys != NULL && i < sk_EVP_PKEY_num(sslkeys); i++) {
  3732. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3733. EVP_PKEY *pkey = sk_EVP_PKEY_value(sslkeys, i);
  3734. rv = convertToRawKey(pkey, &key);
  3735. if (rv == KMF_OK)
  3736. rv = add_key_to_list(keylist, &key, nkeys);
  3737. if (rv != KMF_OK)
  3738. return (rv);
  3739. }
  3740. /* Now add the certificate to the certlist */
  3741. for (i = 0; sslcert != NULL && i < sk_X509_num(sslcert); i++) {
  3742. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3743. X509 *cert = sk_X509_value(sslcert, i);
  3744. rv = add_cert_to_list(kmfh, cert, certlist, ncerts);
  3745. if (rv != KMF_OK)
  3746. return (rv);
  3747. }
  3748. /* Also add any included CA certs to the list */
  3749. for (i = 0; sslcacerts != NULL && i < sk_X509_num(sslcacerts); i++) {
  3750. X509 *c;
  3751. /*
  3752. * sk_X509_value() is macro that embeds a cast to (X509 *).
  3753. * Here it translates into ((X509 *)sk_value((ca), (i))).
  3754. * Lint is complaining about the embedded casting, and
  3755. * to fix it, you need to fix openssl header files.
  3756. */
  3757. /* LINTED E_BAD_PTR_CAST_ALIGN */
  3758. c = sk_X509_value(sslcacerts, i);
  3759. /* Now add the ca cert to the certlist */
  3760. rv = add_cert_to_list(kmfh, c, certlist, ncerts);
  3761. if (rv != KMF_OK)
  3762. return (rv);
  3763. }
  3764. return (rv);
  3765. }
  3766. KMF_RETURN
  3767. openssl_import_objects(KMF_HANDLE *kmfh,
  3768. char *filename, KMF_CREDENTIAL *cred,
  3769. KMF_X509_DER_CERT **certlist, int *ncerts,
  3770. KMF_RAW_KEY_DATA **keylist, int *nkeys)
  3771. {
  3772. KMF_RETURN rv = KMF_OK;
  3773. KMF_ENCODE_FORMAT format;
  3774. BIO *bio = NULL;
  3775. STACK_OF(EVP_PKEY) *privkeys = NULL;
  3776. STACK_OF(X509) *certs = NULL;
  3777. STACK_OF(X509) *cacerts = NULL;
  3778. /*
  3779. * auto-detect the file format, regardless of what
  3780. * the 'format' parameters in the params say.
  3781. */
  3782. rv = kmf_get_file_format(filename, &format);
  3783. if (rv != KMF_OK) {
  3784. return (rv);
  3785. }
  3786. /* This function only works for PEM or PKCS#12 files */
  3787. if (format != KMF_FORMAT_PEM &&
  3788. format != KMF_FORMAT_PEM_KEYPAIR &&
  3789. format != KMF_FORMAT_PKCS12)
  3790. return (KMF_ERR_ENCODING);
  3791. *certlist = NULL;
  3792. *keylist = NULL;
  3793. *ncerts = 0;
  3794. *nkeys = 0;
  3795. if (format == KMF_FORMAT_PKCS12) {
  3796. bio = BIO_new_file(filename, "rb");
  3797. if (bio == NULL) {
  3798. SET_ERROR(kmfh, ERR_get_error());
  3799. rv = KMF_ERR_OPEN_FILE;
  3800. goto end;
  3801. }
  3802. rv = extract_pkcs12(bio, (uchar_t *)cred->cred,
  3803. (uint32_t)cred->credlen, &privkeys, &certs, &cacerts);
  3804. if (rv == KMF_OK)
  3805. /* Convert keys and certs to exportable format */
  3806. rv = convertPK12Objects(kmfh, privkeys, certs, cacerts,
  3807. keylist, nkeys, certlist, ncerts);
  3808. } else {
  3809. EVP_PKEY *pkey;
  3810. KMF_DATA *certdata = NULL;
  3811. KMF_X509_DER_CERT *kmfcerts = NULL;
  3812. int i;
  3813. rv = extract_pem(kmfh, NULL, NULL, NULL, filename,
  3814. (uchar_t *)cred->cred, (uint32_t)cred->credlen,
  3815. &pkey, &certdata, ncerts);
  3816. /* Reached end of import file? */
  3817. if (rv == KMF_OK && pkey != NULL) {
  3818. privkeys = sk_EVP_PKEY_new_null();
  3819. if (privkeys == NULL) {
  3820. rv = KMF_ERR_MEMORY;
  3821. goto end;
  3822. }
  3823. (void) sk_EVP_PKEY_push(privkeys, pkey);
  3824. /* convert the certificate list here */
  3825. if (*ncerts > 0 && certlist != NULL) {
  3826. kmfcerts = (KMF_X509_DER_CERT *)calloc(*ncerts,
  3827. sizeof (KMF_X509_DER_CERT));
  3828. if (kmfcerts == NULL) {
  3829. rv = KMF_ERR_MEMORY;
  3830. goto end;
  3831. }
  3832. for (i = 0; i < *ncerts; i++) {
  3833. kmfcerts[i].certificate = certdata[i];
  3834. kmfcerts[i].kmf_private.keystore_type =
  3835. KMF_KEYSTORE_OPENSSL;
  3836. }
  3837. *certlist = kmfcerts;
  3838. }
  3839. /*
  3840. * Convert keys to exportable format, the certs
  3841. * are already OK.
  3842. */
  3843. rv = convertPK12Objects(kmfh, privkeys, NULL, NULL,
  3844. keylist, nkeys, NULL, NULL);
  3845. }
  3846. }
  3847. end:
  3848. if (bio != NULL)
  3849. (void) BIO_free(bio);
  3850. if (privkeys)
  3851. sk_EVP_PKEY_pop_free(privkeys, EVP_PKEY_free);
  3852. if (certs)
  3853. sk_X509_pop_free(certs, X509_free);
  3854. if (cacerts)
  3855. sk_X509_pop_free(cacerts, X509_free);
  3856. return (rv);
  3857. }
  3858. static KMF_RETURN
  3859. create_deskey(DES_cblock **deskey)
  3860. {
  3861. DES_cblock *key;
  3862. key = (DES_cblock *) malloc(sizeof (DES_cblock));
  3863. if (key == NULL) {
  3864. return (KMF_ERR_MEMORY);
  3865. }
  3866. if (DES_random_key(key) == 0) {
  3867. free(key);
  3868. return (KMF_ERR_KEYGEN_FAILED);
  3869. }
  3870. *deskey = key;
  3871. return (KMF_OK);
  3872. }
  3873. #define KEYGEN_RETRY 3
  3874. #define DES3_KEY_SIZE 24
  3875. static KMF_RETURN
  3876. create_des3key(unsigned char **des3key)
  3877. {
  3878. KMF_RETURN ret = KMF_OK;
  3879. DES_cblock *deskey1 = NULL;
  3880. DES_cblock *deskey2 = NULL;
  3881. DES_cblock *deskey3 = NULL;
  3882. unsigned char *newkey = NULL;
  3883. int retry;
  3884. if ((newkey = malloc(DES3_KEY_SIZE)) == NULL) {
  3885. return (KMF_ERR_MEMORY);
  3886. }
  3887. /* create the 1st DES key */
  3888. if ((ret = create_deskey(&deskey1)) != KMF_OK) {
  3889. goto out;
  3890. }
  3891. /*
  3892. * Create the 2nd DES key and make sure its value is different
  3893. * from the 1st DES key.
  3894. */
  3895. retry = 0;
  3896. do {
  3897. if (deskey2 != NULL) {
  3898. free(deskey2);
  3899. deskey2 = NULL;
  3900. }
  3901. if ((ret = create_deskey(&deskey2)) != KMF_OK) {
  3902. goto out;
  3903. }
  3904. if (memcmp((const void *) deskey1, (const void *) deskey2, 8)
  3905. == 0) {
  3906. ret = KMF_ERR_KEYGEN_FAILED;
  3907. retry++;
  3908. }
  3909. } while (ret == KMF_ERR_KEYGEN_FAILED && retry < KEYGEN_RETRY);
  3910. if (ret != KMF_OK) {
  3911. goto out;
  3912. }
  3913. /*
  3914. * Create the 3rd DES key and make sure its value is different
  3915. * from the 2nd DES key.
  3916. */
  3917. retry = 0;
  3918. do {
  3919. if (deskey3 != NULL) {
  3920. free(deskey3);
  3921. deskey3 = NULL;
  3922. }
  3923. if ((ret = create_deskey(&deskey3)) != KMF_OK) {
  3924. goto out;
  3925. }
  3926. if (memcmp((const void *)deskey2, (const void *)deskey3, 8)
  3927. == 0) {
  3928. ret = KMF_ERR_KEYGEN_FAILED;
  3929. retry++;
  3930. }
  3931. } while (ret == KMF_ERR_KEYGEN_FAILED && retry < KEYGEN_RETRY);
  3932. if (ret != KMF_OK) {
  3933. goto out;
  3934. }
  3935. /* Concatenate 3 DES keys into a DES3 key */
  3936. (void) memcpy((void *)newkey, (const void *)deskey1, 8);
  3937. (void) memcpy((void *)(newkey + 8), (const void *)deskey2, 8);
  3938. (void) memcpy((void *)(newkey + 16), (const void *)deskey3, 8);
  3939. *des3key = newkey;
  3940. out:
  3941. if (deskey1 != NULL)
  3942. free(deskey1);
  3943. if (deskey2 != NULL)
  3944. free(deskey2);
  3945. if (deskey3 != NULL)
  3946. free(deskey3);
  3947. if (ret != KMF_OK && newkey != NULL)
  3948. free(newkey);
  3949. return (ret);
  3950. }
  3951. KMF_RETURN
  3952. OpenSSL_CreateSymKey(KMF_HANDLE_T handle,
  3953. int numattr, KMF_ATTRIBUTE *attrlist)
  3954. {
  3955. KMF_RETURN ret = KMF_OK;
  3956. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  3957. char *fullpath = NULL;
  3958. KMF_RAW_SYM_KEY *rkey = NULL;
  3959. DES_cblock *deskey = NULL;
  3960. unsigned char *des3key = NULL;
  3961. unsigned char *random = NULL;
  3962. int fd = -1;
  3963. KMF_KEY_HANDLE *symkey;
  3964. KMF_KEY_ALG keytype;
  3965. uint32_t keylen;
  3966. uint32_t keylen_size = sizeof (keylen);
  3967. char *dirpath;
  3968. char *keyfile;
  3969. if (kmfh == NULL)
  3970. return (KMF_ERR_UNINITIALIZED);
  3971. symkey = kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR, attrlist, numattr);
  3972. if (symkey == NULL)
  3973. return (KMF_ERR_BAD_PARAMETER);
  3974. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  3975. keyfile = kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR, attrlist, numattr);
  3976. if (keyfile == NULL)
  3977. return (KMF_ERR_BAD_PARAMETER);
  3978. ret = kmf_get_attr(KMF_KEYALG_ATTR, attrlist, numattr,
  3979. (void *)&keytype, NULL);
  3980. if (ret != KMF_OK)
  3981. return (KMF_ERR_BAD_PARAMETER);
  3982. ret = kmf_get_attr(KMF_KEYLENGTH_ATTR, attrlist, numattr,
  3983. &keylen, &keylen_size);
  3984. if (ret == KMF_ERR_ATTR_NOT_FOUND &&
  3985. (keytype == KMF_DES || keytype == KMF_DES3))
  3986. /* keylength is not required for DES and 3DES */
  3987. ret = KMF_OK;
  3988. if (ret != KMF_OK)
  3989. return (KMF_ERR_BAD_PARAMETER);
  3990. fullpath = get_fullpath(dirpath, keyfile);
  3991. if (fullpath == NULL)
  3992. return (KMF_ERR_BAD_PARAMETER);
  3993. /* If the requested file exists, return an error */
  3994. if (test_for_file(fullpath, 0400) == 1) {
  3995. free(fullpath);
  3996. return (KMF_ERR_DUPLICATE_KEYFILE);
  3997. }
  3998. fd = open(fullpath, O_CREAT|O_TRUNC|O_RDWR, 0400);
  3999. if (fd == -1) {
  4000. ret = KMF_ERR_OPEN_FILE;
  4001. goto out;
  4002. }
  4003. rkey = malloc(sizeof (KMF_RAW_SYM_KEY));
  4004. if (rkey == NULL) {
  4005. ret = KMF_ERR_MEMORY;
  4006. goto out;
  4007. }
  4008. (void) memset(rkey, 0, sizeof (KMF_RAW_SYM_KEY));
  4009. if (keytype == KMF_DES) {
  4010. if ((ret = create_deskey(&deskey)) != KMF_OK) {
  4011. goto out;
  4012. }
  4013. rkey->keydata.val = (uchar_t *)deskey;
  4014. rkey->keydata.len = 8;
  4015. symkey->keyalg = KMF_DES;
  4016. } else if (keytype == KMF_DES3) {
  4017. if ((ret = create_des3key(&des3key)) != KMF_OK) {
  4018. goto out;
  4019. }
  4020. rkey->keydata.val = (uchar_t *)des3key;
  4021. rkey->keydata.len = DES3_KEY_SIZE;
  4022. symkey->keyalg = KMF_DES3;
  4023. } else if (keytype == KMF_AES || keytype == KMF_RC4 ||
  4024. keytype == KMF_GENERIC_SECRET) {
  4025. int bytes;
  4026. if (keylen % 8 != 0) {
  4027. ret = KMF_ERR_BAD_KEY_SIZE;
  4028. goto out;
  4029. }
  4030. if (keytype == KMF_AES) {
  4031. if (keylen != 128 &&
  4032. keylen != 192 &&
  4033. keylen != 256) {
  4034. ret = KMF_ERR_BAD_KEY_SIZE;
  4035. goto out;
  4036. }
  4037. }
  4038. bytes = keylen/8;
  4039. random = malloc(bytes);
  4040. if (random == NULL) {
  4041. ret = KMF_ERR_MEMORY;
  4042. goto out;
  4043. }
  4044. if (RAND_bytes(random, bytes) != 1) {
  4045. ret = KMF_ERR_KEYGEN_FAILED;
  4046. goto out;
  4047. }
  4048. rkey->keydata.val = (uchar_t *)random;
  4049. rkey->keydata.len = bytes;
  4050. symkey->keyalg = keytype;
  4051. } else {
  4052. ret = KMF_ERR_BAD_KEY_TYPE;
  4053. goto out;
  4054. }
  4055. (void) write(fd, (const void *) rkey->keydata.val, rkey->keydata.len);
  4056. symkey->kstype = KMF_KEYSTORE_OPENSSL;
  4057. symkey->keyclass = KMF_SYMMETRIC;
  4058. symkey->keylabel = (char *)fullpath;
  4059. symkey->israw = TRUE;
  4060. symkey->keyp = rkey;
  4061. out:
  4062. if (fd != -1)
  4063. (void) close(fd);
  4064. if (ret != KMF_OK && fullpath != NULL) {
  4065. free(fullpath);
  4066. }
  4067. if (ret != KMF_OK) {
  4068. kmf_free_raw_sym_key(rkey);
  4069. symkey->keyp = NULL;
  4070. symkey->keyalg = KMF_KEYALG_NONE;
  4071. }
  4072. return (ret);
  4073. }
  4074. /*
  4075. * Check a file to see if it is a CRL file with PEM or DER format.
  4076. * If success, return its format in the "pformat" argument.
  4077. */
  4078. KMF_RETURN
  4079. OpenSSL_IsCRLFile(KMF_HANDLE_T handle, char *filename, int *pformat)
  4080. {
  4081. KMF_RETURN ret = KMF_OK;
  4082. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4083. BIO *bio = NULL;
  4084. X509_CRL *xcrl = NULL;
  4085. if (filename == NULL) {
  4086. return (KMF_ERR_BAD_PARAMETER);
  4087. }
  4088. bio = BIO_new_file(filename, "rb");
  4089. if (bio == NULL) {
  4090. SET_ERROR(kmfh, ERR_get_error());
  4091. ret = KMF_ERR_OPEN_FILE;
  4092. goto out;
  4093. }
  4094. if ((xcrl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL)) != NULL) {
  4095. *pformat = KMF_FORMAT_PEM;
  4096. goto out;
  4097. }
  4098. (void) BIO_free(bio);
  4099. /*
  4100. * Now try to read it as raw DER data.
  4101. */
  4102. bio = BIO_new_file(filename, "rb");
  4103. if (bio == NULL) {
  4104. SET_ERROR(kmfh, ERR_get_error());
  4105. ret = KMF_ERR_OPEN_FILE;
  4106. goto out;
  4107. }
  4108. if ((xcrl = d2i_X509_CRL_bio(bio, NULL)) != NULL) {
  4109. *pformat = KMF_FORMAT_ASN1;
  4110. } else {
  4111. ret = KMF_ERR_BAD_CRLFILE;
  4112. }
  4113. out:
  4114. if (bio != NULL)
  4115. (void) BIO_free(bio);
  4116. if (xcrl != NULL)
  4117. X509_CRL_free(xcrl);
  4118. return (ret);
  4119. }
  4120. KMF_RETURN
  4121. OpenSSL_GetSymKeyValue(KMF_HANDLE_T handle, KMF_KEY_HANDLE *symkey,
  4122. KMF_RAW_SYM_KEY *rkey)
  4123. {
  4124. KMF_RETURN rv = KMF_OK;
  4125. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4126. KMF_DATA keyvalue;
  4127. if (kmfh == NULL)
  4128. return (KMF_ERR_UNINITIALIZED);
  4129. if (symkey == NULL || rkey == NULL)
  4130. return (KMF_ERR_BAD_PARAMETER);
  4131. else if (symkey->keyclass != KMF_SYMMETRIC)
  4132. return (KMF_ERR_BAD_KEY_CLASS);
  4133. if (symkey->israw) {
  4134. KMF_RAW_SYM_KEY *rawkey = (KMF_RAW_SYM_KEY *)symkey->keyp;
  4135. if (rawkey == NULL ||
  4136. rawkey->keydata.val == NULL ||
  4137. rawkey->keydata.len == 0)
  4138. return (KMF_ERR_BAD_KEYHANDLE);
  4139. rkey->keydata.len = rawkey->keydata.len;
  4140. if ((rkey->keydata.val = malloc(rkey->keydata.len)) == NULL)
  4141. return (KMF_ERR_MEMORY);
  4142. (void) memcpy(rkey->keydata.val, rawkey->keydata.val,
  4143. rkey->keydata.len);
  4144. } else {
  4145. rv = kmf_read_input_file(handle, symkey->keylabel, &keyvalue);
  4146. if (rv != KMF_OK)
  4147. return (rv);
  4148. rkey->keydata.len = keyvalue.Length;
  4149. rkey->keydata.val = keyvalue.Data;
  4150. }
  4151. return (rv);
  4152. }
  4153. /*
  4154. * substitute for the unsafe access(2) function.
  4155. * If the file in question already exists, return 1.
  4156. * else 0. If an error occurs during testing (other
  4157. * than EEXIST), return -1.
  4158. */
  4159. static int
  4160. test_for_file(char *filename, mode_t mode)
  4161. {
  4162. int fd;
  4163. /*
  4164. * Try to create the file with the EXCL flag.
  4165. * The call should fail if the file exists.
  4166. */
  4167. fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, mode);
  4168. if (fd == -1 && errno == EEXIST)
  4169. return (1);
  4170. else if (fd == -1) /* some other error */
  4171. return (-1);
  4172. /* The file did NOT exist. Delete the testcase. */
  4173. (void) close(fd);
  4174. (void) unlink(filename);
  4175. return (0);
  4176. }
  4177. KMF_RETURN
  4178. OpenSSL_StoreKey(KMF_HANDLE_T handle, int numattr,
  4179. KMF_ATTRIBUTE *attrlist)
  4180. {
  4181. KMF_RETURN rv = KMF_OK;
  4182. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4183. KMF_KEY_HANDLE *pubkey = NULL, *prikey = NULL;
  4184. KMF_RAW_KEY_DATA *rawkey;
  4185. EVP_PKEY *pkey = NULL;
  4186. KMF_ENCODE_FORMAT format = KMF_FORMAT_PEM;
  4187. KMF_CREDENTIAL cred = {NULL, 0};
  4188. BIO *out = NULL;
  4189. int keys = 0;
  4190. char *fullpath = NULL;
  4191. char *keyfile = NULL;
  4192. char *dirpath = NULL;
  4193. pubkey = kmf_get_attr_ptr(KMF_PUBKEY_HANDLE_ATTR, attrlist, numattr);
  4194. if (pubkey != NULL)
  4195. keys++;
  4196. prikey = kmf_get_attr_ptr(KMF_PRIVKEY_HANDLE_ATTR, attrlist, numattr);
  4197. if (prikey != NULL)
  4198. keys++;
  4199. rawkey = kmf_get_attr_ptr(KMF_RAW_KEY_ATTR, attrlist, numattr);
  4200. if (rawkey != NULL)
  4201. keys++;
  4202. /*
  4203. * Exactly 1 type of key must be passed to this function.
  4204. */
  4205. if (keys != 1)
  4206. return (KMF_ERR_BAD_PARAMETER);
  4207. keyfile = (char *)kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR, attrlist,
  4208. numattr);
  4209. if (keyfile == NULL)
  4210. return (KMF_ERR_BAD_PARAMETER);
  4211. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  4212. fullpath = get_fullpath(dirpath, keyfile);
  4213. /* Once we have the full path, we don't need the pieces */
  4214. if (fullpath == NULL)
  4215. return (KMF_ERR_BAD_PARAMETER);
  4216. /* If the requested file exists, return an error */
  4217. if (test_for_file(fullpath, 0400) == 1) {
  4218. free(fullpath);
  4219. return (KMF_ERR_DUPLICATE_KEYFILE);
  4220. }
  4221. rv = kmf_get_attr(KMF_ENCODE_FORMAT_ATTR, attrlist, numattr,
  4222. &format, NULL);
  4223. if (rv != KMF_OK)
  4224. /* format is optional. */
  4225. rv = KMF_OK;
  4226. /* CRED is not required for OpenSSL files */
  4227. (void) kmf_get_attr(KMF_CREDENTIAL_ATTR, attrlist, numattr,
  4228. &cred, NULL);
  4229. /* Store the private key to the keyfile */
  4230. out = BIO_new_file(fullpath, "wb");
  4231. if (out == NULL) {
  4232. SET_ERROR(kmfh, ERR_get_error());
  4233. rv = KMF_ERR_OPEN_FILE;
  4234. goto end;
  4235. }
  4236. if (prikey != NULL && prikey->keyp != NULL) {
  4237. if (prikey->keyalg == KMF_RSA ||
  4238. prikey->keyalg == KMF_DSA) {
  4239. pkey = (EVP_PKEY *)prikey->keyp;
  4240. rv = ssl_write_key(kmfh, format,
  4241. out, &cred, pkey, TRUE);
  4242. if (rv == KMF_OK && prikey->keylabel == NULL) {
  4243. prikey->keylabel = strdup(fullpath);
  4244. if (prikey->keylabel == NULL)
  4245. rv = KMF_ERR_MEMORY;
  4246. }
  4247. }
  4248. } else if (pubkey != NULL && pubkey->keyp != NULL) {
  4249. if (pubkey->keyalg == KMF_RSA ||
  4250. pubkey->keyalg == KMF_DSA) {
  4251. pkey = (EVP_PKEY *)pubkey->keyp;
  4252. rv = ssl_write_key(kmfh, format,
  4253. out, &cred, pkey, FALSE);
  4254. if (rv == KMF_OK && pubkey->keylabel == NULL) {
  4255. pubkey->keylabel = strdup(fullpath);
  4256. if (pubkey->keylabel == NULL)
  4257. rv = KMF_ERR_MEMORY;
  4258. }
  4259. }
  4260. } else if (rawkey != NULL) {
  4261. if (rawkey->keytype == KMF_RSA) {
  4262. pkey = ImportRawRSAKey(&rawkey->rawdata.rsa);
  4263. } else if (rawkey->keytype == KMF_DSA) {
  4264. pkey = ImportRawDSAKey(&rawkey->rawdata.dsa);
  4265. } else {
  4266. rv = KMF_ERR_BAD_PARAMETER;
  4267. }
  4268. if (pkey != NULL) {
  4269. KMF_KEY_CLASS kclass = KMF_ASYM_PRI;
  4270. rv = kmf_get_attr(KMF_KEYCLASS_ATTR, attrlist, numattr,
  4271. (void *)&kclass, NULL);
  4272. if (rv != KMF_OK)
  4273. rv = KMF_OK;
  4274. rv = ssl_write_key(kmfh, format, out,
  4275. &cred, pkey, (kclass == KMF_ASYM_PRI));
  4276. EVP_PKEY_free(pkey);
  4277. }
  4278. }
  4279. end:
  4280. if (out)
  4281. (void) BIO_free(out);
  4282. if (rv == KMF_OK)
  4283. (void) chmod(fullpath, 0400);
  4284. free(fullpath);
  4285. return (rv);
  4286. }
  4287. KMF_RETURN
  4288. OpenSSL_ImportCRL(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  4289. {
  4290. KMF_RETURN ret = KMF_OK;
  4291. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4292. X509_CRL *xcrl = NULL;
  4293. X509 *xcert = NULL;
  4294. EVP_PKEY *pkey;
  4295. KMF_ENCODE_FORMAT format;
  4296. BIO *in = NULL, *out = NULL;
  4297. int openssl_ret = 0;
  4298. KMF_ENCODE_FORMAT outformat;
  4299. boolean_t crlcheck = FALSE;
  4300. char *certfile, *dirpath, *crlfile, *incrl, *outcrl, *outcrlfile;
  4301. if (numattr == 0 || attrlist == NULL) {
  4302. return (KMF_ERR_BAD_PARAMETER);
  4303. }
  4304. /* CRL check is optional */
  4305. (void) kmf_get_attr(KMF_CRL_CHECK_ATTR, attrlist, numattr,
  4306. &crlcheck, NULL);
  4307. certfile = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist, numattr);
  4308. if (crlcheck == B_TRUE && certfile == NULL) {
  4309. return (KMF_ERR_BAD_CERTFILE);
  4310. }
  4311. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  4312. incrl = kmf_get_attr_ptr(KMF_CRL_FILENAME_ATTR, attrlist, numattr);
  4313. outcrl = kmf_get_attr_ptr(KMF_CRL_OUTFILE_ATTR, attrlist, numattr);
  4314. crlfile = get_fullpath(dirpath, incrl);
  4315. if (crlfile == NULL)
  4316. return (KMF_ERR_BAD_CRLFILE);
  4317. outcrlfile = get_fullpath(dirpath, outcrl);
  4318. if (outcrlfile == NULL)
  4319. return (KMF_ERR_BAD_CRLFILE);
  4320. if (isdir(outcrlfile)) {
  4321. free(outcrlfile);
  4322. return (KMF_ERR_BAD_CRLFILE);
  4323. }
  4324. ret = kmf_is_crl_file(handle, crlfile, &format);
  4325. if (ret != KMF_OK) {
  4326. free(outcrlfile);
  4327. return (ret);
  4328. }
  4329. in = BIO_new_file(crlfile, "rb");
  4330. if (in == NULL) {
  4331. SET_ERROR(kmfh, ERR_get_error());
  4332. ret = KMF_ERR_OPEN_FILE;
  4333. goto end;
  4334. }
  4335. if (format == KMF_FORMAT_ASN1) {
  4336. xcrl = d2i_X509_CRL_bio(in, NULL);
  4337. } else if (format == KMF_FORMAT_PEM) {
  4338. xcrl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
  4339. }
  4340. if (xcrl == NULL) {
  4341. SET_ERROR(kmfh, ERR_get_error());
  4342. ret = KMF_ERR_BAD_CRLFILE;
  4343. goto end;
  4344. }
  4345. /* If bypasscheck is specified, no need to verify. */
  4346. if (crlcheck == B_FALSE)
  4347. goto output;
  4348. ret = kmf_is_cert_file(handle, certfile, &format);
  4349. if (ret != KMF_OK)
  4350. goto end;
  4351. /* Read in the CA cert file and convert to X509 */
  4352. if (BIO_read_filename(in, certfile) <= 0) {
  4353. SET_ERROR(kmfh, ERR_get_error());
  4354. ret = KMF_ERR_OPEN_FILE;
  4355. goto end;
  4356. }
  4357. if (format == KMF_FORMAT_ASN1) {
  4358. xcert = d2i_X509_bio(in, NULL);
  4359. } else if (format == KMF_FORMAT_PEM) {
  4360. xcert = PEM_read_bio_X509(in, NULL, NULL, NULL);
  4361. } else {
  4362. ret = KMF_ERR_BAD_CERT_FORMAT;
  4363. goto end;
  4364. }
  4365. if (xcert == NULL) {
  4366. SET_ERROR(kmfh, ERR_get_error());
  4367. ret = KMF_ERR_BAD_CERT_FORMAT;
  4368. goto end;
  4369. }
  4370. /* Now get the public key from the CA cert */
  4371. pkey = X509_get_pubkey(xcert);
  4372. if (pkey == NULL) {
  4373. SET_ERROR(kmfh, ERR_get_error());
  4374. ret = KMF_ERR_BAD_CERTFILE;
  4375. goto end;
  4376. }
  4377. /* Verify the CRL with the CA's public key */
  4378. openssl_ret = X509_CRL_verify(xcrl, pkey);
  4379. EVP_PKEY_free(pkey);
  4380. if (openssl_ret > 0) {
  4381. ret = KMF_OK; /* verify succeed */
  4382. } else {
  4383. SET_ERROR(kmfh, openssl_ret);
  4384. ret = KMF_ERR_BAD_CRLFILE;
  4385. }
  4386. output:
  4387. ret = kmf_get_attr(KMF_ENCODE_FORMAT_ATTR, attrlist, numattr,
  4388. &outformat, NULL);
  4389. if (ret != KMF_OK) {
  4390. ret = KMF_OK;
  4391. outformat = KMF_FORMAT_PEM;
  4392. }
  4393. out = BIO_new_file(outcrlfile, "wb");
  4394. if (out == NULL) {
  4395. SET_ERROR(kmfh, ERR_get_error());
  4396. ret = KMF_ERR_OPEN_FILE;
  4397. goto end;
  4398. }
  4399. if (outformat == KMF_FORMAT_ASN1) {
  4400. openssl_ret = (int)i2d_X509_CRL_bio(out, xcrl);
  4401. } else if (outformat == KMF_FORMAT_PEM) {
  4402. openssl_ret = PEM_write_bio_X509_CRL(out, xcrl);
  4403. } else {
  4404. ret = KMF_ERR_BAD_PARAMETER;
  4405. goto end;
  4406. }
  4407. if (openssl_ret <= 0) {
  4408. SET_ERROR(kmfh, ERR_get_error());
  4409. ret = KMF_ERR_WRITE_FILE;
  4410. } else {
  4411. ret = KMF_OK;
  4412. }
  4413. end:
  4414. if (xcrl != NULL)
  4415. X509_CRL_free(xcrl);
  4416. if (xcert != NULL)
  4417. X509_free(xcert);
  4418. if (in != NULL)
  4419. (void) BIO_free(in);
  4420. if (out != NULL)
  4421. (void) BIO_free(out);
  4422. if (outcrlfile != NULL)
  4423. free(outcrlfile);
  4424. return (ret);
  4425. }
  4426. KMF_RETURN
  4427. OpenSSL_ListCRL(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  4428. {
  4429. KMF_RETURN ret = KMF_OK;
  4430. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4431. X509_CRL *x = NULL;
  4432. KMF_ENCODE_FORMAT format;
  4433. char *crlfile = NULL;
  4434. BIO *in = NULL;
  4435. BIO *mem = NULL;
  4436. long len;
  4437. char *memptr;
  4438. char *data = NULL;
  4439. char **crldata;
  4440. char *crlfilename, *dirpath;
  4441. if (numattr == 0 || attrlist == NULL) {
  4442. return (KMF_ERR_BAD_PARAMETER);
  4443. }
  4444. crlfilename = kmf_get_attr_ptr(KMF_CRL_FILENAME_ATTR,
  4445. attrlist, numattr);
  4446. if (crlfilename == NULL)
  4447. return (KMF_ERR_BAD_CRLFILE);
  4448. crldata = (char **)kmf_get_attr_ptr(KMF_CRL_DATA_ATTR,
  4449. attrlist, numattr);
  4450. if (crldata == NULL)
  4451. return (KMF_ERR_BAD_PARAMETER);
  4452. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  4453. crlfile = get_fullpath(dirpath, crlfilename);
  4454. if (crlfile == NULL)
  4455. return (KMF_ERR_BAD_CRLFILE);
  4456. if (isdir(crlfile)) {
  4457. free(crlfile);
  4458. return (KMF_ERR_BAD_CRLFILE);
  4459. }
  4460. ret = kmf_is_crl_file(handle, crlfile, &format);
  4461. if (ret != KMF_OK) {
  4462. free(crlfile);
  4463. return (ret);
  4464. }
  4465. if (bio_err == NULL)
  4466. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  4467. in = BIO_new_file(crlfile, "rb");
  4468. if (in == NULL) {
  4469. SET_ERROR(kmfh, ERR_get_error());
  4470. ret = KMF_ERR_OPEN_FILE;
  4471. goto end;
  4472. }
  4473. if (format == KMF_FORMAT_ASN1) {
  4474. x = d2i_X509_CRL_bio(in, NULL);
  4475. } else if (format == KMF_FORMAT_PEM) {
  4476. x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
  4477. }
  4478. if (x == NULL) { /* should not happen */
  4479. SET_ERROR(kmfh, ERR_get_error());
  4480. ret = KMF_ERR_OPEN_FILE;
  4481. goto end;
  4482. }
  4483. mem = BIO_new(BIO_s_mem());
  4484. if (mem == NULL) {
  4485. SET_ERROR(kmfh, ERR_get_error());
  4486. ret = KMF_ERR_MEMORY;
  4487. goto end;
  4488. }
  4489. (void) X509_CRL_print(mem, x);
  4490. len = BIO_get_mem_data(mem, &memptr);
  4491. if (len <= 0) {
  4492. SET_ERROR(kmfh, ERR_get_error());
  4493. ret = KMF_ERR_MEMORY;
  4494. goto end;
  4495. }
  4496. data = malloc(len + 1);
  4497. if (data == NULL) {
  4498. ret = KMF_ERR_MEMORY;
  4499. goto end;
  4500. }
  4501. (void) memcpy(data, memptr, len);
  4502. data[len] = '\0';
  4503. *crldata = data;
  4504. end:
  4505. if (x != NULL)
  4506. X509_CRL_free(x);
  4507. if (crlfile != NULL)
  4508. free(crlfile);
  4509. if (in != NULL)
  4510. (void) BIO_free(in);
  4511. if (mem != NULL)
  4512. (void) BIO_free(mem);
  4513. return (ret);
  4514. }
  4515. KMF_RETURN
  4516. OpenSSL_DeleteCRL(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  4517. {
  4518. KMF_RETURN ret = KMF_OK;
  4519. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4520. KMF_ENCODE_FORMAT format;
  4521. char *crlfile = NULL;
  4522. BIO *in = NULL;
  4523. char *crlfilename, *dirpath;
  4524. if (numattr == 0 || attrlist == NULL) {
  4525. return (KMF_ERR_BAD_PARAMETER);
  4526. }
  4527. crlfilename = kmf_get_attr_ptr(KMF_CRL_FILENAME_ATTR,
  4528. attrlist, numattr);
  4529. if (crlfilename == NULL)
  4530. return (KMF_ERR_BAD_CRLFILE);
  4531. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  4532. crlfile = get_fullpath(dirpath, crlfilename);
  4533. if (crlfile == NULL)
  4534. return (KMF_ERR_BAD_CRLFILE);
  4535. if (isdir(crlfile)) {
  4536. ret = KMF_ERR_BAD_CRLFILE;
  4537. goto end;
  4538. }
  4539. ret = kmf_is_crl_file(handle, crlfile, &format);
  4540. if (ret != KMF_OK)
  4541. goto end;
  4542. if (unlink(crlfile) != 0) {
  4543. SET_SYS_ERROR(kmfh, errno);
  4544. ret = KMF_ERR_INTERNAL;
  4545. goto end;
  4546. }
  4547. end:
  4548. if (in != NULL)
  4549. (void) BIO_free(in);
  4550. if (crlfile != NULL)
  4551. free(crlfile);
  4552. return (ret);
  4553. }
  4554. KMF_RETURN
  4555. OpenSSL_FindCertInCRL(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  4556. {
  4557. KMF_RETURN ret = KMF_OK;
  4558. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4559. KMF_ENCODE_FORMAT format;
  4560. BIO *in = NULL;
  4561. X509 *xcert = NULL;
  4562. X509_CRL *xcrl = NULL;
  4563. STACK_OF(X509_REVOKED) *revoke_stack = NULL;
  4564. X509_REVOKED *revoke;
  4565. int i;
  4566. char *crlfilename, *crlfile, *dirpath, *certfile;
  4567. if (numattr == 0 || attrlist == NULL) {
  4568. return (KMF_ERR_BAD_PARAMETER);
  4569. }
  4570. crlfilename = kmf_get_attr_ptr(KMF_CRL_FILENAME_ATTR,
  4571. attrlist, numattr);
  4572. if (crlfilename == NULL)
  4573. return (KMF_ERR_BAD_CRLFILE);
  4574. certfile = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist, numattr);
  4575. if (certfile == NULL)
  4576. return (KMF_ERR_BAD_CRLFILE);
  4577. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  4578. crlfile = get_fullpath(dirpath, crlfilename);
  4579. if (crlfile == NULL)
  4580. return (KMF_ERR_BAD_CRLFILE);
  4581. if (isdir(crlfile)) {
  4582. ret = KMF_ERR_BAD_CRLFILE;
  4583. goto end;
  4584. }
  4585. ret = kmf_is_crl_file(handle, crlfile, &format);
  4586. if (ret != KMF_OK)
  4587. goto end;
  4588. /* Read the CRL file and load it into a X509_CRL structure */
  4589. in = BIO_new_file(crlfilename, "rb");
  4590. if (in == NULL) {
  4591. SET_ERROR(kmfh, ERR_get_error());
  4592. ret = KMF_ERR_OPEN_FILE;
  4593. goto end;
  4594. }
  4595. if (format == KMF_FORMAT_ASN1) {
  4596. xcrl = d2i_X509_CRL_bio(in, NULL);
  4597. } else if (format == KMF_FORMAT_PEM) {
  4598. xcrl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
  4599. }
  4600. if (xcrl == NULL) {
  4601. SET_ERROR(kmfh, ERR_get_error());
  4602. ret = KMF_ERR_BAD_CRLFILE;
  4603. goto end;
  4604. }
  4605. (void) BIO_free(in);
  4606. /* Read the Certificate file and load it into a X509 structure */
  4607. ret = kmf_is_cert_file(handle, certfile, &format);
  4608. if (ret != KMF_OK)
  4609. goto end;
  4610. in = BIO_new_file(certfile, "rb");
  4611. if (in == NULL) {
  4612. SET_ERROR(kmfh, ERR_get_error());
  4613. ret = KMF_ERR_OPEN_FILE;
  4614. goto end;
  4615. }
  4616. if (format == KMF_FORMAT_ASN1) {
  4617. xcert = d2i_X509_bio(in, NULL);
  4618. } else if (format == KMF_FORMAT_PEM) {
  4619. xcert = PEM_read_bio_X509(in, NULL, NULL, NULL);
  4620. }
  4621. if (xcert == NULL) {
  4622. SET_ERROR(kmfh, ERR_get_error());
  4623. ret = KMF_ERR_BAD_CERTFILE;
  4624. goto end;
  4625. }
  4626. /* Check if the certificate and the CRL have same issuer */
  4627. if (X509_NAME_cmp(xcert->cert_info->issuer, xcrl->crl->issuer) != 0) {
  4628. ret = KMF_ERR_ISSUER;
  4629. goto end;
  4630. }
  4631. /* Check to see if the certificate serial number is revoked */
  4632. revoke_stack = X509_CRL_get_REVOKED(xcrl);
  4633. if (sk_X509_REVOKED_num(revoke_stack) <= 0) {
  4634. /* No revoked certificates in the CRL file */
  4635. SET_ERROR(kmfh, ERR_get_error());
  4636. ret = KMF_ERR_EMPTY_CRL;
  4637. goto end;
  4638. }
  4639. for (i = 0; i < sk_X509_REVOKED_num(revoke_stack); i++) {
  4640. /* LINTED E_BAD_PTR_CAST_ALIGN */
  4641. revoke = sk_X509_REVOKED_value(revoke_stack, i);
  4642. if (ASN1_INTEGER_cmp(xcert->cert_info->serialNumber,
  4643. revoke->serialNumber) == 0) {
  4644. break;
  4645. }
  4646. }
  4647. if (i < sk_X509_REVOKED_num(revoke_stack)) {
  4648. ret = KMF_OK;
  4649. } else {
  4650. ret = KMF_ERR_NOT_REVOKED;
  4651. }
  4652. end:
  4653. if (in != NULL)
  4654. (void) BIO_free(in);
  4655. if (xcrl != NULL)
  4656. X509_CRL_free(xcrl);
  4657. if (xcert != NULL)
  4658. X509_free(xcert);
  4659. return (ret);
  4660. }
  4661. KMF_RETURN
  4662. OpenSSL_VerifyCRLFile(KMF_HANDLE_T handle, char *crlname, KMF_DATA *tacert)
  4663. {
  4664. KMF_RETURN ret = KMF_OK;
  4665. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4666. BIO *bcrl = NULL;
  4667. X509_CRL *xcrl = NULL;
  4668. X509 *xcert = NULL;
  4669. EVP_PKEY *pkey;
  4670. int sslret;
  4671. KMF_ENCODE_FORMAT crl_format;
  4672. unsigned char *p;
  4673. long len;
  4674. if (handle == NULL || crlname == NULL || tacert == NULL) {
  4675. return (KMF_ERR_BAD_PARAMETER);
  4676. }
  4677. ret = kmf_get_file_format(crlname, &crl_format);
  4678. if (ret != KMF_OK)
  4679. return (ret);
  4680. bcrl = BIO_new_file(crlname, "rb");
  4681. if (bcrl == NULL) {
  4682. SET_ERROR(kmfh, ERR_get_error());
  4683. ret = KMF_ERR_OPEN_FILE;
  4684. goto cleanup;
  4685. }
  4686. if (crl_format == KMF_FORMAT_ASN1) {
  4687. xcrl = d2i_X509_CRL_bio(bcrl, NULL);
  4688. } else if (crl_format == KMF_FORMAT_PEM) {
  4689. xcrl = PEM_read_bio_X509_CRL(bcrl, NULL, NULL, NULL);
  4690. } else {
  4691. ret = KMF_ERR_BAD_PARAMETER;
  4692. goto cleanup;
  4693. }
  4694. if (xcrl == NULL) {
  4695. SET_ERROR(kmfh, ERR_get_error());
  4696. ret = KMF_ERR_BAD_CRLFILE;
  4697. goto cleanup;
  4698. }
  4699. p = tacert->Data;
  4700. len = tacert->Length;
  4701. xcert = d2i_X509(NULL, (const uchar_t **)&p, len);
  4702. if (xcert == NULL) {
  4703. SET_ERROR(kmfh, ERR_get_error());
  4704. ret = KMF_ERR_BAD_CERTFILE;
  4705. goto cleanup;
  4706. }
  4707. /* Get issuer certificate public key */
  4708. pkey = X509_get_pubkey(xcert);
  4709. if (pkey == NULL) {
  4710. SET_ERROR(kmfh, ERR_get_error());
  4711. ret = KMF_ERR_BAD_CERT_FORMAT;
  4712. goto cleanup;
  4713. }
  4714. /* Verify CRL signature */
  4715. sslret = X509_CRL_verify(xcrl, pkey);
  4716. EVP_PKEY_free(pkey);
  4717. if (sslret > 0) {
  4718. ret = KMF_OK;
  4719. } else {
  4720. SET_ERROR(kmfh, sslret);
  4721. ret = KMF_ERR_BAD_CRLFILE;
  4722. }
  4723. cleanup:
  4724. if (bcrl != NULL)
  4725. (void) BIO_free(bcrl);
  4726. if (xcrl != NULL)
  4727. X509_CRL_free(xcrl);
  4728. if (xcert != NULL)
  4729. X509_free(xcert);
  4730. return (ret);
  4731. }
  4732. KMF_RETURN
  4733. OpenSSL_CheckCRLDate(KMF_HANDLE_T handle, char *crlname)
  4734. {
  4735. KMF_RETURN ret = KMF_OK;
  4736. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  4737. KMF_ENCODE_FORMAT crl_format;
  4738. BIO *bcrl = NULL;
  4739. X509_CRL *xcrl = NULL;
  4740. int i;
  4741. if (handle == NULL || crlname == NULL) {
  4742. return (KMF_ERR_BAD_PARAMETER);
  4743. }
  4744. ret = kmf_is_crl_file(handle, crlname, &crl_format);
  4745. if (ret != KMF_OK)
  4746. return (ret);
  4747. bcrl = BIO_new_file(crlname, "rb");
  4748. if (bcrl == NULL) {
  4749. SET_ERROR(kmfh, ERR_get_error());
  4750. ret = KMF_ERR_OPEN_FILE;
  4751. goto cleanup;
  4752. }
  4753. if (crl_format == KMF_FORMAT_ASN1)
  4754. xcrl = d2i_X509_CRL_bio(bcrl, NULL);
  4755. else if (crl_format == KMF_FORMAT_PEM)
  4756. xcrl = PEM_read_bio_X509_CRL(bcrl, NULL, NULL, NULL);
  4757. if (xcrl == NULL) {
  4758. SET_ERROR(kmfh, ERR_get_error());
  4759. ret = KMF_ERR_BAD_CRLFILE;
  4760. goto cleanup;
  4761. }
  4762. i = X509_cmp_time(X509_CRL_get_lastUpdate(xcrl), NULL);
  4763. if (i >= 0) {
  4764. ret = KMF_ERR_VALIDITY_PERIOD;
  4765. goto cleanup;
  4766. }
  4767. if (X509_CRL_get_nextUpdate(xcrl)) {
  4768. i = X509_cmp_time(X509_CRL_get_nextUpdate(xcrl), NULL);
  4769. if (i <= 0) {
  4770. ret = KMF_ERR_VALIDITY_PERIOD;
  4771. goto cleanup;
  4772. }
  4773. }
  4774. ret = KMF_OK;
  4775. cleanup:
  4776. if (bcrl != NULL)
  4777. (void) BIO_free(bcrl);
  4778. if (xcrl != NULL)
  4779. X509_CRL_free(xcrl);
  4780. return (ret);
  4781. }