secmime.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 902 行 · 第 1/2 页
C
902 行
int capi, pref; SECStatus dstat; pref = smime_symmetric_count; profile = CERT_FindSMimeProfile (rcerts[rcount]); if (profile != NULL && profile->data != NULL && profile->len > 0) { caps = NULL; dstat = SEC_ASN1DecodeItem (poolp, &caps, smime_capabilities_template, profile); if (dstat == SECSuccess && caps != NULL) { for (capi = 0; caps[capi] != NULL; capi++) { smime_fill_capability (caps[capi]); mapi = smime_mapi_by_cipher (caps[capi]->cipher); if (mapi >= 0) { cipher_abilities[mapi]++; cipher_votes[mapi] += pref; --pref; } } } } else { SECKEYPublicKey *key; unsigned int pklen_bits; /* * XXX This is probably only good for RSA keys. What I would * really like is a function to just say; Is the public key in * this cert an export-length key? Then I would not have to * know things like the value 512, or the kind of key, or what * a subjectPublicKeyInfo is, etc. */ key = CERT_ExtractPublicKey (rcerts[rcount]); if (key != NULL) { pklen_bits = SECKEY_PublicKeyStrength (key) * 8; SECKEY_DestroyPublicKey (key); if (pklen_bits > 512) { cipher_abilities[strong_mapi]++; cipher_votes[strong_mapi] += pref; } } } if (profile != NULL) SECITEM_FreeItem (profile, PR_TRUE); } max = 0; for (mapi = 0; mapi < smime_symmetric_count; mapi++) { if (cipher_abilities[mapi] != rcount) continue; if (! smime_cipher_allowed (smime_cipher_maps[mapi].cipher)) continue; if (!isFortezza && (smime_cipher_maps[mapi].cipher == SMIME_FORTEZZA)) continue; if (cipher_votes[mapi] > max) { chosen_cipher = smime_cipher_maps[mapi].cipher; max = cipher_votes[mapi]; } /* XXX else if a tie, let scert break it? */ }done: if (poolp != NULL) PORT_FreeArena (poolp, PR_FALSE); return chosen_cipher;}/* * XXX This is a hack for now to satisfy our current interface. * Eventually, with more parameters needing to be specified, just * looking up the keysize is not going to be sufficient. */static intsmime_keysize_by_cipher (unsigned long which){ int keysize; switch (which) { case SMIME_RC2_CBC_40: keysize = 40; break; case SMIME_RC2_CBC_64: keysize = 64; break; case SMIME_RC2_CBC_128: keysize = 128; break;#ifdef SMIME_DOES_RC5 case SMIME_RC5PAD_64_16_40: case SMIME_RC5PAD_64_16_64: case SMIME_RC5PAD_64_16_128: /* XXX See comment above; keysize is not enough... */ PORT_Assert (0); PORT_SetError (SEC_ERROR_INVALID_ALGORITHM); keysize = -1; break;#endif case SMIME_DES_CBC_56: case SMIME_DES_EDE3_168: case SMIME_FORTEZZA: /* * These are special; since the key size is fixed, we actually * want to *avoid* specifying a key size. */ keysize = 0; break; default: keysize = -1; break; } return keysize;}/* * Start an S/MIME encrypting context. * * "scert" is the cert for the sender. It will be checked for validity. * "rcerts" are the certs for the recipients. They will also be checked. * * "certdb" is the cert database to use for verifying the certs. * It can be NULL if a default database is available (like in the client). * * This function already does all of the stuff specific to S/MIME protocol * and local policy; the return value just needs to be passed to * SEC_PKCS7Encode() or to SEC_PKCS7EncoderStart() to create the encoded data, * and finally to SEC_PKCS7DestroyContentInfo(). * * An error results in a return value of NULL and an error set. * (Retrieve specific errors via PORT_GetError()/XP_GetError().) */SEC_PKCS7ContentInfo *SECMIME_CreateEncrypted(CERTCertificate *scert, CERTCertificate **rcerts, CERTCertDBHandle *certdb, SECKEYGetPasswordKey pwfn, void *pwfn_arg){ SEC_PKCS7ContentInfo *cinfo; long cipher; SECOidTag encalg; int keysize; int mapi, rci; cipher = smime_choose_cipher (scert, rcerts); if (cipher < 0) return NULL; mapi = smime_mapi_by_cipher (cipher); if (mapi < 0) return NULL; /* * XXX This is stretching it -- CreateEnvelopedData should probably * take a cipher itself of some sort, because we cannot know what the * future will bring in terms of parameters for each type of algorithm. * For example, just an algorithm and keysize is *not* sufficient to * fully specify the usage of RC5 (which also needs to know rounds and * block size). Work this out into a better API! */ encalg = smime_cipher_maps[mapi].algtag; keysize = smime_keysize_by_cipher (cipher); if (keysize < 0) return NULL; cinfo = SEC_PKCS7CreateEnvelopedData (scert, certUsageEmailRecipient, certdb, encalg, keysize, pwfn, pwfn_arg); if (cinfo == NULL) return NULL; for (rci = 0; rcerts[rci] != NULL; rci++) { if (rcerts[rci] == scert) continue; if (SEC_PKCS7AddRecipient (cinfo, rcerts[rci], certUsageEmailRecipient, NULL) != SECSuccess) { SEC_PKCS7DestroyContentInfo (cinfo); return NULL; } } return cinfo;}static smime_capability **smime_capabilities;static SECItem *smime_encoded_caps;static PRBool lastUsedFortezza;static SECStatussmime_init_caps (PRBool isFortezza){ smime_capability *cap; smime_cipher_map *map; SECOidData *oiddata; SECStatus rv; int i, capIndex; if (smime_encoded_caps != NULL && (! smime_prefs_changed) && lastUsedFortezza == isFortezza) return SECSuccess; if (smime_encoded_caps != NULL) { SECITEM_FreeItem (smime_encoded_caps, PR_TRUE); smime_encoded_caps = NULL; } if (smime_capabilities == NULL) { smime_capabilities = (smime_capability**)PORT_ZAlloc ( (smime_symmetric_count + 1) * sizeof(smime_capability *)); if (smime_capabilities == NULL) return SECFailure; } rv = SECFailure; /* The process of creating the encoded PKCS7 cipher capability list involves two basic steps: (a) Convert our internal representation of cipher preferences (smime_prefs) into an array containing cipher OIDs and parameter data (smime_capabilities). This step is performed here. (b) Encode, using ASN.1, the cipher information in smime_capabilities, leaving the encoded result in smime_encoded_caps. (In the process of performing (a), Lisa put in some optimizations which allow us to avoid needlessly re-populating elements in smime_capabilities as we walk through smime_prefs.) We want to use separate loop variables for smime_prefs and smime_capabilities because in the case where the Skipjack cipher is turned on in the prefs, but where we don't want to include Skipjack in the encoded capabilities (presumably due to using a non-fortezza cert when sending a message), we want to avoid creating an empty element in smime_capabilities. This would otherwise cause the encoding step to produce an empty set, since Skipjack happens to be the first cipher in smime_prefs, if it is turned on. */ for (i = 0, capIndex = 0; i < smime_current_pref_index; i++, capIndex++) { int mapi; /* Get the next cipher preference in smime_prefs. */ mapi = smime_mapi_by_cipher (smime_prefs[i]); if (mapi < 0) break; /* Find the corresponding entry in the cipher map. */ PORT_Assert (mapi < smime_symmetric_count); map = &(smime_cipher_maps[mapi]); /* If we're using a non-Fortezza cert, only advertise non-Fortezza capabilities. (We advertise all capabilities if we have a Fortezza cert.) */ if ((!isFortezza) && (map->cipher == SMIME_FORTEZZA)) { capIndex--; /* we want to visit the same caps index entry next time */ continue; } /* * Convert the next preference found in smime_prefs into an * smime_capability. */ cap = smime_capabilities[capIndex]; if (cap == NULL) { cap = (smime_capability*)PORT_ZAlloc (sizeof(smime_capability)); if (cap == NULL) break; smime_capabilities[capIndex] = cap; } else if (cap->cipher == smime_prefs[i]) { continue; /* no change to this one */ } cap->capIDTag = map->algtag; oiddata = SECOID_FindOIDByTag (map->algtag); if (oiddata == NULL) break; if (cap->capabilityID.data != NULL) { SECITEM_FreeItem (&(cap->capabilityID), PR_FALSE); cap->capabilityID.data = NULL; cap->capabilityID.len = 0; } rv = SECITEM_CopyItem (NULL, &(cap->capabilityID), &(oiddata->oid)); if (rv != SECSuccess) break; if (map->parms == NULL) { cap->parameters.data = NULL; cap->parameters.len = 0; } else { cap->parameters.data = map->parms->data; cap->parameters.len = map->parms->len; } cap->cipher = smime_prefs[i]; } if (i != smime_current_pref_index) return rv; while (capIndex < smime_symmetric_count) { cap = smime_capabilities[capIndex]; if (cap != NULL) { SECITEM_FreeItem (&(cap->capabilityID), PR_FALSE); PORT_Free (cap); } smime_capabilities[capIndex] = NULL; capIndex++; } smime_capabilities[capIndex] = NULL; smime_encoded_caps = SEC_ASN1EncodeItem (NULL, NULL, &smime_capabilities, smime_capabilities_template); if (smime_encoded_caps == NULL) return SECFailure; lastUsedFortezza = isFortezza; return SECSuccess;}static SECStatussmime_add_profile (CERTCertificate *cert, SEC_PKCS7ContentInfo *cinfo){ PRBool isFortezza = PR_FALSE; PORT_Assert (smime_prefs_complete); if (! smime_prefs_complete) return SECFailure; /* See if the sender's cert specifies Fortezza key exchange. */ if (cert != NULL) isFortezza = PK11_FortezzaHasKEA(cert); /* For that matter, if capabilities haven't been initialized yet, do so now. */ if (isFortezza != lastUsedFortezza || smime_encoded_caps == NULL || smime_prefs_changed) { SECStatus rv; rv = smime_init_caps(isFortezza); if (rv != SECSuccess) return rv; PORT_Assert (smime_encoded_caps != NULL); } return SEC_PKCS7AddSignedAttribute (cinfo, SEC_OID_PKCS9_SMIME_CAPABILITIES, smime_encoded_caps);}/* * Start an S/MIME signing context. * * "scert" is the cert that will be used to sign the data. It will be * checked for validity. * * "ecert" is the signer's encryption cert. If it is different from * scert, then it will be included in the signed message so that the * recipient can save it for future encryptions. * * "certdb" is the cert database to use for verifying the cert. * It can be NULL if a default database is available (like in the client). * * "digestalg" names the digest algorithm (e.g. SEC_OID_SHA1). * XXX There should be SECMIME functions for hashing, or the hashing should * be built into this interface, which we would like because we would * support more smartcards that way, and then this argument should go away.) * * "digest" is the actual digest of the data. It must be provided in * the case of detached data or NULL if the content will be included. * * This function already does all of the stuff specific to S/MIME protocol * and local policy; the return value just needs to be passed to * SEC_PKCS7Encode() or to SEC_PKCS7EncoderStart() to create the encoded data, * and finally to SEC_PKCS7DestroyContentInfo(). * * An error results in a return value of NULL and an error set. * (Retrieve specific errors via PORT_GetError()/XP_GetError().) */SEC_PKCS7ContentInfo *SECMIME_CreateSigned (CERTCertificate *scert, CERTCertificate *ecert, CERTCertDBHandle *certdb, SECOidTag digestalg, SECItem *digest, SECKEYGetPasswordKey pwfn, void *pwfn_arg){ SEC_PKCS7ContentInfo *cinfo; SECStatus rv; /* See note in header comment above about digestalg. */ PORT_Assert (digestalg == SEC_OID_SHA1); cinfo = SEC_PKCS7CreateSignedData (scert, certUsageEmailSigner, certdb, digestalg, digest, pwfn, pwfn_arg); if (cinfo == NULL) return NULL; if (SEC_PKCS7IncludeCertChain (cinfo, NULL) != SECSuccess) { SEC_PKCS7DestroyContentInfo (cinfo); return NULL; } /* if the encryption cert and the signing cert differ, then include * the encryption cert too. */ /* it is ok to compare the pointers since we ref count, and the same * cert will always have the same pointer */ if ( ( ecert != NULL ) && ( ecert != scert ) ) { rv = SEC_PKCS7AddCertificate(cinfo, ecert); if ( rv != SECSuccess ) { SEC_PKCS7DestroyContentInfo (cinfo); return NULL; } } /* * Add the signing time. But if it fails for some reason, * may as well not give up altogether -- just assert. */ rv = SEC_PKCS7AddSigningTime (cinfo); PORT_Assert (rv == SECSuccess); /* * Add the email profile. Again, if it fails for some reason, * may as well not give up altogether -- just assert. */ rv = smime_add_profile (ecert, cinfo); PORT_Assert (rv == SECSuccess); return cinfo;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?