PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/kpkcs11/cki_ssleay.c

https://github.com/secure-endpoints/kcacred
C | 1012 lines | 822 code | 113 blank | 77 comment | 119 complexity | 4cd2beb21ad92710f55d77cdee42d300 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. * Copyright (c) 1999
  3. * The Trustees of Columbia University in the City of New York.
  4. * All rights reserved.
  5. *
  6. * Permission is granted to you to use, copy, create derivative works,
  7. * and redistribute this software and such derivative works for any
  8. * purpose, so long as the name of Columbia University is not used in any
  9. * advertising, publicity, or for any other purpose pertaining to the use
  10. * or distribution of this software, other than for including the
  11. * copyright notice set forth herein, without specific, written prior
  12. * authorization. Columbia University reserves the rights to use, copy,
  13. * and distribute any such derivative works for any purposes. The above
  14. * copyright notice must be included in any copy of any portion of this
  15. * software and the disclaimer below must also be included.
  16. *
  17. * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
  18. * TRUSTEES OF COLUMBIA UNIVERSITY IN THE CITY OF NEW YORK AS TO ITS
  19. * FITNESS FOR ANY PURPOSE, AND WITHOUT WARRANTY BY THE TRUSTEES OF
  20. * COLUMBIA UNIVERSITY IN THE CITY OF NEW YORK OF ANY KIND, EITHER
  21. * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. * THE TRUSTEES OF COLUMBIA UNIVERSITY IN THE CITY OF NEW YORK SHALL
  24. * NOT BE LIABLE FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT,
  25. * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM
  26. * ARISING OUT OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN IF
  27. * IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH
  28. * DAMAGES. YOU SHALL INDEMNIFY AND HOLD HARMLESS THE TRUSTEES OF
  29. * COLUMBIA UNIVERSITY IN THE CITY OF NEW YORK, ITS EMPLOYEES AND
  30. * AGENTS FROM AND AGAINST ANY AND ALL CLAIMS, DEMANDS, LOSS, DAMAGE OR
  31. * EXPENSE (INCLUDING ATTORNEYS' FEES) ARISING OUT OF YOUR USE OF THIS
  32. * SOFTWARE.
  33. *
  34. * The Trustees of Columbia University in the City of New York reserves
  35. * the right to revoke this permission if any of the terms of use set
  36. * forth above are breached.
  37. */
  38. /*
  39. * Copyright © 2000
  40. * The Regents of the University of Michigan
  41. * ALL RIGHTS RESERVED
  42. *
  43. * permission is granted to use, copy, create derivative works
  44. * and redistribute this software and such derivative works
  45. * for any purpose, so long as the name of the university of
  46. * michigan is not used in any advertising or publicity
  47. * pertaining to the use or distribution of this software
  48. * without specific, written prior authorization. if the
  49. * above copyright notice or any other identification of the
  50. * university of michigan is included in any copy of any
  51. * portion of this software, then the disclaimer below must
  52. * also be included.
  53. *
  54. * this software is provided as is, without representation
  55. * from the university of michigan as to its fitness for any
  56. * purpose, and without warranty by the university of
  57. * michigan of any kind, either express or implied, including
  58. * without limitation the implied warranties of
  59. * merchantability and fitness for a particular purpose. the
  60. * regents of the university of michigan shall not be liable
  61. * for any damages, including special, indirect, incidental, or
  62. * consequential damages, with respect to any claim arising
  63. * out of or in connection with the use of the software, even
  64. * if it has been or is hereafter advised of the possibility of
  65. * such damages.
  66. */
  67. /*
  68. * Copyright © 2006-2008
  69. * Secure Endpoints Inc.
  70. * ALL RIGHTS RESERVED
  71. *
  72. */
  73. /* functions that use SSLeay routines */
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include "cki_types.h"
  77. #include "pkcs11_types.h"
  78. #include "cki_funcs.h"
  79. #include "pkcs11_funcs.h"
  80. #include "cki_globals.h"
  81. #include "pkcs11_globals.h"
  82. #include "cki_new_free.h"
  83. #include "pkcs11_new_free.h"
  84. #include "cki_dup.h"
  85. #include "b64.h"
  86. #include "debug.h"
  87. #ifdef DEBUG
  88. #include <assert.h>
  89. #endif
  90. static CK_KEY_TYPE keyType=CKK_RSA; /* XXX KWC Made static */
  91. static CK_BBOOL True=TRUE; /* XXX KWC Made static */
  92. static CK_BBOOL False=FALSE; /* XXX KWC Made static */
  93. static CK_CERTIFICATE_TYPE certType=CKC_X_509; /* KWC */
  94. #define NUM_ATTRS 29
  95. CK_RV PKCS11_RSA_to_RsaPrivateKey(CK_SESSION_HANDLE hSession, RSA *rsa,
  96. char *username, char *subject, int subject_len,
  97. CK_CHAR_PTR pID) {
  98. CK_ATTRIBUTE_PTR pTemplate;
  99. CK_OBJECT_CLASS *pObjectClass; /* KWC */
  100. int i=0;
  101. CK_CHAR_PTR label = NULL;
  102. CK_CHAR_PTR id = NULL;
  103. unsigned char *n,*e,*d,*p,*q,*dmp1,*dmq1,*iqmp;
  104. CK_OBJECT_HANDLE_PTR pObject; /* we don't use this after it gets filled, oh well. */
  105. CK_DATE date;
  106. CK_BYTE empty='\0';
  107. CK_RV res = CKR_OK;
  108. log_printf("entering PKCS11_RSA_to_RsaPrivateKey\n");
  109. CKI_Date_Init(&date);
  110. pObject=(CK_OBJECT_HANDLE *)malloc(sizeof(CK_OBJECT_HANDLE));
  111. if (!pObject)
  112. return(CKR_HOST_MEMORY);
  113. pTemplate=(CK_ATTRIBUTE_PTR)malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  114. if (!pTemplate) {
  115. free(pObject);
  116. return(CKR_HOST_MEMORY);
  117. }
  118. for (i=0; i<NUM_ATTRS; i++) {
  119. pTemplate[i].ulValueLen=(CK_ULONG)-1L;
  120. pTemplate[i].value=NULL_PTR;
  121. }
  122. i=0;
  123. pObjectClass=(CK_OBJECT_CLASS *)malloc(sizeof(CK_OBJECT_CLASS));
  124. if (!pObjectClass) {
  125. free(pObject);
  126. free(pTemplate);
  127. return(CKR_HOST_MEMORY);
  128. }
  129. *pObjectClass = CKO_PRIVATE_KEY;
  130. pTemplate[i].type=CKA_CLASS;
  131. pTemplate[i].ulValueLen=sizeof(CK_OBJECT_CLASS);
  132. res=CKI_SetAttrValue(&(pTemplate[i]), pObjectClass);
  133. if (res!=CKR_OK)
  134. goto error;
  135. i++;
  136. pTemplate[i].type=CKA_TOKEN;
  137. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  138. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  139. if (res!=CKR_OK)
  140. goto error;
  141. i++;
  142. pTemplate[i].type=CKA_PRIVATE;
  143. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  144. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  145. if (res!=CKR_OK)
  146. goto error;
  147. i++;
  148. pTemplate[i].type=CKA_MODIFIABLE;
  149. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  150. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  151. if (res!=CKR_OK)
  152. goto error;
  153. i++;
  154. label=(CK_CHAR_PTR)malloc(strlen(username)+strlen("'s private key")+2);
  155. if (!label) {
  156. res = CKR_HOST_MEMORY;
  157. goto error;
  158. }
  159. sprintf((char *)label,"%s's private key",username);
  160. pTemplate[i].type=CKA_LABEL;
  161. pTemplate[i].ulValueLen=(CK_ULONG)strlen((char *)label);
  162. pTemplate[i].value=label;
  163. label = NULL;
  164. i++;
  165. pTemplate[i].type=CKA_KEY_TYPE;
  166. pTemplate[i].ulValueLen=sizeof(CK_KEY_TYPE);
  167. res=CKI_SetAttrValue(&(pTemplate[i]),&keyType);
  168. if (res!=CKR_OK)
  169. goto error;
  170. i++;
  171. id=_strdup(pID);
  172. if (!id) {
  173. res = CKR_HOST_MEMORY;
  174. goto error;
  175. }
  176. pTemplate[i].type=CKA_ID;
  177. pTemplate[i].ulValueLen=(CK_ULONG)strlen((char *)id);
  178. pTemplate[i].value=id;
  179. id = NULL;
  180. i++;
  181. pTemplate[i].type=CKA_START_DATE;
  182. pTemplate[i].ulValueLen=sizeof(CK_DATE);
  183. res=CKI_SetAttrValue(&(pTemplate[i]),&date);
  184. if (res!=CKR_OK)
  185. goto error;
  186. i++;
  187. pTemplate[i].type=CKA_END_DATE;
  188. pTemplate[i].ulValueLen=sizeof(CK_DATE);
  189. res=CKI_SetAttrValue(&(pTemplate[i]),&date);
  190. if (res!=CKR_OK)
  191. goto error;
  192. i++;
  193. pTemplate[i].type=CKA_DERIVE;
  194. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  195. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  196. if (res!=CKR_OK)
  197. goto error;
  198. i++;
  199. pTemplate[i].type=CKA_LOCAL;
  200. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  201. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  202. if (res!=CKR_OK)
  203. goto error;
  204. i++;
  205. pTemplate[i].type=CKA_SUBJECT;
  206. if (subject) {
  207. pTemplate[i].ulValueLen=subject_len;
  208. res=CKI_SetAttrValue(&(pTemplate[i]),subject);
  209. }
  210. else {
  211. pTemplate[i].ulValueLen=1L;
  212. res=CKI_SetAttrValue(&(pTemplate[i]),&empty);
  213. }
  214. if (res!=CKR_OK)
  215. goto error;
  216. i++;
  217. pTemplate[i].type=CKA_SENSITIVE;
  218. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  219. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  220. if (res!=CKR_OK)
  221. goto error;
  222. i++;
  223. pTemplate[i].type=CKA_DECRYPT;
  224. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  225. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  226. if (res!=CKR_OK)
  227. goto error;
  228. i++;
  229. pTemplate[i].type=CKA_SIGN;
  230. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  231. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  232. if (res!=CKR_OK)
  233. goto error;
  234. i++;
  235. pTemplate[i].type=CKA_SIGN_RECOVER;
  236. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  237. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  238. if (res!=CKR_OK)
  239. goto error;
  240. i++;
  241. pTemplate[i].type=CKA_UNWRAP;
  242. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  243. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  244. if (res!=CKR_OK)
  245. goto error;
  246. i++;
  247. pTemplate[i].type=CKA_EXTRACTABLE;
  248. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  249. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  250. if (res!=CKR_OK)
  251. goto error;
  252. i++;
  253. pTemplate[i].type=CKA_ALWAYS_SENSITIVE;
  254. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  255. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  256. if (res!=CKR_OK)
  257. goto error;
  258. i++;
  259. pTemplate[i].type=CKA_NEVER_EXTRACTABLE;
  260. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  261. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  262. if (res!=CKR_OK)
  263. goto error;
  264. i++;
  265. pTemplate[i].type=CKA_MODULUS;
  266. pTemplate[i].ulValueLen=BN_num_bytes(rsa->n);
  267. n=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  268. BN_bn2bin(rsa->n,n);
  269. res=CKI_SetAttrValue(&(pTemplate[i]),n);
  270. if (res!=CKR_OK)
  271. goto error;
  272. i++;
  273. pTemplate[i].type=CKA_PUBLIC_EXPONENT;
  274. pTemplate[i].ulValueLen=BN_num_bytes(rsa->e);
  275. e=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  276. BN_bn2bin(rsa->e,e);
  277. res=CKI_SetAttrValue(&(pTemplate[i]),e);
  278. if (res!=CKR_OK)
  279. goto error;
  280. i++;
  281. pTemplate[i].type=CKA_PRIVATE_EXPONENT;
  282. pTemplate[i].ulValueLen=BN_num_bytes(rsa->d);
  283. d=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  284. BN_bn2bin(rsa->d,d);
  285. res=CKI_SetAttrValue(&(pTemplate[i]),d);
  286. if (res!=CKR_OK)
  287. goto error;
  288. i++;
  289. pTemplate[i].type=CKA_PRIME_1;
  290. pTemplate[i].ulValueLen=BN_num_bytes(rsa->p);
  291. p=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  292. BN_bn2bin(rsa->p,p);
  293. res=CKI_SetAttrValue(&(pTemplate[i]),p);
  294. if (res!=CKR_OK)
  295. goto error;
  296. i++;
  297. pTemplate[i].type=CKA_PRIME_2;
  298. pTemplate[i].ulValueLen=BN_num_bytes(rsa->q);
  299. q=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  300. BN_bn2bin(rsa->q,q);
  301. res=CKI_SetAttrValue(&(pTemplate[i]),q);
  302. if (res!=CKR_OK)
  303. goto error;
  304. i++;
  305. pTemplate[i].type=CKA_EXPONENT_1;
  306. pTemplate[i].ulValueLen=BN_num_bytes(rsa->dmp1);
  307. dmp1=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  308. BN_bn2bin(rsa->dmp1,dmp1);
  309. res=CKI_SetAttrValue(&(pTemplate[i]),dmp1);
  310. if (res!=CKR_OK)
  311. goto error;
  312. i++;
  313. pTemplate[i].type=CKA_EXPONENT_2;
  314. pTemplate[i].ulValueLen=BN_num_bytes(rsa->dmq1);
  315. dmq1=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  316. BN_bn2bin(rsa->dmq1,dmq1);
  317. res=CKI_SetAttrValue(&(pTemplate[i]),dmq1);
  318. if (res!=CKR_OK)
  319. goto error;
  320. i++;
  321. pTemplate[i].type=CKA_COEFFICIENT;
  322. pTemplate[i].ulValueLen=BN_num_bytes(rsa->iqmp);
  323. iqmp=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  324. BN_bn2bin(rsa->iqmp,iqmp);
  325. res=CKI_SetAttrValue(&(pTemplate[i]),iqmp);
  326. if (res!=CKR_OK)
  327. goto error;
  328. #ifdef DEBUG
  329. assert(i < NUM_ATTRS);
  330. #endif
  331. /* pObject is an object handle or something. do we do anything with it? */
  332. res=C_CreateObject(hSession,pTemplate,1L,pObject);
  333. if (res!=CKR_OK)
  334. {
  335. log_printf("PKCS11_RSA_to_RsaPrivateKey: error creating pObject (0x%08x)\n", res);
  336. goto error;
  337. }
  338. log_printf("PKCS11_RSA_to_RsaPrivateKey: private key created with handle 0x%0x\n",
  339. *pObject);
  340. error:
  341. if (id)
  342. free(id);
  343. if (label)
  344. free(label);
  345. if (pTemplate)
  346. CKI_AttributePtr_Free(pTemplate);
  347. if (pObject)
  348. free(pObject);
  349. if (pObjectClass)
  350. free(pObjectClass);
  351. return res;
  352. }
  353. #undef NUM_ATTRS
  354. #define NUM_ATTRS 29
  355. CK_RV PKCS11_RSA_to_RsaPublicKey(CK_SESSION_HANDLE hSession, RSA *rsa,
  356. char *username, char *subject, int subject_len,
  357. CK_CHAR_PTR pID) {
  358. CK_ATTRIBUTE_PTR pTemplate = NULL;
  359. CK_OBJECT_CLASS *pObjectClass = NULL; /* KWC */
  360. int i=0;
  361. CK_CHAR_PTR label = NULL;
  362. CK_CHAR_PTR id = NULL;
  363. CK_RV res = CKR_OK;
  364. unsigned char *n, *e;
  365. CK_OBJECT_HANDLE_PTR pObject = NULL; /* we don't use this after it gets filled,
  366. oh well. */
  367. CK_DATE date;
  368. CK_BYTE empty='\0';
  369. CK_ULONG mod_length;
  370. log_printf("entering PKCS11_RSA_to_RsaPublicKey\n");
  371. CKI_Date_Init(&date);
  372. pObject=(CK_OBJECT_HANDLE *)malloc(sizeof(CK_OBJECT_HANDLE));
  373. if (!pObject) {
  374. res = CKR_HOST_MEMORY;
  375. goto error;
  376. }
  377. pTemplate=(CK_ATTRIBUTE_PTR)malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  378. if (!pTemplate) {
  379. res = CKR_HOST_MEMORY;
  380. goto error;
  381. }
  382. for (i=0; i<NUM_ATTRS; i++) {
  383. pTemplate[i].ulValueLen=(CK_ULONG)-1L;
  384. pTemplate[i].value=NULL_PTR;
  385. }
  386. i=0;
  387. pObjectClass=(CK_OBJECT_CLASS *)malloc(sizeof(CK_OBJECT_CLASS));
  388. if (!pObjectClass) {
  389. res = CKR_HOST_MEMORY;
  390. goto error;
  391. }
  392. *pObjectClass = CKO_PUBLIC_KEY;
  393. pTemplate[i].type=CKA_CLASS;
  394. pTemplate[i].ulValueLen=sizeof(CK_OBJECT_CLASS);
  395. res=CKI_SetAttrValue(&(pTemplate[i]),pObjectClass);
  396. if (res!=CKR_OK)
  397. goto error;
  398. i++;
  399. pTemplate[i].type=CKA_TOKEN;
  400. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  401. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  402. if (res!=CKR_OK)
  403. goto error;
  404. i++;
  405. pTemplate[i].type=CKA_PRIVATE;
  406. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  407. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  408. if (res!=CKR_OK)
  409. goto error;
  410. i++;
  411. pTemplate[i].type=CKA_MODIFIABLE;
  412. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  413. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  414. if (res!=CKR_OK)
  415. goto error;
  416. i++;
  417. label=(CK_CHAR_PTR)malloc(strlen(username)+strlen("'s public key")+2);
  418. if (!label) {
  419. res = CKR_HOST_MEMORY;
  420. goto error;
  421. }
  422. sprintf((char *)label,"%s's public key",username);
  423. pTemplate[i].type=CKA_LABEL;
  424. pTemplate[i].ulValueLen=(CK_ULONG)strlen((char *)label);
  425. pTemplate[i].value=label;
  426. label = NULL;
  427. i++;
  428. pTemplate[i].type=CKA_KEY_TYPE;
  429. pTemplate[i].ulValueLen=sizeof(CK_KEY_TYPE);
  430. res=CKI_SetAttrValue(&(pTemplate[i]),&keyType);
  431. if (res!=CKR_OK)
  432. goto error;
  433. i++;
  434. id=_strdup(pID);
  435. if (!id) {
  436. res = CKR_HOST_MEMORY;
  437. goto error;
  438. }
  439. pTemplate[i].type=CKA_ID;
  440. pTemplate[i].ulValueLen=(CK_ULONG)strlen((char *)id);
  441. pTemplate[i].value=id;
  442. id = NULL;
  443. i++;
  444. pTemplate[i].type=CKA_START_DATE;
  445. pTemplate[i].ulValueLen=sizeof(CK_DATE);
  446. res=CKI_SetAttrValue(&(pTemplate[i]),&date);
  447. if (res!=CKR_OK)
  448. goto error;
  449. i++;
  450. pTemplate[i].type=CKA_END_DATE;
  451. pTemplate[i].ulValueLen=sizeof(CK_DATE);
  452. res=CKI_SetAttrValue(&(pTemplate[i]),&date);
  453. if (res!=CKR_OK)
  454. goto error;
  455. i++;
  456. pTemplate[i].type=CKA_DERIVE;
  457. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  458. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  459. if (res!=CKR_OK)
  460. goto error;
  461. i++;
  462. pTemplate[i].type=CKA_LOCAL;
  463. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  464. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  465. if (res!=CKR_OK)
  466. goto error;
  467. i++;
  468. pTemplate[i].type=CKA_SUBJECT;
  469. if (subject) {
  470. pTemplate[i].ulValueLen=subject_len;
  471. res=CKI_SetAttrValue(&(pTemplate[i]),subject);
  472. }
  473. else {
  474. pTemplate[i].ulValueLen=1L;
  475. res=CKI_SetAttrValue(&(pTemplate[i]),&empty);
  476. }
  477. if (res!=CKR_OK)
  478. goto error;
  479. i++;
  480. pTemplate[i].type=CKA_ENCRYPT;
  481. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  482. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  483. if (res!=CKR_OK)
  484. goto error;
  485. i++;
  486. pTemplate[i].type=CKA_VERIFY;
  487. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  488. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  489. if (res!=CKR_OK)
  490. goto error;
  491. i++;
  492. pTemplate[i].type=CKA_VERIFY_RECOVER;
  493. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  494. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  495. if (res!=CKR_OK)
  496. goto error;
  497. i++;
  498. pTemplate[i].type=CKA_WRAP;
  499. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  500. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  501. if (res!=CKR_OK)
  502. goto error;
  503. i++;
  504. pTemplate[i].type=CKA_MODULUS;
  505. pTemplate[i].ulValueLen=BN_num_bytes(rsa->n);
  506. n=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  507. BN_bn2bin(rsa->n,n);
  508. res=CKI_SetAttrValue(&(pTemplate[i]),n);
  509. if (res!=CKR_OK)
  510. goto error;
  511. i++;
  512. pTemplate[i].type=CKA_MODULUS_BITS;
  513. pTemplate[i].ulValueLen=sizeof(CK_ULONG);
  514. mod_length=BN_num_bits(rsa->n);
  515. res=CKI_SetAttrValue(&(pTemplate[i]),&mod_length);
  516. if (res!=CKR_OK)
  517. goto error;
  518. i++;
  519. pTemplate[i].type=CKA_PUBLIC_EXPONENT;
  520. pTemplate[i].ulValueLen=BN_num_bytes(rsa->e);
  521. e=(unsigned char *)malloc(pTemplate[i].ulValueLen);
  522. BN_bn2bin(rsa->e,e);
  523. res=CKI_SetAttrValue(&(pTemplate[i]),e);
  524. if (res!=CKR_OK)
  525. goto error;
  526. #ifdef DEBUG
  527. assert(i < NUM_ATTRS);
  528. #endif
  529. /* pObject is an object handle or something. do we do anything with it? */
  530. res=C_CreateObject(hSession,pTemplate,1L,pObject);
  531. if (res!=CKR_OK)
  532. {
  533. log_printf("PKCS11_RSA_to_RsaPublicKey: error creating pObject (0x%08x)\n", res);
  534. goto error;
  535. }
  536. log_printf("PKCS11_RSA_to_RsaPublicKey: public key created with handle 0x%0x\n",
  537. *pObject);
  538. error:
  539. if (id)
  540. free(id);
  541. if (label)
  542. free(label);
  543. if (pTemplate)
  544. CKI_AttributePtr_Free(pTemplate);
  545. if (pObject)
  546. free(pObject);
  547. if (pObjectClass)
  548. free(pObjectClass);
  549. return(res);
  550. }
  551. #undef NUM_ATTRS
  552. #define NUM_ATTRS 14
  553. CK_RV PKCS11_X509_to_X509Certificate(CK_SESSION_HANDLE hSession, X509 *x, char *username, CK_CHAR_PTR * ppID)
  554. {
  555. CK_ATTRIBUTE_PTR pTemplate = NULL;
  556. CK_OBJECT_CLASS *pObjectClass = NULL; /* KWC */
  557. int i=0;
  558. CK_CHAR_PTR label = NULL;
  559. CK_CHAR_PTR id = NULL;
  560. char *cert_der = NULL;
  561. char *serial_der = NULL;
  562. char *subject_der = NULL;
  563. char *issuer_der = NULL;
  564. char *issuer_enc = NULL;
  565. char *serial_enc = NULL;
  566. int cert_len;
  567. int serial_len, subject_len, issuer_len;
  568. X509_NAME *issuer = NULL, *subject = NULL;
  569. ASN1_INTEGER *serial;
  570. CK_RV res = CKR_OK;
  571. char *ptr = NULL;
  572. CK_OBJECT_HANDLE_PTR pObject = NULL;
  573. log_printf("entering PKCS11_X509_to_X509Certificate\n");
  574. pObject=(CK_OBJECT_HANDLE *)malloc(sizeof(CK_OBJECT_HANDLE));
  575. if (!pObject)
  576. {
  577. log_printf("PKCS11_X509_to_X509Certificate: could not malloc object handle\n");
  578. res = CKR_HOST_MEMORY;
  579. goto error;
  580. }
  581. pTemplate=(CK_ATTRIBUTE_PTR)malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  582. if (!pTemplate)
  583. {
  584. log_printf("PKCS11_X509_to_X509Certificate: could not malloc attribute space\n");
  585. res = CKR_HOST_MEMORY;
  586. goto error;
  587. }
  588. for (i=0; i<NUM_ATTRS; i++) {
  589. pTemplate[i].ulValueLen=(CK_ULONG)-1L;
  590. pTemplate[i].value=NULL_PTR;
  591. }
  592. i=0;
  593. pObjectClass=(CK_OBJECT_CLASS *)malloc(sizeof(CK_OBJECT_CLASS));
  594. if (!pObjectClass)
  595. {
  596. log_printf("PKCS11_X509_to_X509Certificate: could not malloc object class space\n");
  597. res = CKR_HOST_MEMORY;
  598. goto error;
  599. }
  600. *pObjectClass = CKO_CERTIFICATE;
  601. pTemplate[i].type=CKA_CLASS;
  602. pTemplate[i].ulValueLen=sizeof(CK_OBJECT_CLASS);
  603. res=CKI_SetAttrValue(&(pTemplate[i]), pObjectClass);
  604. log_printf("PKCS11_X509_to_X509Certificate: pTemplate[%d].value is %ld\n",
  605. i, *(CK_ULONG *)pTemplate[i].value);
  606. if (res!=CKR_OK)
  607. {
  608. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_CLASS (0x%08x)\n", res);
  609. goto error;
  610. }
  611. i++;
  612. pTemplate[i].type=CKA_TOKEN;
  613. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  614. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  615. if (res!=CKR_OK)
  616. {
  617. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_TOKEN (0x%08x)\n", res);
  618. goto error;
  619. }
  620. i++;
  621. pTemplate[i].type=CKA_PRIVATE;
  622. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  623. res=CKI_SetAttrValue(&(pTemplate[i]),&True);
  624. if (res!=CKR_OK)
  625. {
  626. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_PRIVATE (0x%08x)\n", res);
  627. goto error;
  628. }
  629. i++;
  630. pTemplate[i].type=CKA_MODIFIABLE;
  631. pTemplate[i].ulValueLen=sizeof(CK_BBOOL);
  632. res=CKI_SetAttrValue(&(pTemplate[i]),&False);
  633. if (res!=CKR_OK)
  634. {
  635. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_MODIFIABLE (0x%08x)\n", res);
  636. goto error;
  637. }
  638. i++;
  639. label=(CK_CHAR_PTR)malloc(strlen(username)+strlen("'s certificate")+2);
  640. if (!label)
  641. {
  642. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for label\n");
  643. res = CKR_HOST_MEMORY;
  644. goto error;
  645. }
  646. sprintf((char *)label,"%s's certificate",username);
  647. pTemplate[i].type=CKA_LABEL;
  648. pTemplate[i].ulValueLen=(CK_ULONG)strlen((char *)label);
  649. pTemplate[i].value=label;
  650. label = NULL;
  651. i++;
  652. pTemplate[i].type=CKA_CERTIFICATE_TYPE;
  653. pTemplate[i].ulValueLen=sizeof(CK_CERTIFICATE_TYPE);
  654. res=CKI_SetAttrValue(&(pTemplate[i]),&certType);
  655. if (res!=CKR_OK)
  656. {
  657. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_CERTIFICATE_TYPE (0x%08x)\n", res);
  658. goto error;
  659. }
  660. i++;
  661. cert_len=i2d_X509(x,NULL);
  662. cert_der=(char *)malloc(cert_len);
  663. if (!cert_der)
  664. {
  665. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for cert_der\n");
  666. res = CKR_HOST_MEMORY;
  667. goto error;
  668. }
  669. ptr=cert_der;
  670. i2d_X509(x,(unsigned char **)&ptr);
  671. issuer=X509_get_issuer_name(x);
  672. issuer_len=i2d_X509_NAME(issuer,NULL);
  673. issuer_der=(char *)malloc(issuer_len);
  674. if (!issuer_der)
  675. {
  676. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for issuer_der\n");
  677. res = CKR_HOST_MEMORY;
  678. goto error;
  679. }
  680. ptr=issuer_der;
  681. i2d_X509_NAME(issuer,(unsigned char **)&ptr);
  682. subject=X509_get_subject_name(x);
  683. subject_len=i2d_X509_NAME(subject,NULL);
  684. subject_der=(char *)malloc(subject_len);
  685. if (!subject_der)
  686. {
  687. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for subject_der\n");
  688. res = CKR_HOST_MEMORY;
  689. goto error;
  690. }
  691. ptr=subject_der;
  692. i2d_X509_NAME(subject,(unsigned char **)&ptr);
  693. serial=X509_get_serialNumber(x); /* does not get freed */
  694. if (serial==NULL) {
  695. log_printf("PKCS11_X509_to_X509Certificate: couldn't get serial number from cert\n");
  696. res =CKR_FUNCTION_FAILED;
  697. goto error;
  698. }
  699. serial_len=i2d_ASN1_INTEGER(serial,NULL);
  700. serial_der=(char *)malloc(serial_len);
  701. if (!serial_der)
  702. {
  703. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for serial_der\n");
  704. res = CKR_HOST_MEMORY;
  705. goto error;
  706. }
  707. ptr=serial_der;
  708. log_printf("PKCS11_X509_to_X509Certificate: serial_len is %d\n",serial_len);
  709. i2d_ASN1_INTEGER(serial,(unsigned char **)&ptr);
  710. pTemplate[i].type=CKA_SUBJECT;
  711. pTemplate[i].ulValueLen=subject_len;
  712. res=CKI_SetAttrValue(&(pTemplate[i]),subject_der);
  713. if (res!=CKR_OK)
  714. {
  715. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_SUBJECT (0x%08x)\n", res);
  716. goto error;
  717. }
  718. i++;
  719. pTemplate[i].type=CKA_ISSUER;
  720. pTemplate[i].ulValueLen=issuer_len;
  721. res=CKI_SetAttrValue(&(pTemplate[i]),issuer_der);
  722. if (res!=CKR_OK)
  723. {
  724. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_ISSUER (0x%08x)\n", res);
  725. goto error;
  726. }
  727. i++;
  728. pTemplate[i].type=CKA_SERIAL_NUMBER;
  729. pTemplate[i].ulValueLen=serial_len;
  730. res=CKI_SetAttrValue(&(pTemplate[i]),serial_der);
  731. if (res!=CKR_OK)
  732. {
  733. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_SERIAL_NUMBER (0x%08x)\n", res);
  734. goto error;
  735. }
  736. i++;
  737. pTemplate[i].type=CKA_VALUE;
  738. pTemplate[i].ulValueLen=cert_len;
  739. res=CKI_SetAttrValue(&(pTemplate[i]),cert_der);
  740. if (res!=CKR_OK)
  741. {
  742. log_printf("PKCS11_X509_to_X509Certificate: error setting CKA_VALUE (0x%08x)\n", res);
  743. goto error;
  744. }
  745. i++;
  746. issuer_enc=(char *)malloc((issuer_len+1)*2);
  747. if (!issuer_enc) {
  748. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for issuer_enc\n");
  749. res = CKR_HOST_MEMORY;
  750. goto error;
  751. }
  752. b64_encode(issuer_der,issuer_len,issuer_enc);
  753. serial_enc=(char *)malloc((serial_len+1)*2);
  754. if (!serial_enc) {
  755. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for serial_enc\n");
  756. res = CKR_HOST_MEMORY;
  757. goto error;
  758. }
  759. b64_encode(serial_der,serial_len,serial_enc);
  760. id=(CK_CHAR_PTR)malloc(strlen(issuer_enc)+strlen(serial_enc)+2);
  761. if (!id)
  762. {
  763. log_printf("PKCS11_X509_to_X509Certificate: error allocating space for id\n");
  764. res = CKR_HOST_MEMORY;
  765. goto error;
  766. }
  767. sprintf(id,"%s:%s",issuer_enc,serial_enc);
  768. pTemplate[i].type=CKA_ID;
  769. pTemplate[i].ulValueLen=(CK_ULONG)strlen((char *)id);
  770. pTemplate[i].value=id;
  771. *ppID = _strdup(id);
  772. id = NULL;
  773. #ifdef DEBUG
  774. assert(i < NUM_ATTRS);
  775. #endif
  776. /* pObject is an object handle or something. do we do anything with it? */
  777. res=C_CreateObject(hSession,pTemplate,1L,pObject);
  778. if (res!=CKR_OK)
  779. {
  780. log_printf("PKCS11_X509_to_X509Certificate: error creating pObject (0x%08x)\n", res);
  781. goto error;
  782. }
  783. log_printf("PKCS11_X509_to_X509Certificate: certficate created with handle 0x%0x\n",
  784. *pObject);
  785. error:
  786. if (cert_der)
  787. free(cert_der);
  788. if (serial_der)
  789. free(serial_der);
  790. if (subject_der)
  791. free(subject_der);
  792. if (issuer_der)
  793. free(issuer_der);
  794. if (issuer_enc)
  795. free(issuer_enc);
  796. if (serial_enc)
  797. free(serial_enc);
  798. if (id)
  799. free(id);
  800. if (label)
  801. free(label);
  802. if (pTemplate)
  803. CKI_AttributePtr_Free(pTemplate);
  804. if (pObject)
  805. free(pObject);
  806. if (pObjectClass)
  807. free(pObjectClass);
  808. log_printf("PKCS11_X509_to_X509Certificate: returning 0x%08x\n", res);
  809. return(res);
  810. }
  811. #undef NUM_ATTRS
  812. RSA *PKCS11_RsaPrivateKey_to_RSA(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey) {
  813. RSA *rsa = NULL; /* freed by caller */
  814. int i=0;
  815. int ctr = 0;
  816. PKCS11_SESSION *pSession = NULL;
  817. CK_ATTRIBUTE_PTR pAttributes = NULL;
  818. CK_ATTRIBUTE_PTR attr = NULL;
  819. CK_RV res = CKR_OK;
  820. int bFound = FALSE;
  821. log_printf("entering PKCS11_RsaPrivateKey_to_RSA: hKey = 0x%0x\n", hKey);
  822. if ((pSession=PKCS11_FindSession(hSession))==NULL)
  823. return(NULL);
  824. if (!pSession->pToken->ppTokenObject)
  825. return(NULL);
  826. for (ctr = 0; pSession->pToken->ppTokenObject[ctr]; ctr++) {
  827. if (pSession->pToken->ppTokenObject[ctr]->ulObjectHandle==hKey) {
  828. pAttributes=pSession->pToken->ppTokenObject[ctr]->pAttribute;
  829. break;
  830. }
  831. }
  832. if (!pAttributes)
  833. {
  834. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_MODULUS\n");
  835. goto error;
  836. }
  837. attr=PKCS11_FindAttribute_p(pAttributes,CKA_MODULUS);
  838. if (!attr)
  839. {
  840. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_MODULUS\n");
  841. goto error;
  842. }
  843. rsa=RSA_new();
  844. rsa->n=BN_bin2bn(attr->value,attr->ulValueLen,rsa->n);
  845. attr=PKCS11_FindAttribute_p(pAttributes,CKA_PUBLIC_EXPONENT);
  846. if (!attr)
  847. {
  848. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_PUBLIC_EXPONENT\n");
  849. goto error;
  850. }
  851. rsa->e=BN_bin2bn(attr->value,attr->ulValueLen,rsa->e);
  852. attr=PKCS11_FindAttribute_p(pAttributes,CKA_PRIVATE_EXPONENT);
  853. if (!attr)
  854. {
  855. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_PRIVATE_EXPONENT\n");
  856. goto error;
  857. }
  858. rsa->d=BN_bin2bn(attr->value,attr->ulValueLen,rsa->d);
  859. attr=PKCS11_FindAttribute_p(pAttributes,CKA_PRIME_1);
  860. if (!attr)
  861. {
  862. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_PRIME_1\n");
  863. goto error;
  864. }
  865. rsa->p=BN_bin2bn(attr->value,attr->ulValueLen,rsa->p);
  866. attr=PKCS11_FindAttribute_p(pAttributes,CKA_PRIME_2);
  867. if (!attr)
  868. {
  869. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_PRIME_2\n");
  870. goto error;
  871. }
  872. rsa->q=BN_bin2bn(attr->value,attr->ulValueLen,rsa->q);
  873. attr=PKCS11_FindAttribute_p(pAttributes,CKA_EXPONENT_1);
  874. if (!attr)
  875. {
  876. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_EXPONENT_1\n");
  877. goto error;
  878. }
  879. rsa->dmp1=BN_bin2bn(attr->value,attr->ulValueLen,rsa->dmp1);
  880. attr=PKCS11_FindAttribute_p(pAttributes,CKA_EXPONENT_2);
  881. if (!attr)
  882. {
  883. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_EXPONENT_2\n");
  884. goto error;
  885. }
  886. rsa->dmq1=BN_bin2bn(attr->value,attr->ulValueLen,rsa->dmq1);
  887. attr=PKCS11_FindAttribute_p(pAttributes,CKA_COEFFICIENT);
  888. if (!attr)
  889. {
  890. log_printf("PKCS11_RsaPrivateKey_to_RSA: could not find CKA_COEFFICIENT\n");
  891. goto error;
  892. }
  893. rsa->iqmp=BN_bin2bn(attr->value,attr->ulValueLen,rsa->iqmp);
  894. return(rsa);
  895. error:
  896. if (rsa)
  897. RSA_free(rsa);
  898. return(NULL);
  899. }