@@ -38,27 +38,38 @@ static GHashTable *tokens = NULL, *allowed_plugins = NULL;
3838static gboolean auth_enabled = FALSE;
3939static janus_mutex mutex = JANUS_MUTEX_INITIALIZER ;
4040static char * auth_secret = NULL ;
41+ static const EVP_MD * auth_hash = NULL ;
4142
4243static void janus_auth_free_token (char * token ) {
4344 g_free (token );
4445}
4546
4647/* Setup */
47- void janus_auth_init (gboolean enabled , const char * secret ) {
48+ int janus_auth_init (gboolean enabled , const char * secret , const char * hash ) {
4849 if (enabled ) {
4950 if (secret == NULL ) {
5051 JANUS_LOG (LOG_INFO , "Stored-Token based authentication enabled\n" );
5152 tokens = g_hash_table_new_full (g_str_hash , g_str_equal , (GDestroyNotify )janus_auth_free_token , NULL );
5253 allowed_plugins = g_hash_table_new_full (g_str_hash , g_str_equal , (GDestroyNotify )janus_auth_free_token , NULL );
5354 auth_enabled = TRUE;
5455 } else {
55- JANUS_LOG (LOG_INFO , "Signed-Token based authentication enabled\n" );
56+ if (hash == NULL || !strcasecmp (hash , "sha1" )) {
57+ auth_hash = EVP_sha1 ();
58+ } else if (!strcasecmp (hash , "sha256" )) {
59+ auth_hash = EVP_sha256 ();
60+ } else {
61+ JANUS_LOG (LOG_ERR , "Unsupported token_auth_hash '%s' (supported values: sha1, sha256)\n" , hash );
62+ return -1 ;
63+ }
64+ JANUS_LOG (LOG_INFO , "Signed-Token based authentication enabled (HMAC-%s)\n" ,
65+ auth_hash == EVP_sha256 () ? "SHA256" : "SHA1" );
5666 auth_secret = g_strdup (secret );
5767 auth_enabled = TRUE;
5868 }
5969 } else {
6070 JANUS_LOG (LOG_INFO , "Token based authentication disabled\n" );
6171 }
72+ return 0 ;
6273}
6374
6475gboolean janus_auth_is_enabled (void ) {
@@ -106,10 +117,10 @@ gboolean janus_auth_check_signature(const char *token, const char *realm) {
106117 /* Verify realm */
107118 if (strcmp (data [1 ], realm ))
108119 goto fail ;
109- /* Verify HMAC-SHA1 */
120+ /* Verify HMAC signature */
110121 unsigned char signature [EVP_MAX_MD_SIZE ] = "" ;
111122 unsigned int len ;
112- HMAC (EVP_sha1 () , auth_secret , strlen (auth_secret ), (const unsigned char * )parts [0 ], strlen (parts [0 ]), signature , & len );
123+ HMAC (auth_hash , auth_secret , strlen (auth_secret ), (const unsigned char * )parts [0 ], strlen (parts [0 ]), signature , & len );
113124 gchar * base64 = g_base64_encode (signature , len );
114125 gboolean result = janus_strcmp_const_time (parts [1 ], base64 );
115126 g_strfreev (data );
@@ -157,10 +168,10 @@ gboolean janus_auth_check_signature_contains(const char *token, const char *real
157168 }
158169 if (!result )
159170 goto fail ;
160- /* Verify HMAC-SHA1 */
171+ /* Verify HMAC signature */
161172 unsigned char signature [EVP_MAX_MD_SIZE ] = "" ;
162173 unsigned int len ;
163- HMAC (EVP_sha1 () , auth_secret , strlen (auth_secret ), (const unsigned char * )parts [0 ], strlen (parts [0 ]), signature , & len );
174+ HMAC (auth_hash , auth_secret , strlen (auth_secret ), (const unsigned char * )parts [0 ], strlen (parts [0 ]), signature , & len );
164175 gchar * base64 = g_base64_encode (signature , len );
165176 result = janus_strcmp_const_time (parts [1 ], base64 );
166177 g_strfreev (data );
0 commit comments