PageRenderTime 66ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

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

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