cmscinfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * CMS contentInfo methods.
  6. */
  7. #include "cmslocal.h"
  8. #include "pk11func.h"
  9. #include "secitem.h"
  10. #include "secoid.h"
  11. #include "secerr.h"
  12. /*
  13. * NSS_CMSContentInfo_Create - create a content info
  14. *
  15. * version is set in the _Finalize procedures for each content type
  16. */
  17. SECStatus
  18. NSS_CMSContentInfo_Private_Init(NSSCMSContentInfo *cinfo)
  19. {
  20. if (cinfo->privateInfo) {
  21. return SECSuccess;
  22. }
  23. cinfo->privateInfo = PORT_ZNew(NSSCMSContentInfoPrivate);
  24. return (cinfo->privateInfo) ? SECSuccess : SECFailure;
  25. }
  26. static void
  27. nss_cmsContentInfo_private_destroy(NSSCMSContentInfoPrivate *privateInfo)
  28. {
  29. if (privateInfo->digcx) {
  30. /* must destroy digest objects */
  31. NSS_CMSDigestContext_Cancel(privateInfo->digcx);
  32. privateInfo->digcx = NULL;
  33. }
  34. if (privateInfo->ciphcx) {
  35. NSS_CMSCipherContext_Destroy(privateInfo->ciphcx);
  36. privateInfo->ciphcx = NULL;
  37. }
  38. PORT_Free(privateInfo);
  39. }
  40. /*
  41. * NSS_CMSContentInfo_Destroy - destroy a CMS contentInfo and all of its sub-pieces.
  42. */
  43. void
  44. NSS_CMSContentInfo_Destroy(NSSCMSContentInfo *cinfo)
  45. {
  46. SECOidTag kind;
  47. if (cinfo == NULL) {
  48. return;
  49. }
  50. kind = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  51. switch (kind) {
  52. case SEC_OID_PKCS7_ENVELOPED_DATA:
  53. NSS_CMSEnvelopedData_Destroy(cinfo->content.envelopedData);
  54. break;
  55. case SEC_OID_PKCS7_SIGNED_DATA:
  56. NSS_CMSSignedData_Destroy(cinfo->content.signedData);
  57. break;
  58. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  59. NSS_CMSEncryptedData_Destroy(cinfo->content.encryptedData);
  60. break;
  61. case SEC_OID_PKCS7_DIGESTED_DATA:
  62. NSS_CMSDigestedData_Destroy(cinfo->content.digestedData);
  63. break;
  64. default:
  65. NSS_CMSGenericWrapperData_Destroy(kind, cinfo->content.genericData);
  66. /* XXX Anything else that needs to be "manually" freed/destroyed? */
  67. break;
  68. }
  69. if (cinfo->privateInfo) {
  70. nss_cmsContentInfo_private_destroy(cinfo->privateInfo);
  71. cinfo->privateInfo = NULL;
  72. }
  73. if (cinfo->bulkkey) {
  74. PK11_FreeSymKey(cinfo->bulkkey);
  75. }
  76. }
  77. /*
  78. * NSS_CMSContentInfo_GetChildContentInfo - get content's contentInfo (if it exists)
  79. */
  80. NSSCMSContentInfo *
  81. NSS_CMSContentInfo_GetChildContentInfo(NSSCMSContentInfo *cinfo)
  82. {
  83. NSSCMSContentInfo *ccinfo = NULL;
  84. if (cinfo == NULL) {
  85. return NULL;
  86. }
  87. SECOidTag tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  88. switch (tag) {
  89. case SEC_OID_PKCS7_SIGNED_DATA:
  90. if (cinfo->content.signedData != NULL) {
  91. ccinfo = &(cinfo->content.signedData->contentInfo);
  92. }
  93. break;
  94. case SEC_OID_PKCS7_ENVELOPED_DATA:
  95. if (cinfo->content.envelopedData != NULL) {
  96. ccinfo = &(cinfo->content.envelopedData->contentInfo);
  97. }
  98. break;
  99. case SEC_OID_PKCS7_DIGESTED_DATA:
  100. if (cinfo->content.digestedData != NULL) {
  101. ccinfo = &(cinfo->content.digestedData->contentInfo);
  102. }
  103. break;
  104. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  105. if (cinfo->content.encryptedData != NULL) {
  106. ccinfo = &(cinfo->content.encryptedData->contentInfo);
  107. }
  108. break;
  109. case SEC_OID_PKCS7_DATA:
  110. default:
  111. if (NSS_CMSType_IsWrapper(tag)) {
  112. if (cinfo->content.genericData != NULL) {
  113. ccinfo = &(cinfo->content.genericData->contentInfo);
  114. }
  115. }
  116. break;
  117. }
  118. if (ccinfo && !ccinfo->privateInfo) {
  119. NSS_CMSContentInfo_Private_Init(ccinfo);
  120. }
  121. return ccinfo;
  122. }
  123. SECStatus
  124. NSS_CMSContentInfo_SetDontStream(NSSCMSContentInfo *cinfo, PRBool dontStream)
  125. {
  126. SECStatus rv;
  127. if (cinfo == NULL) {
  128. return SECFailure;
  129. }
  130. rv = NSS_CMSContentInfo_Private_Init(cinfo);
  131. if (rv != SECSuccess) {
  132. /* default is streaming, failure to get ccinfo will not effect this */
  133. return dontStream ? SECFailure : SECSuccess;
  134. }
  135. cinfo->privateInfo->dontStream = dontStream;
  136. return SECSuccess;
  137. }
  138. /*
  139. * NSS_CMSContentInfo_SetContent - set content type & content
  140. */
  141. SECStatus
  142. NSS_CMSContentInfo_SetContent(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo,
  143. SECOidTag type, void *ptr)
  144. {
  145. SECStatus rv;
  146. if (cinfo == NULL || cmsg == NULL) {
  147. return SECFailure;
  148. }
  149. cinfo->contentTypeTag = SECOID_FindOIDByTag(type);
  150. if (cinfo->contentTypeTag == NULL) {
  151. return SECFailure;
  152. }
  153. /* do not copy the oid, just create a reference */
  154. rv = SECITEM_CopyItem(cmsg->poolp, &(cinfo->contentType), &(cinfo->contentTypeTag->oid));
  155. if (rv != SECSuccess) {
  156. return SECFailure;
  157. }
  158. cinfo->content.pointer = ptr;
  159. if (NSS_CMSType_IsData(type) && ptr) {
  160. cinfo->rawContent = ptr;
  161. } else {
  162. /* as we always have some inner data,
  163. * we need to set it to something, just to fool the encoder enough to work on it
  164. * and get us into nss_cms_encoder_notify at that point */
  165. cinfo->rawContent = SECITEM_AllocItem(cmsg->poolp, NULL, 1);
  166. if (cinfo->rawContent == NULL) {
  167. PORT_SetError(SEC_ERROR_NO_MEMORY);
  168. return SECFailure;
  169. }
  170. }
  171. return SECSuccess;
  172. }
  173. /*
  174. * NSS_CMSContentInfo_SetContent_XXXX - typesafe wrappers for NSS_CMSContentInfo_SetContent
  175. */
  176. /*
  177. * data == NULL -> pass in data via NSS_CMSEncoder_Update
  178. * data != NULL -> take this data
  179. */
  180. SECStatus
  181. NSS_CMSContentInfo_SetContent_Data(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo,
  182. SECItem *data, PRBool detached)
  183. {
  184. if (NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DATA, (void *)data) != SECSuccess) {
  185. return SECFailure;
  186. }
  187. if (detached) {
  188. cinfo->rawContent = NULL;
  189. }
  190. return SECSuccess;
  191. }
  192. SECStatus
  193. NSS_CMSContentInfo_SetContent_SignedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo,
  194. NSSCMSSignedData *sigd)
  195. {
  196. return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_SIGNED_DATA, (void *)sigd);
  197. }
  198. SECStatus
  199. NSS_CMSContentInfo_SetContent_EnvelopedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo,
  200. NSSCMSEnvelopedData *envd)
  201. {
  202. return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENVELOPED_DATA, (void *)envd);
  203. }
  204. SECStatus
  205. NSS_CMSContentInfo_SetContent_DigestedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo,
  206. NSSCMSDigestedData *digd)
  207. {
  208. return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_DIGESTED_DATA, (void *)digd);
  209. }
  210. SECStatus
  211. NSS_CMSContentInfo_SetContent_EncryptedData(NSSCMSMessage *cmsg, NSSCMSContentInfo *cinfo,
  212. NSSCMSEncryptedData *encd)
  213. {
  214. return NSS_CMSContentInfo_SetContent(cmsg, cinfo, SEC_OID_PKCS7_ENCRYPTED_DATA, (void *)encd);
  215. }
  216. /*
  217. * NSS_CMSContentInfo_GetContent - get pointer to inner content
  218. *
  219. * needs to be casted...
  220. */
  221. void *
  222. NSS_CMSContentInfo_GetContent(NSSCMSContentInfo *cinfo)
  223. {
  224. if (cinfo == NULL) {
  225. return NULL;
  226. }
  227. SECOidTag tag = cinfo->contentTypeTag
  228. ? cinfo->contentTypeTag->offset
  229. : SEC_OID_UNKNOWN;
  230. switch (tag) {
  231. case SEC_OID_PKCS7_DATA:
  232. case SEC_OID_PKCS7_SIGNED_DATA:
  233. case SEC_OID_PKCS7_ENVELOPED_DATA:
  234. case SEC_OID_PKCS7_DIGESTED_DATA:
  235. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  236. return cinfo->content.pointer;
  237. default:
  238. return NSS_CMSType_IsWrapper(tag) ? cinfo->content.pointer
  239. : (NSS_CMSType_IsData(tag) ? cinfo->rawContent
  240. : NULL);
  241. }
  242. }
  243. /*
  244. * NSS_CMSContentInfo_GetInnerContent - get pointer to innermost content
  245. *
  246. * this is typically only called by NSS_CMSMessage_GetContent()
  247. */
  248. SECItem *
  249. NSS_CMSContentInfo_GetInnerContent(NSSCMSContentInfo *cinfo)
  250. {
  251. NSSCMSContentInfo *ccinfo;
  252. SECOidTag tag;
  253. SECItem *pItem = NULL;
  254. if (cinfo == NULL) {
  255. return NULL;
  256. }
  257. tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  258. if (NSS_CMSType_IsData(tag)) {
  259. pItem = cinfo->content.data;
  260. } else if (NSS_CMSType_IsWrapper(tag)) {
  261. ccinfo = NSS_CMSContentInfo_GetChildContentInfo(cinfo);
  262. if (ccinfo != NULL) {
  263. pItem = NSS_CMSContentInfo_GetContent(ccinfo);
  264. }
  265. } else {
  266. PORT_Assert(0);
  267. }
  268. return pItem;
  269. }
  270. /*
  271. * NSS_CMSContentInfo_GetContentType{Tag,OID} - find out (saving pointer to lookup result
  272. * for future reference) and return the inner content type.
  273. */
  274. SECOidTag
  275. NSS_CMSContentInfo_GetContentTypeTag(NSSCMSContentInfo *cinfo)
  276. {
  277. if (cinfo == NULL) {
  278. return SEC_OID_UNKNOWN;
  279. }
  280. if (cinfo->contentTypeTag == NULL)
  281. cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType));
  282. if (cinfo->contentTypeTag == NULL)
  283. return SEC_OID_UNKNOWN;
  284. return cinfo->contentTypeTag->offset;
  285. }
  286. SECItem *
  287. NSS_CMSContentInfo_GetContentTypeOID(NSSCMSContentInfo *cinfo)
  288. {
  289. if (cinfo == NULL) {
  290. return NULL;
  291. }
  292. if (cinfo->contentTypeTag == NULL) {
  293. cinfo->contentTypeTag = SECOID_FindOID(&(cinfo->contentType));
  294. }
  295. if (cinfo->contentTypeTag == NULL) {
  296. return NULL;
  297. }
  298. return &(cinfo->contentTypeTag->oid);
  299. }
  300. /*
  301. * NSS_CMSContentInfo_GetContentEncAlgTag - find out (saving pointer to lookup result
  302. * for future reference) and return the content encryption algorithm tag.
  303. */
  304. SECOidTag
  305. NSS_CMSContentInfo_GetContentEncAlgTag(NSSCMSContentInfo *cinfo)
  306. {
  307. if (cinfo == NULL) {
  308. return SEC_OID_UNKNOWN;
  309. }
  310. if (cinfo->contentEncAlgTag == SEC_OID_UNKNOWN) {
  311. cinfo->contentEncAlgTag = SECOID_GetAlgorithmTag(&(cinfo->contentEncAlg));
  312. }
  313. return cinfo->contentEncAlgTag;
  314. }
  315. /*
  316. * NSS_CMSContentInfo_GetContentEncAlg - find out and return the content encryption algorithm tag.
  317. */
  318. SECAlgorithmID *
  319. NSS_CMSContentInfo_GetContentEncAlg(NSSCMSContentInfo *cinfo)
  320. {
  321. if (cinfo == NULL) {
  322. return NULL;
  323. }
  324. return &(cinfo->contentEncAlg);
  325. }
  326. SECStatus
  327. NSS_CMSContentInfo_SetContentEncAlg(PLArenaPool *poolp, NSSCMSContentInfo *cinfo,
  328. SECOidTag bulkalgtag, SECItem *parameters, int keysize)
  329. {
  330. SECStatus rv;
  331. if (cinfo == NULL) {
  332. return SECFailure;
  333. }
  334. rv = SECOID_SetAlgorithmID(poolp, &(cinfo->contentEncAlg), bulkalgtag, parameters);
  335. if (rv != SECSuccess) {
  336. return SECFailure;
  337. }
  338. cinfo->keysize = keysize;
  339. return SECSuccess;
  340. }
  341. SECStatus
  342. NSS_CMSContentInfo_SetContentEncAlgID(PLArenaPool *poolp, NSSCMSContentInfo *cinfo,
  343. SECAlgorithmID *algid, int keysize)
  344. {
  345. SECStatus rv;
  346. if (cinfo == NULL) {
  347. return SECFailure;
  348. }
  349. rv = SECOID_CopyAlgorithmID(poolp, &(cinfo->contentEncAlg), algid);
  350. if (rv != SECSuccess) {
  351. return SECFailure;
  352. }
  353. if (keysize >= 0) {
  354. cinfo->keysize = keysize;
  355. }
  356. return SECSuccess;
  357. }
  358. void
  359. NSS_CMSContentInfo_SetBulkKey(NSSCMSContentInfo *cinfo, PK11SymKey *bulkkey)
  360. {
  361. if (cinfo == NULL) {
  362. return;
  363. }
  364. if (bulkkey == NULL) {
  365. cinfo->bulkkey = NULL;
  366. cinfo->keysize = 0;
  367. } else {
  368. cinfo->bulkkey = PK11_ReferenceSymKey(bulkkey);
  369. cinfo->keysize = PK11_GetKeyStrength(cinfo->bulkkey, &(cinfo->contentEncAlg));
  370. }
  371. }
  372. PK11SymKey *
  373. NSS_CMSContentInfo_GetBulkKey(NSSCMSContentInfo *cinfo)
  374. {
  375. if (cinfo == NULL || cinfo->bulkkey == NULL) {
  376. return NULL;
  377. }
  378. return PK11_ReferenceSymKey(cinfo->bulkkey);
  379. }
  380. int
  381. NSS_CMSContentInfo_GetBulkKeySize(NSSCMSContentInfo *cinfo)
  382. {
  383. if (cinfo == NULL) {
  384. return 0;
  385. }
  386. return cinfo->keysize;
  387. }