summaryrefslogtreecommitdiff
path: root/security/nss/lib/softoken/fipsaudt.c
blob: e0fd641eb831c70ee56518e5946f93fdad132fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * This file implements audit logging required by FIPS 140-2 Security
 * Level 2.
 */

#include "prprf.h"
#include "softoken.h"

/*
 * Print the value of the returned object handle in the output buffer
 * on a successful return of the PKCS #11 function.  If the PKCS #11
 * function failed or the pointer to object handle is NULL (which is
 * the case for C_DeriveKey with CKM_TLS_KEY_AND_MAC_DERIVE), an empty
 * string is stored in the output buffer.
 *
 * out: the output buffer
 * outlen: the length of the output buffer
 * argName: the name of the "pointer to object handle" argument
 * phObject: the pointer to object handle
 * rv: the return value of the PKCS #11 function
 */
static void
sftk_PrintReturnedObjectHandle(char *out, PRUint32 outlen,
                               const char *argName, CK_OBJECT_HANDLE_PTR phObject, CK_RV rv)
{
    if ((rv == CKR_OK) && phObject) {
        PR_snprintf(out, outlen,
                    " *%s=0x%08lX", argName, (PRUint32)*phObject);
    } else {
        PORT_Assert(outlen != 0);
        out[0] = '\0';
    }
}

/*
 * MECHANISM_BUFSIZE needs to be large enough for sftk_PrintMechanism,
 * which uses <= 49 bytes.
 */
#define MECHANISM_BUFSIZE 64

static void
sftk_PrintMechanism(char *out, PRUint32 outlen,
                    CK_MECHANISM_PTR pMechanism)
{
    if (pMechanism) {
        /*
         * If we change the format string, we need to make sure
         * MECHANISM_BUFSIZE is still large enough.  We allow
         * 20 bytes for %p on a 64-bit platform.
         */
        PR_snprintf(out, outlen, "%p {mechanism=0x%08lX, ...}",
                    pMechanism, (PRUint32)pMechanism->mechanism);
    } else {
        PR_snprintf(out, outlen, "%p", pMechanism);
    }
}

void
sftk_AuditCreateObject(CK_SESSION_HANDLE hSession,
                       CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
                       CK_OBJECT_HANDLE_PTR phObject, CK_RV rv)
{
    char msg[256];
    char shObject[32];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintReturnedObjectHandle(shObject, sizeof shObject,
                                   "phObject", phObject, rv);
    PR_snprintf(msg, sizeof msg,
                "C_CreateObject(hSession=0x%08lX, pTemplate=%p, ulCount=%lu, "
                "phObject=%p)=0x%08lX%s",
                (PRUint32)hSession, pTemplate, (PRUint32)ulCount,
                phObject, (PRUint32)rv, shObject);
    sftk_LogAuditMessage(severity, NSS_AUDIT_LOAD_KEY, msg);
}

void
sftk_AuditCopyObject(CK_SESSION_HANDLE hSession,
                     CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
                     CK_OBJECT_HANDLE_PTR phNewObject, CK_RV rv)
{
    char msg[256];
    char shNewObject[32];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintReturnedObjectHandle(shNewObject, sizeof shNewObject,
                                   "phNewObject", phNewObject, rv);
    PR_snprintf(msg, sizeof msg,
                "C_CopyObject(hSession=0x%08lX, hObject=0x%08lX, "
                "pTemplate=%p, ulCount=%lu, phNewObject=%p)=0x%08lX%s",
                (PRUint32)hSession, (PRUint32)hObject,
                pTemplate, (PRUint32)ulCount, phNewObject, (PRUint32)rv, shNewObject);
    sftk_LogAuditMessage(severity, NSS_AUDIT_COPY_KEY, msg);
}

/* WARNING: hObject has been destroyed and can only be printed. */
void
sftk_AuditDestroyObject(CK_SESSION_HANDLE hSession,
                        CK_OBJECT_HANDLE hObject, CK_RV rv)
{
    char msg[256];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    PR_snprintf(msg, sizeof msg,
                "C_DestroyObject(hSession=0x%08lX, hObject=0x%08lX)=0x%08lX",
                (PRUint32)hSession, (PRUint32)hObject, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_DESTROY_KEY, msg);
}

void
sftk_AuditGetObjectSize(CK_SESSION_HANDLE hSession,
                        CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pulSize, CK_RV rv)
{
    char msg[256];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    PR_snprintf(msg, sizeof msg,
                "C_GetObjectSize(hSession=0x%08lX, hObject=0x%08lX, "
                "pulSize=%p)=0x%08lX",
                (PRUint32)hSession, (PRUint32)hObject,
                pulSize, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_ACCESS_KEY, msg);
}

void
sftk_AuditGetAttributeValue(CK_SESSION_HANDLE hSession,
                            CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate,
                            CK_ULONG ulCount, CK_RV rv)
{
    char msg[256];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    PR_snprintf(msg, sizeof msg,
                "C_GetAttributeValue(hSession=0x%08lX, hObject=0x%08lX, "
                "pTemplate=%p, ulCount=%lu)=0x%08lX",
                (PRUint32)hSession, (PRUint32)hObject,
                pTemplate, (PRUint32)ulCount, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_ACCESS_KEY, msg);
}

void
sftk_AuditSetAttributeValue(CK_SESSION_HANDLE hSession,
                            CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate,
                            CK_ULONG ulCount, CK_RV rv)
{
    char msg[256];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    PR_snprintf(msg, sizeof msg,
                "C_SetAttributeValue(hSession=0x%08lX, hObject=0x%08lX, "
                "pTemplate=%p, ulCount=%lu)=0x%08lX",
                (PRUint32)hSession, (PRUint32)hObject,
                pTemplate, (PRUint32)ulCount, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_CHANGE_KEY, msg);
}

void
sftk_AuditCryptInit(const char *opName, CK_SESSION_HANDLE hSession,
                    CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey, CK_RV rv)
{
    char msg[256];
    char mech[MECHANISM_BUFSIZE];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintMechanism(mech, sizeof mech, pMechanism);
    PR_snprintf(msg, sizeof msg,
                "C_%sInit(hSession=0x%08lX, pMechanism=%s, "
                "hKey=0x%08lX)=0x%08lX",
                opName, (PRUint32)hSession, mech,
                (PRUint32)hKey, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_CRYPT, msg);
}

void
sftk_AuditGenerateKey(CK_SESSION_HANDLE hSession,
                      CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pTemplate,
                      CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey, CK_RV rv)
{
    char msg[256];
    char mech[MECHANISM_BUFSIZE];
    char shKey[32];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintMechanism(mech, sizeof mech, pMechanism);
    sftk_PrintReturnedObjectHandle(shKey, sizeof shKey, "phKey", phKey, rv);
    PR_snprintf(msg, sizeof msg,
                "C_GenerateKey(hSession=0x%08lX, pMechanism=%s, "
                "pTemplate=%p, ulCount=%lu, phKey=%p)=0x%08lX%s",
                (PRUint32)hSession, mech,
                pTemplate, (PRUint32)ulCount, phKey, (PRUint32)rv, shKey);
    sftk_LogAuditMessage(severity, NSS_AUDIT_GENERATE_KEY, msg);
}

void
sftk_AuditGenerateKeyPair(CK_SESSION_HANDLE hSession,
                          CK_MECHANISM_PTR pMechanism, CK_ATTRIBUTE_PTR pPublicKeyTemplate,
                          CK_ULONG ulPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
                          CK_ULONG ulPrivateKeyAttributeCount, CK_OBJECT_HANDLE_PTR phPublicKey,
                          CK_OBJECT_HANDLE_PTR phPrivateKey, CK_RV rv)
{
    char msg[512];
    char mech[MECHANISM_BUFSIZE];
    char shPublicKey[32];
    char shPrivateKey[32];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintMechanism(mech, sizeof mech, pMechanism);
    sftk_PrintReturnedObjectHandle(shPublicKey, sizeof shPublicKey,
                                   "phPublicKey", phPublicKey, rv);
    sftk_PrintReturnedObjectHandle(shPrivateKey, sizeof shPrivateKey,
                                   "phPrivateKey", phPrivateKey, rv);
    PR_snprintf(msg, sizeof msg,
                "C_GenerateKeyPair(hSession=0x%08lX, pMechanism=%s, "
                "pPublicKeyTemplate=%p, ulPublicKeyAttributeCount=%lu, "
                "pPrivateKeyTemplate=%p, ulPrivateKeyAttributeCount=%lu, "
                "phPublicKey=%p, phPrivateKey=%p)=0x%08lX%s%s",
                (PRUint32)hSession, mech,
                pPublicKeyTemplate, (PRUint32)ulPublicKeyAttributeCount,
                pPrivateKeyTemplate, (PRUint32)ulPrivateKeyAttributeCount,
                phPublicKey, phPrivateKey, (PRUint32)rv, shPublicKey, shPrivateKey);
    sftk_LogAuditMessage(severity, NSS_AUDIT_GENERATE_KEY, msg);
}

void
sftk_AuditWrapKey(CK_SESSION_HANDLE hSession,
                  CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hWrappingKey,
                  CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pWrappedKey,
                  CK_ULONG_PTR pulWrappedKeyLen, CK_RV rv)
{
    char msg[256];
    char mech[MECHANISM_BUFSIZE];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintMechanism(mech, sizeof mech, pMechanism);
    PR_snprintf(msg, sizeof msg,
                "C_WrapKey(hSession=0x%08lX, pMechanism=%s, hWrappingKey=0x%08lX, "
                "hKey=0x%08lX, pWrappedKey=%p, pulWrappedKeyLen=%p)=0x%08lX",
                (PRUint32)hSession, mech, (PRUint32)hWrappingKey,
                (PRUint32)hKey, pWrappedKey, pulWrappedKeyLen, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_WRAP_KEY, msg);
}

void
sftk_AuditUnwrapKey(CK_SESSION_HANDLE hSession,
                    CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hUnwrappingKey,
                    CK_BYTE_PTR pWrappedKey, CK_ULONG ulWrappedKeyLen,
                    CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
                    CK_OBJECT_HANDLE_PTR phKey, CK_RV rv)
{
    char msg[256];
    char mech[MECHANISM_BUFSIZE];
    char shKey[32];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintMechanism(mech, sizeof mech, pMechanism);
    sftk_PrintReturnedObjectHandle(shKey, sizeof shKey, "phKey", phKey, rv);
    PR_snprintf(msg, sizeof msg,
                "C_UnwrapKey(hSession=0x%08lX, pMechanism=%s, "
                "hUnwrappingKey=0x%08lX, pWrappedKey=%p, ulWrappedKeyLen=%lu, "
                "pTemplate=%p, ulAttributeCount=%lu, phKey=%p)=0x%08lX%s",
                (PRUint32)hSession, mech,
                (PRUint32)hUnwrappingKey, pWrappedKey, (PRUint32)ulWrappedKeyLen,
                pTemplate, (PRUint32)ulAttributeCount, phKey, (PRUint32)rv, shKey);
    sftk_LogAuditMessage(severity, NSS_AUDIT_UNWRAP_KEY, msg);
}

void
sftk_AuditDeriveKey(CK_SESSION_HANDLE hSession,
                    CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hBaseKey,
                    CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount,
                    CK_OBJECT_HANDLE_PTR phKey, CK_RV rv)
{
    char msg[512];
    char mech[MECHANISM_BUFSIZE];
    char shKey[32];
    char sTlsKeys[128];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    sftk_PrintMechanism(mech, sizeof mech, pMechanism);
    sftk_PrintReturnedObjectHandle(shKey, sizeof shKey, "phKey", phKey, rv);
    if ((rv == CKR_OK) &&
        (pMechanism->mechanism == CKM_TLS_KEY_AND_MAC_DERIVE)) {
        CK_SSL3_KEY_MAT_PARAMS *param =
            (CK_SSL3_KEY_MAT_PARAMS *)pMechanism->pParameter;
        CK_SSL3_KEY_MAT_OUT *keymat = param->pReturnedKeyMaterial;
        PR_snprintf(sTlsKeys, sizeof sTlsKeys,
                    " hClientMacSecret=0x%08lX hServerMacSecret=0x%08lX"
                    " hClientKey=0x%08lX hServerKey=0x%08lX",
                    (PRUint32)keymat->hClientMacSecret,
                    (PRUint32)keymat->hServerMacSecret,
                    (PRUint32)keymat->hClientKey,
                    (PRUint32)keymat->hServerKey);
    } else {
        sTlsKeys[0] = '\0';
    }
    PR_snprintf(msg, sizeof msg,
                "C_DeriveKey(hSession=0x%08lX, pMechanism=%s, "
                "hBaseKey=0x%08lX, pTemplate=%p, ulAttributeCount=%lu, "
                "phKey=%p)=0x%08lX%s%s",
                (PRUint32)hSession, mech,
                (PRUint32)hBaseKey, pTemplate, (PRUint32)ulAttributeCount,
                phKey, (PRUint32)rv, shKey, sTlsKeys);
    sftk_LogAuditMessage(severity, NSS_AUDIT_DERIVE_KEY, msg);
}

void
sftk_AuditDigestKey(CK_SESSION_HANDLE hSession,
                    CK_OBJECT_HANDLE hKey, CK_RV rv)
{
    char msg[256];
    NSSAuditSeverity severity = (rv == CKR_OK) ? NSS_AUDIT_INFO : NSS_AUDIT_ERROR;

    PR_snprintf(msg, sizeof msg,
                "C_DigestKey(hSession=0x%08lX, hKey=0x%08lX)=0x%08lX",
                (PRUint32)hSession, (PRUint32)hKey, (PRUint32)rv);
    sftk_LogAuditMessage(severity, NSS_AUDIT_DIGEST_KEY, msg);
}