Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 decoding. |
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 "prtime.h" |
michael@0 | 17 | #include "secerr.h" |
michael@0 | 18 | |
michael@0 | 19 | struct NSSCMSDecoderContextStr { |
michael@0 | 20 | SEC_ASN1DecoderContext * dcx; /* ASN.1 decoder context */ |
michael@0 | 21 | NSSCMSMessage * cmsg; /* backpointer to the root message */ |
michael@0 | 22 | SECOidTag type; /* type of message */ |
michael@0 | 23 | NSSCMSContent content; /* pointer to message */ |
michael@0 | 24 | NSSCMSDecoderContext * childp7dcx; /* inner CMS decoder context */ |
michael@0 | 25 | PRBool saw_contents; |
michael@0 | 26 | int error; |
michael@0 | 27 | NSSCMSContentCallback cb; |
michael@0 | 28 | void * cb_arg; |
michael@0 | 29 | PRBool first_decoded; |
michael@0 | 30 | PRBool need_indefinite_finish; |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | struct NSSCMSDecoderDataStr { |
michael@0 | 34 | SECItem data; /* must be first */ |
michael@0 | 35 | unsigned int totalBufferSize; |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | typedef struct NSSCMSDecoderDataStr NSSCMSDecoderData; |
michael@0 | 39 | |
michael@0 | 40 | static void nss_cms_decoder_update_filter (void *arg, const char *data, |
michael@0 | 41 | unsigned long len, int depth, SEC_ASN1EncodingPart data_kind); |
michael@0 | 42 | static SECStatus nss_cms_before_data(NSSCMSDecoderContext *p7dcx); |
michael@0 | 43 | static SECStatus nss_cms_after_data(NSSCMSDecoderContext *p7dcx); |
michael@0 | 44 | static SECStatus nss_cms_after_end(NSSCMSDecoderContext *p7dcx); |
michael@0 | 45 | static void nss_cms_decoder_work_data(NSSCMSDecoderContext *p7dcx, |
michael@0 | 46 | const unsigned char *data, unsigned long len, PRBool final); |
michael@0 | 47 | static NSSCMSDecoderData *nss_cms_create_decoder_data(PLArenaPool *poolp); |
michael@0 | 48 | |
michael@0 | 49 | extern const SEC_ASN1Template NSSCMSMessageTemplate[]; |
michael@0 | 50 | |
michael@0 | 51 | static NSSCMSDecoderData * |
michael@0 | 52 | nss_cms_create_decoder_data(PLArenaPool *poolp) |
michael@0 | 53 | { |
michael@0 | 54 | NSSCMSDecoderData *decoderData = NULL; |
michael@0 | 55 | |
michael@0 | 56 | decoderData = (NSSCMSDecoderData *) |
michael@0 | 57 | PORT_ArenaAlloc(poolp,sizeof(NSSCMSDecoderData)); |
michael@0 | 58 | if (!decoderData) { |
michael@0 | 59 | return NULL; |
michael@0 | 60 | } |
michael@0 | 61 | decoderData->data.data = NULL; |
michael@0 | 62 | decoderData->data.len = 0; |
michael@0 | 63 | decoderData->totalBufferSize = 0; |
michael@0 | 64 | return decoderData; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /* |
michael@0 | 68 | * nss_cms_decoder_notify - |
michael@0 | 69 | * this is the driver of the decoding process. It gets called by the ASN.1 |
michael@0 | 70 | * decoder before and after an object is decoded. |
michael@0 | 71 | * at various points in the decoding process, we intercept to set up and do |
michael@0 | 72 | * further processing. |
michael@0 | 73 | */ |
michael@0 | 74 | static void |
michael@0 | 75 | nss_cms_decoder_notify(void *arg, PRBool before, void *dest, int depth) |
michael@0 | 76 | { |
michael@0 | 77 | NSSCMSDecoderContext *p7dcx; |
michael@0 | 78 | NSSCMSContentInfo *rootcinfo, *cinfo; |
michael@0 | 79 | PRBool after = !before; |
michael@0 | 80 | |
michael@0 | 81 | p7dcx = (NSSCMSDecoderContext *)arg; |
michael@0 | 82 | rootcinfo = &(p7dcx->cmsg->contentInfo); |
michael@0 | 83 | |
michael@0 | 84 | /* XXX error handling: need to set p7dcx->error */ |
michael@0 | 85 | |
michael@0 | 86 | #ifdef CMSDEBUG |
michael@0 | 87 | fprintf(stderr, "%6.6s, dest = 0x%08x, depth = %d\n", before ? "before" : "after", dest, depth); |
michael@0 | 88 | #endif |
michael@0 | 89 | |
michael@0 | 90 | /* so what are we working on right now? */ |
michael@0 | 91 | if (p7dcx->type == SEC_OID_UNKNOWN) { |
michael@0 | 92 | /* |
michael@0 | 93 | * right now, we are still decoding the OUTER (root) cinfo |
michael@0 | 94 | * As soon as we know the inner content type, set up the info, |
michael@0 | 95 | * but NO inner decoder or filter. The root decoder handles the first |
michael@0 | 96 | * level children by itself - only for encapsulated contents (which |
michael@0 | 97 | * are encoded as DER inside of an OCTET STRING) we need to set up a |
michael@0 | 98 | * child decoder... |
michael@0 | 99 | */ |
michael@0 | 100 | if (after && dest == &(rootcinfo->contentType)) { |
michael@0 | 101 | p7dcx->type = NSS_CMSContentInfo_GetContentTypeTag(rootcinfo); |
michael@0 | 102 | p7dcx->content = rootcinfo->content; |
michael@0 | 103 | /* is this ready already ? need to alloc? */ |
michael@0 | 104 | /* XXX yes we need to alloc -- continue here */ |
michael@0 | 105 | } |
michael@0 | 106 | } else if (NSS_CMSType_IsData(p7dcx->type)) { |
michael@0 | 107 | /* this can only happen if the outermost cinfo has DATA in it */ |
michael@0 | 108 | /* otherwise, we handle this type implicitely in the inner decoders */ |
michael@0 | 109 | |
michael@0 | 110 | if (before && dest == &(rootcinfo->content)) { |
michael@0 | 111 | /* cause the filter to put the data in the right place... |
michael@0 | 112 | ** We want the ASN.1 decoder to deliver the decoded bytes to us |
michael@0 | 113 | ** from now on |
michael@0 | 114 | */ |
michael@0 | 115 | SEC_ASN1DecoderSetFilterProc(p7dcx->dcx, |
michael@0 | 116 | nss_cms_decoder_update_filter, |
michael@0 | 117 | p7dcx, |
michael@0 | 118 | (PRBool)(p7dcx->cb != NULL)); |
michael@0 | 119 | } else if (after && dest == &(rootcinfo->content.data)) { |
michael@0 | 120 | /* remove the filter */ |
michael@0 | 121 | SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); |
michael@0 | 122 | } |
michael@0 | 123 | } else if (NSS_CMSType_IsWrapper(p7dcx->type)) { |
michael@0 | 124 | if (!before || dest != &(rootcinfo->content)) { |
michael@0 | 125 | |
michael@0 | 126 | if (p7dcx->content.pointer == NULL) |
michael@0 | 127 | p7dcx->content = rootcinfo->content; |
michael@0 | 128 | |
michael@0 | 129 | /* get this data type's inner contentInfo */ |
michael@0 | 130 | cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, |
michael@0 | 131 | p7dcx->type); |
michael@0 | 132 | |
michael@0 | 133 | if (before && dest == &(cinfo->contentType)) { |
michael@0 | 134 | /* at this point, set up the &%$&$ back pointer */ |
michael@0 | 135 | /* we cannot do it later, because the content itself |
michael@0 | 136 | * is optional! */ |
michael@0 | 137 | switch (p7dcx->type) { |
michael@0 | 138 | case SEC_OID_PKCS7_SIGNED_DATA: |
michael@0 | 139 | p7dcx->content.signedData->cmsg = p7dcx->cmsg; |
michael@0 | 140 | break; |
michael@0 | 141 | case SEC_OID_PKCS7_DIGESTED_DATA: |
michael@0 | 142 | p7dcx->content.digestedData->cmsg = p7dcx->cmsg; |
michael@0 | 143 | break; |
michael@0 | 144 | case SEC_OID_PKCS7_ENVELOPED_DATA: |
michael@0 | 145 | p7dcx->content.envelopedData->cmsg = p7dcx->cmsg; |
michael@0 | 146 | break; |
michael@0 | 147 | case SEC_OID_PKCS7_ENCRYPTED_DATA: |
michael@0 | 148 | p7dcx->content.encryptedData->cmsg = p7dcx->cmsg; |
michael@0 | 149 | break; |
michael@0 | 150 | default: |
michael@0 | 151 | p7dcx->content.genericData->cmsg = p7dcx->cmsg; |
michael@0 | 152 | break; |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | if (before && dest == &(cinfo->rawContent)) { |
michael@0 | 157 | /* we want the ASN.1 decoder to deliver the decoded bytes to us |
michael@0 | 158 | ** from now on |
michael@0 | 159 | */ |
michael@0 | 160 | SEC_ASN1DecoderSetFilterProc(p7dcx->dcx, |
michael@0 | 161 | nss_cms_decoder_update_filter, |
michael@0 | 162 | p7dcx, (PRBool)(p7dcx->cb != NULL)); |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | /* we're right in front of the data */ |
michael@0 | 166 | if (nss_cms_before_data(p7dcx) != SECSuccess) { |
michael@0 | 167 | SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); |
michael@0 | 168 | /* stop all processing */ |
michael@0 | 169 | p7dcx->error = PORT_GetError(); |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | if (after && dest == &(cinfo->rawContent)) { |
michael@0 | 173 | /* we're right after of the data */ |
michael@0 | 174 | if (nss_cms_after_data(p7dcx) != SECSuccess) |
michael@0 | 175 | p7dcx->error = PORT_GetError(); |
michael@0 | 176 | |
michael@0 | 177 | /* we don't need to see the contents anymore */ |
michael@0 | 178 | SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | } else { |
michael@0 | 182 | /* unsupported or unknown message type - fail gracefully */ |
michael@0 | 183 | p7dcx->error = SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | /* |
michael@0 | 188 | * nss_cms_before_data - set up the current encoder to receive data |
michael@0 | 189 | */ |
michael@0 | 190 | static SECStatus |
michael@0 | 191 | nss_cms_before_data(NSSCMSDecoderContext *p7dcx) |
michael@0 | 192 | { |
michael@0 | 193 | SECStatus rv; |
michael@0 | 194 | SECOidTag childtype; |
michael@0 | 195 | PLArenaPool *poolp; |
michael@0 | 196 | NSSCMSDecoderContext *childp7dcx; |
michael@0 | 197 | NSSCMSContentInfo *cinfo; |
michael@0 | 198 | const SEC_ASN1Template *template; |
michael@0 | 199 | void *mark = NULL; |
michael@0 | 200 | size_t size; |
michael@0 | 201 | |
michael@0 | 202 | poolp = p7dcx->cmsg->poolp; |
michael@0 | 203 | |
michael@0 | 204 | /* call _Decode_BeforeData handlers */ |
michael@0 | 205 | switch (p7dcx->type) { |
michael@0 | 206 | case SEC_OID_PKCS7_SIGNED_DATA: |
michael@0 | 207 | /* we're decoding a signedData, so set up the digests */ |
michael@0 | 208 | rv = NSS_CMSSignedData_Decode_BeforeData(p7dcx->content.signedData); |
michael@0 | 209 | break; |
michael@0 | 210 | case SEC_OID_PKCS7_DIGESTED_DATA: |
michael@0 | 211 | /* we're encoding a digestedData, so set up the digest */ |
michael@0 | 212 | rv = NSS_CMSDigestedData_Decode_BeforeData(p7dcx->content.digestedData); |
michael@0 | 213 | break; |
michael@0 | 214 | case SEC_OID_PKCS7_ENVELOPED_DATA: |
michael@0 | 215 | rv = NSS_CMSEnvelopedData_Decode_BeforeData( |
michael@0 | 216 | p7dcx->content.envelopedData); |
michael@0 | 217 | break; |
michael@0 | 218 | case SEC_OID_PKCS7_ENCRYPTED_DATA: |
michael@0 | 219 | rv = NSS_CMSEncryptedData_Decode_BeforeData( |
michael@0 | 220 | p7dcx->content.encryptedData); |
michael@0 | 221 | break; |
michael@0 | 222 | default: |
michael@0 | 223 | rv = NSS_CMSGenericWrapperData_Decode_BeforeData(p7dcx->type, |
michael@0 | 224 | p7dcx->content.genericData); |
michael@0 | 225 | } |
michael@0 | 226 | if (rv != SECSuccess) |
michael@0 | 227 | return SECFailure; |
michael@0 | 228 | |
michael@0 | 229 | /* ok, now we have a pointer to cinfo */ |
michael@0 | 230 | /* find out what kind of data is encapsulated */ |
michael@0 | 231 | |
michael@0 | 232 | cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, p7dcx->type); |
michael@0 | 233 | childtype = NSS_CMSContentInfo_GetContentTypeTag(cinfo); |
michael@0 | 234 | |
michael@0 | 235 | if (NSS_CMSType_IsData(childtype)) { |
michael@0 | 236 | cinfo->content.pointer = (void *) nss_cms_create_decoder_data(poolp); |
michael@0 | 237 | if (cinfo->content.pointer == NULL) |
michael@0 | 238 | /* set memory error */ |
michael@0 | 239 | return SECFailure; |
michael@0 | 240 | |
michael@0 | 241 | p7dcx->childp7dcx = NULL; |
michael@0 | 242 | return SECSuccess; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | /* set up inner decoder */ |
michael@0 | 246 | |
michael@0 | 247 | if ((template = NSS_CMSUtil_GetTemplateByTypeTag(childtype)) == NULL) |
michael@0 | 248 | return SECFailure; |
michael@0 | 249 | |
michael@0 | 250 | childp7dcx = PORT_ZNew(NSSCMSDecoderContext); |
michael@0 | 251 | if (childp7dcx == NULL) |
michael@0 | 252 | return SECFailure; |
michael@0 | 253 | |
michael@0 | 254 | mark = PORT_ArenaMark(poolp); |
michael@0 | 255 | |
michael@0 | 256 | /* allocate space for the stuff we're creating */ |
michael@0 | 257 | size = NSS_CMSUtil_GetSizeByTypeTag(childtype); |
michael@0 | 258 | childp7dcx->content.pointer = (void *)PORT_ArenaZAlloc(poolp, size); |
michael@0 | 259 | if (childp7dcx->content.pointer == NULL) |
michael@0 | 260 | goto loser; |
michael@0 | 261 | |
michael@0 | 262 | /* give the parent a copy of the pointer so that it doesn't get lost */ |
michael@0 | 263 | cinfo->content.pointer = childp7dcx->content.pointer; |
michael@0 | 264 | |
michael@0 | 265 | /* start the child decoder */ |
michael@0 | 266 | childp7dcx->dcx = SEC_ASN1DecoderStart(poolp, childp7dcx->content.pointer, |
michael@0 | 267 | template); |
michael@0 | 268 | if (childp7dcx->dcx == NULL) |
michael@0 | 269 | goto loser; |
michael@0 | 270 | |
michael@0 | 271 | /* the new decoder needs to notify, too */ |
michael@0 | 272 | SEC_ASN1DecoderSetNotifyProc(childp7dcx->dcx, nss_cms_decoder_notify, |
michael@0 | 273 | childp7dcx); |
michael@0 | 274 | |
michael@0 | 275 | /* tell the parent decoder that it needs to feed us the content data */ |
michael@0 | 276 | p7dcx->childp7dcx = childp7dcx; |
michael@0 | 277 | |
michael@0 | 278 | childp7dcx->type = childtype; /* our type */ |
michael@0 | 279 | |
michael@0 | 280 | childp7dcx->cmsg = p7dcx->cmsg; /* backpointer to root message */ |
michael@0 | 281 | |
michael@0 | 282 | /* should the child decoder encounter real data, |
michael@0 | 283 | ** it must give it to the caller |
michael@0 | 284 | */ |
michael@0 | 285 | childp7dcx->cb = p7dcx->cb; |
michael@0 | 286 | childp7dcx->cb_arg = p7dcx->cb_arg; |
michael@0 | 287 | childp7dcx->first_decoded = PR_FALSE; |
michael@0 | 288 | childp7dcx->need_indefinite_finish = PR_FALSE; |
michael@0 | 289 | if (childtype == SEC_OID_PKCS7_SIGNED_DATA) { |
michael@0 | 290 | childp7dcx->first_decoded = PR_TRUE; |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | /* now set up the parent to hand decoded data to the next level decoder */ |
michael@0 | 294 | p7dcx->cb = (NSSCMSContentCallback)NSS_CMSDecoder_Update; |
michael@0 | 295 | p7dcx->cb_arg = childp7dcx; |
michael@0 | 296 | |
michael@0 | 297 | PORT_ArenaUnmark(poolp, mark); |
michael@0 | 298 | |
michael@0 | 299 | return SECSuccess; |
michael@0 | 300 | |
michael@0 | 301 | loser: |
michael@0 | 302 | if (mark) |
michael@0 | 303 | PORT_ArenaRelease(poolp, mark); |
michael@0 | 304 | if (childp7dcx) |
michael@0 | 305 | PORT_Free(childp7dcx); |
michael@0 | 306 | p7dcx->childp7dcx = NULL; |
michael@0 | 307 | return SECFailure; |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | static SECStatus |
michael@0 | 311 | nss_cms_after_data(NSSCMSDecoderContext *p7dcx) |
michael@0 | 312 | { |
michael@0 | 313 | NSSCMSDecoderContext *childp7dcx; |
michael@0 | 314 | SECStatus rv = SECFailure; |
michael@0 | 315 | |
michael@0 | 316 | /* Handle last block. This is necessary to flush out the last bytes |
michael@0 | 317 | * of a possibly incomplete block */ |
michael@0 | 318 | nss_cms_decoder_work_data(p7dcx, NULL, 0, PR_TRUE); |
michael@0 | 319 | |
michael@0 | 320 | /* finish any "inner" decoders - there's no more data coming... */ |
michael@0 | 321 | if (p7dcx->childp7dcx != NULL) { |
michael@0 | 322 | childp7dcx = p7dcx->childp7dcx; |
michael@0 | 323 | if (childp7dcx->dcx != NULL) { |
michael@0 | 324 | /* we started and indefinite sequence somewhere, not complete it */ |
michael@0 | 325 | if (childp7dcx->need_indefinite_finish) { |
michael@0 | 326 | static const char lbuf[2] = { 0, 0 }; |
michael@0 | 327 | NSS_CMSDecoder_Update(childp7dcx, lbuf, sizeof(lbuf)); |
michael@0 | 328 | childp7dcx->need_indefinite_finish = PR_FALSE; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | if (SEC_ASN1DecoderFinish(childp7dcx->dcx) != SECSuccess) { |
michael@0 | 332 | /* do what? free content? */ |
michael@0 | 333 | rv = SECFailure; |
michael@0 | 334 | } else { |
michael@0 | 335 | rv = nss_cms_after_end(childp7dcx); |
michael@0 | 336 | } |
michael@0 | 337 | if (rv != SECSuccess) |
michael@0 | 338 | goto done; |
michael@0 | 339 | } |
michael@0 | 340 | PORT_Free(p7dcx->childp7dcx); |
michael@0 | 341 | p7dcx->childp7dcx = NULL; |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | switch (p7dcx->type) { |
michael@0 | 345 | case SEC_OID_PKCS7_SIGNED_DATA: |
michael@0 | 346 | /* this will finish the digests and verify */ |
michael@0 | 347 | rv = NSS_CMSSignedData_Decode_AfterData(p7dcx->content.signedData); |
michael@0 | 348 | break; |
michael@0 | 349 | case SEC_OID_PKCS7_ENVELOPED_DATA: |
michael@0 | 350 | rv = NSS_CMSEnvelopedData_Decode_AfterData( |
michael@0 | 351 | p7dcx->content.envelopedData); |
michael@0 | 352 | break; |
michael@0 | 353 | case SEC_OID_PKCS7_DIGESTED_DATA: |
michael@0 | 354 | rv = NSS_CMSDigestedData_Decode_AfterData( |
michael@0 | 355 | p7dcx->content.digestedData); |
michael@0 | 356 | break; |
michael@0 | 357 | case SEC_OID_PKCS7_ENCRYPTED_DATA: |
michael@0 | 358 | rv = NSS_CMSEncryptedData_Decode_AfterData( |
michael@0 | 359 | p7dcx->content.encryptedData); |
michael@0 | 360 | break; |
michael@0 | 361 | case SEC_OID_PKCS7_DATA: |
michael@0 | 362 | /* do nothing */ |
michael@0 | 363 | break; |
michael@0 | 364 | default: |
michael@0 | 365 | rv = NSS_CMSGenericWrapperData_Decode_AfterData(p7dcx->type, |
michael@0 | 366 | p7dcx->content.genericData); |
michael@0 | 367 | break; |
michael@0 | 368 | } |
michael@0 | 369 | done: |
michael@0 | 370 | return rv; |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | static SECStatus |
michael@0 | 374 | nss_cms_after_end(NSSCMSDecoderContext *p7dcx) |
michael@0 | 375 | { |
michael@0 | 376 | SECStatus rv = SECSuccess; |
michael@0 | 377 | |
michael@0 | 378 | switch (p7dcx->type) { |
michael@0 | 379 | case SEC_OID_PKCS7_SIGNED_DATA: |
michael@0 | 380 | if (p7dcx->content.signedData) |
michael@0 | 381 | rv = NSS_CMSSignedData_Decode_AfterEnd(p7dcx->content.signedData); |
michael@0 | 382 | break; |
michael@0 | 383 | case SEC_OID_PKCS7_ENVELOPED_DATA: |
michael@0 | 384 | if (p7dcx->content.envelopedData) |
michael@0 | 385 | rv = NSS_CMSEnvelopedData_Decode_AfterEnd( |
michael@0 | 386 | p7dcx->content.envelopedData); |
michael@0 | 387 | break; |
michael@0 | 388 | case SEC_OID_PKCS7_DIGESTED_DATA: |
michael@0 | 389 | if (p7dcx->content.digestedData) |
michael@0 | 390 | rv = NSS_CMSDigestedData_Decode_AfterEnd( |
michael@0 | 391 | p7dcx->content.digestedData); |
michael@0 | 392 | break; |
michael@0 | 393 | case SEC_OID_PKCS7_ENCRYPTED_DATA: |
michael@0 | 394 | if (p7dcx->content.encryptedData) |
michael@0 | 395 | rv = NSS_CMSEncryptedData_Decode_AfterEnd( |
michael@0 | 396 | p7dcx->content.encryptedData); |
michael@0 | 397 | break; |
michael@0 | 398 | case SEC_OID_PKCS7_DATA: |
michael@0 | 399 | break; |
michael@0 | 400 | default: |
michael@0 | 401 | rv = NSS_CMSGenericWrapperData_Decode_AfterEnd(p7dcx->type, |
michael@0 | 402 | p7dcx->content.genericData); |
michael@0 | 403 | break; |
michael@0 | 404 | } |
michael@0 | 405 | return rv; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | /* |
michael@0 | 409 | * nss_cms_decoder_work_data - handle decoded data bytes. |
michael@0 | 410 | * |
michael@0 | 411 | * This function either decrypts the data if needed, and/or calculates digests |
michael@0 | 412 | * on it, then either stores it or passes it on to the next level decoder. |
michael@0 | 413 | */ |
michael@0 | 414 | static void |
michael@0 | 415 | nss_cms_decoder_work_data(NSSCMSDecoderContext *p7dcx, |
michael@0 | 416 | const unsigned char *data, unsigned long len, |
michael@0 | 417 | PRBool final) |
michael@0 | 418 | { |
michael@0 | 419 | NSSCMSContentInfo *cinfo; |
michael@0 | 420 | unsigned char *buf = NULL; |
michael@0 | 421 | unsigned char *dest; |
michael@0 | 422 | unsigned int offset; |
michael@0 | 423 | SECStatus rv; |
michael@0 | 424 | |
michael@0 | 425 | /* |
michael@0 | 426 | * We should really have data to process, or we should be trying |
michael@0 | 427 | * to finish/flush the last block. (This is an overly paranoid |
michael@0 | 428 | * check since all callers are in this file and simple inspection |
michael@0 | 429 | * proves they do it right. But it could find a bug in future |
michael@0 | 430 | * modifications/development, that is why it is here.) |
michael@0 | 431 | */ |
michael@0 | 432 | PORT_Assert ((data != NULL && len) || final); |
michael@0 | 433 | |
michael@0 | 434 | cinfo = NSS_CMSContent_GetContentInfo(p7dcx->content.pointer, p7dcx->type); |
michael@0 | 435 | if (!cinfo) { |
michael@0 | 436 | /* The original programmer didn't expect this to happen */ |
michael@0 | 437 | p7dcx->error = SEC_ERROR_LIBRARY_FAILURE; |
michael@0 | 438 | goto loser; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | if (cinfo->privateInfo && cinfo->privateInfo->ciphcx != NULL) { |
michael@0 | 442 | /* |
michael@0 | 443 | * we are decrypting. |
michael@0 | 444 | * |
michael@0 | 445 | * XXX If we get an error, we do not want to do the digest or callback, |
michael@0 | 446 | * but we want to keep decoding. Or maybe we want to stop decoding |
michael@0 | 447 | * altogether if there is a callback, because obviously we are not |
michael@0 | 448 | * sending the data back and they want to know that. |
michael@0 | 449 | */ |
michael@0 | 450 | |
michael@0 | 451 | unsigned int outlen = 0; /* length of decrypted data */ |
michael@0 | 452 | unsigned int buflen; /* length available for decrypted data */ |
michael@0 | 453 | |
michael@0 | 454 | /* find out about the length of decrypted data */ |
michael@0 | 455 | buflen = NSS_CMSCipherContext_DecryptLength(cinfo->privateInfo->ciphcx, len, final); |
michael@0 | 456 | |
michael@0 | 457 | /* |
michael@0 | 458 | * it might happen that we did not provide enough data for a full |
michael@0 | 459 | * block (decryption unit), and that there is no output available |
michael@0 | 460 | */ |
michael@0 | 461 | |
michael@0 | 462 | /* no output available, AND no input? */ |
michael@0 | 463 | if (buflen == 0 && len == 0) |
michael@0 | 464 | goto loser; /* bail out */ |
michael@0 | 465 | |
michael@0 | 466 | /* |
michael@0 | 467 | * have inner decoder: pass the data on (means inner content type is NOT data) |
michael@0 | 468 | * no inner decoder: we have DATA in here: either call callback or store |
michael@0 | 469 | */ |
michael@0 | 470 | if (buflen != 0) { |
michael@0 | 471 | /* there will be some output - need to make room for it */ |
michael@0 | 472 | /* allocate buffer from the heap */ |
michael@0 | 473 | buf = (unsigned char *)PORT_Alloc(buflen); |
michael@0 | 474 | if (buf == NULL) { |
michael@0 | 475 | p7dcx->error = SEC_ERROR_NO_MEMORY; |
michael@0 | 476 | goto loser; |
michael@0 | 477 | } |
michael@0 | 478 | } |
michael@0 | 479 | |
michael@0 | 480 | /* |
michael@0 | 481 | * decrypt incoming data |
michael@0 | 482 | * buf can still be NULL here (and buflen == 0) here if we don't expect |
michael@0 | 483 | * any output (see above), but we still need to call NSS_CMSCipherContext_Decrypt to |
michael@0 | 484 | * keep track of incoming data |
michael@0 | 485 | */ |
michael@0 | 486 | rv = NSS_CMSCipherContext_Decrypt(cinfo->privateInfo->ciphcx, buf, &outlen, buflen, |
michael@0 | 487 | data, len, final); |
michael@0 | 488 | if (rv != SECSuccess) { |
michael@0 | 489 | p7dcx->error = PORT_GetError(); |
michael@0 | 490 | goto loser; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | PORT_Assert (final || outlen == buflen); |
michael@0 | 494 | |
michael@0 | 495 | /* swap decrypted data in */ |
michael@0 | 496 | data = buf; |
michael@0 | 497 | len = outlen; |
michael@0 | 498 | } |
michael@0 | 499 | |
michael@0 | 500 | if (len == 0) |
michael@0 | 501 | goto done; /* nothing more to do */ |
michael@0 | 502 | |
michael@0 | 503 | /* |
michael@0 | 504 | * Update the running digests with plaintext bytes (if we need to). |
michael@0 | 505 | */ |
michael@0 | 506 | if (cinfo->privateInfo && cinfo->privateInfo->digcx) |
michael@0 | 507 | NSS_CMSDigestContext_Update(cinfo->privateInfo->digcx, data, len); |
michael@0 | 508 | |
michael@0 | 509 | /* at this point, we have the plain decoded & decrypted data |
michael@0 | 510 | ** which is either more encoded DER (which we need to hand to the child |
michael@0 | 511 | ** decoder) or data we need to hand back to our caller |
michael@0 | 512 | */ |
michael@0 | 513 | |
michael@0 | 514 | /* pass the content back to our caller or */ |
michael@0 | 515 | /* feed our freshly decrypted and decoded data into child decoder */ |
michael@0 | 516 | if (p7dcx->cb != NULL) { |
michael@0 | 517 | (*p7dcx->cb)(p7dcx->cb_arg, (const char *)data, len); |
michael@0 | 518 | } |
michael@0 | 519 | #if 1 |
michael@0 | 520 | else |
michael@0 | 521 | #endif |
michael@0 | 522 | if (NSS_CMSContentInfo_GetContentTypeTag(cinfo) == SEC_OID_PKCS7_DATA) { |
michael@0 | 523 | /* store it in "inner" data item as well */ |
michael@0 | 524 | /* find the DATA item in the encapsulated cinfo and store it there */ |
michael@0 | 525 | NSSCMSDecoderData *decoderData = |
michael@0 | 526 | (NSSCMSDecoderData *)cinfo->content.pointer; |
michael@0 | 527 | SECItem *dataItem = &decoderData->data; |
michael@0 | 528 | |
michael@0 | 529 | offset = dataItem->len; |
michael@0 | 530 | if (dataItem->len+len > decoderData->totalBufferSize) { |
michael@0 | 531 | int needLen = (dataItem->len+len) * 2; |
michael@0 | 532 | dest = (unsigned char *) |
michael@0 | 533 | PORT_ArenaAlloc(p7dcx->cmsg->poolp, needLen); |
michael@0 | 534 | if (dest == NULL) { |
michael@0 | 535 | p7dcx->error = SEC_ERROR_NO_MEMORY; |
michael@0 | 536 | goto loser; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | if (dataItem->len) { |
michael@0 | 540 | PORT_Memcpy(dest, dataItem->data, dataItem->len); |
michael@0 | 541 | } |
michael@0 | 542 | decoderData->totalBufferSize = needLen; |
michael@0 | 543 | dataItem->data = dest; |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | /* copy it in */ |
michael@0 | 547 | PORT_Memcpy(dataItem->data + offset, data, len); |
michael@0 | 548 | dataItem->len += len; |
michael@0 | 549 | } |
michael@0 | 550 | |
michael@0 | 551 | done: |
michael@0 | 552 | loser: |
michael@0 | 553 | if (buf) |
michael@0 | 554 | PORT_Free (buf); |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | /* |
michael@0 | 558 | * nss_cms_decoder_update_filter - process ASN.1 data |
michael@0 | 559 | * |
michael@0 | 560 | * once we have set up a filter in nss_cms_decoder_notify(), |
michael@0 | 561 | * all data processed by the ASN.1 decoder is also passed through here. |
michael@0 | 562 | * we pass the content bytes (as opposed to length and tag bytes) on to |
michael@0 | 563 | * nss_cms_decoder_work_data(). |
michael@0 | 564 | */ |
michael@0 | 565 | static void |
michael@0 | 566 | nss_cms_decoder_update_filter (void *arg, const char *data, unsigned long len, |
michael@0 | 567 | int depth, SEC_ASN1EncodingPart data_kind) |
michael@0 | 568 | { |
michael@0 | 569 | NSSCMSDecoderContext *p7dcx; |
michael@0 | 570 | |
michael@0 | 571 | PORT_Assert (len); /* paranoia */ |
michael@0 | 572 | if (len == 0) |
michael@0 | 573 | return; |
michael@0 | 574 | |
michael@0 | 575 | p7dcx = (NSSCMSDecoderContext*)arg; |
michael@0 | 576 | |
michael@0 | 577 | p7dcx->saw_contents = PR_TRUE; |
michael@0 | 578 | |
michael@0 | 579 | /* pass on the content bytes only */ |
michael@0 | 580 | if (data_kind == SEC_ASN1_Contents) |
michael@0 | 581 | nss_cms_decoder_work_data(p7dcx, (const unsigned char *) data, len, |
michael@0 | 582 | PR_FALSE); |
michael@0 | 583 | } |
michael@0 | 584 | |
michael@0 | 585 | /* |
michael@0 | 586 | * NSS_CMSDecoder_Start - set up decoding of a DER-encoded CMS message |
michael@0 | 587 | * |
michael@0 | 588 | * "poolp" - pointer to arena for message, or NULL if new pool should be created |
michael@0 | 589 | * "cb", "cb_arg" - callback function and argument for delivery of inner content |
michael@0 | 590 | * "pwfn", pwfn_arg" - callback function for getting token password |
michael@0 | 591 | * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData |
michael@0 | 592 | */ |
michael@0 | 593 | NSSCMSDecoderContext * |
michael@0 | 594 | NSS_CMSDecoder_Start(PLArenaPool *poolp, |
michael@0 | 595 | NSSCMSContentCallback cb, void *cb_arg, |
michael@0 | 596 | PK11PasswordFunc pwfn, void *pwfn_arg, |
michael@0 | 597 | NSSCMSGetDecryptKeyCallback decrypt_key_cb, |
michael@0 | 598 | void *decrypt_key_cb_arg) |
michael@0 | 599 | { |
michael@0 | 600 | NSSCMSDecoderContext *p7dcx; |
michael@0 | 601 | NSSCMSMessage *cmsg; |
michael@0 | 602 | |
michael@0 | 603 | cmsg = NSS_CMSMessage_Create(poolp); |
michael@0 | 604 | if (cmsg == NULL) |
michael@0 | 605 | return NULL; |
michael@0 | 606 | |
michael@0 | 607 | NSS_CMSMessage_SetEncodingParams(cmsg, pwfn, pwfn_arg, decrypt_key_cb, |
michael@0 | 608 | decrypt_key_cb_arg, NULL, NULL); |
michael@0 | 609 | |
michael@0 | 610 | p7dcx = PORT_ZNew(NSSCMSDecoderContext); |
michael@0 | 611 | if (p7dcx == NULL) { |
michael@0 | 612 | NSS_CMSMessage_Destroy(cmsg); |
michael@0 | 613 | return NULL; |
michael@0 | 614 | } |
michael@0 | 615 | |
michael@0 | 616 | p7dcx->dcx = SEC_ASN1DecoderStart(cmsg->poolp, cmsg, NSSCMSMessageTemplate); |
michael@0 | 617 | if (p7dcx->dcx == NULL) { |
michael@0 | 618 | PORT_Free (p7dcx); |
michael@0 | 619 | NSS_CMSMessage_Destroy(cmsg); |
michael@0 | 620 | return NULL; |
michael@0 | 621 | } |
michael@0 | 622 | |
michael@0 | 623 | SEC_ASN1DecoderSetNotifyProc (p7dcx->dcx, nss_cms_decoder_notify, p7dcx); |
michael@0 | 624 | |
michael@0 | 625 | p7dcx->cmsg = cmsg; |
michael@0 | 626 | p7dcx->type = SEC_OID_UNKNOWN; |
michael@0 | 627 | |
michael@0 | 628 | p7dcx->cb = cb; |
michael@0 | 629 | p7dcx->cb_arg = cb_arg; |
michael@0 | 630 | p7dcx->first_decoded = PR_FALSE; |
michael@0 | 631 | p7dcx->need_indefinite_finish = PR_FALSE; |
michael@0 | 632 | return p7dcx; |
michael@0 | 633 | } |
michael@0 | 634 | |
michael@0 | 635 | /* |
michael@0 | 636 | * NSS_CMSDecoder_Update - feed DER-encoded data to decoder |
michael@0 | 637 | */ |
michael@0 | 638 | SECStatus |
michael@0 | 639 | NSS_CMSDecoder_Update(NSSCMSDecoderContext *p7dcx, const char *buf, |
michael@0 | 640 | unsigned long len) |
michael@0 | 641 | { |
michael@0 | 642 | SECStatus rv = SECSuccess; |
michael@0 | 643 | if (p7dcx->dcx != NULL && p7dcx->error == 0) { |
michael@0 | 644 | /* if error is set already, don't bother */ |
michael@0 | 645 | if ((p7dcx->type == SEC_OID_PKCS7_SIGNED_DATA) |
michael@0 | 646 | && (p7dcx->first_decoded==PR_TRUE) |
michael@0 | 647 | && (buf[0] == SEC_ASN1_INTEGER)) { |
michael@0 | 648 | /* Microsoft Windows 2008 left out the Sequence wrapping in some |
michael@0 | 649 | * of their kerberos replies. If we are here, we most likely are |
michael@0 | 650 | * dealing with one of those replies. Supply the Sequence wrap |
michael@0 | 651 | * as indefinite encoding (since we don't know the total length |
michael@0 | 652 | * yet) */ |
michael@0 | 653 | static const char lbuf[2] = |
michael@0 | 654 | { SEC_ASN1_SEQUENCE|SEC_ASN1_CONSTRUCTED, 0x80 }; |
michael@0 | 655 | rv = SEC_ASN1DecoderUpdate(p7dcx->dcx, lbuf, sizeof(lbuf)); |
michael@0 | 656 | if (rv != SECSuccess) { |
michael@0 | 657 | goto loser; |
michael@0 | 658 | } |
michael@0 | 659 | /* ok, we're going to need the indefinite finish when we are done */ |
michael@0 | 660 | p7dcx->need_indefinite_finish = PR_TRUE; |
michael@0 | 661 | } |
michael@0 | 662 | |
michael@0 | 663 | rv = SEC_ASN1DecoderUpdate(p7dcx->dcx, buf, len); |
michael@0 | 664 | } |
michael@0 | 665 | |
michael@0 | 666 | loser: |
michael@0 | 667 | p7dcx->first_decoded = PR_FALSE; |
michael@0 | 668 | if (rv != SECSuccess) { |
michael@0 | 669 | p7dcx->error = PORT_GetError(); |
michael@0 | 670 | PORT_Assert (p7dcx->error); |
michael@0 | 671 | if (p7dcx->error == 0) |
michael@0 | 672 | p7dcx->error = -1; |
michael@0 | 673 | } |
michael@0 | 674 | |
michael@0 | 675 | if (p7dcx->error == 0) |
michael@0 | 676 | return SECSuccess; |
michael@0 | 677 | |
michael@0 | 678 | /* there has been a problem, let's finish the decoder */ |
michael@0 | 679 | if (p7dcx->dcx != NULL) { |
michael@0 | 680 | (void) SEC_ASN1DecoderFinish (p7dcx->dcx); |
michael@0 | 681 | p7dcx->dcx = NULL; |
michael@0 | 682 | } |
michael@0 | 683 | PORT_SetError (p7dcx->error); |
michael@0 | 684 | |
michael@0 | 685 | return SECFailure; |
michael@0 | 686 | } |
michael@0 | 687 | |
michael@0 | 688 | /* |
michael@0 | 689 | * NSS_CMSDecoder_Cancel - stop decoding in case of error |
michael@0 | 690 | */ |
michael@0 | 691 | void |
michael@0 | 692 | NSS_CMSDecoder_Cancel(NSSCMSDecoderContext *p7dcx) |
michael@0 | 693 | { |
michael@0 | 694 | if (p7dcx->dcx != NULL) |
michael@0 | 695 | (void)SEC_ASN1DecoderFinish(p7dcx->dcx); |
michael@0 | 696 | NSS_CMSMessage_Destroy(p7dcx->cmsg); |
michael@0 | 697 | PORT_Free(p7dcx); |
michael@0 | 698 | } |
michael@0 | 699 | |
michael@0 | 700 | /* |
michael@0 | 701 | * NSS_CMSDecoder_Finish - mark the end of inner content and finish decoding |
michael@0 | 702 | */ |
michael@0 | 703 | NSSCMSMessage * |
michael@0 | 704 | NSS_CMSDecoder_Finish(NSSCMSDecoderContext *p7dcx) |
michael@0 | 705 | { |
michael@0 | 706 | NSSCMSMessage *cmsg; |
michael@0 | 707 | |
michael@0 | 708 | cmsg = p7dcx->cmsg; |
michael@0 | 709 | |
michael@0 | 710 | if (p7dcx->dcx == NULL || |
michael@0 | 711 | SEC_ASN1DecoderFinish(p7dcx->dcx) != SECSuccess || |
michael@0 | 712 | nss_cms_after_end(p7dcx) != SECSuccess) |
michael@0 | 713 | { |
michael@0 | 714 | NSS_CMSMessage_Destroy(cmsg); /* get rid of pool if it's ours */ |
michael@0 | 715 | cmsg = NULL; |
michael@0 | 716 | } |
michael@0 | 717 | |
michael@0 | 718 | PORT_Free(p7dcx); |
michael@0 | 719 | return cmsg; |
michael@0 | 720 | } |
michael@0 | 721 | |
michael@0 | 722 | NSSCMSMessage * |
michael@0 | 723 | NSS_CMSMessage_CreateFromDER(SECItem *DERmessage, |
michael@0 | 724 | NSSCMSContentCallback cb, void *cb_arg, |
michael@0 | 725 | PK11PasswordFunc pwfn, void *pwfn_arg, |
michael@0 | 726 | NSSCMSGetDecryptKeyCallback decrypt_key_cb, |
michael@0 | 727 | void *decrypt_key_cb_arg) |
michael@0 | 728 | { |
michael@0 | 729 | NSSCMSDecoderContext *p7dcx; |
michael@0 | 730 | |
michael@0 | 731 | /* first arg(poolp) == NULL => create our own pool */ |
michael@0 | 732 | p7dcx = NSS_CMSDecoder_Start(NULL, cb, cb_arg, pwfn, pwfn_arg, |
michael@0 | 733 | decrypt_key_cb, decrypt_key_cb_arg); |
michael@0 | 734 | if (p7dcx == NULL) |
michael@0 | 735 | return NULL; |
michael@0 | 736 | NSS_CMSDecoder_Update(p7dcx, (char *)DERmessage->data, DERmessage->len); |
michael@0 | 737 | return NSS_CMSDecoder_Finish(p7dcx); |
michael@0 | 738 | } |
michael@0 | 739 |