53
53
#include "err.h" /* for srtp_debug */
54
54
#include "crypto_types.h"
55
55
#include "cipher_types.h"
56
- #include <nss.h>
57
56
#include <secerr.h>
58
57
#include <nspr.h>
59
58
@@ -82,6 +81,7 @@ static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
82
81
int tlen )
83
82
{
84
83
srtp_aes_gcm_ctx_t * gcm ;
84
+ NSSInitContext * nss ;
85
85
86
86
debug_print (srtp_mod_aes_gcm , "allocating cipher with key length %d" ,
87
87
key_len );
@@ -99,24 +99,32 @@ static srtp_err_status_t srtp_aes_gcm_nss_alloc(srtp_cipher_t **c,
99
99
return (srtp_err_status_bad_param );
100
100
}
101
101
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 ) {
104
108
return (srtp_err_status_cipher_fail );
105
109
}
106
110
107
111
/* allocate memory a cipher of type aes_gcm */
108
112
* c = (srtp_cipher_t * )srtp_crypto_alloc (sizeof (srtp_cipher_t ));
109
113
if (* c == NULL ) {
114
+ NSS_ShutdownContext (nss );
110
115
return (srtp_err_status_alloc_fail );
111
116
}
112
117
113
118
gcm = (srtp_aes_gcm_ctx_t * )srtp_crypto_alloc (sizeof (srtp_aes_gcm_ctx_t ));
114
119
if (gcm == NULL ) {
120
+ NSS_ShutdownContext (nss );
115
121
srtp_crypto_free (* c );
116
122
* c = NULL ;
117
123
return (srtp_err_status_alloc_fail );
118
124
}
119
125
126
+ gcm -> nss = nss ;
127
+
120
128
/* set pointers */
121
129
(* c )-> state = gcm ;
122
130
@@ -161,6 +169,11 @@ static srtp_err_status_t srtp_aes_gcm_nss_dealloc(srtp_cipher_t *c)
161
169
PK11_FreeSymKey (ctx -> key );
162
170
}
163
171
172
+ if (ctx -> nss ) {
173
+ NSS_ShutdownContext (ctx -> nss );
174
+ ctx -> nss = NULL ;
175
+ }
176
+
164
177
/* zeroize the key material */
165
178
octet_string_set_to_zero (ctx , sizeof (srtp_aes_gcm_ctx_t ));
166
179
srtp_crypto_free (ctx );
0 commit comments