/security/nss/lib/smime/cmsudf.c

http://github.com/zpao/v8monkey · C · 480 lines · 342 code · 55 blank · 83 comment · 66 complexity · 44f38f8edcc61133abc98fbdb54c3764 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. /*
  37. * CMS User Define Types
  38. *
  39. * $Id: cmsudf.c,v 1.3 2011/02/11 01:53:17 emaldona%redhat.com Exp $
  40. */
  41. #include "cmslocal.h"
  42. #include "prinit.h"
  43. #include "pk11func.h"
  44. #include "secitem.h"
  45. #include "secoid.h"
  46. #include "secerr.h"
  47. #include "nss.h"
  48. typedef struct nsscmstypeInfoStr nsscmstypeInfo;
  49. struct nsscmstypeInfoStr {
  50. SECOidTag type;
  51. SEC_ASN1Template *template;
  52. size_t size;
  53. PRBool isData;
  54. NSSCMSGenericWrapperDataDestroy destroy;
  55. NSSCMSGenericWrapperDataCallback decode_before;
  56. NSSCMSGenericWrapperDataCallback decode_after;
  57. NSSCMSGenericWrapperDataCallback decode_end;
  58. NSSCMSGenericWrapperDataCallback encode_start;
  59. NSSCMSGenericWrapperDataCallback encode_before;
  60. NSSCMSGenericWrapperDataCallback encode_after;
  61. };
  62. /* make sure the global tables are only initialized once */
  63. static PRCallOnceType nsscmstypeOnce;
  64. static PRCallOnceType nsscmstypeClearOnce;
  65. /* lock for adding a new entry */
  66. static PRLock *nsscmstypeAddLock;
  67. /* lock for the hash table */
  68. static PRLock *nsscmstypeHashLock;
  69. /* the hash table itself */
  70. static PLHashTable *nsscmstypeHash;
  71. /* arena to hold all the hash table data */
  72. static PRArenaPool *nsscmstypeArena;
  73. /*
  74. * clean up our global tables
  75. */
  76. SECStatus
  77. nss_cmstype_shutdown(void *appData, void *reserved)
  78. {
  79. if (nsscmstypeHashLock) {
  80. PR_Lock(nsscmstypeHashLock);
  81. }
  82. if (nsscmstypeHash) {
  83. PL_HashTableDestroy(nsscmstypeHash);
  84. nsscmstypeHash = NULL;
  85. }
  86. if (nsscmstypeArena) {
  87. PORT_FreeArena(nsscmstypeArena, PR_FALSE);
  88. nsscmstypeArena = NULL;
  89. }
  90. if (nsscmstypeAddLock) {
  91. PR_DestroyLock(nsscmstypeAddLock);
  92. }
  93. if (nsscmstypeHashLock) {
  94. PRLock *oldLock = nsscmstypeHashLock;
  95. nsscmstypeHashLock = NULL;
  96. PR_Unlock(oldLock);
  97. PR_DestroyLock(oldLock);
  98. }
  99. /* don't clear out the PR_ONCE data if we failed our inital call */
  100. if (appData == NULL) {
  101. nsscmstypeOnce = nsscmstypeClearOnce;
  102. }
  103. return SECSuccess;
  104. }
  105. static PLHashNumber
  106. nss_cmstype_hash_key(const void *key)
  107. {
  108. return (PLHashNumber) key;
  109. }
  110. static PRIntn
  111. nss_cmstype_compare_keys(const void *v1, const void *v2)
  112. {
  113. PLHashNumber value1 = (PLHashNumber) v1;
  114. PLHashNumber value2 = (PLHashNumber) v2;
  115. return (value1 == value2);
  116. }
  117. /*
  118. * initialize our hash tables, called once on the first attemat to register
  119. * a new SMIME type.
  120. */
  121. static PRStatus
  122. nss_cmstype_init(void)
  123. {
  124. SECStatus rv;
  125. nsscmstypeHashLock = PR_NewLock();
  126. if (nsscmstypeHashLock == NULL) {
  127. return PR_FAILURE;
  128. }
  129. nsscmstypeAddLock = PR_NewLock();
  130. if (nsscmstypeHashLock == NULL) {
  131. goto fail;
  132. }
  133. nsscmstypeHash = PL_NewHashTable(64, nss_cmstype_hash_key,
  134. nss_cmstype_compare_keys, PL_CompareValues, NULL, NULL);
  135. if (nsscmstypeHash == NULL) {
  136. goto fail;
  137. }
  138. nsscmstypeArena = PORT_NewArena(2048);
  139. if (nsscmstypeArena == NULL) {
  140. goto fail;
  141. }
  142. rv = NSS_RegisterShutdown(nss_cmstype_shutdown, NULL);
  143. if (rv != SECSuccess) {
  144. goto fail;
  145. }
  146. return PR_SUCCESS;
  147. fail:
  148. nss_cmstype_shutdown(&nsscmstypeOnce, NULL);
  149. return PR_FAILURE;
  150. }
  151. /*
  152. * look up and registered SIME type
  153. */
  154. static const nsscmstypeInfo *
  155. nss_cmstype_lookup(SECOidTag type)
  156. {
  157. nsscmstypeInfo *typeInfo = NULL;;
  158. if (!nsscmstypeHash) {
  159. return NULL;
  160. }
  161. PR_Lock(nsscmstypeHashLock);
  162. if (nsscmstypeHash) {
  163. typeInfo = PL_HashTableLookupConst(nsscmstypeHash, (void *)type);
  164. }
  165. PR_Unlock(nsscmstypeHashLock);
  166. return typeInfo;
  167. }
  168. /*
  169. * add a new type to the SMIME type table
  170. */
  171. static SECStatus
  172. nss_cmstype_add(SECOidTag type, nsscmstypeInfo *typeinfo)
  173. {
  174. PLHashEntry *entry;
  175. if (!nsscmstypeHash) {
  176. /* assert? this shouldn't happen */
  177. return SECFailure;
  178. }
  179. PR_Lock(nsscmstypeHashLock);
  180. /* this is really paranoia. If we really are racing nsscmstypeHash, we'll
  181. * also be racing nsscmstypeHashLock... */
  182. if (!nsscmstypeHash) {
  183. PR_Unlock(nsscmstypeHashLock);
  184. return SECFailure;
  185. }
  186. entry = PL_HashTableAdd(nsscmstypeHash, (void *)type, typeinfo);
  187. PR_Unlock(nsscmstypeHashLock);
  188. return entry ? SECSuccess : SECFailure;
  189. }
  190. /* helper functions to manage new content types
  191. */
  192. PRBool
  193. NSS_CMSType_IsWrapper(SECOidTag type)
  194. {
  195. const nsscmstypeInfo *typeInfo = NULL;
  196. switch (type) {
  197. case SEC_OID_PKCS7_SIGNED_DATA:
  198. case SEC_OID_PKCS7_ENVELOPED_DATA:
  199. case SEC_OID_PKCS7_DIGESTED_DATA:
  200. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  201. return PR_TRUE;
  202. default:
  203. typeInfo = nss_cmstype_lookup(type);
  204. if (typeInfo && !typeInfo->isData) {
  205. return PR_TRUE;
  206. }
  207. }
  208. return PR_FALSE;
  209. }
  210. PRBool
  211. NSS_CMSType_IsData(SECOidTag type)
  212. {
  213. const nsscmstypeInfo *typeInfo = NULL;
  214. switch (type) {
  215. case SEC_OID_PKCS7_DATA:
  216. return PR_TRUE;
  217. default:
  218. typeInfo = nss_cmstype_lookup(type);
  219. if (typeInfo && typeInfo->isData) {
  220. return PR_TRUE;
  221. }
  222. }
  223. return PR_FALSE;
  224. }
  225. const SEC_ASN1Template *
  226. NSS_CMSType_GetTemplate(SECOidTag type)
  227. {
  228. const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
  229. if (typeInfo && typeInfo->template) {
  230. return typeInfo->template;
  231. }
  232. return SEC_ASN1_GET(SEC_PointerToOctetStringTemplate);
  233. }
  234. size_t
  235. NSS_CMSType_GetContentSize(SECOidTag type)
  236. {
  237. const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
  238. if (typeInfo) {
  239. return typeInfo->size;
  240. }
  241. return sizeof(SECItem *);
  242. }
  243. void
  244. NSS_CMSGenericWrapperData_Destroy(SECOidTag type, NSSCMSGenericWrapperData *gd)
  245. {
  246. const nsscmstypeInfo *typeInfo = nss_cmstype_lookup(type);
  247. if (typeInfo && typeInfo->destroy) {
  248. (*typeInfo->destroy)(gd);
  249. }
  250. }
  251. SECStatus
  252. NSS_CMSGenericWrapperData_Decode_BeforeData(SECOidTag type,
  253. NSSCMSGenericWrapperData *gd)
  254. {
  255. const nsscmstypeInfo *typeInfo;
  256. /* short cut common case */
  257. if (type == SEC_OID_PKCS7_DATA) {
  258. return SECSuccess;
  259. }
  260. typeInfo = nss_cmstype_lookup(type);
  261. if (typeInfo) {
  262. if (typeInfo->decode_before) {
  263. return (*typeInfo->decode_before)(gd);
  264. }
  265. /* decoder ops optional for data tags */
  266. if (typeInfo->isData) {
  267. return SECSuccess;
  268. }
  269. }
  270. /* expected a function, but none existed */
  271. return SECFailure;
  272. }
  273. SECStatus
  274. NSS_CMSGenericWrapperData_Decode_AfterData(SECOidTag type,
  275. NSSCMSGenericWrapperData *gd)
  276. {
  277. const nsscmstypeInfo *typeInfo;
  278. /* short cut common case */
  279. if (type == SEC_OID_PKCS7_DATA) {
  280. return SECSuccess;
  281. }
  282. typeInfo = nss_cmstype_lookup(type);
  283. if (typeInfo) {
  284. if (typeInfo->decode_after) {
  285. return (*typeInfo->decode_after)(gd);
  286. }
  287. /* decoder ops optional for data tags */
  288. if (typeInfo->isData) {
  289. return SECSuccess;
  290. }
  291. }
  292. /* expected a function, but none existed */
  293. return SECFailure;
  294. }
  295. SECStatus
  296. NSS_CMSGenericWrapperData_Decode_AfterEnd(SECOidTag type,
  297. NSSCMSGenericWrapperData *gd)
  298. {
  299. const nsscmstypeInfo *typeInfo;
  300. /* short cut common case */
  301. if (type == SEC_OID_PKCS7_DATA) {
  302. return SECSuccess;
  303. }
  304. typeInfo = nss_cmstype_lookup(type);
  305. if (typeInfo) {
  306. if (typeInfo->decode_end) {
  307. return (*typeInfo->decode_end)(gd);
  308. }
  309. /* decoder ops optional for data tags */
  310. if (typeInfo->isData) {
  311. return SECSuccess;
  312. }
  313. }
  314. /* expected a function, but none existed */
  315. return SECFailure;
  316. }
  317. SECStatus
  318. NSS_CMSGenericWrapperData_Encode_BeforeStart(SECOidTag type,
  319. NSSCMSGenericWrapperData *gd)
  320. {
  321. const nsscmstypeInfo *typeInfo;
  322. /* short cut common case */
  323. if (type == SEC_OID_PKCS7_DATA) {
  324. return SECSuccess;
  325. }
  326. typeInfo = nss_cmstype_lookup(type);
  327. if (typeInfo) {
  328. if (typeInfo->encode_start) {
  329. return (*typeInfo->encode_start)(gd);
  330. }
  331. /* decoder ops optional for data tags */
  332. if (typeInfo->isData) {
  333. return SECSuccess;
  334. }
  335. }
  336. /* expected a function, but none existed */
  337. return SECFailure;
  338. }
  339. SECStatus
  340. NSS_CMSGenericWrapperData_Encode_BeforeData(SECOidTag type,
  341. NSSCMSGenericWrapperData *gd)
  342. {
  343. const nsscmstypeInfo *typeInfo;
  344. /* short cut common case */
  345. if (type == SEC_OID_PKCS7_DATA) {
  346. return SECSuccess;
  347. }
  348. typeInfo = nss_cmstype_lookup(type);
  349. if (typeInfo) {
  350. if (typeInfo->encode_before) {
  351. return (*typeInfo->encode_before)(gd);
  352. }
  353. /* decoder ops optional for data tags */
  354. if (typeInfo->isData) {
  355. return SECSuccess;
  356. }
  357. }
  358. /* expected a function, but none existed */
  359. return SECFailure;
  360. }
  361. SECStatus
  362. NSS_CMSGenericWrapperData_Encode_AfterData(SECOidTag type,
  363. NSSCMSGenericWrapperData *gd)
  364. {
  365. const nsscmstypeInfo *typeInfo;
  366. /* short cut common case */
  367. if (type == SEC_OID_PKCS7_DATA) {
  368. return SECSuccess;
  369. }
  370. typeInfo = nss_cmstype_lookup(type);
  371. if (typeInfo) {
  372. if (typeInfo->encode_after) {
  373. return (*typeInfo->encode_after)(gd);
  374. }
  375. /* decoder ops optional for data tags */
  376. if (typeInfo->isData) {
  377. return SECSuccess;
  378. }
  379. }
  380. /* expected a function, but none existed */
  381. return SECFailure;
  382. }
  383. SECStatus
  384. NSS_CMSType_RegisterContentType(SECOidTag type,
  385. SEC_ASN1Template *asn1Template, size_t size,
  386. NSSCMSGenericWrapperDataDestroy destroy,
  387. NSSCMSGenericWrapperDataCallback decode_before,
  388. NSSCMSGenericWrapperDataCallback decode_after,
  389. NSSCMSGenericWrapperDataCallback decode_end,
  390. NSSCMSGenericWrapperDataCallback encode_start,
  391. NSSCMSGenericWrapperDataCallback encode_before,
  392. NSSCMSGenericWrapperDataCallback encode_after,
  393. PRBool isData)
  394. {
  395. PRStatus rc;
  396. SECStatus rv;
  397. nsscmstypeInfo *typeInfo;
  398. const nsscmstypeInfo *exists;
  399. rc = PR_CallOnce( &nsscmstypeOnce, nss_cmstype_init);
  400. if (rc == PR_FAILURE) {
  401. return SECFailure;
  402. }
  403. PR_Lock(nsscmstypeAddLock);
  404. exists = nss_cmstype_lookup(type);
  405. if (exists) {
  406. PR_Unlock(nsscmstypeAddLock);
  407. /* already added */
  408. return SECSuccess;
  409. }
  410. typeInfo = PORT_ArenaNew(nsscmstypeArena, nsscmstypeInfo);
  411. typeInfo->type = type;
  412. typeInfo->size = size;
  413. typeInfo->isData = isData;
  414. typeInfo->template = asn1Template;
  415. typeInfo->destroy = destroy;
  416. typeInfo->decode_before = decode_before;
  417. typeInfo->decode_after = decode_after;
  418. typeInfo->decode_end = decode_end;
  419. typeInfo->encode_start = encode_start;
  420. typeInfo->encode_before = encode_before;
  421. typeInfo->encode_after = encode_after;
  422. rv = nss_cmstype_add(type, typeInfo);
  423. PR_Unlock(nsscmstypeAddLock);
  424. return rv;
  425. }