PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/kpkcs11/cki_objs.c

https://github.com/secure-endpoints/kcacred
C | 1397 lines | 1166 code | 139 blank | 92 comment | 180 complexity | 6ecb51d736460c11febdfedeaa30f0cc 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,2002
  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
  69. * Secure Endpoints Inc.
  70. * ALL RIGHTS RESERVED
  71. *
  72. */
  73. #include <stdlib.h>
  74. #include <string.h>
  75. #include "cki_types.h"
  76. #include "pkcs11_types.h"
  77. #include "cki_funcs.h"
  78. #include "pkcs11_funcs.h"
  79. #include "cki_globals.h"
  80. #include "pkcs11_globals.h"
  81. #include "cki_new_free.h"
  82. #include "pkcs11_new_free.h"
  83. #include "cki_dup.h"
  84. #include "debug.h"
  85. #ifdef DEBUG
  86. #include <assert.h>
  87. #endif
  88. CK_RV CK_ENTRY C_CreateObject(CK_SESSION_HANDLE hSession,
  89. CK_ATTRIBUTE_PTR pTemplate,
  90. CK_ULONG ulCount,
  91. CK_OBJECT_HANDLE_PTR pObject) {
  92. PKCS11_SESSION *pSession;
  93. CK_RV res;
  94. CK_ATTRIBUTE_PTR attr;
  95. CK_OBJECT_CLASS objV;
  96. CK_CERTIFICATE_TYPE certV;
  97. CK_KEY_TYPE keyV;
  98. #ifdef DEBUG
  99. int i;
  100. #endif
  101. log_printf("entering C_CreateObject\n");
  102. /* now we have to check the attributes for every object they might
  103. * create and that we know about. */
  104. /* first: is it a valid session handle? */
  105. if ((pSession=PKCS11_FindSession(hSession))==NULL)
  106. return(CKR_SESSION_HANDLE_INVALID);
  107. #ifdef DEBUG
  108. /* debug only */
  109. for (i = 0; i < 20; i++) {
  110. if (pTemplate[i].ulValueLen == 0L) break;
  111. if (pTemplate[i].type == CKA_CLASS) {
  112. log_printf("attr type is CKA_CLASS, value is 0x%08x\n",*(CK_OBJECT_CLASS *)pTemplate[i].value);
  113. break;
  114. }
  115. }
  116. /* end debug */
  117. #endif
  118. attr = PKCS11_GetAttribute(pTemplate,CKA_CLASS);
  119. if (!attr) {
  120. log_printf("C_CreateObject: could not locate CKA_CLASS attribute, returning with CKR_TEMPLATE_INCOMPLETE\n");
  121. return(CKR_TEMPLATE_INCOMPLETE);
  122. }
  123. objV = *(CK_OBJECT_CLASS *)attr->value;
  124. log_printf("C_CreateObject: CKA_CLASS object class value 0x%08x\n", objV);
  125. CKI_Attribute_Free(attr);
  126. attr = NULL;
  127. switch (objV) {
  128. case CKO_DATA:
  129. log_printf("C_CreateObject: processing class value CKO_DATA\n");
  130. res = PKCS11_CreateDataObject(pSession, pTemplate, ulCount, pObject);
  131. break;
  132. case CKO_CERTIFICATE:
  133. log_printf("C_CreateObject: processing class value CKO_CERTIFICATE\n");
  134. attr = PKCS11_GetAttribute(pTemplate,CKA_CERTIFICATE_TYPE);
  135. if (!attr) {
  136. log_printf("C_CreateObject: could not get CKA_CERTIFICATE_TYPE attribute\n");
  137. return(CKR_TEMPLATE_INCOMPLETE);
  138. }
  139. certV = *((CK_CERTIFICATE_TYPE *)(attr->value));
  140. CKI_Attribute_Free(attr);
  141. attr = NULL;
  142. switch (certV) {
  143. case CKC_X_509:
  144. log_printf("C_CreateObject: processing CKC_X_509 certificate\n");
  145. res = PKCS11_CreateX509CertificateObject(pSession, pTemplate, ulCount, pObject);
  146. break;
  147. default:
  148. log_printf("C_CreateObject: invalid certificate type encounted (0x%08x)\n", certV);
  149. return(CKR_ATTRIBUTE_VALUE_INVALID);
  150. }
  151. break;
  152. case CKO_PUBLIC_KEY:
  153. log_printf("C_CreateObject: processing class value CKO_PUBLIC_KEY\n");
  154. attr = PKCS11_GetAttribute(pTemplate,CKA_KEY_TYPE);
  155. if (!attr) {
  156. log_printf("C_CreateObject: could not get CKA_KEY_TYPE\n");
  157. return(CKR_TEMPLATE_INCOMPLETE);
  158. }
  159. keyV = *((CK_KEY_TYPE *)(attr->value));
  160. CKI_Attribute_Free(attr);
  161. attr = NULL;
  162. switch (keyV) {
  163. case CKK_RSA:
  164. res = PKCS11_CreateRSAPublicKeyObject(pSession, pTemplate, ulCount, pObject);
  165. break;
  166. case CKK_DSA:
  167. case CKK_ECDSA:
  168. case CKK_DH:
  169. case CKK_KEA:
  170. case CKK_MAYFLY:
  171. log_printf("C_CreateObject: returning CKR_FUNCTION_NOT_SUPPORTED\n");
  172. return(CKR_FUNCTION_NOT_SUPPORTED);
  173. default:
  174. log_printf("C_CreateObject: returning CKR_ATTRIBUTE_VALUE_INVALID\n");
  175. return(CKR_ATTRIBUTE_VALUE_INVALID);
  176. }
  177. break;
  178. case CKO_PRIVATE_KEY:
  179. log_printf("C_CreateObject: processing class value CKO_PRIVATE_KEY\n");
  180. attr = PKCS11_GetAttribute(pTemplate,CKA_KEY_TYPE);
  181. if (!attr) {
  182. log_printf("C_CreateObject: could not get CKA_KEY_TYPE\n");
  183. return(CKR_TEMPLATE_INCOMPLETE);
  184. }
  185. keyV = *((CK_KEY_TYPE *)(attr->value));
  186. CKI_Attribute_Free(attr);
  187. attr = NULL;
  188. switch (keyV) {
  189. case CKK_RSA:
  190. res = PKCS11_CreateRSAPrivateKeyObject(pSession, pTemplate, ulCount, pObject);
  191. break;
  192. case CKK_DSA:
  193. case CKK_ECDSA:
  194. case CKK_DH:
  195. case CKK_KEA:
  196. case CKK_MAYFLY:
  197. log_printf("C_CreateObject: returning CKR_FUNCTION_NOT_SUPPORTED\n");
  198. return(CKR_FUNCTION_NOT_SUPPORTED);
  199. default:
  200. log_printf("C_CreateObject: returning CKR_ATTRIBUTE_VALUE_INVALID\n");
  201. return(CKR_ATTRIBUTE_VALUE_INVALID);
  202. }
  203. break;
  204. case CKO_SECRET_KEY:
  205. log_printf("C_CreateObject: processing class value CKO_SECRET_KEY\n");
  206. res = PKCS11_CreateSecretKeyObject(pSession, pTemplate, ulCount, pObject);
  207. break;
  208. case CKO_VENDOR_DEFINED:
  209. log_printf("C_CreateObject: processing class value CKO_VENDOR_DEFINED\n");
  210. res = PKCS11_CreateVendorDefinedObject(pSession, pTemplate, ulCount, pObject);
  211. break;
  212. default:
  213. log_printf("C_CreateObject: processing class value *UNKNOWN* (0x%08x)\n", objV);
  214. return(CKR_ATTRIBUTE_TYPE_INVALID);
  215. }
  216. log_printf("C_CreateObject: returning with result %d\n", res);
  217. return(res);
  218. }
  219. CK_RV CK_ENTRY C_DestroyObject(CK_SESSION_HANDLE hSession,
  220. CK_OBJECT_HANDLE hObject) {
  221. int j;
  222. PKCS11_SESSION *pSession;
  223. int ctr = 0;
  224. log_printf("entering C_DestroyObject\n");
  225. /* first: is it a valid session handle? */
  226. if ((pSession=PKCS11_FindSession(hSession))==NULL)
  227. return(CKR_SESSION_HANDLE_INVALID);
  228. for(ctr = 0; pSession->pToken->ppTokenObject[ctr]; ctr++) {
  229. if (pSession->pToken->ppTokenObject[ctr]->ulObjectHandle == hObject) {
  230. break;
  231. }
  232. }
  233. if (!pSession->pToken->ppTokenObject[ctr])
  234. return(CKR_OBJECT_HANDLE_INVALID);
  235. log_printf("C_DestroyObject: found an object to free\n");
  236. PKCS11_Object_Free(pSession->pToken->ppTokenObject[ctr]);
  237. pSession->pToken->ppTokenObject[ctr] = NULL_PTR;
  238. /* move 'em all up in the chain */
  239. j = ctr;
  240. while (pSession->pToken->ppTokenObject[j+1]) {
  241. pSession->pToken->ppTokenObject[j] = pSession->pToken->ppTokenObject[j+1];
  242. j++;
  243. }
  244. pSession->pToken->ppTokenObject[j] = NULL_PTR;
  245. return(CKR_OK);
  246. }
  247. CK_ATTRIBUTE_PTR PKCS11_FindAttribute_p(CK_ATTRIBUTE_PTR pAttributes,
  248. CK_ATTRIBUTE_TYPE Type) {
  249. int i;
  250. log_printf("PKCS11_FindAttribute_p: entered to find Type 0x%08x\n", Type);
  251. if (!pAttributes)
  252. {
  253. log_printf("PKCS11_FindAttribute_p: no CK_ATTRIBUTE_PTR given\n");
  254. return(NULL);
  255. }
  256. for (i=0; pAttributes[i].value; i++) {
  257. #ifdef VERBBOSE_DEBUG
  258. log_printf("PKCS11_FindAttribute_p: checking index %02d (0x%08x vs. 0x%08x)\n",
  259. i, pAttributes[i].type, Type);
  260. #endif
  261. if (pAttributes[i].type == Type) {
  262. log_printf("PKCS11_FindAttribute_p: found a match at index %d\n", i);
  263. return(&(pAttributes[i]));
  264. }
  265. }
  266. log_printf("PKCS11_FindAttribute_p: returning with no match\n");
  267. return(NULL);
  268. }
  269. CK_ATTRIBUTE_PTR PKCS11_GetAttribute(CK_ATTRIBUTE_PTR pAttributes,
  270. CK_ATTRIBUTE_TYPE Type) {
  271. CK_ATTRIBUTE_PTR attr_copy;
  272. CK_ATTRIBUTE_PTR attr_source;
  273. CK_RV res;
  274. int i = 0;
  275. log_printf("PKCS11_GetAttribute: looking for attribute type 0x%08x\n", Type);
  276. if (!pAttributes)
  277. {
  278. log_printf("PKCS11_GetAttribute: no CK_ATTRIBUTE_PTR given\n");
  279. return(NULL);
  280. }
  281. attr_source = PKCS11_FindAttribute_p(pAttributes, Type);
  282. if (!attr_source)
  283. {
  284. log_printf("PKCS11_GetAttribute: returning without finding a match\n");
  285. return(NULL);
  286. }
  287. attr_copy = CKI_Attribute_New();
  288. if (!attr_copy)
  289. {
  290. log_printf("PKCS11_GetAttribute: could not CKI_Attribute_New()\n");
  291. return(NULL);
  292. }
  293. res = CKI_Attribute_Dup(attr_copy, attr_source);
  294. if (res != CKR_OK)
  295. {
  296. log_printf("PKCS11_GetAttribute: failed to duplicate the attribute!\n");
  297. CKI_Attribute_Free(attr_copy);
  298. return(NULL);
  299. }
  300. log_printf("PKCS11_GetAttribute: returning attribute at location 0x%08x\n", attr_copy);
  301. return(attr_copy);
  302. }
  303. CK_ULONG PKCS11_NextObjectHandle() {
  304. PKCS11_ObjectHandleCounter++;
  305. return(PKCS11_ObjectHandleCounter);
  306. }
  307. #define NUM_ATTRS 8
  308. CK_RV PKCS11_CreateDataObject(PKCS11_SESSION *pSession, CK_ATTRIBUTE_PTR pTemplate,
  309. CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR pObject) {
  310. int i;
  311. CK_ATTRIBUTE_PTR pAttributes, pTemp, attr;
  312. PKCS11_OBJECT ** ppTokenObject, *object;
  313. CK_RV res;
  314. CK_BYTE empty = '\0';
  315. log_printf("entering PKCS11_CreateDataObject\n");
  316. pAttributes = malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  317. if (!pAttributes)
  318. {
  319. log_printf("PKCS11_CreateDataObject: unable to allocate space for attributes\n");
  320. return(CKR_HOST_MEMORY);
  321. }
  322. /* set default values first */
  323. memset(pAttributes,0,sizeof(sizeof(CK_ATTRIBUTE)*NUM_ATTRS));
  324. i = 0;
  325. res = PKCS11_SetCommonObjectAttrs(pAttributes, CKO_DATA, &i);
  326. if (res != CKR_OK) {
  327. log_printf("PKCS11_CreateDataObject: failed to PKCS11_SetCommonObjectAttrs (0x%08x)\n", res);
  328. CKI_AttributePtr_Free(pAttributes);
  329. return(res);
  330. }
  331. pAttributes[i].type = CKA_APPLICATION;
  332. pAttributes[i].ulValueLen = 1L;
  333. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  334. if (res != CKR_OK) {
  335. log_printf("PKCS11_CreateDataObject: failed to set attribute CKA_APPLICATION (0x%08x)\n", res);
  336. CKI_AttributePtr_Free(pAttributes);
  337. return(res);
  338. }
  339. i++;
  340. pAttributes[i].type = CKA_VALUE;
  341. pAttributes[i].ulValueLen = 1L;
  342. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  343. if (res != CKR_OK) {
  344. log_printf("PKCS11_CreateDataObject: failed to set attribute CKA_VALUE (0x%08x)\n", res);
  345. CKI_AttributePtr_Free(pAttributes);
  346. return(res);
  347. }
  348. #ifdef DEBUG
  349. assert(i < NUM_ATTRS);
  350. #endif
  351. for (i = 0; (char *)pTemplate[i].value; i++) {
  352. attr = &(pTemplate[i]);
  353. switch (attr->type) {
  354. case CKA_CLASS:
  355. break;
  356. case CKA_TOKEN:
  357. case CKA_PRIVATE:
  358. case CKA_LABEL:
  359. case CKA_APPLICATION:
  360. case CKA_VALUE:
  361. case CKA_MODIFIABLE:
  362. pTemp = PKCS11_FindAttribute_p(pAttributes, attr->type);
  363. res = CKI_Attribute_Dup(pTemp, attr);
  364. if (res != CKR_OK) {
  365. log_printf("PKCS11_CreateDataObject: failed to dup attribute type 0x%08x (result 0x%08x)\n",
  366. attr->type, res);
  367. CKI_AttributePtr_Free(pAttributes);
  368. return(CKR_FUNCTION_FAILED);
  369. }
  370. break;
  371. default:
  372. log_printf("PKCS11_CreateDataObject: returning CKR_ATTRIBUTE_TYPE_INVALID\n");
  373. CKI_AttributePtr_Free(pAttributes);
  374. return(CKR_ATTRIBUTE_TYPE_INVALID);
  375. }
  376. }
  377. /* now put them somewhere... */
  378. if (!pSession->pToken->ppTokenObject) {
  379. pSession->pToken->ppTokenObject = (PKCS11_OBJECT **)malloc(sizeof(PKCS11_OBJECT *));
  380. pSession->pToken->ppTokenObject[0] = NULL;
  381. }
  382. ppTokenObject = pSession->pToken->ppTokenObject;
  383. for (i = 0; ppTokenObject[i]; i++)
  384. ;
  385. ppTokenObject = (PKCS11_OBJECT **)realloc(ppTokenObject,sizeof(PKCS11_OBJECT *)*(i+2));
  386. if (!ppTokenObject)
  387. {
  388. log_printf("PKCS11_CreateDataObject: failed to realloc space for object\n");
  389. CKI_AttributePtr_Free(pAttributes);
  390. return(CKR_HOST_MEMORY);
  391. }
  392. pSession->pToken->ppTokenObject = ppTokenObject;
  393. ppTokenObject[i+1] = NULL;
  394. ppTokenObject[i] = PKCS11_Object_New();
  395. if (!ppTokenObject[i])
  396. {
  397. log_printf("PKCS11_CreateDataObject: failed to allocate new object\n");
  398. CKI_AttributePtr_Free(pAttributes);
  399. return(CKR_HOST_MEMORY);
  400. }
  401. object = ppTokenObject[i];
  402. object->ulObjectHandle = PKCS11_NextObjectHandle();
  403. *pObject = object->ulObjectHandle;
  404. object->ulObjectClass = CKO_DATA;
  405. object->pAttribute = pAttributes;
  406. log_printf("PKCS11_CreateDataObject: returning CKR_OK\n");
  407. return(CKR_OK);
  408. }
  409. #undef NUM_ATTRS
  410. #define NUM_ATTRS 12
  411. CK_RV PKCS11_CreateX509CertificateObject(PKCS11_SESSION *pSession, CK_ATTRIBUTE_PTR pTemplate,
  412. CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR pObject) {
  413. int i;
  414. CK_ATTRIBUTE_PTR pAttributes, pTemp, attr;
  415. PKCS11_OBJECT ** ppTokenObject, *object;
  416. CK_CERTIFICATE_TYPE certValue = CKC_X_509;
  417. CK_BYTE empty = '\0';
  418. CK_RV res;
  419. log_printf("entering PKCS11_CreateX509CertificateObject\n");
  420. pAttributes = malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  421. if (!pAttributes)
  422. {
  423. log_printf("PKCS11_CreateX509CertificateObject: could not get memory for attributes\n");
  424. return(CKR_HOST_MEMORY);
  425. }
  426. /* set default values first */
  427. memset(pAttributes, 0, sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  428. i = 0;
  429. res = PKCS11_SetCommonObjectAttrs(pAttributes, CKO_CERTIFICATE, &i);
  430. if (res != CKR_OK) {
  431. log_printf("PKCS11_CreateX509CertificateObject: error from PKCS11_SetCommonObjectAttrs (0x%08x)\n", res);
  432. CKI_AttributePtr_Free(pAttributes);
  433. return(res);
  434. }
  435. pAttributes[i].type = CKA_CERTIFICATE_TYPE;
  436. pAttributes[i].ulValueLen = sizeof(CK_CERTIFICATE_TYPE);
  437. res = CKI_SetAttrValue(&(pAttributes[i]),&certValue);
  438. if (res != CKR_OK) {
  439. log_printf("PKCS11_CreateX509CertificateObject: error while setting CKA_CERTIFICATE_TYPE (0x%08x)\n", res);
  440. CKI_AttributePtr_Free(pAttributes);
  441. return(res);
  442. }
  443. i++;
  444. pAttributes[i].type = CKA_SUBJECT;
  445. pAttributes[i].ulValueLen = 1L;
  446. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  447. if (res != CKR_OK) {
  448. log_printf("PKCS11_CreateX509CertificateObject: error while setting CKA_SUBJECT (0x%08x)\n", res);
  449. CKI_AttributePtr_Free(pAttributes);
  450. return(res);
  451. }
  452. i++;
  453. pAttributes[i].type = CKA_ID;
  454. pAttributes[i].ulValueLen = 1L;
  455. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  456. if (res != CKR_OK) {
  457. log_printf("PKCS11_CreateX509CertificateObject: error while setting CKA_ID (0x%08x)\n", res);
  458. CKI_AttributePtr_Free(pAttributes);
  459. return(res);
  460. }
  461. i++;
  462. pAttributes[i].type = CKA_ISSUER;
  463. pAttributes[i].ulValueLen = 1L;
  464. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  465. if (res != CKR_OK) {
  466. log_printf("PKCS11_CreateX509CertificateObject: error while setting CKA_ISSUER (0x%08x)\n", res);
  467. CKI_AttributePtr_Free(pAttributes);
  468. return(res);
  469. }
  470. i++;
  471. pAttributes[i].type = CKA_SERIAL_NUMBER;
  472. pAttributes[i].ulValueLen = 1L;
  473. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  474. if (res != CKR_OK) {
  475. log_printf("PKCS11_CreateX509CertificateObject: error while setting CKA_SERIAL_NUMBER (0x%08x)\n", res);
  476. CKI_AttributePtr_Free(pAttributes);
  477. return(res);
  478. }
  479. i++;
  480. pAttributes[i].type = CKA_VALUE;
  481. pAttributes[i].ulValueLen = 1L;
  482. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  483. if (res != CKR_OK) {
  484. log_printf("PKCS11_CreateX509CertificateObject: error while setting CKA_VALUE (0x%08x)\n", res);
  485. CKI_AttributePtr_Free(pAttributes);
  486. return(res);
  487. }
  488. #ifdef DEBUG
  489. assert(i < NUM_ATTRS);
  490. #endif
  491. for (i = 0; pTemplate[i].value; i++) {
  492. attr = &(pTemplate[i]);
  493. log_printf("PKCS11_CreateX509CertificateObject: attr->type is 0x%08x\n",attr->type);
  494. switch (attr->type) {
  495. case CKA_CLASS:
  496. case CKA_CERTIFICATE_TYPE:
  497. log_printf("PKCS11_CreateX509CertificateObject: ignoring CKA_CLASS or CKA_CERTIFICATE_TYPE\n");
  498. break;
  499. case CKA_TOKEN:
  500. case CKA_PRIVATE:
  501. case CKA_MODIFIABLE:
  502. case CKA_LABEL:
  503. case CKA_VALUE:
  504. case CKA_SUBJECT:
  505. case CKA_ID:
  506. case CKA_ISSUER:
  507. case CKA_SERIAL_NUMBER:
  508. log_printf("PKCS11_CreateX509CertificateObject: processing attribute\n");
  509. pTemp = PKCS11_FindAttribute_p(pAttributes,attr->type);
  510. if (attr->type == CKA_VALUE)
  511. log_printf("PKCS11_CreateX509CertificateObject: attr value length for CKA_VALUE is %ld\n",attr->ulValueLen);
  512. if (attr->type == CKA_VALUE)
  513. log_printf("PKCS11_CreateX509CertificateObject: pTemp->type is 0x%08x\n",pTemp->type);
  514. res = CKI_Attribute_Dup(pTemp,attr);
  515. if (res != CKR_OK) {
  516. log_printf("PKCS11_CreateX509CertificateObject: unable to dup attribute (0x%08x)\n", res);
  517. CKI_AttributePtr_Free(pAttributes);
  518. return(CKR_FUNCTION_FAILED);
  519. }
  520. break;
  521. default:
  522. log_printf("PKCS11_CreateX509CertificateObject: returning CKR_ATTRIBUTE_TYPE_INVALID\n");
  523. CKI_AttributePtr_Free(pAttributes);
  524. return(CKR_ATTRIBUTE_TYPE_INVALID);
  525. }
  526. }
  527. /* now put them somewhere... */
  528. log_printf("PKCS11_CreateX509CertificateObject: now creating object...\n");
  529. if (!pSession->pToken->ppTokenObject) {
  530. log_printf("PKCS11_CreateX509CertificateObject: initial malloc of objects space\n");
  531. pSession->pToken->ppTokenObject = (PKCS11_OBJECT **)malloc(sizeof(PKCS11_OBJECT *));
  532. pSession->pToken->ppTokenObject[0] = NULL;
  533. }
  534. ppTokenObject = pSession->pToken->ppTokenObject;
  535. for (i = 0; ppTokenObject[i]; i++)
  536. ;
  537. log_printf("PKCS11_CreateX509CertificateObject: create new object at index %d\n",i);
  538. ppTokenObject = (PKCS11_OBJECT **)realloc(ppTokenObject,sizeof(PKCS11_OBJECT *)*(i+2));
  539. if (!ppTokenObject)
  540. {
  541. log_printf("PKCS11_CreateX509CertificateObject: realloc failed for new object!\n");
  542. CKI_AttributePtr_Free(pAttributes);
  543. return(CKR_HOST_MEMORY);
  544. }
  545. pSession->pToken->ppTokenObject = ppTokenObject;
  546. ppTokenObject[i+1] = NULL;
  547. ppTokenObject[i] = PKCS11_Object_New();
  548. if (!ppTokenObject[i])
  549. {
  550. log_printf("PKCS11_CreateX509CertificateObject: PKCS11_Object_New failed!\n");
  551. CKI_AttributePtr_Free(pAttributes);
  552. return(CKR_HOST_MEMORY);
  553. }
  554. object = ppTokenObject[i];
  555. object->ulObjectHandle = PKCS11_NextObjectHandle();
  556. *pObject = object->ulObjectHandle;
  557. object->ulObjectClass = CKO_CERTIFICATE;
  558. object->pAttribute = pAttributes;
  559. log_printf("PKCS11_CreateX509CertificateObject: returning CKR_OK\n");
  560. return(CKR_OK);
  561. }
  562. #undef NUM_ATTRS
  563. #define NUM_ATTRS 21
  564. CK_RV PKCS11_CreateRSAPublicKeyObject(PKCS11_SESSION *pSession, CK_ATTRIBUTE_PTR pTemplate,
  565. CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR pObject) {
  566. int i;
  567. CK_ATTRIBUTE_PTR pAttributes, pTemp, attr;
  568. PKCS11_OBJECT ** ppTokenObject, *object;
  569. CK_BYTE empty = '\0';
  570. CK_ULONG zero = 0;
  571. CK_RV res;
  572. log_printf("entering CreateRSAPublicKey\n");
  573. pAttributes = malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  574. if (!pAttributes)
  575. return(CKR_HOST_MEMORY);
  576. /* set default values first */
  577. memset(pAttributes, 0, sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  578. i = 0;
  579. res = PKCS11_SetCommonObjectAttrs(pAttributes, CKO_PUBLIC_KEY, &i);
  580. if (res != CKR_OK) {
  581. log_printf("CreateRSAPublicKey: PKCS11_SetCommonObjectAttrs failed! (0x%08x)\n", res);
  582. CKI_AttributePtr_Free(pAttributes);
  583. return(res);
  584. }
  585. res = PKCS11_SetCommonKeyObjectAttrs(pAttributes, CKK_RSA, &i);
  586. if (res != CKR_OK) {
  587. log_printf("CreateRSAPublicKey: PKCS11_SetCommonKeyObjectAttrs failed! (0x%08x)\n", res);
  588. CKI_AttributePtr_Free(pAttributes);
  589. return(res);
  590. }
  591. res = PKCS11_SetCommonPublicKeyObjectAttrs(pAttributes, &i);
  592. if (res != CKR_OK) {
  593. log_printf("CreateRSAPublicKey: PKCS11_SetCommonPublicKeyObjectAttrs failed! (0x%08x)\n", res);
  594. CKI_AttributePtr_Free(pAttributes);
  595. return(res);
  596. }
  597. pAttributes[i].type = CKA_MODULUS;
  598. pAttributes[i].ulValueLen = 1L;
  599. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  600. if (res != CKR_OK) {
  601. log_printf("CreateRSAPublicKey: CKI_SetAttrValue failed for CKA_MODULUS! (0x%08x)\n", res);
  602. CKI_AttributePtr_Free(pAttributes);
  603. return(res);
  604. }
  605. i++;
  606. pAttributes[i].type = CKA_MODULUS_BITS;
  607. pAttributes[i].ulValueLen = sizeof(CK_ULONG);
  608. res = CKI_SetAttrValue(&(pAttributes[i]),&zero);
  609. if (res != CKR_OK) {
  610. log_printf("CreateRSAPublicKey: CKI_SetAttrValue failed for CKA_MODULUS_BITS! (0x%08x)\n", res);
  611. CKI_AttributePtr_Free(pAttributes);
  612. return(res);
  613. }
  614. i++;
  615. pAttributes[i].type = CKA_PUBLIC_EXPONENT;
  616. pAttributes[i].ulValueLen = 1L;
  617. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  618. if (res != CKR_OK) {
  619. log_printf("CreateRSAPublicKey: CKI_SetAttrValue failed for CKA_PUBLIC_EXPONENT! (0x%08x)\n", res);
  620. CKI_AttributePtr_Free(pAttributes);
  621. return(res);
  622. }
  623. i++;
  624. #ifdef DEBUG
  625. assert(i < NUM_ATTRS);
  626. #endif
  627. for (i = 0; pTemplate[i].value; i++) {
  628. attr = &(pTemplate[i]);
  629. switch (attr->type) {
  630. case CKA_CLASS:
  631. case CKA_KEY_TYPE:
  632. break;
  633. case CKA_TOKEN:
  634. case CKA_PRIVATE:
  635. case CKA_MODIFIABLE:
  636. case CKA_LABEL:
  637. case CKA_VALUE:
  638. case CKA_ID:
  639. case CKA_START_DATE:
  640. case CKA_END_DATE:
  641. case CKA_DERIVE:
  642. case CKA_LOCAL:
  643. case CKA_SUBJECT:
  644. case CKA_ENCRYPT:
  645. case CKA_VERIFY:
  646. case CKA_VERIFY_RECOVER:
  647. case CKA_WRAP:
  648. case CKA_MODULUS:
  649. case CKA_MODULUS_BITS:
  650. case CKA_PUBLIC_EXPONENT:
  651. pTemp = PKCS11_FindAttribute_p(pAttributes,attr->type);
  652. res = CKI_Attribute_Dup(pTemp,attr);
  653. if (res != CKR_OK) {
  654. log_printf("CreateRSAPublicKey: dup failed (0x%08x)\n", res);
  655. CKI_AttributePtr_Free(pAttributes);
  656. return(CKR_FUNCTION_FAILED);
  657. }
  658. break;
  659. default:
  660. log_printf("CreateRSAPublicKey: invalid attr type 0x%08x\n", attr->type);
  661. CKI_AttributePtr_Free(pAttributes);
  662. return(CKR_ATTRIBUTE_TYPE_INVALID);
  663. }
  664. }
  665. /* now put them somewhere... */
  666. if (!pSession->pToken->ppTokenObject) {
  667. log_printf("CreateRSAPublicKey: initial malloc of objects space\n");
  668. pSession->pToken->ppTokenObject = (PKCS11_OBJECT **)malloc(sizeof(PKCS11_OBJECT *));
  669. pSession->pToken->ppTokenObject[0] = NULL;
  670. }
  671. ppTokenObject = pSession->pToken->ppTokenObject;
  672. for (i = 0; ppTokenObject[i]; i++)
  673. ;
  674. log_printf("CreateRSAPublicKey: creating new object at index %d\n", i);
  675. ppTokenObject = (PKCS11_OBJECT **)realloc(ppTokenObject,sizeof(PKCS11_OBJECT *)*(i+2));
  676. if (!ppTokenObject)
  677. {
  678. log_printf("CreateRSAPublicKey: realloc failed for sessionobject\n");
  679. CKI_AttributePtr_Free(pAttributes);
  680. return(CKR_HOST_MEMORY);
  681. }
  682. pSession->pToken->ppTokenObject = ppTokenObject;
  683. ppTokenObject[i+1] = NULL;
  684. ppTokenObject[i] = PKCS11_Object_New();
  685. if (!ppTokenObject[i])
  686. {
  687. log_printf("CreateRSAPublicKey: PKCS11_Object_New failed!\n");
  688. CKI_AttributePtr_Free(pAttributes);
  689. return(CKR_HOST_MEMORY);
  690. }
  691. object = ppTokenObject[i];
  692. object->ulObjectHandle = PKCS11_NextObjectHandle();
  693. *pObject = object->ulObjectHandle;
  694. object->ulObjectClass = CKO_PUBLIC_KEY;
  695. object->pAttribute = pAttributes;
  696. log_printf("CreateRSAPublicKey: returning CKR_OK\n");
  697. return(CKR_OK);
  698. }
  699. #undef NUM_ATTRS
  700. #define NUM_ATTRS 29
  701. CK_RV PKCS11_CreateRSAPrivateKeyObject(PKCS11_SESSION *pSession, CK_ATTRIBUTE_PTR pTemplate,
  702. CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR pObject) {
  703. int i;
  704. CK_ATTRIBUTE_PTR pAttributes, pTemp, attr;
  705. PKCS11_OBJECT ** ppTokenObject, *object;
  706. CK_BYTE empty = '\0';
  707. CK_RV res;
  708. log_printf("entering PKCS11_CreateRSAPrivateKeyObject\n");
  709. pAttributes = malloc(sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  710. if (!pAttributes)
  711. {
  712. log_printf("PKCS11_CreateRSAPrivateKeyObject: could not allocate memory for attributes\n");
  713. return(CKR_HOST_MEMORY);
  714. }
  715. /* set default values first */
  716. memset(pAttributes, 0, sizeof(CK_ATTRIBUTE)*NUM_ATTRS);
  717. i = 0;
  718. res = PKCS11_SetCommonObjectAttrs(pAttributes, CKO_PRIVATE_KEY, &i);
  719. if (res != CKR_OK) {
  720. log_printf("PKCS11_CreateRSAPrivateKeyObject: PKCS11_SetCommonObjectAttrs failed! (0x%08x)\n", res);
  721. CKI_AttributePtr_Free(pAttributes);
  722. return(res);
  723. }
  724. res = PKCS11_SetCommonKeyObjectAttrs(pAttributes, CKK_RSA, &i);
  725. if (res != CKR_OK) {
  726. log_printf("PKCS11_CreateRSAPrivateKeyObject: PKCS11_SetCommonKeyObjectAttrs failed! (0x%08x)\n", res);
  727. CKI_AttributePtr_Free(pAttributes);
  728. return(res);
  729. }
  730. res = PKCS11_SetCommonPrivateKeyObjectAttrs(pAttributes, &i);
  731. if (res != CKR_OK) {
  732. log_printf("PKCS11_CreateRSAPrivateKeyObject: PKCS11_SetCommonPrivateKeyObjectAttrs failed! (0x%08x)\n", res);
  733. CKI_AttributePtr_Free(pAttributes);
  734. return(res);
  735. }
  736. pAttributes[i].type = CKA_MODULUS;
  737. pAttributes[i].ulValueLen = 1L;
  738. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  739. if (res != CKR_OK) {
  740. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_MODULUS! (0x%08x)\n", res);
  741. CKI_AttributePtr_Free(pAttributes);
  742. return(res);
  743. }
  744. i++;
  745. pAttributes[i].type = CKA_PUBLIC_EXPONENT;
  746. pAttributes[i].ulValueLen = 1L;
  747. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  748. if (res != CKR_OK) {
  749. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_PUBLIC_EXPONENT! (0x%08x)\n", res);
  750. CKI_AttributePtr_Free(pAttributes);
  751. return(res);
  752. }
  753. i++;
  754. pAttributes[i].type = CKA_PRIVATE_EXPONENT;
  755. pAttributes[i].ulValueLen = 1L;
  756. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  757. if (res != CKR_OK) {
  758. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_PRIVATE_EXPONENT! (0x%08x)\n", res);
  759. CKI_AttributePtr_Free(pAttributes);
  760. return(res);
  761. }
  762. i++;
  763. pAttributes[i].type = CKA_PRIME_1;
  764. pAttributes[i].ulValueLen = 1L;
  765. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  766. if (res != CKR_OK) {
  767. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_PRIME_1! (0x%08x)\n", res);
  768. CKI_AttributePtr_Free(pAttributes);
  769. return(res);
  770. }
  771. i++;
  772. pAttributes[i].type = CKA_PRIME_2;
  773. pAttributes[i].ulValueLen = 1L;
  774. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  775. if (res != CKR_OK) {
  776. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_PRIME_2! (0x%08x)\n", res);
  777. CKI_AttributePtr_Free(pAttributes);
  778. return(res);
  779. }
  780. i++;
  781. pAttributes[i].type = CKA_EXPONENT_1;
  782. pAttributes[i].ulValueLen = 1L;
  783. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  784. if (res != CKR_OK) {
  785. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_EXPONENT_1! (0x%08x)\n", res);
  786. CKI_AttributePtr_Free(pAttributes);
  787. return(res);
  788. }
  789. i++;
  790. pAttributes[i].type = CKA_EXPONENT_2;
  791. pAttributes[i].ulValueLen = 1L;
  792. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  793. if (res != CKR_OK) {
  794. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_EXPONENT_2! (0x%08x)\n", res);
  795. CKI_AttributePtr_Free(pAttributes);
  796. return(res);
  797. }
  798. i++;
  799. pAttributes[i].type = CKA_COEFFICIENT;
  800. pAttributes[i].ulValueLen = 1L;
  801. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  802. if (res != CKR_OK) {
  803. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_SetAttrValue failed for CKA_COEFFICIENT! (0x%08x)\n", res);
  804. CKI_AttributePtr_Free(pAttributes);
  805. return(res);
  806. }
  807. #ifdef DEBUG
  808. assert(i < NUM_ATTRS);
  809. #endif
  810. for (i = 0; (char *)pTemplate[i].value; i++) {
  811. attr = &(pTemplate[i]);
  812. switch (attr->type) {
  813. case CKA_CLASS:
  814. case CKA_KEY_TYPE:
  815. break;
  816. case CKA_TOKEN:
  817. case CKA_PRIVATE:
  818. case CKA_MODIFIABLE:
  819. case CKA_LABEL:
  820. case CKA_SUBJECT:
  821. case CKA_ID:
  822. case CKA_START_DATE:
  823. case CKA_END_DATE:
  824. case CKA_DERIVE:
  825. case CKA_LOCAL:
  826. case CKA_SENSITIVE:
  827. case CKA_DECRYPT:
  828. case CKA_SIGN:
  829. case CKA_SIGN_RECOVER:
  830. case CKA_UNWRAP:
  831. case CKA_EXTRACTABLE:
  832. case CKA_ALWAYS_SENSITIVE:
  833. case CKA_NEVER_EXTRACTABLE:
  834. case CKA_MODULUS:
  835. case CKA_PUBLIC_EXPONENT:
  836. case CKA_PRIVATE_EXPONENT:
  837. case CKA_PRIME_1:
  838. case CKA_PRIME_2:
  839. case CKA_EXPONENT_1:
  840. case CKA_EXPONENT_2:
  841. case CKA_COEFFICIENT:
  842. pTemp = PKCS11_FindAttribute_p(pAttributes,attr->type);
  843. res = CKI_Attribute_Dup(pTemp,attr);
  844. if (res != CKR_OK) {
  845. log_printf("PKCS11_CreateRSAPrivateKeyObject: CKI_AttrDup failed for type 0x%08x! (result 0x%08x)\n", attr->type, res);
  846. CKI_AttributePtr_Free(pAttributes);
  847. return(CKR_FUNCTION_FAILED);
  848. }
  849. break;
  850. default:
  851. log_printf("PKCS11_CreateRSAPrivateKeyObject: invalid attr 0x%08x\n",attr->type);
  852. CKI_AttributePtr_Free(pAttributes);
  853. return(CKR_ATTRIBUTE_TYPE_INVALID);
  854. }
  855. }
  856. /* now put them somewhere... */
  857. if (!pSession->pToken->ppTokenObject) {
  858. log_printf("PKCS11_CreateRSAPrivateKeyObject: initial malloc of objects space\n");
  859. pSession->pToken->ppTokenObject = (PKCS11_OBJECT **)malloc(sizeof(PKCS11_OBJECT *));
  860. pSession->pToken->ppTokenObject[0] = NULL;
  861. }
  862. ppTokenObject = pSession->pToken->ppTokenObject;
  863. for (i=0; ppTokenObject[i]; i++)
  864. ;
  865. log_printf("PKCS11_CreateRSAPrivateKeyObject: inserting new objects starting at index %d \n",i);
  866. ppTokenObject = (PKCS11_OBJECT **)realloc(ppTokenObject,sizeof(PKCS11_OBJECT *)*(i+2));
  867. if (!ppTokenObject)
  868. {
  869. log_printf("PKCS11_CreateRSAPrivateKeyObject: could not realloc space for session objects\n");
  870. CKI_AttributePtr_Free(pAttributes);
  871. return(CKR_HOST_MEMORY);
  872. }
  873. pSession->pToken->ppTokenObject = ppTokenObject;
  874. ppTokenObject[i+1] = NULL;
  875. ppTokenObject[i] = PKCS11_Object_New();
  876. if (!ppTokenObject[i])
  877. {
  878. log_printf("PKCS11_CreateRSAPrivateKeyObject: PKCS11_Object_New failed!\n");
  879. CKI_AttributePtr_Free(pAttributes);
  880. return(CKR_HOST_MEMORY);
  881. }
  882. object = ppTokenObject[i];
  883. object->ulObjectHandle = PKCS11_NextObjectHandle();
  884. object->ulSessionHandle = pSession->ulSessionHandle;
  885. *pObject = object->ulObjectHandle;
  886. object->ulObjectClass = CKO_PRIVATE_KEY;
  887. object->pAttribute = pAttributes;
  888. log_printf("PKCS11_CreateRSAPrivateKeyObject: returning CKR_OK\n");
  889. return(CKR_OK);
  890. }
  891. #undef NUM_ATTRS
  892. CK_RV PKCS11_CreateSecretKeyObject(PKCS11_SESSION *pSession, CK_ATTRIBUTE_PTR pTemplate,
  893. CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR pObject) {
  894. return(CKR_FUNCTION_NOT_SUPPORTED);
  895. }
  896. CK_RV PKCS11_CreateVendorDefinedObject(PKCS11_SESSION *pSession, CK_ATTRIBUTE_PTR pTemplate,
  897. CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR pObject) {
  898. return(CKR_FUNCTION_NOT_SUPPORTED);
  899. }
  900. CK_RV PKCS11_SetCommonObjectAttrs(CK_ATTRIBUTE_PTR pAttributes,
  901. CK_OBJECT_CLASS objectClass, int *ctr) {
  902. int i;
  903. CK_RV res;
  904. CK_BBOOL false = FALSE;
  905. CK_BBOOL true = TRUE;
  906. CK_BYTE empty = '\0';
  907. log_printf("entering PKCS11_SetCommonObjectAttrs\n");
  908. i = *ctr;
  909. pAttributes[i].type = CKA_CLASS;
  910. pAttributes[i].ulValueLen = sizeof(CK_OBJECT_CLASS);
  911. res = CKI_SetAttrValue(&(pAttributes[i]), &objectClass);
  912. if (res != CKR_OK)
  913. {
  914. log_printf("PKCS11_SetCommonObjectAttrs: error setting CKA_CLASS (0x%08x)\n", res);
  915. return(res);
  916. }
  917. i++;
  918. pAttributes[i].type = CKA_TOKEN;
  919. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  920. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  921. if (res != CKR_OK)
  922. {
  923. log_printf("PKCS11_SetCommonObjectAttrs: error setting CKA_TOKEN (0x%08x)\n", res);
  924. return(res);
  925. }
  926. i++;
  927. pAttributes[i].type = CKA_PRIVATE;
  928. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  929. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  930. if (res != CKR_OK)
  931. {
  932. log_printf("PKCS11_SetCommonObjectAttrs: error setting CKA_PRIVATE (0x%08x)\n", res);
  933. return(res);
  934. }
  935. i++;
  936. pAttributes[i].type = CKA_MODIFIABLE;
  937. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  938. res = CKI_SetAttrValue(&(pAttributes[i]),&true);
  939. if (res != CKR_OK)
  940. {
  941. log_printf("PKCS11_SetCommonObjectAttrs: error setting CKA_MODIFIABLE (0x%08x)\n", res);
  942. return(res);
  943. }
  944. i++;
  945. pAttributes[i].type = CKA_LABEL;
  946. pAttributes[i].ulValueLen = 1L;
  947. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  948. if (res != CKR_OK)
  949. {
  950. log_printf("PKCS11_SetCommonObjectAttrs: error setting CKA_LABEL (0x%08x)\n", res);
  951. return(res);
  952. }
  953. i++;
  954. *ctr = i;
  955. log_printf("PKCS11_SetCommonObjectAttrs: returning CKR_OK\n", res);
  956. return(CKR_OK);
  957. }
  958. void CKI_Date_Init(CK_DATE *Date) {
  959. log_printf("CKI_Date_Init: yr 0x%08x mo 0x%08x da 0x%08x\n",
  960. Date->year, Date->month, Date->day);
  961. memset(Date->year,' ',sizeof(Date->year));
  962. memset(Date->month,' ',sizeof(Date->month));
  963. memset(Date->day,' ',sizeof(Date->day));
  964. return;
  965. }
  966. CK_RV PKCS11_SetCommonKeyObjectAttrs(CK_ATTRIBUTE_PTR pAttributes, CK_KEY_TYPE keyType, int *ctr) {
  967. int i;
  968. CK_RV res;
  969. CK_BBOOL false = FALSE;
  970. CK_BYTE empty = '\0';
  971. CK_DATE date;
  972. CKI_Date_Init(&date);
  973. i = *ctr;
  974. pAttributes[i].type = CKA_KEY_TYPE;
  975. pAttributes[i].ulValueLen = sizeof(CK_KEY_TYPE);
  976. res = CKI_SetAttrValue(&(pAttributes[i]), &keyType);
  977. if (res != CKR_OK) return(res);
  978. i++;
  979. pAttributes[i].type = CKA_ID;
  980. pAttributes[i].ulValueLen = 1L;
  981. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  982. if (res != CKR_OK) return(res);
  983. i++;
  984. pAttributes[i].type = CKA_START_DATE;
  985. pAttributes[i].ulValueLen = sizeof(CK_DATE);
  986. res = CKI_SetAttrValue(&(pAttributes[i]),&date);
  987. if (res != CKR_OK) return(res);
  988. i++;
  989. pAttributes[i].type = CKA_END_DATE;
  990. pAttributes[i].ulValueLen = sizeof(CK_DATE);
  991. res = CKI_SetAttrValue(&(pAttributes[i]),&date);
  992. if (res != CKR_OK) return(res);
  993. i++;
  994. pAttributes[i].type = CKA_DERIVE;
  995. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  996. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  997. if (res != CKR_OK) return(res);
  998. i++;
  999. pAttributes[i].type = CKA_LOCAL;
  1000. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1001. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1002. if (res != CKR_OK) return(res);
  1003. i++;
  1004. *ctr = i;
  1005. return(CKR_OK);
  1006. }
  1007. CK_RV PKCS11_SetCommonPublicKeyObjectAttrs(CK_ATTRIBUTE_PTR pAttributes, int *ctr) {
  1008. int i;
  1009. CK_RV res;
  1010. CK_BBOOL false = FALSE;
  1011. CK_BYTE empty = '\0';
  1012. i = *ctr;
  1013. pAttributes[i].type = CKA_SUBJECT;
  1014. pAttributes[i].ulValueLen = 1L;
  1015. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  1016. if (res != CKR_OK) return(res);
  1017. i++;
  1018. pAttributes[i].type = CKA_ENCRYPT;
  1019. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1020. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1021. if (res != CKR_OK) return(res);
  1022. i++;
  1023. pAttributes[i].type = CKA_VERIFY;
  1024. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1025. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1026. if (res != CKR_OK) return(res);
  1027. i++;
  1028. pAttributes[i].type = CKA_VERIFY_RECOVER;
  1029. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1030. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1031. if (res != CKR_OK) return(res);
  1032. i++;
  1033. pAttributes[i].type = CKA_WRAP;
  1034. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1035. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1036. if (res != CKR_OK) return(res);
  1037. i++;
  1038. *ctr = i;
  1039. return(CKR_OK);
  1040. }
  1041. CK_RV PKCS11_SetCommonPrivateKeyObjectAttrs(CK_ATTRIBUTE_PTR pAttributes, int *ctr) {
  1042. int i;
  1043. CK_RV res;
  1044. CK_BBOOL false = FALSE;
  1045. CK_BYTE empty = '\0';
  1046. i = *ctr;
  1047. pAttributes[i].type = CKA_SUBJECT;
  1048. pAttributes[i].ulValueLen = 1L;
  1049. res = CKI_SetAttrValue(&(pAttributes[i]),&empty);
  1050. if (res != CKR_OK) return(res);
  1051. i++;
  1052. pAttributes[i].type = CKA_SENSITIVE;
  1053. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1054. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1055. if (res != CKR_OK) return(res);
  1056. i++;
  1057. pAttributes[i].type = CKA_DECRYPT;
  1058. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1059. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1060. if (res != CKR_OK) return(res);
  1061. i++;
  1062. pAttributes[i].type = CKA_SIGN;
  1063. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1064. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1065. if (res != CKR_OK) return(res);
  1066. i++;
  1067. pAttributes[i].type = CKA_SIGN_RECOVER;
  1068. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1069. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1070. if (res != CKR_OK) return(res);
  1071. i++;
  1072. pAttributes[i].type = CKA_UNWRAP;
  1073. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1074. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1075. if (res != CKR_OK) return(res);
  1076. i++;
  1077. pAttributes[i].type = CKA_EXTRACTABLE;
  1078. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1079. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1080. if (res != CKR_OK) return(res);
  1081. i++;
  1082. pAttributes[i].type = CKA_ALWAYS_SENSITIVE;
  1083. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1084. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1085. if (res != CKR_OK) return(res);
  1086. i++;
  1087. pAttributes[i].type = CKA_NEVER_EXTRACTABLE;
  1088. pAttributes[i].ulValueLen = sizeof(CK_BBOOL);
  1089. res = CKI_SetAttrValue(&(pAttributes[i]),&false);
  1090. if (res != CKR_OK) return(res);
  1091. i++;
  1092. *ctr = i;
  1093. return(CKR_OK);
  1094. }
  1095. /* expects pAttribute->type and pAttribute->ulValueLen to be set */
  1096. CK_RV CKI_SetAttrValue_nf(CK_ATTRIBUTE_PTR pAttribute,CK_VOID_PTR pValue) {
  1097. CK_RV res;
  1098. log_printf("entering CKI_SetAttrValue_nf with type 0x%08x, len %03ld\n",
  1099. pAttribute->type, pAttribute->ulValueLen);
  1100. switch (pAttribute->type) {
  1101. case CKA_TOKEN:
  1102. case CKA_PRIVATE:
  1103. case CKA_MODIFIABLE:
  1104. case CKA_DERIVE:
  1105. case CKA_LOCAL:
  1106. case CKA_ENCRYPT:
  1107. case CKA_VERIFY:
  1108. case CKA_VERIFY_RECOVER:
  1109. case CKA_WRAP:
  1110. case CKA_SENSITIVE:
  1111. case CKA_DECRYPT:
  1112. case CKA_SIGN:
  1113. case CKA_SIGN_RECOVER:
  1114. case CKA_UNWRAP:
  1115. case CKA_EXTRACTABLE:
  1116. case CKA_ALWAYS_SENSITIVE:
  1117. case CKA_NEVER_EXTRACTABLE:
  1118. *(CK_BBOOL *)pAttribute->value = *(CK_BBOOL *)pValue;
  1119. break;
  1120. case CKA_LABEL:
  1121. case CKA_APPLICATION:
  1122. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1123. break;
  1124. case CKA_CERTIFICATE_TYPE:
  1125. *(CK_CERTIFICATE_TYPE *)pAttribute->value = *(CK_CERTIFICATE_TYPE *)pValue;
  1126. break;
  1127. case CKA_VALUE: /* Bigint and BYTE_PTR are the same now; later, FIXME. */
  1128. case CKA_SUBJECT:
  1129. case CKA_ID:
  1130. case CKA_ISSUER:
  1131. case CKA_SERIAL_NUMBER:
  1132. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1133. break;
  1134. case CKA_KEY_TYPE:
  1135. *(CK_KEY_TYPE *)pAttribute->value = *(CK_KEY_TYPE *)pValue;
  1136. break;
  1137. case CKA_MODULUS:
  1138. case CKA_PUBLIC_EXPONENT:
  1139. case CKA_PRIME:
  1140. case CKA_SUBPRIME:
  1141. case CKA_BASE:
  1142. case CKA_PRIVATE_EXPONENT:
  1143. case CKA_PRIME_1:
  1144. case CKA_PRIME_2:
  1145. case CKA_EXPONENT_1:
  1146. case CKA_EXPONENT_2:
  1147. case CKA_COEFFICIENT:
  1148. /* these are 'bigints' */
  1149. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1150. break;
  1151. case CKA_CLASS:
  1152. case CKA_MODULUS_BITS:
  1153. case CKA_VALUE_BITS:
  1154. case CKA_VALUE_LEN:
  1155. *(CK_ULONG_PTR)pAttribute->value = *(CK_ULONG_PTR)pValue;
  1156. break;
  1157. case CKA_START_DATE:
  1158. case CKA_END_DATE:
  1159. res = CKI_Date_Dup(pAttribute->value,(CK_DATE *)pValue);
  1160. if (res != CKR_OK)
  1161. {
  1162. log_printf("CKI_SetAttrValue_nf: error processing START_DATE, END_DATE (0x%08x)\n", res);
  1163. return(res);
  1164. }
  1165. break;
  1166. case CKA_VENDOR_DEFINED:
  1167. default:
  1168. return(CKR_FUNCTION_NOT_SUPPORTED);
  1169. }
  1170. display_attribute(pAttribute);
  1171. return(CKR_OK);
  1172. }
  1173. /* expects pAttribute->type and pAttribute->ulValueLen to be set;
  1174. if pAttribute->value is set, it is freed first */
  1175. CK_RV CKI_SetAttrValue(CK_ATTRIBUTE_PTR pAttribute,CK_VOID_PTR pValue) {
  1176. CK_RV res;
  1177. log_printf("entering CKI_SetAttrValue with type 0x%08x, len %03ld, value 0x%08x\n",
  1178. pAttribute->type, pAttribute->ulValueLen, pAttribute->value);
  1179. if (pAttribute->value) {
  1180. free(pAttribute->value);
  1181. pAttribute->value = NULL;
  1182. }
  1183. switch (pAttribute->type) {
  1184. case CKA_TOKEN:
  1185. case CKA_PRIVATE:
  1186. case CKA_MODIFIABLE:
  1187. case CKA_DERIVE:
  1188. case CKA_LOCAL:
  1189. case CKA_ENCRYPT:
  1190. case CKA_VERIFY:
  1191. case CKA_VERIFY_RECOVER:
  1192. case CKA_WRAP:
  1193. case CKA_SENSITIVE:
  1194. case CKA_DECRYPT:
  1195. case CKA_SIGN:
  1196. case CKA_SIGN_RECOVER:
  1197. case CKA_UNWRAP:
  1198. case CKA_EXTRACTABLE:
  1199. case CKA_ALWAYS_SENSITIVE:
  1200. case CKA_NEVER_EXTRACTABLE:
  1201. pAttribute->value = (CK_BBOOL *)malloc(pAttribute->ulValueLen);
  1202. *(CK_BBOOL *)pAttribute->value = *(CK_BBOOL *)pValue;
  1203. break;
  1204. case CKA_LABEL:
  1205. case CKA_APPLICATION:
  1206. pAttribute->value = (CK_CHAR_PTR)malloc(pAttribute->ulValueLen);
  1207. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1208. break;
  1209. case CKA_CERTIFICATE_TYPE:
  1210. pAttribute->value = (CK_CERTIFICATE_TYPE *)malloc(pAttribute->ulValueLen);
  1211. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1212. break;
  1213. case CKA_VALUE: /* Bigint and BYTE_PTR are the same now; later, FIXME. */
  1214. case CKA_SUBJECT:
  1215. case CKA_ID:
  1216. case CKA_ISSUER:
  1217. case CKA_SERIAL_NUMBER:
  1218. pAttribute->value = (CK_BYTE_PTR)malloc(pAttribute->ulValueLen);
  1219. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1220. break;
  1221. case CKA_KEY_TYPE:
  1222. pAttribute->value = (CK_KEY_TYPE *)malloc(pAttribute->ulValueLen);
  1223. *(CK_KEY_TYPE *)pAttribute->value = *(CK_KEY_TYPE *)pValue;
  1224. break;
  1225. case CKA_MODULUS:
  1226. case CKA_PUBLIC_EXPONENT:
  1227. case CKA_PRIME:
  1228. case CKA_SUBPRIME:
  1229. case CKA_BASE:
  1230. case CKA_PRIVATE_EXPONENT:
  1231. case CKA_PRIME_1:
  1232. case CKA_PRIME_2:
  1233. case CKA_EXPONENT_1:
  1234. case CKA_EXPONENT_2:
  1235. case CKA_COEFFICIENT:
  1236. /* these are 'bigints' */
  1237. pAttribute->value = (CK_BYTE_PTR)malloc(pAttribute->ulValueLen);
  1238. memcpy(pAttribute->value,pValue,pAttribute->ulValueLen);
  1239. break;
  1240. case CKA_CLASS:
  1241. case CKA_MODULUS_BITS:
  1242. case CKA_VALUE_BITS:
  1243. case CKA_VALUE_LEN:
  1244. pAttribute->value = (CK_ULONG_PTR)malloc(pAttribute->ulValueLen);
  1245. *(CK_ULONG_PTR)pAttribute->value = *(CK_ULONG_PTR)pValue;
  1246. break;
  1247. case CKA_START_DATE:
  1248. case CKA_END_DATE:
  1249. pAttribute->value = (CK_DATE *)malloc(pAttribute->ulValueLen);
  1250. res = CKI_Date_Dup(pAttribute->value,(CK_DATE *)pValue);
  1251. if (res != CKR_OK) return(res);
  1252. break;
  1253. case CKA_VENDOR_DEFINED:
  1254. default:
  1255. return(CKR_FUNCTION_NOT_SUPPORTED);
  1256. }
  1257. return(CKR_OK);
  1258. }