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