PageRenderTime 74ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/a3217055/illumos-gate
C | 5579 lines | 4336 code | 798 blank | 445 comment | 1725 complexity | 87d81b820a778af910d92a235ab80a7a MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, LGPL-2.0, 0BSD, AGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * Use is subject to license terms.
  5. */
  6. /*
  7. * Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  8. * project 2000.
  9. */
  10. /*
  11. * ====================================================================
  12. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * licensing@OpenSSL.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include <stdlib.h>
  65. #include <kmfapiP.h>
  66. #include <ber_der.h>
  67. #include <fcntl.h>
  68. #include <sys/stat.h>
  69. #include <dirent.h>
  70. #include <cryptoutil.h>
  71. #include <synch.h>
  72. #include <thread.h>
  73. /* OPENSSL related headers */
  74. #include <openssl/bio.h>
  75. #include <openssl/bn.h>
  76. #include <openssl/asn1.h>
  77. #include <openssl/err.h>
  78. #include <openssl/bn.h>
  79. #include <openssl/x509.h>
  80. #include <openssl/rsa.h>
  81. #include <openssl/dsa.h>
  82. #include <openssl/x509v3.h>
  83. #include <openssl/objects.h>
  84. #include <openssl/pem.h>
  85. #include <openssl/pkcs12.h>
  86. #include <openssl/ocsp.h>
  87. #include <openssl/des.h>
  88. #include <openssl/rand.h>
  89. #define PRINT_ANY_EXTENSION (\
  90. KMF_X509_EXT_KEY_USAGE |\
  91. KMF_X509_EXT_CERT_POLICIES |\
  92. KMF_X509_EXT_SUBJALTNAME |\
  93. KMF_X509_EXT_BASIC_CONSTRAINTS |\
  94. KMF_X509_EXT_NAME_CONSTRAINTS |\
  95. KMF_X509_EXT_POLICY_CONSTRAINTS |\
  96. KMF_X509_EXT_EXT_KEY_USAGE |\
  97. KMF_X509_EXT_INHIBIT_ANY_POLICY |\
  98. KMF_X509_EXT_AUTH_KEY_ID |\
  99. KMF_X509_EXT_SUBJ_KEY_ID |\
  100. KMF_X509_EXT_POLICY_MAPPING)
  101. static uchar_t P[] = { 0x00, 0x8d, 0xf2, 0xa4, 0x94, 0x49, 0x22, 0x76,
  102. 0xaa, 0x3d, 0x25, 0x75, 0x9b, 0xb0, 0x68, 0x69,
  103. 0xcb, 0xea, 0xc0, 0xd8, 0x3a, 0xfb, 0x8d, 0x0c,
  104. 0xf7, 0xcb, 0xb8, 0x32, 0x4f, 0x0d, 0x78, 0x82,
  105. 0xe5, 0xd0, 0x76, 0x2f, 0xc5, 0xb7, 0x21, 0x0e,
  106. 0xaf, 0xc2, 0xe9, 0xad, 0xac, 0x32, 0xab, 0x7a,
  107. 0xac, 0x49, 0x69, 0x3d, 0xfb, 0xf8, 0x37, 0x24,
  108. 0xc2, 0xec, 0x07, 0x36, 0xee, 0x31, 0xc8, 0x02,
  109. 0x91 };
  110. static uchar_t Q[] = { 0x00, 0xc7, 0x73, 0x21, 0x8c, 0x73, 0x7e, 0xc8,
  111. 0xee, 0x99, 0x3b, 0x4f, 0x2d, 0xed, 0x30, 0xf4,
  112. 0x8e, 0xda, 0xce, 0x91, 0x5f };
  113. static uchar_t G[] = { 0x00, 0x62, 0x6d, 0x02, 0x78, 0x39, 0xea, 0x0a,
  114. 0x13, 0x41, 0x31, 0x63, 0xa5, 0x5b, 0x4c, 0xb5,
  115. 0x00, 0x29, 0x9d, 0x55, 0x22, 0x95, 0x6c, 0xef,
  116. 0xcb, 0x3b, 0xff, 0x10, 0xf3, 0x99, 0xce, 0x2c,
  117. 0x2e, 0x71, 0xcb, 0x9d, 0xe5, 0xfa, 0x24, 0xba,
  118. 0xbf, 0x58, 0xe5, 0xb7, 0x95, 0x21, 0x92, 0x5c,
  119. 0x9c, 0xc4, 0x2e, 0x9f, 0x6f, 0x46, 0x4b, 0x08,
  120. 0x8c, 0xc5, 0x72, 0xaf, 0x53, 0xe6, 0xd7, 0x88,
  121. 0x02 };
  122. #define SET_ERROR(h, c) h->lasterr.kstype = KMF_KEYSTORE_OPENSSL; \
  123. h->lasterr.errcode = c;
  124. #define SET_SYS_ERROR(h, c) h->lasterr.kstype = -1; h->lasterr.errcode = c;
  125. /*
  126. * Declare some new macros for managing stacks of EVP_PKEYS, similar to
  127. * what wanboot did.
  128. */
  129. DECLARE_STACK_OF(EVP_PKEY)
  130. #define sk_EVP_PKEY_new_null() SKM_sk_new_null(EVP_PKEY)
  131. #define sk_EVP_PKEY_free(st) SKM_sk_free(EVP_PKEY, (st))
  132. #define sk_EVP_PKEY_num(st) SKM_sk_num(EVP_PKEY, (st))
  133. #define sk_EVP_PKEY_value(st, i) SKM_sk_value(EVP_PKEY, (st), (i))
  134. #define sk_EVP_PKEY_push(st, val) SKM_sk_push(EVP_PKEY, (st), (val))
  135. #define sk_EVP_PKEY_pop_free(st, free_func) SKM_sk_pop_free(EVP_PKEY, (st), \
  136. (free_func))
  137. mutex_t init_lock = DEFAULTMUTEX;
  138. static int ssl_initialized = 0;
  139. static BIO *bio_err = NULL;
  140. static int
  141. test_for_file(char *, mode_t);
  142. static KMF_RETURN
  143. openssl_parse_bag(PKCS12_SAFEBAG *, char *, int,
  144. STACK_OF(EVP_PKEY) *, STACK_OF(X509) *);
  145. static KMF_RETURN
  146. local_export_pk12(KMF_HANDLE_T, KMF_CREDENTIAL *, int, KMF_X509_DER_CERT *,
  147. int, KMF_KEY_HANDLE *, char *);
  148. static KMF_RETURN set_pkey_attrib(EVP_PKEY *, ASN1_TYPE *, int);
  149. static KMF_RETURN
  150. extract_pem(KMF_HANDLE *, char *, char *, KMF_BIGINT *, char *,
  151. CK_UTF8CHAR *, CK_ULONG, EVP_PKEY **, KMF_DATA **, int *);
  152. static KMF_RETURN
  153. kmf_load_cert(KMF_HANDLE *, char *, char *, KMF_BIGINT *, KMF_CERT_VALIDITY,
  154. char *, KMF_DATA *);
  155. static KMF_RETURN
  156. load_certs(KMF_HANDLE *, char *, char *, KMF_BIGINT *, KMF_CERT_VALIDITY,
  157. char *, KMF_DATA **, uint32_t *);
  158. static KMF_RETURN
  159. sslBN2KMFBN(BIGNUM *, KMF_BIGINT *);
  160. static EVP_PKEY *
  161. ImportRawRSAKey(KMF_RAW_RSA_KEY *);
  162. static KMF_RETURN
  163. convertToRawKey(EVP_PKEY *, KMF_RAW_KEY_DATA *);
  164. KMF_RETURN
  165. OpenSSL_FindCert(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  166. void
  167. OpenSSL_FreeKMFCert(KMF_HANDLE_T, KMF_X509_DER_CERT *);
  168. KMF_RETURN
  169. OpenSSL_StoreCert(KMF_HANDLE_T handle, int, KMF_ATTRIBUTE *);
  170. KMF_RETURN
  171. OpenSSL_DeleteCert(KMF_HANDLE_T handle, int, KMF_ATTRIBUTE *);
  172. KMF_RETURN
  173. OpenSSL_CreateKeypair(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  174. KMF_RETURN
  175. OpenSSL_StoreKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  176. KMF_RETURN
  177. OpenSSL_EncodePubKeyData(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_DATA *);
  178. KMF_RETURN
  179. OpenSSL_SignData(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_OID *,
  180. KMF_DATA *, KMF_DATA *);
  181. KMF_RETURN
  182. OpenSSL_DeleteKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  183. KMF_RETURN
  184. OpenSSL_ImportCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  185. KMF_RETURN
  186. OpenSSL_DeleteCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  187. KMF_RETURN
  188. OpenSSL_ListCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  189. KMF_RETURN
  190. OpenSSL_FindCertInCRL(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  191. KMF_RETURN
  192. OpenSSL_CertGetPrintable(KMF_HANDLE_T, const KMF_DATA *,
  193. KMF_PRINTABLE_ITEM, char *);
  194. KMF_RETURN
  195. OpenSSL_GetErrorString(KMF_HANDLE_T, char **);
  196. KMF_RETURN
  197. OpenSSL_FindPrikeyByCert(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  198. KMF_RETURN
  199. OpenSSL_DecryptData(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_OID *,
  200. KMF_DATA *, KMF_DATA *);
  201. KMF_RETURN
  202. OpenSSL_CreateOCSPRequest(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  203. KMF_RETURN
  204. OpenSSL_GetOCSPStatusForCert(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  205. KMF_RETURN
  206. OpenSSL_FindKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  207. KMF_RETURN
  208. OpenSSL_ExportPK12(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  209. KMF_RETURN
  210. OpenSSL_CreateSymKey(KMF_HANDLE_T, int, KMF_ATTRIBUTE *);
  211. KMF_RETURN
  212. OpenSSL_GetSymKeyValue(KMF_HANDLE_T, KMF_KEY_HANDLE *, KMF_RAW_SYM_KEY *);
  213. KMF_RETURN
  214. OpenSSL_VerifyCRLFile(KMF_HANDLE_T, char *, KMF_DATA *);
  215. KMF_RETURN
  216. OpenSSL_CheckCRLDate(KMF_HANDLE_T, char *);
  217. static
  218. KMF_PLUGIN_FUNCLIST openssl_plugin_table =
  219. {
  220. 1, /* Version */
  221. NULL, /* ConfigureKeystore */
  222. OpenSSL_FindCert,
  223. OpenSSL_FreeKMFCert,
  224. OpenSSL_StoreCert,
  225. NULL, /* ImportCert */
  226. OpenSSL_ImportCRL,
  227. OpenSSL_DeleteCert,
  228. OpenSSL_DeleteCRL,
  229. OpenSSL_CreateKeypair,
  230. OpenSSL_FindKey,
  231. OpenSSL_EncodePubKeyData,
  232. OpenSSL_SignData,
  233. OpenSSL_DeleteKey,
  234. OpenSSL_ListCRL,
  235. NULL, /* FindCRL */
  236. OpenSSL_FindCertInCRL,
  237. OpenSSL_GetErrorString,
  238. OpenSSL_FindPrikeyByCert,
  239. OpenSSL_DecryptData,
  240. OpenSSL_ExportPK12,
  241. OpenSSL_CreateSymKey,
  242. OpenSSL_GetSymKeyValue,
  243. NULL, /* SetTokenPin */
  244. OpenSSL_StoreKey,
  245. NULL /* Finalize */
  246. };
  247. static mutex_t *lock_cs;
  248. static long *lock_count;
  249. static void
  250. /* ARGSUSED1 */
  251. locking_cb(int mode, int type, char *file, int line)
  252. {
  253. if (mode & CRYPTO_LOCK) {
  254. (void) mutex_lock(&(lock_cs[type]));
  255. lock_count[type]++;
  256. } else {
  257. (void) mutex_unlock(&(lock_cs[type]));
  258. }
  259. }
  260. static unsigned long
  261. thread_id()
  262. {
  263. return ((unsigned long)thr_self());
  264. }
  265. KMF_PLUGIN_FUNCLIST *
  266. KMF_Plugin_Initialize()
  267. {
  268. int i;
  269. (void) mutex_lock(&init_lock);
  270. if (!ssl_initialized) {
  271. /*
  272. * Add support for extension OIDs that are not yet in the
  273. * openssl default set.
  274. */
  275. (void) OBJ_create("2.5.29.30", "nameConstraints",
  276. "X509v3 Name Constraints");
  277. (void) OBJ_create("2.5.29.33", "policyMappings",
  278. "X509v3 Policy Mappings");
  279. (void) OBJ_create("2.5.29.36", "policyConstraints",
  280. "X509v3 Policy Constraints");
  281. (void) OBJ_create("2.5.29.46", "freshestCRL",
  282. "X509v3 Freshest CRL");
  283. (void) OBJ_create("2.5.29.54", "inhibitAnyPolicy",
  284. "X509v3 Inhibit Any-Policy");
  285. /*
  286. * Set up for thread-safe operation.
  287. */
  288. lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof (mutex_t));
  289. if (lock_cs == NULL) {
  290. (void) mutex_unlock(&init_lock);
  291. return (NULL);
  292. }
  293. lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof (long));
  294. if (lock_count == NULL) {
  295. OPENSSL_free(lock_cs);
  296. (void) mutex_unlock(&init_lock);
  297. return (NULL);
  298. }
  299. for (i = 0; i < CRYPTO_num_locks(); i++) {
  300. lock_count[i] = 0;
  301. (void) mutex_init(&lock_cs[i], USYNC_THREAD, NULL);
  302. }
  303. CRYPTO_set_id_callback((unsigned long (*)())thread_id);
  304. if (CRYPTO_get_locking_callback() == NULL)
  305. CRYPTO_set_locking_callback((void (*)())locking_cb);
  306. OpenSSL_add_all_algorithms();
  307. /* Enable error strings for reporting */
  308. ERR_load_crypto_strings();
  309. ssl_initialized = 1;
  310. }
  311. (void) mutex_unlock(&init_lock);
  312. return (&openssl_plugin_table);
  313. }
  314. /*
  315. * Convert an SSL DN to a KMF DN.
  316. */
  317. static KMF_RETURN
  318. get_x509_dn(X509_NAME *sslDN, KMF_X509_NAME *kmfDN)
  319. {
  320. KMF_DATA derdata;
  321. KMF_RETURN rv = KMF_OK;
  322. uchar_t *tmp;
  323. /* Convert to raw DER format */
  324. derdata.Length = i2d_X509_NAME(sslDN, NULL);
  325. if ((tmp = derdata.Data = (uchar_t *)OPENSSL_malloc(derdata.Length))
  326. == NULL) {
  327. return (KMF_ERR_MEMORY);
  328. }
  329. (void) i2d_X509_NAME(sslDN, &tmp);
  330. /* Decode to KMF format */
  331. rv = DerDecodeName(&derdata, kmfDN);
  332. if (rv != KMF_OK) {
  333. rv = KMF_ERR_BAD_CERT_FORMAT;
  334. }
  335. OPENSSL_free(derdata.Data);
  336. return (rv);
  337. }
  338. int
  339. isdir(char *path)
  340. {
  341. struct stat s;
  342. if (stat(path, &s) == -1)
  343. return (0);
  344. return ((s.st_mode & S_IFMT) == S_IFDIR);
  345. }
  346. static KMF_RETURN
  347. ssl_cert2KMFDATA(KMF_HANDLE *kmfh, X509 *x509cert, KMF_DATA *cert)
  348. {
  349. KMF_RETURN rv = KMF_OK;
  350. unsigned char *buf = NULL, *p;
  351. int len;
  352. /*
  353. * Convert the X509 internal struct to DER encoded data
  354. */
  355. if ((len = i2d_X509(x509cert, NULL)) < 0) {
  356. SET_ERROR(kmfh, ERR_get_error());
  357. rv = KMF_ERR_BAD_CERT_FORMAT;
  358. goto cleanup;
  359. }
  360. if ((buf = malloc(len)) == NULL) {
  361. SET_SYS_ERROR(kmfh, errno);
  362. rv = KMF_ERR_MEMORY;
  363. goto cleanup;
  364. }
  365. /*
  366. * i2d_X509 will increment the buf pointer so that we need to
  367. * save it.
  368. */
  369. p = buf;
  370. if ((len = i2d_X509(x509cert, &p)) < 0) {
  371. SET_ERROR(kmfh, ERR_get_error());
  372. free(buf);
  373. rv = KMF_ERR_BAD_CERT_FORMAT;
  374. goto cleanup;
  375. }
  376. /* caller's responsibility to free it */
  377. cert->Data = buf;
  378. cert->Length = len;
  379. cleanup:
  380. if (rv != KMF_OK) {
  381. if (buf)
  382. free(buf);
  383. cert->Data = NULL;
  384. cert->Length = 0;
  385. }
  386. return (rv);
  387. }
  388. static KMF_RETURN
  389. check_cert(X509 *xcert, char *issuer, char *subject, KMF_BIGINT *serial,
  390. boolean_t *match)
  391. {
  392. KMF_RETURN rv = KMF_OK;
  393. boolean_t findIssuer = FALSE;
  394. boolean_t findSubject = FALSE;
  395. boolean_t findSerial = FALSE;
  396. KMF_X509_NAME issuerDN, subjectDN;
  397. KMF_X509_NAME certIssuerDN, certSubjectDN;
  398. *match = FALSE;
  399. if (xcert == NULL) {
  400. return (KMF_ERR_BAD_PARAMETER);
  401. }
  402. (void) memset(&issuerDN, 0, sizeof (KMF_X509_NAME));
  403. (void) memset(&subjectDN, 0, sizeof (KMF_X509_NAME));
  404. (void) memset(&certIssuerDN, 0, sizeof (KMF_X509_NAME));
  405. (void) memset(&certSubjectDN, 0, sizeof (KMF_X509_NAME));
  406. if (issuer != NULL && strlen(issuer)) {
  407. rv = kmf_dn_parser(issuer, &issuerDN);
  408. if (rv != KMF_OK)
  409. return (KMF_ERR_BAD_PARAMETER);
  410. rv = get_x509_dn(xcert->cert_info->issuer, &certIssuerDN);
  411. if (rv != KMF_OK) {
  412. kmf_free_dn(&issuerDN);
  413. return (KMF_ERR_BAD_PARAMETER);
  414. }
  415. findIssuer = TRUE;
  416. }
  417. if (subject != NULL && strlen(subject)) {
  418. rv = kmf_dn_parser(subject, &subjectDN);
  419. if (rv != KMF_OK) {
  420. rv = KMF_ERR_BAD_PARAMETER;
  421. goto cleanup;
  422. }
  423. rv = get_x509_dn(xcert->cert_info->subject, &certSubjectDN);
  424. if (rv != KMF_OK) {
  425. rv = KMF_ERR_BAD_PARAMETER;
  426. goto cleanup;
  427. }
  428. findSubject = TRUE;
  429. }
  430. if (serial != NULL && serial->val != NULL)
  431. findSerial = TRUE;
  432. if (findSerial) {
  433. BIGNUM *bn;
  434. /* Comparing BIGNUMs is a pain! */
  435. bn = ASN1_INTEGER_to_BN(xcert->cert_info->serialNumber, NULL);
  436. if (bn != NULL) {
  437. int bnlen = BN_num_bytes(bn);
  438. if (bnlen == serial->len) {
  439. uchar_t *a = malloc(bnlen);
  440. if (a == NULL) {
  441. rv = KMF_ERR_MEMORY;
  442. BN_free(bn);
  443. goto cleanup;
  444. }
  445. bnlen = BN_bn2bin(bn, a);
  446. *match = (memcmp(a, serial->val, serial->len) ==
  447. 0);
  448. rv = KMF_OK;
  449. free(a);
  450. }
  451. BN_free(bn);
  452. if (!(*match))
  453. goto cleanup;
  454. } else {
  455. rv = KMF_OK;
  456. goto cleanup;
  457. }
  458. }
  459. if (findIssuer) {
  460. *match = (kmf_compare_rdns(&issuerDN, &certIssuerDN) == 0);
  461. if ((*match) == B_FALSE) {
  462. /* stop checking and bail */
  463. rv = KMF_OK;
  464. goto cleanup;
  465. }
  466. }
  467. if (findSubject) {
  468. *match = (kmf_compare_rdns(&subjectDN, &certSubjectDN) == 0);
  469. if ((*match) == B_FALSE) {
  470. /* stop checking and bail */
  471. rv = KMF_OK;
  472. goto cleanup;
  473. }
  474. }
  475. *match = TRUE;
  476. cleanup:
  477. if (findIssuer) {
  478. kmf_free_dn(&issuerDN);
  479. kmf_free_dn(&certIssuerDN);
  480. }
  481. if (findSubject) {
  482. kmf_free_dn(&subjectDN);
  483. kmf_free_dn(&certSubjectDN);
  484. }
  485. return (rv);
  486. }
  487. /*
  488. * This function loads a certificate file into an X509 data structure, and
  489. * checks if its issuer, subject or the serial number matches with those
  490. * values. If it matches, then return the X509 data structure.
  491. */
  492. static KMF_RETURN
  493. load_X509cert(KMF_HANDLE *kmfh,
  494. char *issuer, char *subject, KMF_BIGINT *serial,
  495. char *pathname, X509 **outcert)
  496. {
  497. KMF_RETURN rv = KMF_OK;
  498. X509 *xcert = NULL;
  499. BIO *bcert = NULL;
  500. boolean_t match = FALSE;
  501. KMF_ENCODE_FORMAT format;
  502. /*
  503. * auto-detect the file format, regardless of what
  504. * the 'format' parameters in the params say.
  505. */
  506. rv = kmf_get_file_format(pathname, &format);
  507. if (rv != KMF_OK) {
  508. if (rv == KMF_ERR_OPEN_FILE)
  509. rv = KMF_ERR_CERT_NOT_FOUND;
  510. return (rv);
  511. }
  512. /* Not ASN1(DER) format */
  513. if ((bcert = BIO_new_file(pathname, "rb")) == NULL) {
  514. SET_ERROR(kmfh, ERR_get_error());
  515. rv = KMF_ERR_OPEN_FILE;
  516. goto cleanup;
  517. }
  518. if (format == KMF_FORMAT_PEM)
  519. xcert = PEM_read_bio_X509_AUX(bcert, NULL, NULL, NULL);
  520. else if (format == KMF_FORMAT_ASN1)
  521. xcert = d2i_X509_bio(bcert, NULL);
  522. else if (format == KMF_FORMAT_PKCS12) {
  523. PKCS12 *p12 = d2i_PKCS12_bio(bcert, NULL);
  524. if (p12 != NULL) {
  525. (void) PKCS12_parse(p12, NULL, NULL, &xcert, NULL);
  526. PKCS12_free(p12);
  527. p12 = NULL;
  528. } else {
  529. SET_ERROR(kmfh, ERR_get_error());
  530. rv = KMF_ERR_BAD_CERT_FORMAT;
  531. }
  532. } else {
  533. rv = KMF_ERR_BAD_PARAMETER;
  534. goto cleanup;
  535. }
  536. if (xcert == NULL) {
  537. SET_ERROR(kmfh, ERR_get_error());
  538. rv = KMF_ERR_BAD_CERT_FORMAT;
  539. goto cleanup;
  540. }
  541. if (check_cert(xcert, issuer, subject, serial, &match) != KMF_OK ||
  542. match == FALSE) {
  543. rv = KMF_ERR_CERT_NOT_FOUND;
  544. goto cleanup;
  545. }
  546. if (outcert != NULL) {
  547. *outcert = xcert;
  548. }
  549. cleanup:
  550. if (bcert != NULL) (void) BIO_free(bcert);
  551. if (rv != KMF_OK && xcert != NULL)
  552. X509_free(xcert);
  553. return (rv);
  554. }
  555. static int
  556. datacmp(const void *a, const void *b)
  557. {
  558. KMF_DATA *adata = (KMF_DATA *)a;
  559. KMF_DATA *bdata = (KMF_DATA *)b;
  560. if (adata->Length > bdata->Length)
  561. return (-1);
  562. if (adata->Length < bdata->Length)
  563. return (1);
  564. return (0);
  565. }
  566. static KMF_RETURN
  567. load_certs(KMF_HANDLE *kmfh, char *issuer, char *subject, KMF_BIGINT *serial,
  568. KMF_CERT_VALIDITY validity, char *pathname,
  569. KMF_DATA **certlist, uint32_t *numcerts)
  570. {
  571. KMF_RETURN rv = KMF_OK;
  572. int i;
  573. KMF_DATA *certs = NULL;
  574. int nc = 0;
  575. int hits = 0;
  576. KMF_ENCODE_FORMAT format;
  577. rv = kmf_get_file_format(pathname, &format);
  578. if (rv != KMF_OK) {
  579. if (rv == KMF_ERR_OPEN_FILE)
  580. rv = KMF_ERR_CERT_NOT_FOUND;
  581. return (rv);
  582. }
  583. if (format == KMF_FORMAT_ASN1) {
  584. /* load a single certificate */
  585. certs = (KMF_DATA *)malloc(sizeof (KMF_DATA));
  586. if (certs == NULL)
  587. return (KMF_ERR_MEMORY);
  588. certs->Data = NULL;
  589. certs->Length = 0;
  590. rv = kmf_load_cert(kmfh, issuer, subject, serial, validity,
  591. pathname, certs);
  592. if (rv == KMF_OK) {
  593. *certlist = certs;
  594. *numcerts = 1;
  595. } else {
  596. kmf_free_data(certs);
  597. free(certs);
  598. certs = NULL;
  599. }
  600. return (rv);
  601. } else if (format == KMF_FORMAT_PKCS12) {
  602. /* We need a credential to access a PKCS#12 file */
  603. rv = KMF_ERR_BAD_CERT_FORMAT;
  604. } else if (format == KMF_FORMAT_PEM ||
  605. format != KMF_FORMAT_PEM_KEYPAIR) {
  606. /* This function only works on PEM files */
  607. rv = extract_pem(kmfh, issuer, subject, serial, pathname,
  608. (uchar_t *)NULL, 0, NULL, &certs, &nc);
  609. } else {
  610. return (KMF_ERR_ENCODING);
  611. }
  612. if (rv != KMF_OK)
  613. return (rv);
  614. for (i = 0; i < nc; i++) {
  615. if (validity == KMF_NONEXPIRED_CERTS) {
  616. rv = kmf_check_cert_date(kmfh, &certs[i]);
  617. } else if (validity == KMF_EXPIRED_CERTS) {
  618. rv = kmf_check_cert_date(kmfh, &certs[i]);
  619. if (rv == KMF_OK)
  620. rv = KMF_ERR_CERT_NOT_FOUND;
  621. if (rv == KMF_ERR_VALIDITY_PERIOD)
  622. rv = KMF_OK;
  623. }
  624. if (rv != KMF_OK) {
  625. /* Remove this cert from the list by clearing it. */
  626. kmf_free_data(&certs[i]);
  627. } else {
  628. hits++; /* count valid certs found */
  629. }
  630. rv = KMF_OK;
  631. }
  632. if (rv == KMF_OK && hits > 0) {
  633. /*
  634. * Sort the list of certs by length to put the cleared ones
  635. * at the end so they don't get accessed by the caller.
  636. */
  637. qsort((void *)certs, nc, sizeof (KMF_DATA), datacmp);
  638. *certlist = certs;
  639. /* since we sorted the list, just return the number of hits */
  640. *numcerts = hits;
  641. } else {
  642. if (rv == KMF_OK && hits == 0)
  643. rv = KMF_ERR_CERT_NOT_FOUND;
  644. if (certs != NULL) {
  645. free(certs);
  646. certs = NULL;
  647. }
  648. }
  649. return (rv);
  650. }
  651. static KMF_RETURN
  652. kmf_load_cert(KMF_HANDLE *kmfh,
  653. char *issuer, char *subject, KMF_BIGINT *serial,
  654. KMF_CERT_VALIDITY validity,
  655. char *pathname,
  656. KMF_DATA *cert)
  657. {
  658. KMF_RETURN rv = KMF_OK;
  659. X509 *x509cert = NULL;
  660. rv = load_X509cert(kmfh, issuer, subject, serial, pathname, &x509cert);
  661. if (rv == KMF_OK && x509cert != NULL && cert != NULL) {
  662. rv = ssl_cert2KMFDATA(kmfh, x509cert, cert);
  663. if (rv != KMF_OK) {
  664. goto cleanup;
  665. }
  666. if (validity == KMF_NONEXPIRED_CERTS) {
  667. rv = kmf_check_cert_date(kmfh, cert);
  668. } else if (validity == KMF_EXPIRED_CERTS) {
  669. rv = kmf_check_cert_date(kmfh, cert);
  670. if (rv == KMF_OK) {
  671. /*
  672. * This is a valid cert so skip it.
  673. */
  674. rv = KMF_ERR_CERT_NOT_FOUND;
  675. }
  676. if (rv == KMF_ERR_VALIDITY_PERIOD) {
  677. /*
  678. * We want to return success when we
  679. * find an invalid cert.
  680. */
  681. rv = KMF_OK;
  682. goto cleanup;
  683. }
  684. }
  685. }
  686. cleanup:
  687. if (x509cert != NULL)
  688. X509_free(x509cert);
  689. return (rv);
  690. }
  691. static KMF_RETURN
  692. readAltFormatPrivateKey(KMF_DATA *filedata, EVP_PKEY **pkey)
  693. {
  694. KMF_RETURN ret = KMF_OK;
  695. KMF_RAW_RSA_KEY rsa;
  696. BerElement *asn1 = NULL;
  697. BerValue filebuf;
  698. BerValue OID = { NULL, 0 };
  699. BerValue *Mod = NULL, *PubExp = NULL;
  700. BerValue *PriExp = NULL, *Prime1 = NULL, *Prime2 = NULL;
  701. BerValue *Coef = NULL;
  702. BIGNUM *D = NULL, *P = NULL, *Q = NULL, *COEF = NULL;
  703. BIGNUM *Exp1 = NULL, *Exp2 = NULL, *pminus1 = NULL;
  704. BIGNUM *qminus1 = NULL;
  705. BN_CTX *ctx = NULL;
  706. *pkey = NULL;
  707. filebuf.bv_val = (char *)filedata->Data;
  708. filebuf.bv_len = filedata->Length;
  709. asn1 = kmfder_init(&filebuf);
  710. if (asn1 == NULL) {
  711. ret = KMF_ERR_MEMORY;
  712. goto out;
  713. }
  714. if (kmfber_scanf(asn1, "{{Dn{IIIIII}}}",
  715. &OID, &Mod, &PubExp, &PriExp, &Prime1,
  716. &Prime2, &Coef) == -1) {
  717. ret = KMF_ERR_ENCODING;
  718. goto out;
  719. }
  720. /*
  721. * We have to derive the 2 Exponents using Bignumber math.
  722. * Exp1 = PriExp mod (Prime1 - 1)
  723. * Exp2 = PriExp mod (Prime2 - 1)
  724. */
  725. /* D = PrivateExponent */
  726. D = BN_bin2bn((const uchar_t *)PriExp->bv_val, PriExp->bv_len, D);
  727. if (D == NULL) {
  728. ret = KMF_ERR_MEMORY;
  729. goto out;
  730. }
  731. /* P = Prime1 (first prime factor of Modulus) */
  732. P = BN_bin2bn((const uchar_t *)Prime1->bv_val, Prime1->bv_len, P);
  733. if (D == NULL) {
  734. ret = KMF_ERR_MEMORY;
  735. goto out;
  736. }
  737. /* Q = Prime2 (second prime factor of Modulus) */
  738. Q = BN_bin2bn((const uchar_t *)Prime2->bv_val, Prime2->bv_len, Q);
  739. if ((ctx = BN_CTX_new()) == NULL) {
  740. ret = KMF_ERR_MEMORY;
  741. goto out;
  742. }
  743. /* Compute (P - 1) */
  744. pminus1 = BN_new();
  745. (void) BN_sub(pminus1, P, BN_value_one());
  746. /* Exponent1 = D mod (P - 1) */
  747. Exp1 = BN_new();
  748. (void) BN_mod(Exp1, D, pminus1, ctx);
  749. /* Compute (Q - 1) */
  750. qminus1 = BN_new();
  751. (void) BN_sub(qminus1, Q, BN_value_one());
  752. /* Exponent2 = D mod (Q - 1) */
  753. Exp2 = BN_new();
  754. (void) BN_mod(Exp2, D, qminus1, ctx);
  755. /* Coef = (Inverse Q) mod P */
  756. COEF = BN_new();
  757. (void) BN_mod_inverse(COEF, Q, P, ctx);
  758. /* Convert back to KMF format */
  759. (void) memset(&rsa, 0, sizeof (rsa));
  760. if ((ret = sslBN2KMFBN(Exp1, &rsa.exp1)) != KMF_OK)
  761. goto out;
  762. if ((ret = sslBN2KMFBN(Exp2, &rsa.exp2)) != KMF_OK)
  763. goto out;
  764. if ((ret = sslBN2KMFBN(COEF, &rsa.coef)) != KMF_OK)
  765. goto out;
  766. rsa.mod.val = (uchar_t *)Mod->bv_val;
  767. rsa.mod.len = Mod->bv_len;
  768. rsa.pubexp.val = (uchar_t *)PubExp->bv_val;
  769. rsa.pubexp.len = PubExp->bv_len;
  770. rsa.priexp.val = (uchar_t *)PriExp->bv_val;
  771. rsa.priexp.len = PriExp->bv_len;
  772. rsa.prime1.val = (uchar_t *)Prime1->bv_val;
  773. rsa.prime1.len = Prime1->bv_len;
  774. rsa.prime2.val = (uchar_t *)Prime2->bv_val;
  775. rsa.prime2.len = Prime2->bv_len;
  776. *pkey = ImportRawRSAKey(&rsa);
  777. out:
  778. if (asn1 != NULL)
  779. kmfber_free(asn1, 1);
  780. if (OID.bv_val) {
  781. free(OID.bv_val);
  782. }
  783. if (PriExp)
  784. free(PriExp);
  785. if (Mod)
  786. free(Mod);
  787. if (PubExp)
  788. free(PubExp);
  789. if (Coef) {
  790. (void) memset(Coef->bv_val, 0, Coef->bv_len);
  791. free(Coef->bv_val);
  792. free(Coef);
  793. }
  794. if (Prime1)
  795. free(Prime1);
  796. if (Prime2)
  797. free(Prime2);
  798. if (ctx != NULL)
  799. BN_CTX_free(ctx);
  800. if (D)
  801. BN_clear_free(D);
  802. if (P)
  803. BN_clear_free(P);
  804. if (Q)
  805. BN_clear_free(Q);
  806. if (pminus1)
  807. BN_clear_free(pminus1);
  808. if (qminus1)
  809. BN_clear_free(qminus1);
  810. if (Exp1)
  811. BN_clear_free(Exp1);
  812. if (Exp2)
  813. BN_clear_free(Exp2);
  814. return (ret);
  815. }
  816. static EVP_PKEY *
  817. openssl_load_key(KMF_HANDLE_T handle, const char *file)
  818. {
  819. BIO *keyfile = NULL;
  820. EVP_PKEY *pkey = NULL;
  821. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  822. KMF_ENCODE_FORMAT format;
  823. KMF_RETURN rv;
  824. KMF_DATA filedata;
  825. if (file == NULL) {
  826. return (NULL);
  827. }
  828. if (kmf_get_file_format((char *)file, &format) != KMF_OK)
  829. return (NULL);
  830. keyfile = BIO_new_file(file, "rb");
  831. if (keyfile == NULL) {
  832. goto end;
  833. }
  834. if (format == KMF_FORMAT_ASN1) {
  835. pkey = d2i_PrivateKey_bio(keyfile, NULL);
  836. if (pkey == NULL) {
  837. (void) BIO_free(keyfile);
  838. keyfile = NULL;
  839. /* Try odd ASN.1 variations */
  840. rv = kmf_read_input_file(kmfh, (char *)file,
  841. &filedata);
  842. if (rv == KMF_OK) {
  843. (void) readAltFormatPrivateKey(&filedata,
  844. &pkey);
  845. kmf_free_data(&filedata);
  846. }
  847. }
  848. } else if (format == KMF_FORMAT_PEM ||
  849. format == KMF_FORMAT_PEM_KEYPAIR) {
  850. pkey = PEM_read_bio_PrivateKey(keyfile, NULL, NULL, NULL);
  851. if (pkey == NULL) {
  852. KMF_DATA derdata;
  853. /*
  854. * Check if this is the alt. format
  855. * RSA private key file.
  856. */
  857. rv = kmf_read_input_file(kmfh, (char *)file,
  858. &filedata);
  859. if (rv == KMF_OK) {
  860. uchar_t *d = NULL;
  861. int len;
  862. rv = kmf_pem_to_der(filedata.Data,
  863. filedata.Length, &d, &len);
  864. if (rv == KMF_OK && d != NULL) {
  865. derdata.Data = d;
  866. derdata.Length = (size_t)len;
  867. (void) readAltFormatPrivateKey(
  868. &derdata, &pkey);
  869. free(d);
  870. }
  871. kmf_free_data(&filedata);
  872. }
  873. }
  874. }
  875. end:
  876. if (pkey == NULL)
  877. SET_ERROR(kmfh, ERR_get_error());
  878. if (keyfile != NULL)
  879. (void) BIO_free(keyfile);
  880. return (pkey);
  881. }
  882. KMF_RETURN
  883. OpenSSL_FindCert(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  884. {
  885. KMF_RETURN rv = KMF_OK;
  886. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  887. int i, n;
  888. uint32_t maxcerts = 0;
  889. uint32_t *num_certs;
  890. KMF_X509_DER_CERT *kmf_cert = NULL;
  891. char *dirpath = NULL;
  892. char *filename = NULL;
  893. char *fullpath = NULL;
  894. char *issuer = NULL;
  895. char *subject = NULL;
  896. KMF_BIGINT *serial = NULL;
  897. KMF_CERT_VALIDITY validity;
  898. num_certs = kmf_get_attr_ptr(KMF_COUNT_ATTR, attrlist, numattr);
  899. if (num_certs == NULL)
  900. return (KMF_ERR_BAD_PARAMETER);
  901. /* num_certs should reference the size of kmf_cert */
  902. maxcerts = *num_certs;
  903. if (maxcerts == 0)
  904. maxcerts = 0xFFFFFFFF;
  905. *num_certs = 0;
  906. /* Get the optional returned certificate list */
  907. kmf_cert = kmf_get_attr_ptr(KMF_X509_DER_CERT_ATTR, attrlist,
  908. numattr);
  909. /*
  910. * The dirpath attribute and the filename attribute can not be NULL
  911. * at the same time.
  912. */
  913. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  914. filename = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist,
  915. numattr);
  916. fullpath = get_fullpath(dirpath, filename);
  917. if (fullpath == NULL)
  918. return (KMF_ERR_BAD_PARAMETER);
  919. /* Get optional search criteria attributes */
  920. issuer = kmf_get_attr_ptr(KMF_ISSUER_NAME_ATTR, attrlist, numattr);
  921. subject = kmf_get_attr_ptr(KMF_SUBJECT_NAME_ATTR, attrlist, numattr);
  922. serial = kmf_get_attr_ptr(KMF_BIGINT_ATTR, attrlist, numattr);
  923. rv = kmf_get_attr(KMF_CERT_VALIDITY_ATTR, attrlist, numattr,
  924. &validity, NULL);
  925. if (rv != KMF_OK) {
  926. validity = KMF_ALL_CERTS;
  927. rv = KMF_OK;
  928. }
  929. if (isdir(fullpath)) {
  930. DIR *dirp;
  931. struct dirent *dp;
  932. n = 0;
  933. /* open all files in the directory and attempt to read them */
  934. if ((dirp = opendir(fullpath)) == NULL) {
  935. return (KMF_ERR_BAD_PARAMETER);
  936. }
  937. while ((dp = readdir(dirp)) != NULL) {
  938. char *fname;
  939. KMF_DATA *certlist = NULL;
  940. uint32_t loaded_certs = 0;
  941. if (strcmp(dp->d_name, ".") == 0 ||
  942. strcmp(dp->d_name, "..") == 0)
  943. continue;
  944. fname = get_fullpath(fullpath, (char *)&dp->d_name);
  945. rv = load_certs(kmfh, issuer, subject, serial,
  946. validity, fname, &certlist, &loaded_certs);
  947. if (rv != KMF_OK) {
  948. free(fname);
  949. if (certlist != NULL) {
  950. for (i = 0; i < loaded_certs; i++)
  951. kmf_free_data(&certlist[i]);
  952. free(certlist);
  953. }
  954. continue;
  955. }
  956. /* If load succeeds, add certdata to the list */
  957. if (kmf_cert != NULL) {
  958. for (i = 0; i < loaded_certs &&
  959. n < maxcerts; i++) {
  960. kmf_cert[n].certificate.Data =
  961. certlist[i].Data;
  962. kmf_cert[n].certificate.Length =
  963. certlist[i].Length;
  964. kmf_cert[n].kmf_private.keystore_type =
  965. KMF_KEYSTORE_OPENSSL;
  966. kmf_cert[n].kmf_private.flags =
  967. KMF_FLAG_CERT_VALID;
  968. kmf_cert[n].kmf_private.label =
  969. strdup(fname);
  970. n++;
  971. }
  972. /*
  973. * If maxcerts < loaded_certs, clean up the
  974. * certs that were not used.
  975. */
  976. for (; i < loaded_certs; i++)
  977. kmf_free_data(&certlist[i]);
  978. } else {
  979. for (i = 0; i < loaded_certs; i++)
  980. kmf_free_data(&certlist[i]);
  981. n += loaded_certs;
  982. }
  983. free(certlist);
  984. free(fname);
  985. }
  986. (*num_certs) = n;
  987. if (*num_certs == 0)
  988. rv = KMF_ERR_CERT_NOT_FOUND;
  989. if (*num_certs > 0)
  990. rv = KMF_OK;
  991. exit:
  992. (void) closedir(dirp);
  993. } else {
  994. KMF_DATA *certlist = NULL;
  995. uint32_t loaded_certs = 0;
  996. rv = load_certs(kmfh, issuer, subject, serial, validity,
  997. fullpath, &certlist, &loaded_certs);
  998. if (rv != KMF_OK) {
  999. free(fullpath);
  1000. return (rv);
  1001. }
  1002. n = 0;
  1003. if (kmf_cert != NULL && certlist != NULL) {
  1004. for (i = 0; i < loaded_certs && i < maxcerts; i++) {
  1005. kmf_cert[n].certificate.Data =
  1006. certlist[i].Data;
  1007. kmf_cert[n].certificate.Length =
  1008. certlist[i].Length;
  1009. kmf_cert[n].kmf_private.keystore_type =
  1010. KMF_KEYSTORE_OPENSSL;
  1011. kmf_cert[n].kmf_private.flags =
  1012. KMF_FLAG_CERT_VALID;
  1013. kmf_cert[n].kmf_private.label =
  1014. strdup(fullpath);
  1015. n++;
  1016. }
  1017. /* If maxcerts < loaded_certs, clean up */
  1018. for (; i < loaded_certs; i++)
  1019. kmf_free_data(&certlist[i]);
  1020. } else if (certlist != NULL) {
  1021. for (i = 0; i < loaded_certs; i++)
  1022. kmf_free_data(&certlist[i]);
  1023. n = loaded_certs;
  1024. }
  1025. if (certlist != NULL)
  1026. free(certlist);
  1027. *num_certs = n;
  1028. }
  1029. free(fullpath);
  1030. return (rv);
  1031. }
  1032. void
  1033. /*ARGSUSED*/
  1034. OpenSSL_FreeKMFCert(KMF_HANDLE_T handle,
  1035. KMF_X509_DER_CERT *kmf_cert)
  1036. {
  1037. if (kmf_cert != NULL) {
  1038. if (kmf_cert->certificate.Data != NULL) {
  1039. kmf_free_data(&kmf_cert->certificate);
  1040. }
  1041. if (kmf_cert->kmf_private.label)
  1042. free(kmf_cert->kmf_private.label);
  1043. }
  1044. }
  1045. /*ARGSUSED*/
  1046. KMF_RETURN
  1047. OpenSSL_StoreCert(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  1048. {
  1049. KMF_RETURN ret = KMF_OK;
  1050. KMF_DATA *cert = NULL;
  1051. char *outfilename = NULL;
  1052. char *dirpath = NULL;
  1053. char *fullpath = NULL;
  1054. KMF_ENCODE_FORMAT format;
  1055. /* Get the cert data */
  1056. cert = kmf_get_attr_ptr(KMF_CERT_DATA_ATTR, attrlist, numattr);
  1057. if (cert == NULL || cert->Data == NULL)
  1058. return (KMF_ERR_BAD_PARAMETER);
  1059. /* Check the output filename and directory attributes. */
  1060. outfilename = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist,
  1061. numattr);
  1062. if (outfilename == NULL)
  1063. return (KMF_ERR_BAD_PARAMETER);
  1064. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1065. fullpath = get_fullpath(dirpath, outfilename);
  1066. if (fullpath == NULL)
  1067. return (KMF_ERR_BAD_CERTFILE);
  1068. /* Check the optional format attribute */
  1069. ret = kmf_get_attr(KMF_ENCODE_FORMAT_ATTR, attrlist, numattr,
  1070. &format, NULL);
  1071. if (ret != KMF_OK) {
  1072. /* If there is no format attribute, then default to PEM */
  1073. format = KMF_FORMAT_PEM;
  1074. ret = KMF_OK;
  1075. } else if (format != KMF_FORMAT_ASN1 && format != KMF_FORMAT_PEM) {
  1076. ret = KMF_ERR_BAD_CERT_FORMAT;
  1077. goto out;
  1078. }
  1079. /* Store the certificate in the file with the specified format */
  1080. ret = kmf_create_cert_file(cert, format, fullpath);
  1081. out:
  1082. if (fullpath != NULL)
  1083. free(fullpath);
  1084. return (ret);
  1085. }
  1086. KMF_RETURN
  1087. OpenSSL_DeleteCert(KMF_HANDLE_T handle, int numattr, KMF_ATTRIBUTE *attrlist)
  1088. {
  1089. KMF_RETURN rv;
  1090. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1091. KMF_DATA certdata = {NULL, 0};
  1092. char *dirpath = NULL;
  1093. char *filename = NULL;
  1094. char *fullpath = NULL;
  1095. char *issuer = NULL;
  1096. char *subject = NULL;
  1097. KMF_BIGINT *serial = NULL;
  1098. KMF_CERT_VALIDITY validity;
  1099. /*
  1100. * Get the DIRPATH and CERT_FILENAME attributes. They can not be
  1101. * NULL at the same time.
  1102. */
  1103. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1104. filename = kmf_get_attr_ptr(KMF_CERT_FILENAME_ATTR, attrlist,
  1105. numattr);
  1106. fullpath = get_fullpath(dirpath, filename);
  1107. if (fullpath == NULL)
  1108. return (KMF_ERR_BAD_PARAMETER);
  1109. /* Get optional search criteria attributes */
  1110. issuer = kmf_get_attr_ptr(KMF_ISSUER_NAME_ATTR, attrlist, numattr);
  1111. subject = kmf_get_attr_ptr(KMF_SUBJECT_NAME_ATTR, attrlist, numattr);
  1112. serial = kmf_get_attr_ptr(KMF_BIGINT_ATTR, attrlist, numattr);
  1113. rv = kmf_get_attr(KMF_CERT_VALIDITY_ATTR, attrlist, numattr,
  1114. &validity, NULL);
  1115. if (rv != KMF_OK) {
  1116. validity = KMF_ALL_CERTS;
  1117. rv = KMF_OK;
  1118. }
  1119. if (isdir(fullpath)) {
  1120. DIR *dirp;
  1121. struct dirent *dp;
  1122. /* open all files in the directory and attempt to read them */
  1123. if ((dirp = opendir(fullpath)) == NULL) {
  1124. return (KMF_ERR_BAD_PARAMETER);
  1125. }
  1126. while ((dp = readdir(dirp)) != NULL) {
  1127. if (strcmp(dp->d_name, ".") != 0 &&
  1128. strcmp(dp->d_name, "..") != 0) {
  1129. char *fname;
  1130. fname = get_fullpath(fullpath,
  1131. (char *)&dp->d_name);
  1132. if (fname == NULL) {
  1133. rv = KMF_ERR_MEMORY;
  1134. break;
  1135. }
  1136. rv = kmf_load_cert(kmfh, issuer, subject,
  1137. serial, validity, fname, &certdata);
  1138. if (rv == KMF_ERR_CERT_NOT_FOUND) {
  1139. free(fname);
  1140. kmf_free_data(&certdata);
  1141. rv = KMF_OK;
  1142. continue;
  1143. } else if (rv != KMF_OK) {
  1144. free(fname);
  1145. break;
  1146. }
  1147. if (unlink(fname) != 0) {
  1148. SET_SYS_ERROR(kmfh, errno);
  1149. rv = KMF_ERR_INTERNAL;
  1150. free(fname);
  1151. break;
  1152. }
  1153. free(fname);
  1154. kmf_free_data(&certdata);
  1155. }
  1156. }
  1157. (void) closedir(dirp);
  1158. } else {
  1159. /* Just try to load a single certificate */
  1160. rv = kmf_load_cert(kmfh, issuer, subject, serial, validity,
  1161. fullpath, &certdata);
  1162. if (rv == KMF_OK) {
  1163. if (unlink(fullpath) != 0) {
  1164. SET_SYS_ERROR(kmfh, errno);
  1165. rv = KMF_ERR_INTERNAL;
  1166. }
  1167. }
  1168. }
  1169. out:
  1170. if (fullpath != NULL)
  1171. free(fullpath);
  1172. kmf_free_data(&certdata);
  1173. return (rv);
  1174. }
  1175. KMF_RETURN
  1176. OpenSSL_EncodePubKeyData(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key,
  1177. KMF_DATA *keydata)
  1178. {
  1179. KMF_RETURN rv = KMF_OK;
  1180. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1181. int n;
  1182. if (key == NULL || keydata == NULL ||
  1183. key->keyp == NULL)
  1184. return (KMF_ERR_BAD_PARAMETER);
  1185. if (key->keyalg == KMF_RSA) {
  1186. RSA *pubkey = EVP_PKEY_get1_RSA(key->keyp);
  1187. if (!(n = i2d_RSA_PUBKEY(pubkey, &keydata->Data))) {
  1188. SET_ERROR(kmfh, ERR_get_error());
  1189. return (KMF_ERR_ENCODING);
  1190. }
  1191. RSA_free(pubkey);
  1192. } else if (key->keyalg == KMF_DSA) {
  1193. DSA *pubkey = EVP_PKEY_get1_DSA(key->keyp);
  1194. if (!(n = i2d_DSA_PUBKEY(pubkey, &keydata->Data))) {
  1195. SET_ERROR(kmfh, ERR_get_error());
  1196. return (KMF_ERR_ENCODING);
  1197. }
  1198. DSA_free(pubkey);
  1199. } else {
  1200. return (KMF_ERR_BAD_PARAMETER);
  1201. }
  1202. keydata->Length = n;
  1203. cleanup:
  1204. if (rv != KMF_OK) {
  1205. if (keydata->Data)
  1206. free(keydata->Data);
  1207. keydata->Data = NULL;
  1208. keydata->Length = 0;
  1209. }
  1210. return (rv);
  1211. }
  1212. static KMF_RETURN
  1213. ssl_write_key(KMF_HANDLE *kmfh, KMF_ENCODE_FORMAT format, BIO *out,
  1214. KMF_CREDENTIAL *cred, EVP_PKEY *pkey, boolean_t private)
  1215. {
  1216. int rv = 0;
  1217. RSA *rsa;
  1218. DSA *dsa;
  1219. if (pkey == NULL || out == NULL)
  1220. return (KMF_ERR_BAD_PARAMETER);
  1221. switch (format) {
  1222. case KMF_FORMAT_RAWKEY:
  1223. /* same as ASN.1 */
  1224. case KMF_FORMAT_ASN1:
  1225. if (pkey->type == EVP_PKEY_RSA) {
  1226. rsa = EVP_PKEY_get1_RSA(pkey);
  1227. if (private)
  1228. rv = i2d_RSAPrivateKey_bio(out, rsa);
  1229. else
  1230. rv = i2d_RSAPublicKey_bio(out, rsa);
  1231. RSA_free(rsa);
  1232. } else if (pkey->type == EVP_PKEY_DSA) {
  1233. dsa = EVP_PKEY_get1_DSA(pkey);
  1234. rv = i2d_DSAPrivateKey_bio(out, dsa);
  1235. DSA_free(dsa);
  1236. }
  1237. if (rv == 1) {
  1238. rv = KMF_OK;
  1239. } else {
  1240. SET_ERROR(kmfh, rv);
  1241. }
  1242. break;
  1243. case KMF_FORMAT_PEM:
  1244. if (pkey->type == EVP_PKEY_RSA) {
  1245. rsa = EVP_PKEY_get1_RSA(pkey);
  1246. if (private)
  1247. rv = PEM_write_bio_RSAPrivateKey(out,
  1248. rsa, NULL, NULL, 0, NULL,
  1249. (cred != NULL ? cred->cred : NULL));
  1250. else
  1251. rv = PEM_write_bio_RSAPublicKey(out,
  1252. rsa);
  1253. RSA_free(rsa);
  1254. } else if (pkey->type == EVP_PKEY_DSA) {
  1255. dsa = EVP_PKEY_get1_DSA(pkey);
  1256. rv = PEM_write_bio_DSAPrivateKey(out,
  1257. dsa, NULL, NULL, 0, NULL,
  1258. (cred != NULL ? cred->cred : NULL));
  1259. DSA_free(dsa);
  1260. }
  1261. if (rv == 1) {
  1262. rv = KMF_OK;
  1263. } else {
  1264. SET_ERROR(kmfh, rv);
  1265. }
  1266. break;
  1267. default:
  1268. rv = KMF_ERR_BAD_PARAMETER;
  1269. }
  1270. return (rv);
  1271. }
  1272. KMF_RETURN
  1273. OpenSSL_CreateKeypair(KMF_HANDLE_T handle, int numattr,
  1274. KMF_ATTRIBUTE *attrlist)
  1275. {
  1276. KMF_RETURN rv = KMF_OK;
  1277. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1278. uint32_t eValue = 0x010001;
  1279. RSA *sslPrivKey = NULL;
  1280. DSA *sslDSAKey = NULL;
  1281. EVP_PKEY *eprikey = NULL;
  1282. EVP_PKEY *epubkey = NULL;
  1283. BIO *out = NULL;
  1284. KMF_KEY_HANDLE *pubkey = NULL, *privkey = NULL;
  1285. uint32_t keylen = 1024;
  1286. uint32_t keylen_size = sizeof (uint32_t);
  1287. boolean_t storekey = TRUE;
  1288. KMF_KEY_ALG keytype = KMF_RSA;
  1289. rv = kmf_get_attr(KMF_STOREKEY_BOOL_ATTR, attrlist, numattr,
  1290. &storekey, NULL);
  1291. if (rv != KMF_OK) {
  1292. /* "storekey" is optional. Default is TRUE */
  1293. rv = KMF_OK;
  1294. }
  1295. rv = kmf_get_attr(KMF_KEYALG_ATTR, attrlist, numattr,
  1296. (void *)&keytype, NULL);
  1297. if (rv != KMF_OK)
  1298. /* keytype is optional. KMF_RSA is default */
  1299. rv = KMF_OK;
  1300. pubkey = kmf_get_attr_ptr(KMF_PUBKEY_HANDLE_ATTR, attrlist, numattr);
  1301. if (pubkey == NULL)
  1302. return (KMF_ERR_BAD_PARAMETER);
  1303. privkey = kmf_get_attr_ptr(KMF_PRIVKEY_HANDLE_ATTR, attrlist, numattr);
  1304. if (privkey == NULL)
  1305. return (KMF_ERR_BAD_PARAMETER);
  1306. (void) memset(pubkey, 0, sizeof (KMF_KEY_HANDLE));
  1307. (void) memset(privkey, 0, sizeof (KMF_KEY_HANDLE));
  1308. eprikey = EVP_PKEY_new();
  1309. if (eprikey == NULL) {
  1310. SET_ERROR(kmfh, ERR_get_error());
  1311. rv = KMF_ERR_KEYGEN_FAILED;
  1312. goto cleanup;
  1313. }
  1314. epubkey = EVP_PKEY_new();
  1315. if (epubkey == NULL) {
  1316. SET_ERROR(kmfh, ERR_get_error());
  1317. rv = KMF_ERR_KEYGEN_FAILED;
  1318. goto cleanup;
  1319. }
  1320. if (keytype == KMF_RSA) {
  1321. KMF_BIGINT *rsaexp = NULL;
  1322. rsaexp = kmf_get_attr_ptr(KMF_RSAEXP_ATTR, attrlist, numattr);
  1323. if (rsaexp != NULL) {
  1324. if (rsaexp->len > 0 &&
  1325. rsaexp->len <= sizeof (eValue) &&
  1326. rsaexp->val != NULL) {
  1327. /* LINTED E_BAD_PTR_CAST_ALIGN */
  1328. eValue = *(uint32_t *)rsaexp->val;
  1329. } else {
  1330. rv = KMF_ERR_BAD_PARAMETER;
  1331. goto cleanup;
  1332. }
  1333. } else {
  1334. /* RSA Exponent is optional. Default is 0x10001 */
  1335. rv = KMF_OK;
  1336. }
  1337. rv = kmf_get_attr(KMF_KEYLENGTH_ATTR, attrlist, numattr,
  1338. &keylen, &keylen_size);
  1339. if (rv == KMF_ERR_ATTR_NOT_FOUND)
  1340. /* keylen is optional, default is 1024 */
  1341. rv = KMF_OK;
  1342. if (rv != KMF_OK) {
  1343. rv = KMF_ERR_BAD_PARAMETER;
  1344. goto cleanup;
  1345. }
  1346. sslPrivKey = RSA_generate_key(keylen, eValue, NULL, NULL);
  1347. if (sslPrivKey == NULL) {
  1348. SET_ERROR(kmfh, ERR_get_error());
  1349. rv = KMF_ERR_KEYGEN_FAILED;
  1350. } else {
  1351. (void) EVP_PKEY_set1_RSA(eprikey, sslPrivKey);
  1352. privkey->kstype = KMF_KEYSTORE_OPENSSL;
  1353. privkey->keyalg = KMF_RSA;
  1354. privkey->keyclass = KMF_ASYM_PRI;
  1355. privkey->israw = FALSE;
  1356. privkey->keyp = (void *)eprikey;
  1357. /* OpenSSL derives the public key from the private */
  1358. (void) EVP_PKEY_set1_RSA(epubkey, sslPrivKey);
  1359. pubkey->kstype = KMF_KEYSTORE_OPENSSL;
  1360. pubkey->keyalg = KMF_RSA;
  1361. pubkey->israw = FALSE;
  1362. pubkey->keyclass = KMF_ASYM_PUB;
  1363. pubkey->keyp = (void *)epubkey;
  1364. }
  1365. } else if (keytype == KMF_DSA) {
  1366. DSA *dp;
  1367. sslDSAKey = DSA_new();
  1368. if (sslDSAKey == NULL) {
  1369. SET_ERROR(kmfh, ERR_get_error());
  1370. return (KMF_ERR_MEMORY);
  1371. }
  1372. if ((sslDSAKey->p = BN_bin2bn(P, sizeof (P), sslDSAKey->p)) ==
  1373. NULL) {
  1374. SET_ERROR(kmfh, ERR_get_error());
  1375. rv = KMF_ERR_KEYGEN_FAILED;
  1376. goto cleanup;
  1377. }
  1378. if ((sslDSAKey->q = BN_bin2bn(Q, sizeof (Q), sslDSAKey->q)) ==
  1379. NULL) {
  1380. SET_ERROR(kmfh, ERR_get_error());
  1381. rv = KMF_ERR_KEYGEN_FAILED;
  1382. goto cleanup;
  1383. }
  1384. if ((sslDSAKey->g = BN_bin2bn(G, sizeof (G), sslDSAKey->g)) ==
  1385. NULL) {
  1386. SET_ERROR(kmfh, ERR_get_error());
  1387. rv = KMF_ERR_KEYGEN_FAILED;
  1388. goto cleanup;
  1389. }
  1390. if (!DSA_generate_key(sslDSAKey)) {
  1391. SET_ERROR(kmfh, ERR_get_error());
  1392. rv = KMF_ERR_KEYGEN_FAILED;
  1393. goto cleanup;
  1394. }
  1395. privkey->kstype = KMF_KEYSTORE_OPENSSL;
  1396. privkey->keyalg = KMF_DSA;
  1397. privkey->keyclass = KMF_ASYM_PRI;
  1398. privkey->israw = FALSE;
  1399. if (EVP_PKEY_set1_DSA(eprikey, sslDSAKey)) {
  1400. privkey->keyp = (void *)eprikey;
  1401. } else {
  1402. SET_ERROR(kmfh, ERR_get_error());
  1403. rv = KMF_ERR_KEYGEN_FAILED;
  1404. goto cleanup;
  1405. }
  1406. dp = DSA_new();
  1407. /* Make a copy for the public key */
  1408. if (dp != NULL) {
  1409. if ((dp->p = BN_new()) == NULL) {
  1410. SET_ERROR(kmfh, ERR_get_error());
  1411. rv = KMF_ERR_MEMORY;
  1412. DSA_free(dp);
  1413. goto cleanup;
  1414. }
  1415. if ((dp->q = BN_new()) == NULL) {
  1416. SET_ERROR(kmfh, ERR_get_error());
  1417. rv = KMF_ERR_MEMORY;
  1418. BN_free(dp->p);
  1419. DSA_free(dp);
  1420. goto cleanup;
  1421. }
  1422. if ((dp->g = BN_new()) == NULL) {
  1423. SET_ERROR(kmfh, ERR_get_error());
  1424. rv = KMF_ERR_MEMORY;
  1425. BN_free(dp->q);
  1426. BN_free(dp->p);
  1427. DSA_free(dp);
  1428. goto cleanup;
  1429. }
  1430. if ((dp->pub_key = BN_new()) == NULL) {
  1431. SET_ERROR(kmfh, ERR_get_error());
  1432. rv = KMF_ERR_MEMORY;
  1433. BN_free(dp->q);
  1434. BN_free(dp->p);
  1435. BN_free(dp->g);
  1436. DSA_free(dp);
  1437. goto cleanup;
  1438. }
  1439. (void) BN_copy(dp->p, sslDSAKey->p);
  1440. (void) BN_copy(dp->q, sslDSAKey->q);
  1441. (void) BN_copy(dp->g, sslDSAKey->g);
  1442. (void) BN_copy(dp->pub_key, sslDSAKey->pub_key);
  1443. pubkey->kstype = KMF_KEYSTORE_OPENSSL;
  1444. pubkey->keyalg = KMF_DSA;
  1445. pubkey->keyclass = KMF_ASYM_PUB;
  1446. pubkey->israw = FALSE;
  1447. if (EVP_PKEY_set1_DSA(epubkey, sslDSAKey)) {
  1448. pubkey->keyp = (void *)epubkey;
  1449. } else {
  1450. SET_ERROR(kmfh, ERR_get_error());
  1451. rv = KMF_ERR_KEYGEN_FAILED;
  1452. goto cleanup;
  1453. }
  1454. }
  1455. }
  1456. if (rv != KMF_OK) {
  1457. goto cleanup;
  1458. }
  1459. if (storekey) {
  1460. KMF_ATTRIBUTE storeattrs[4]; /* max. 4 attributes needed */
  1461. int i = 0;
  1462. char *keyfile = NULL, *dirpath = NULL;
  1463. KMF_ENCODE_FORMAT format;
  1464. /*
  1465. * Construct a new attribute arrray and call openssl_store_key
  1466. */
  1467. kmf_set_attr_at_index(storeattrs, i, KMF_PRIVKEY_HANDLE_ATTR,
  1468. privkey, sizeof (privkey));
  1469. i++;
  1470. dirpath = kmf_get_attr_ptr(KMF_DIRPATH_ATTR, attrlist, numattr);
  1471. if (dirpath != NULL) {
  1472. storeattrs[i].type = KMF_DIRPATH_ATTR;
  1473. storeattrs[i].pValue = dirpath;
  1474. storeattrs[i].valueLen = strlen(dirpath);
  1475. i++;
  1476. } else {
  1477. rv = KMF_OK; /* DIRPATH is optional */
  1478. }
  1479. keyfile = kmf_get_attr_ptr(KMF_KEY_FILENAME_ATTR,
  1480. attrlist, numattr);
  1481. if (keyfile != NULL) {
  1482. storeattrs[i].type = KMF_KEY_FILENAME_ATTR;
  1483. storeattrs[i].pValue = keyfile;
  1484. storeattrs[i].valueLen = strlen(keyfile);
  1485. i++;
  1486. } else {
  1487. goto cleanup; /* KEYFILE is required */
  1488. }
  1489. rv = kmf_get_attr(KMF_ENCODE_FORMAT_ATTR, attrlist, numattr,
  1490. (void *)&format, NULL);
  1491. if (rv == KMF_OK) {
  1492. storeattrs[i].type = KMF_ENCODE_FORMAT_ATTR;
  1493. storeattrs[i].pValue = &format;
  1494. storeattrs[i].valueLen = sizeof (format);
  1495. i++;
  1496. }
  1497. rv = OpenSSL_StoreKey(handle, i, storeattrs);
  1498. }
  1499. cleanup:
  1500. if (rv != KMF_OK) {
  1501. if (eprikey != NULL)
  1502. EVP_PKEY_free(eprikey);
  1503. if (epubkey != NULL)
  1504. EVP_PKEY_free(epubkey);
  1505. if (pubkey->keylabel) {
  1506. free(pubkey->keylabel);
  1507. pubkey->keylabel = NULL;
  1508. }
  1509. if (privkey->keylabel) {
  1510. free(privkey->keylabel);
  1511. privkey->keylabel = NULL;
  1512. }
  1513. pubkey->keyp = NULL;
  1514. privkey->keyp = NULL;
  1515. }
  1516. if (sslPrivKey)
  1517. RSA_free(sslPrivKey);
  1518. if (sslDSAKey)
  1519. DSA_free(sslDSAKey);
  1520. if (out != NULL)
  1521. (void) BIO_free(out);
  1522. return (rv);
  1523. }
  1524. /*
  1525. * Make sure the BN conversion is properly padded with 0x00
  1526. * bytes. If not, signature verification for DSA signatures
  1527. * may fail in the case where the bignum value does not use
  1528. * all of the bits.
  1529. */
  1530. static int
  1531. fixbnlen(BIGNUM *bn, unsigned char *buf, int len) {
  1532. int bytes = len - BN_num_bytes(bn);
  1533. /* prepend with leading 0x00 if necessary */
  1534. while (bytes-- > 0)
  1535. *buf++ = 0;
  1536. (void) BN_bn2bin(bn, buf);
  1537. /*
  1538. * Return the desired length since we prepended it
  1539. * with the necessary 0x00 padding.
  1540. */
  1541. return (len);
  1542. }
  1543. KMF_RETURN
  1544. OpenSSL_SignData(KMF_HANDLE_T handle, KMF_KEY_HANDLE *key,
  1545. KMF_OID *AlgOID, KMF_DATA *tobesigned, KMF_DATA *output)
  1546. {
  1547. KMF_RETURN ret = KMF_OK;
  1548. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1549. KMF_ALGORITHM_INDEX AlgId;
  1550. EVP_MD_CTX ctx;
  1551. const EVP_MD *md;
  1552. if (key == NULL || AlgOID == NULL ||
  1553. tobesigned == NULL || output == NULL ||
  1554. tobesigned->Data == NULL ||
  1555. output->Data == NULL)
  1556. return (KMF_ERR_BAD_PARAMETER);
  1557. /* Map the OID to an OpenSSL algorithm */
  1558. AlgId = x509_algoid_to_algid(AlgOID);
  1559. if (AlgId == KMF_ALGID_NONE)
  1560. return (KMF_ERR_BAD_ALGORITHM);
  1561. if (key->keyalg == KMF_RSA) {
  1562. EVP_PKEY *pkey = (EVP_PKEY *)key->keyp;
  1563. uchar_t *p;
  1564. int len;
  1565. if (AlgId == KMF_ALGID_MD5WithRSA)
  1566. md = EVP_md5();
  1567. else if (AlgId == KMF_ALGID_MD2WithRSA)
  1568. md = EVP_md2();
  1569. else if (AlgId == KMF_ALGID_SHA1WithRSA)
  1570. md = EVP_sha1();
  1571. else if (AlgId == KMF_ALGID_SHA256WithRSA)
  1572. md = EVP_sha256();
  1573. else if (AlgId == KMF_ALGID_SHA384WithRSA)
  1574. md = EVP_sha384();
  1575. else if (AlgId == KMF_ALGID_SHA512WithRSA)
  1576. md = EVP_sha512();
  1577. else if (AlgId == KMF_ALGID_RSA)
  1578. md = NULL;
  1579. else
  1580. return (KMF_ERR_BAD_ALGORITHM);
  1581. if ((md == NULL) && (AlgId == KMF_ALGID_RSA)) {
  1582. RSA *rsa = EVP_PKEY_get1_RSA((EVP_PKEY *)pkey);
  1583. p = output->Data;
  1584. if ((len = RSA_private_encrypt(tobesigned->Length,
  1585. tobesigned->Data, p, rsa,
  1586. RSA_PKCS1_PADDING)) <= 0) {
  1587. SET_ERROR(kmfh, ERR_get_error());
  1588. ret = KMF_ERR_INTERNAL;
  1589. }
  1590. output->Length = len;
  1591. } else {
  1592. (void) EVP_MD_CTX_init(&ctx);
  1593. (void) EVP_SignInit_ex(&ctx, md, NULL);
  1594. (void) EVP_SignUpdate(&ctx, tobesigned->Data,
  1595. (uint32_t)tobesigned->Length);
  1596. len = (uint32_t)output->Length;
  1597. p = output->Data;
  1598. if (!EVP_SignFinal(&ctx, p, (uint32_t *)&len, pkey)) {
  1599. SET_ERROR(kmfh, ERR_get_error());
  1600. len = 0;
  1601. ret = KMF_ERR_INTERNAL;
  1602. }
  1603. output->Length = len;
  1604. (void) EVP_MD_CTX_cleanup(&ctx);
  1605. }
  1606. } else if (key->keyalg == KMF_DSA) {
  1607. DSA *dsa = EVP_PKEY_get1_DSA(key->keyp);
  1608. uchar_t hash[EVP_MAX_MD_SIZE];
  1609. uint32_t hashlen;
  1610. DSA_SIG *dsasig;
  1611. if (AlgId == KMF_ALGID_DSA ||
  1612. AlgId == KMF_ALGID_SHA1WithDSA)
  1613. md = EVP_sha1();
  1614. else if (AlgId == KMF_ALGID_SHA256WithDSA)
  1615. md = EVP_sha256();
  1616. else /* Bad algorithm */
  1617. return (KMF_ERR_BAD_ALGORITHM);
  1618. /*
  1619. * OpenSSL EVP_Sign operation automatically converts to
  1620. * ASN.1 output so we do the operations separately so we
  1621. * are assured of NOT getting ASN.1 output returned.
  1622. * KMF does not want ASN.1 encoded results because
  1623. * not all mechanisms return ASN.1 encodings (PKCS#11
  1624. * and NSS return raw signature data).
  1625. */
  1626. EVP_MD_CTX_init(&ctx);
  1627. (void) EVP_DigestInit_ex(&ctx, md, NULL);
  1628. (void) EVP_DigestUpdate(&ctx, tobesigned->Data,
  1629. tobesigned->Length);
  1630. (void) EVP_DigestFinal_ex(&ctx, hash, &hashlen);
  1631. /* Only sign first 20 bytes for SHA2 */
  1632. if (AlgId == KMF_ALGID_SHA256WithDSA)
  1633. hashlen = 20;
  1634. dsasig = DSA_do_sign(hash, hashlen, dsa);
  1635. if (dsasig != NULL) {
  1636. int i;
  1637. output->Length = i = fixbnlen(dsasig->r, output->Data,
  1638. hashlen);
  1639. output->Length += fixbnlen(dsasig->s, &output->Data[i],
  1640. hashlen);
  1641. DSA_SIG_free(dsasig);
  1642. } else {
  1643. SET_ERROR(kmfh, ERR_get_error());
  1644. }
  1645. (void) EVP_MD_CTX_cleanup(&ctx);
  1646. } else {
  1647. return (KMF_ERR_BAD_PARAMETER);
  1648. }
  1649. cleanup:
  1650. return (ret);
  1651. }
  1652. KMF_RETURN
  1653. /*ARGSUSED*/
  1654. OpenSSL_DeleteKey(KMF_HANDLE_T handle,
  1655. int numattr, KMF_ATTRIBUTE *attrlist)
  1656. {
  1657. KMF_RETURN rv = KMF_OK;
  1658. KMF_KEY_HANDLE *key;
  1659. boolean_t destroy = B_TRUE;
  1660. key = kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR, attrlist, numattr);
  1661. if (key == NULL || key->keyp == NULL)
  1662. return (KMF_ERR_BAD_PARAMETER);
  1663. rv = kmf_get_attr(KMF_DESTROY_BOOL_ATTR, attrlist, numattr,
  1664. (void *)&destroy, NULL);
  1665. if (rv != KMF_OK) {
  1666. /* "destroy" is optional. Default is TRUE */
  1667. rv = KMF_OK;
  1668. }
  1669. if (key->keyclass != KMF_ASYM_PUB &&
  1670. key->keyclass != KMF_ASYM_PRI &&
  1671. key->keyclass != KMF_SYMMETRIC)
  1672. return (KMF_ERR_BAD_KEY_CLASS);
  1673. if (key->keyclass == KMF_SYMMETRIC) {
  1674. kmf_free_raw_sym_key((KMF_RAW_SYM_KEY *)key->keyp);
  1675. key->keyp = NULL;
  1676. } else {
  1677. if (key->keyp != NULL) {
  1678. EVP_PKEY_free(key->keyp);
  1679. key->keyp = NULL;
  1680. }
  1681. }
  1682. if (key->keylabel != NULL) {
  1683. EVP_PKEY *pkey = NULL;
  1684. /* If the file exists, make sure it is a proper key. */
  1685. pkey = openssl_load_key(handle, key->keylabel);
  1686. if (pkey == NULL) {
  1687. if (key->keylabel != NULL) {
  1688. free(key->keylabel);
  1689. key->keylabel = NULL;
  1690. }
  1691. return (KMF_ERR_KEY_NOT_FOUND);
  1692. }
  1693. EVP_PKEY_free(pkey);
  1694. if (destroy) {
  1695. if (unlink(key->keylabel) != 0) {
  1696. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1697. SET_SYS_ERROR(kmfh, errno);
  1698. rv = KMF_ERR_INTERNAL;
  1699. }
  1700. }
  1701. if (key->keylabel != NULL) {
  1702. free(key->keylabel);
  1703. key->keylabel = NULL;
  1704. }
  1705. }
  1706. return (rv);
  1707. }
  1708. KMF_RETURN
  1709. OpenSSL_GetErrorString(KMF_HANDLE_T handle, char **msgstr)
  1710. {
  1711. KMF_RETURN ret = KMF_OK;
  1712. KMF_HANDLE *kmfh = (KMF_HANDLE *)handle;
  1713. char str[256]; /* OpenSSL needs at least 120 byte buffer */
  1714. ERR_error_string_n(kmfh->lasterr.errcode, str, sizeof (str));
  1715. if (strlen(str)) {
  1716. *msgstr = (char *)strdup(str);
  1717. if ((*msgstr) == NULL)
  1718. ret = KMF_ERR_MEMORY;
  1719. } else {
  1720. *msgstr = NULL;
  1721. }
  1722. return (ret);
  1723. }
  1724. static int
  1725. ext2NID(int kmfext)
  1726. {
  1727. switch (kmfext) {
  1728. case KMF_X509_EXT_KEY_USAGE:
  1729. return (NID_key_usage);
  1730. case KMF_X509_EXT_PRIV_KEY_USAGE_PERIOD:
  1731. return (NID_private_key_usage_period);
  1732. case KMF_X509_EXT_CERT_POLICIES:
  1733. return (NID_certificate_policies);
  1734. case KMF_X509_EXT_SUBJ_ALTNAME:
  1735. return (NID_subject_alt_name);
  1736. case KMF_X509_EXT_ISSUER_ALTNAME:
  1737. return (NID_issuer_alt_name);
  1738. case KMF_X509_EXT_BASIC_CONSTRAINTS:
  1739. return (NID_basic_constraints);
  1740. case KMF_X509_EXT_EXT_KEY_USAGE:
  1741. return (NID_ext_key_usage);
  1742. case KMF_X509_EXT_AUTH_KEY_ID:
  1743. return (NID_authority_key_identifier);
  1744. case KMF_X509_EXT_CRL_DIST_POINTS:
  1745. return (NID_crl_distribution_points);
  1746. case KMF_X509_EXT_SUBJ_KEY_ID:
  1747. return (NID_subject_key_identifier);
  1748. case KMF_X509_EXT_POLICY_MAPPINGS:
  1749. return (OBJ_sn2nid("policyMappings"));
  1750. case KMF_X509_EXT_NAME_CONSTRAINTS:
  1751. return (OBJ_sn2nid("nameConstraints"));
  1752. case KMF_X509_EXT_POLICY_CONSTRAINTS:
  1753. return (OBJ_sn2nid("policyConstraints"));
  1754. case KMF_X509_EXT_INHIBIT_ANY_POLICY:
  1755. return (OBJ_sn2nid("inhibitAnyPolicy"));
  1756. case KMF_X509_EXT_FRESHEST_CRL:
  1757. return (OBJ_sn2nid("fres…

Large files files are truncated, but you can click here to view the full file