andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * This file implements audit logging required by FIPS 140-2 Security andre@0: * Level 2. andre@0: */ andre@0: andre@0: #include "prprf.h" andre@0: #include "softoken.h" andre@0: andre@0: /* andre@0: * Print the value of the returned object handle in the output buffer andre@0: * on a successful return of the PKCS #11 function. If the PKCS #11 andre@0: * function failed or the pointer to object handle is NULL (which is andre@0: * the case for C_DeriveKey with CKM_TLS_KEY_AND_MAC_DERIVE), an empty andre@0: * string is stored in the output buffer. andre@0: * andre@0: * out: the output buffer andre@0: * outlen: the length of the output buffer andre@0: * argName: the name of the "pointer to object handle" argument andre@0: * phObject: the pointer to object handle andre@0: * rv: the return value of the PKCS #11 function andre@0: */ andre@0: static void sftk_PrintReturnedObjectHandle(char *out, PRUint32 outlen, andre@0: const char *argName, CK_OBJECT_HANDLE_PTR phObject, CK_RV rv) andre@0: { andre@0: if ((rv == CKR_OK) && phObject) { andre@0: PR_snprintf(out, outlen, andre@0: " *%s=0x%08lX", argName, (PRUint32)*phObject); andre@0: } else { andre@0: PORT_Assert(outlen != 0); andre@0: out[0] = '\0'; andre@0: } andre@0: } andre@0: andre@0: /* andre@0: * MECHANISM_BUFSIZE needs to be large enough for sftk_PrintMechanism, andre@0: * which uses <= 49 bytes. andre@0: */ andre@0: #define MECHANISM_BUFSIZE 64 andre@0: andre@0: static void sftk_PrintMechanism(char *out, PRUint32 outlen, andre@0: CK_MECHANISM_PTR pMechanism) andre@0: { andre@0: if (pMechanism) { andre@0: /* andre@0: * If we change the format string, we need to make sure andre@0: * MECHANISM_BUFSIZE is still large enough. We allow andre@0: * 20 bytes for %p on a 64-bit platform. andre@0: */ andre@0: PR_snprintf(out, outlen, "%p {mechanism=0x%08lX, ...}", andre@0: pMechanism, (PRUint32)pMechanism->mechanism); andre@0: } else { andre@0: PR_snprintf(out, outlen, "%p", pMechanism); andre@0: } andre@0: } andre@0: andre@0: void sftk_AuditCreateObject(CK_SESSION_HANDLE hSession, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, andre@0: CK_OBJECT_HANDLE_PTR phObject, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: char shObject[32]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintReturnedObjectHandle(shObject, sizeof shObject, andre@0: "phObject", phObject, rv); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_CreateObject(hSession=0x%08lX, pTemplate=%p, ulCount=%lu, " andre@0: "phObject=%p)=0x%08lX%s", andre@0: (PRUint32)hSession, pTemplate, (PRUint32)ulCount, andre@0: phObject, (PRUint32)rv, shObject); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_LOAD_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditCopyObject(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, andre@0: CK_OBJECT_HANDLE_PTR phNewObject, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: char shNewObject[32]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintReturnedObjectHandle(shNewObject, sizeof shNewObject, andre@0: "phNewObject", phNewObject, rv); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_CopyObject(hSession=0x%08lX, hObject=0x%08lX, " andre@0: "pTemplate=%p, ulCount=%lu, phNewObject=%p)=0x%08lX%s", andre@0: (PRUint32)hSession, (PRUint32)hObject, andre@0: pTemplate, (PRUint32)ulCount, phNewObject, (PRUint32)rv, shNewObject); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_COPY_KEY, msg); andre@0: } andre@0: andre@0: /* WARNING: hObject has been destroyed and can only be printed. */ andre@0: void sftk_AuditDestroyObject(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_DestroyObject(hSession=0x%08lX, hObject=0x%08lX)=0x%08lX", andre@0: (PRUint32)hSession, (PRUint32)hObject, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_DESTROY_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditGetObjectSize(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_GetObjectSize(hSession=0x%08lX, hObject=0x%08lX, " andre@0: "pulSize=%p)=0x%08lX", andre@0: (PRUint32)hSession, (PRUint32)hObject, andre@0: pulSize, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_ACCESS_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditGetAttributeValue(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, andre@0: CK_ULONG ulCount, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_GetAttributeValue(hSession=0x%08lX, hObject=0x%08lX, " andre@0: "pTemplate=%p, ulCount=%lu)=0x%08lX", andre@0: (PRUint32)hSession, (PRUint32)hObject, andre@0: pTemplate, (PRUint32)ulCount, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_ACCESS_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditSetAttributeValue(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, andre@0: CK_ULONG ulCount, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_SetAttributeValue(hSession=0x%08lX, hObject=0x%08lX, " andre@0: "pTemplate=%p, ulCount=%lu)=0x%08lX", andre@0: (PRUint32)hSession, (PRUint32)hObject, andre@0: pTemplate, (PRUint32)ulCount, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_CHANGE_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditCryptInit(const char *opName, CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: char mech[MECHANISM_BUFSIZE]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintMechanism(mech, sizeof mech, pMechanism); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_%sInit(hSession=0x%08lX, pMechanism=%s, " andre@0: "hKey=0x%08lX)=0x%08lX", andre@0: opName, (PRUint32)hSession, mech, andre@0: (PRUint32)hKey, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_CRYPT, msg); andre@0: } andre@0: andre@0: void sftk_AuditGenerateKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pTemplate, andre@0: CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: char mech[MECHANISM_BUFSIZE]; andre@0: char shKey[32]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintMechanism(mech, sizeof mech, pMechanism); andre@0: sftk_PrintReturnedObjectHandle(shKey, sizeof shKey, "phKey", phKey, rv); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_GenerateKey(hSession=0x%08lX, pMechanism=%s, " andre@0: "pTemplate=%p, ulCount=%lu, phKey=%p)=0x%08lX%s", andre@0: (PRUint32)hSession, mech, andre@0: pTemplate, (PRUint32)ulCount, phKey, (PRUint32)rv, shKey); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_GENERATE_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditGenerateKeyPair(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pPublicKeyTemplate, andre@0: CK_ULONG ulPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate, andre@0: CK_ULONG ulPrivateKeyAttributeCount, CK_OBJECT_HANDLE_PTR phPublicKey, andre@0: CK_OBJECT_HANDLE_PTR phPrivateKey, CK_RV rv) andre@0: { andre@0: char msg[512]; andre@0: char mech[MECHANISM_BUFSIZE]; andre@0: char shPublicKey[32]; andre@0: char shPrivateKey[32]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintMechanism(mech, sizeof mech, pMechanism); andre@0: sftk_PrintReturnedObjectHandle(shPublicKey, sizeof shPublicKey, andre@0: "phPublicKey", phPublicKey, rv); andre@0: sftk_PrintReturnedObjectHandle(shPrivateKey, sizeof shPrivateKey, andre@0: "phPrivateKey", phPrivateKey, rv); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_GenerateKeyPair(hSession=0x%08lX, pMechanism=%s, " andre@0: "pPublicKeyTemplate=%p, ulPublicKeyAttributeCount=%lu, " andre@0: "pPrivateKeyTemplate=%p, ulPrivateKeyAttributeCount=%lu, " andre@0: "phPublicKey=%p, phPrivateKey=%p)=0x%08lX%s%s", andre@0: (PRUint32)hSession, mech, andre@0: pPublicKeyTemplate, (PRUint32)ulPublicKeyAttributeCount, andre@0: pPrivateKeyTemplate, (PRUint32)ulPrivateKeyAttributeCount, andre@0: phPublicKey, phPrivateKey, (PRUint32)rv, shPublicKey, shPrivateKey); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_GENERATE_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditWrapKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hWrappingKey, andre@0: CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pWrappedKey, andre@0: CK_ULONG_PTR pulWrappedKeyLen, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: char mech[MECHANISM_BUFSIZE]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintMechanism(mech, sizeof mech, pMechanism); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_WrapKey(hSession=0x%08lX, pMechanism=%s, hWrappingKey=0x%08lX, " andre@0: "hKey=0x%08lX, pWrappedKey=%p, pulWrappedKeyLen=%p)=0x%08lX", andre@0: (PRUint32)hSession, mech, (PRUint32)hWrappingKey, andre@0: (PRUint32)hKey, pWrappedKey, pulWrappedKeyLen, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_WRAP_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditUnwrapKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hUnwrappingKey, andre@0: CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, andre@0: CK_OBJECT_HANDLE_PTR phKey, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: char mech[MECHANISM_BUFSIZE]; andre@0: char shKey[32]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintMechanism(mech, sizeof mech, pMechanism); andre@0: sftk_PrintReturnedObjectHandle(shKey, sizeof shKey, "phKey", phKey, rv); andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_UnwrapKey(hSession=0x%08lX, pMechanism=%s, " andre@0: "hUnwrappingKey=0x%08lX, pWrappedKey=%p, ulWrappedKeyLen=%lu, " andre@0: "pTemplate=%p, ulAttributeCount=%lu, phKey=%p)=0x%08lX%s", andre@0: (PRUint32)hSession, mech, andre@0: (PRUint32)hUnwrappingKey, pWrappedKey, (PRUint32)ulWrappedKeyLen, andre@0: pTemplate, (PRUint32)ulAttributeCount, phKey, (PRUint32)rv, shKey); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_UNWRAP_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditDeriveKey(CK_SESSION_HANDLE hSession, andre@0: CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hBaseKey, andre@0: CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, andre@0: CK_OBJECT_HANDLE_PTR phKey, CK_RV rv) andre@0: { andre@0: char msg[512]; andre@0: char mech[MECHANISM_BUFSIZE]; andre@0: char shKey[32]; andre@0: char sTlsKeys[128]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: sftk_PrintMechanism(mech, sizeof mech, pMechanism); andre@0: sftk_PrintReturnedObjectHandle(shKey, sizeof shKey, "phKey", phKey, rv); andre@0: if ((rv == CKR_OK) && andre@0: (pMechanism->mechanism == CKM_TLS_KEY_AND_MAC_DERIVE)) { andre@0: CK_SSL3_KEY_MAT_PARAMS *param = andre@0: (CK_SSL3_KEY_MAT_PARAMS *)pMechanism->pParameter; andre@0: CK_SSL3_KEY_MAT_OUT *keymat = param->pReturnedKeyMaterial; andre@0: PR_snprintf(sTlsKeys, sizeof sTlsKeys, andre@0: " hClientMacSecret=0x%08lX hServerMacSecret=0x%08lX" andre@0: " hClientKey=0x%08lX hServerKey=0x%08lX", andre@0: (PRUint32)keymat->hClientMacSecret, andre@0: (PRUint32)keymat->hServerMacSecret, andre@0: (PRUint32)keymat->hClientKey, andre@0: (PRUint32)keymat->hServerKey); andre@0: } else { andre@0: sTlsKeys[0] = '\0'; andre@0: } andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_DeriveKey(hSession=0x%08lX, pMechanism=%s, " andre@0: "hBaseKey=0x%08lX, pTemplate=%p, ulAttributeCount=%lu, " andre@0: "phKey=%p)=0x%08lX%s%s", andre@0: (PRUint32)hSession, mech, andre@0: (PRUint32)hBaseKey, pTemplate,(PRUint32)ulAttributeCount, andre@0: phKey, (PRUint32)rv, shKey, sTlsKeys); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_DERIVE_KEY, msg); andre@0: } andre@0: andre@0: void sftk_AuditDigestKey(CK_SESSION_HANDLE hSession, andre@0: CK_OBJECT_HANDLE hKey, CK_RV rv) andre@0: { andre@0: char msg[256]; andre@0: NSSAuditSeverity severity = (rv == CKR_OK) ? andre@0: NSS_AUDIT_INFO : NSS_AUDIT_ERROR; andre@0: andre@0: PR_snprintf(msg, sizeof msg, andre@0: "C_DigestKey(hSession=0x%08lX, hKey=0x%08lX)=0x%08lX", andre@0: (PRUint32)hSession, (PRUint32)hKey, (PRUint32)rv); andre@0: sftk_LogAuditMessage(severity, NSS_AUDIT_DIGEST_KEY, msg); andre@0: }