Skip to content

Commit 2037711

Browse files
committed
switch NSS_NoDB_Init() to NSS_InitContext()
libsrtp uses nss in the context of a system library. It is up to the application to call NSS_XXX_Init(). Changing to NSS_InitContext() allows libsrtp to initialize as much of nss as it requires without effecting the application. It also allows nss cleanup through the use of NSS_ShutdownContext() This fixes potential memory leaks.
1 parent 85d6a5d commit 2037711

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

crypto/cipher/aes_gcm_nss.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#include "err.h" /* for srtp_debug */
5454
#include "crypto_types.h"
5555
#include "cipher_types.h"
56-
#include <nss.h>
5756
#include <secerr.h>
5857
#include <nspr.h>
5958

@@ -82,6 +81,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
8281
int tlen)
8382
{
8483
srtp_aes_gcm_ctx_t *gcm;
84+
NSSInitContext *nss;
8585

8686
debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
8787
key_len);
@@ -99,24 +99,32 @@ static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
9999
return (srtp_err_status_bad_param);
100100
}
101101

102-
/* Initialize NSS */
103-
if (!NSS_IsInitialized() && NSS_NoDB_Init(NULL) != SECSuccess) {
102+
/* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
103+
nss = NSS_InitContext("", "", "", "", NULL,
104+
NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
105+
NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
106+
NSS_INIT_OPTIMIZESPACE);
107+
if (!nss) {
104108
return (srtp_err_status_cipher_fail);
105109
}
106110

107111
/* allocate memory a cipher of type aes_gcm */
108112
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
109113
if (*c == NULL) {
114+
NSS_ShutdownContext(nss);
110115
return (srtp_err_status_alloc_fail);
111116
}
112117

113118
gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
114119
if (gcm == NULL) {
120+
NSS_ShutdownContext(nss);
115121
srtp_crypto_free(*c);
116122
*c = NULL;
117123
return (srtp_err_status_alloc_fail);
118124
}
119125

126+
gcm->nss = nss;
127+
120128
/* set pointers */
121129
(*c)->state = gcm;
122130

@@ -161,6 +169,11 @@ static srtp_err_status_t srtp_aes_gcm_nss_dealloc(srtp_cipher_t *c)
161169
PK11_FreeSymKey(ctx->key);
162170
}
163171

172+
if (ctx->nss) {
173+
NSS_ShutdownContext(ctx->nss);
174+
ctx->nss = NULL;
175+
}
176+
164177
/* zeroize the key material */
165178
octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
166179
srtp_crypto_free(ctx);

crypto/cipher/aes_icm_nss.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include "err.h" /* for srtp_debug */
5353
#include "alloc.h"
5454
#include "cipher_types.h"
55-
#include <nss.h>
5655

5756
srtp_debug_module_t srtp_mod_aes_icm = {
5857
0, /* debugging is off by default */
@@ -106,6 +105,7 @@ static srtp_err_status_t srtp_aes_icm_nss_alloc(srtp_cipher_t **c,
106105
int tlen)
107106
{
108107
srtp_aes_icm_ctx_t *icm;
108+
NSSInitContext *nss;
109109

110110
debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
111111
key_len);
@@ -119,26 +119,33 @@ static srtp_err_status_t srtp_aes_icm_nss_alloc(srtp_cipher_t **c,
119119
return srtp_err_status_bad_param;
120120
}
121121

122-
/* Initialize NSS */
123-
if (!NSS_IsInitialized() && NSS_NoDB_Init(NULL) != SECSuccess) {
122+
/* Initialize NSS equiv of NSS_NoDB_Init(NULL) */
123+
nss = NSS_InitContext("", "", "", "", NULL,
124+
NSS_INIT_READONLY | NSS_INIT_NOCERTDB |
125+
NSS_INIT_NOMODDB | NSS_INIT_FORCEOPEN |
126+
NSS_INIT_OPTIMIZESPACE);
127+
if (!nss) {
124128
return (srtp_err_status_cipher_fail);
125129
}
126130

127131
/* allocate memory a cipher of type aes_icm */
128132
*c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
129133
if (*c == NULL) {
134+
NSS_ShutdownContext(nss);
130135
return srtp_err_status_alloc_fail;
131136
}
132137

133138
icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
134139
if (icm == NULL) {
140+
NSS_ShutdownContext(nss);
135141
srtp_crypto_free(*c);
136142
*c = NULL;
137143
return srtp_err_status_alloc_fail;
138144
}
139145

140146
icm->key = NULL;
141147
icm->ctx = NULL;
148+
icm->nss = nss;
142149

143150
/* set pointers */
144151
(*c)->state = icm;
@@ -188,6 +195,11 @@ static srtp_err_status_t srtp_aes_icm_nss_dealloc(srtp_cipher_t *c)
188195
ctx->ctx = NULL;
189196
}
190197

198+
if (ctx->nss) {
199+
NSS_ShutdownContext(ctx->nss);
200+
ctx->nss = NULL;
201+
}
202+
191203
/* zeroize everything */
192204
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
193205
srtp_crypto_free(ctx);

crypto/include/aes_gcm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ typedef struct {
6666

6767
#ifdef NSS
6868

69+
#include <nss.h>
6970
#include <pk11pub.h>
7071

7172
#define MAX_AD_SIZE 2048
@@ -74,6 +75,7 @@ typedef struct {
7475
int key_size;
7576
int tag_size;
7677
srtp_cipher_direction_t dir;
78+
NSSInitContext *nss;
7779
PK11SymKey *key;
7880
uint8_t iv[12];
7981
uint8_t aad[MAX_AD_SIZE];

crypto/include/aes_icm_ext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ typedef struct {
6565

6666
#ifdef NSS
6767

68+
#include <nss.h>
6869
#include <pk11pub.h>
6970

7071
typedef struct {
7172
v128_t counter;
7273
v128_t offset;
7374
int key_size;
7475
uint8_t iv[16];
76+
NSSInitContext *nss;
7577
PK11SymKey *key;
7678
PK11Context *ctx;
7779
} srtp_aes_icm_ctx_t;

0 commit comments

Comments
 (0)