cmsreclist.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 recipient list functions
  6. */
  7. #include "cmslocal.h"
  8. #include "cert.h"
  9. #include "keyhi.h"
  10. #include "secasn1.h"
  11. #include "secitem.h"
  12. #include "secoid.h"
  13. #include "pk11func.h"
  14. #include "prtime.h"
  15. #include "secerr.h"
  16. static int
  17. nss_cms_recipients_traverse(NSSCMSRecipientInfo **recipientinfos,
  18. NSSCMSRecipient **recipient_list)
  19. {
  20. int count = 0;
  21. int rlindex = 0;
  22. int i, j;
  23. NSSCMSRecipient *rle;
  24. NSSCMSRecipientInfo *ri;
  25. NSSCMSRecipientEncryptedKey *rek;
  26. for (i = 0; recipientinfos[i] != NULL; i++) {
  27. ri = recipientinfos[i];
  28. switch (ri->recipientInfoType) {
  29. case NSSCMSRecipientInfoID_KeyTrans:
  30. if (recipient_list) {
  31. NSSCMSRecipientIdentifier *recipId =
  32. &ri->ri.keyTransRecipientInfo.recipientIdentifier;
  33. if (recipId->identifierType != NSSCMSRecipientID_IssuerSN &&
  34. recipId->identifierType != NSSCMSRecipientID_SubjectKeyID) {
  35. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  36. return -1;
  37. }
  38. /* alloc one & fill it out */
  39. rle = (NSSCMSRecipient *)PORT_ZAlloc(sizeof(NSSCMSRecipient));
  40. if (!rle)
  41. return -1;
  42. rle->riIndex = i;
  43. rle->subIndex = -1;
  44. switch (recipId->identifierType) {
  45. case NSSCMSRecipientID_IssuerSN:
  46. rle->kind = RLIssuerSN;
  47. rle->id.issuerAndSN = recipId->id.issuerAndSN;
  48. break;
  49. case NSSCMSRecipientID_SubjectKeyID:
  50. rle->kind = RLSubjKeyID;
  51. rle->id.subjectKeyID = recipId->id.subjectKeyID;
  52. break;
  53. default: /* we never get here because of identifierType check
  54. we done before. Leaving it to kill compiler warning */
  55. break;
  56. }
  57. recipient_list[rlindex++] = rle;
  58. } else {
  59. count++;
  60. }
  61. break;
  62. case NSSCMSRecipientInfoID_KeyAgree:
  63. if (ri->ri.keyAgreeRecipientInfo.recipientEncryptedKeys == NULL)
  64. break;
  65. for (j = 0; ri->ri.keyAgreeRecipientInfo.recipientEncryptedKeys[j] != NULL; j++) {
  66. if (recipient_list) {
  67. rek = ri->ri.keyAgreeRecipientInfo.recipientEncryptedKeys[j];
  68. /* alloc one & fill it out */
  69. rle = (NSSCMSRecipient *)PORT_ZAlloc(sizeof(NSSCMSRecipient));
  70. if (!rle)
  71. return -1;
  72. rle->riIndex = i;
  73. rle->subIndex = j;
  74. switch (rek->recipientIdentifier.identifierType) {
  75. case NSSCMSKeyAgreeRecipientID_IssuerSN:
  76. rle->kind = RLIssuerSN;
  77. rle->id.issuerAndSN = rek->recipientIdentifier.id.issuerAndSN;
  78. break;
  79. case NSSCMSKeyAgreeRecipientID_RKeyID:
  80. rle->kind = RLSubjKeyID;
  81. rle->id.subjectKeyID =
  82. rek->recipientIdentifier.id.recipientKeyIdentifier.subjectKeyIdentifier;
  83. break;
  84. }
  85. recipient_list[rlindex++] = rle;
  86. } else {
  87. count++;
  88. }
  89. }
  90. break;
  91. case NSSCMSRecipientInfoID_KEK:
  92. /* KEK is not implemented */
  93. break;
  94. }
  95. }
  96. /* if we have a recipient list, we return on success (-1, above, on failure) */
  97. /* otherwise, we return the count. */
  98. if (recipient_list) {
  99. recipient_list[rlindex] = NULL;
  100. return 0;
  101. } else {
  102. return count;
  103. }
  104. }
  105. NSSCMSRecipient **
  106. nss_cms_recipient_list_create(NSSCMSRecipientInfo **recipientinfos)
  107. {
  108. int count, rv;
  109. NSSCMSRecipient **recipient_list;
  110. /* count the number of recipient identifiers */
  111. count = nss_cms_recipients_traverse(recipientinfos, NULL);
  112. if (count <= 0) {
  113. /* no recipients? */
  114. PORT_SetError(SEC_ERROR_BAD_DATA);
  115. #if 0
  116. PORT_SetErrorString("Cannot find recipient data in envelope.");
  117. #endif
  118. return NULL;
  119. }
  120. /* allocate an array of pointers */
  121. recipient_list = (NSSCMSRecipient **)
  122. PORT_ZAlloc((count + 1) * sizeof(NSSCMSRecipient *));
  123. if (recipient_list == NULL)
  124. return NULL;
  125. /* now fill in the recipient_list */
  126. rv = nss_cms_recipients_traverse(recipientinfos, recipient_list);
  127. if (rv < 0) {
  128. nss_cms_recipient_list_destroy(recipient_list);
  129. return NULL;
  130. }
  131. return recipient_list;
  132. }
  133. void
  134. nss_cms_recipient_list_destroy(NSSCMSRecipient **recipient_list)
  135. {
  136. int i;
  137. NSSCMSRecipient *recipient;
  138. for (i = 0; recipient_list[i] != NULL; i++) {
  139. recipient = recipient_list[i];
  140. if (recipient->cert)
  141. CERT_DestroyCertificate(recipient->cert);
  142. if (recipient->privkey)
  143. SECKEY_DestroyPrivateKey(recipient->privkey);
  144. if (recipient->slot)
  145. PK11_FreeSlot(recipient->slot);
  146. PORT_Free(recipient);
  147. }
  148. PORT_Free(recipient_list);
  149. }
  150. NSSCMSRecipientEncryptedKey *
  151. NSS_CMSRecipientEncryptedKey_Create(PLArenaPool *poolp)
  152. {
  153. return (NSSCMSRecipientEncryptedKey *)PORT_ArenaZAlloc(poolp,
  154. sizeof(NSSCMSRecipientEncryptedKey));
  155. }