Coverage Report

Created: 2026-06-07 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/pk11wrap/pk11kea.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
/*
5
 * This file implements the Symkey wrapper and the PKCS context
6
 * Interfaces.
7
 */
8
9
#include <stddef.h>
10
11
#include "seccomon.h"
12
#include "secmod.h"
13
#include "secmodi.h"
14
#include "secmodti.h"
15
#include "pkcs11.h"
16
#include "pk11func.h"
17
#include "secitem.h"
18
#include "keyhi.h"
19
#include "secasn1.h"
20
#include "sechash.h"
21
#include "cert.h"
22
#include "secerr.h"
23
24
/*
25
 * find an RSA public key on a card
26
 */
27
static CK_OBJECT_HANDLE
28
pk11_FindRSAPubKey(PK11SlotInfo *slot)
29
0
{
30
0
    CK_KEY_TYPE key_type = CKK_RSA;
31
0
    CK_OBJECT_CLASS class_type = CKO_PUBLIC_KEY;
32
0
    CK_ATTRIBUTE theTemplate[2];
33
0
    size_t template_count = sizeof(theTemplate) / sizeof(theTemplate[0]);
34
0
    CK_ATTRIBUTE *attrs = theTemplate;
35
36
0
    PK11_SETATTRS(attrs, CKA_CLASS, &class_type, sizeof(class_type));
37
0
    attrs++;
38
0
    PK11_SETATTRS(attrs, CKA_KEY_TYPE, &key_type, sizeof(key_type));
39
0
    attrs++;
40
0
    template_count = attrs - theTemplate;
41
0
    PR_ASSERT(template_count <= sizeof(theTemplate) / sizeof(CK_ATTRIBUTE));
42
43
0
    return pk11_FindObjectByTemplate(slot, theTemplate, template_count);
44
0
}
45
46
PK11SymKey *
47
pk11_KeyExchange(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
48
                 CK_ATTRIBUTE_TYPE operation, CK_FLAGS flags,
49
                 PRBool isPerm, PK11SymKey *symKey)
50
0
{
51
0
    PK11SymKey *newSymKey = NULL;
52
0
    SECStatus rv;
53
    /* performance improvement can go here --- use a generated key at startup
54
     * to generate a per token wrapping key. If it exists, use it, otherwise
55
     * do a full key exchange. */
56
57
    /* find a common Key Exchange algorithm */
58
    /* RSA */
59
0
    if (PK11_DoesMechanism(symKey->slot, CKM_RSA_PKCS) &&
60
0
        PK11_DoesMechanism(slot, CKM_RSA_PKCS)) {
61
0
        CK_OBJECT_HANDLE pubKeyHandle = CK_INVALID_HANDLE;
62
0
        CK_OBJECT_HANDLE privKeyHandle = CK_INVALID_HANDLE;
63
0
        SECKEYPublicKey *pubKey = NULL;
64
0
        SECKEYPrivateKey *privKey = NULL;
65
0
        SECItem wrapData;
66
0
        unsigned int symKeyLength = PK11_GetKeyLength(symKey);
67
68
0
        wrapData.data = NULL;
69
70
        /* find RSA Public Key on target */
71
0
        pubKeyHandle = pk11_FindRSAPubKey(slot);
72
0
        if (pubKeyHandle != CK_INVALID_HANDLE) {
73
0
            privKeyHandle = PK11_MatchItem(slot, pubKeyHandle, CKO_PRIVATE_KEY);
74
0
        }
75
76
        /* if no key exists, generate a key pair */
77
0
        if (privKeyHandle == CK_INVALID_HANDLE) {
78
0
            PK11RSAGenParams rsaParams;
79
80
0
            if (symKeyLength > 120) /* bytes */ {
81
                /* we'd have to generate an RSA key pair > 1024 bits long,
82
                ** and that's too costly.  Don't even try.
83
                */
84
0
                PORT_SetError(SEC_ERROR_CANNOT_MOVE_SENSITIVE_KEY);
85
0
                goto rsa_failed;
86
0
            }
87
0
            rsaParams.keySizeInBits = 1024;
88
0
            rsaParams.pe = 0x10001;
89
0
            privKey = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN,
90
0
                                           &rsaParams, &pubKey, PR_FALSE, PR_TRUE, symKey->cx);
91
0
        } else {
92
            /* if keys exist, build SECKEY data structures for them */
93
0
            privKey = pk11_MakePrivKey(slot, nullKey, PR_FALSE, privKeyHandle,
94
0
                                       symKey->cx);
95
0
            if (privKey != NULL) {
96
0
                pubKey = PK11_ExtractPublicKey(slot, rsaKey, pubKeyHandle);
97
0
                if (pubKey && pubKey->pkcs11Slot) {
98
0
                    PK11_FreeSlot(pubKey->pkcs11Slot);
99
0
                    pubKey->pkcs11Slot = NULL;
100
0
                    pubKey->pkcs11ID = CK_INVALID_HANDLE;
101
0
                }
102
0
            }
103
0
        }
104
0
        if (privKey == NULL)
105
0
            goto rsa_failed;
106
0
        if (pubKey == NULL)
107
0
            goto rsa_failed;
108
109
0
        wrapData.len = SECKEY_PublicKeyStrength(pubKey);
110
0
        if (!wrapData.len)
111
0
            goto rsa_failed;
112
0
        wrapData.data = PORT_Alloc(wrapData.len);
113
0
        if (wrapData.data == NULL)
114
0
            goto rsa_failed;
115
116
        /* now wrap the keys in and out */
117
0
        rv = PK11_PubWrapSymKey(CKM_RSA_PKCS, pubKey, symKey, &wrapData);
118
0
        if (rv == SECSuccess) {
119
0
            newSymKey = PK11_PubUnwrapSymKeyWithFlagsPerm(privKey,
120
0
                                                          &wrapData, type, operation,
121
0
                                                          symKeyLength, flags, isPerm);
122
            /* make sure we wound up where we wanted to be! */
123
0
            if (newSymKey && newSymKey->slot != slot) {
124
0
                PK11_FreeSymKey(newSymKey);
125
0
                newSymKey = NULL;
126
0
            }
127
0
        }
128
0
    rsa_failed:
129
0
        if (wrapData.data != NULL)
130
0
            PORT_Free(wrapData.data);
131
0
        if (privKey != NULL)
132
0
            SECKEY_DestroyPrivateKey(privKey);
133
0
        if (pubKey != NULL)
134
0
            SECKEY_DestroyPublicKey(pubKey);
135
136
0
        return newSymKey;
137
0
    }
138
0
    PORT_SetError(SEC_ERROR_NO_MODULE);
139
    return NULL;
140
0
}