lm_ssl.c
changeset 57 37ed3c7ac1b6
parent 54 6bef2082e5f9
child 59 19cfaceda6bb
equal deleted inserted replaced
56:d53804c0fb6f 57:37ed3c7ac1b6
    66 		return LM_SSL_RESPONSE_CONTINUE;
    66 		return LM_SSL_RESPONSE_CONTINUE;
    67 	else
    67 	else
    68 		return LM_SSL_RESPONSE_STOP;
    68 		return LM_SSL_RESPONSE_STOP;
    69 }
    69 }
    70 
    70 
       
    71 #ifndef HAVE_LM_SHA256_FINGERPRINTS
    71 static void string2fingerprint (const char *string, char *buffer)
    72 static void string2fingerprint (const char *string, char *buffer)
    72 {
    73 {
    73 	int i;
    74 	int i;
    74 	for (i = 0; i < 16; i++) {
    75 	for (i = 0; i < 16; i++) {
    75 		int h = g_ascii_xdigit_value ((char)string[i*3]);
    76 		int h = g_ascii_xdigit_value ((char)string[i*3]);
    76 		int l = g_ascii_xdigit_value ((char)string[i*3+1]);
    77 		int l = g_ascii_xdigit_value ((char)string[i*3+1]);
    77 		buffer[i] = (char) ((h >= 0 && l >= 0) ? h*16 + l : 0);
    78 		buffer[i] = (char) ((h >= 0 && l >= 0) ? h*16 + l : 0);
    78 	}
    79 	}
    79 }
    80 }
       
    81 #endif
    80 
    82 
    81 /// lm.ssl.new
    83 /// lm.ssl.new
    82 /// Creates new ssl object for use with connection.
    84 /// Creates new ssl object for use with connection.
    83 /// You can specify server key fingerprint, callback function for error handling,
    85 /// You can specify server key fingerprint, callback function for error handling,
    84 /// both, or neither. Though, fingerprint should go before callback function.
    86 /// both, or neither. Though, fingerprint should go before callback function.
    85 /// SSL fingerprint is a string like '01:23:45:67:89:AB:CD:EF:FE:DC:BA:98:76:54:32:10'.
    87 /// SSL fingerprint is a string like 'SHA256:ABCDEF123456...' (or
       
    88 /// '01:23:45:67:89:AB:CD:EF:FE:DC:BA:98:76:54:32:10' for LM versions, older than 1.5.3).
    86 /// A: string (optional ssl fingerprint), ssl callback function (optional)
    89 /// A: string (optional ssl fingerprint), ssl callback function (optional)
    87 /// R: userdata (lm ssl object)
    90 /// R: userdata (lm ssl object)
    88 static int new_lm_ssl (lua_State *L)
    91 static int new_lm_ssl (lua_State *L)
    89 {
    92 {
    90 	int args = lua_gettop (L);
    93 	int args = lua_gettop (L);
    91 	LmSSL *ssl;
    94 	LmSSL *ssl;
    92 	if (args == 0)
    95 	if (args == 0)
    93 		ssl = lm_ssl_new (NULL, NULL, NULL, NULL);
    96 		ssl = lm_ssl_new (NULL, NULL, NULL, NULL);
    94 	else if (args == 1 && !lua_isfunction (L, 1)) {
    97 	else if (args == 1 && !lua_isfunction (L, 1)) {
       
    98 		const char *fingerprint = luaL_checkstring (L, 1);
       
    99 #ifndef HAVE_LM_SHA256_FINGERPRINTS
    95 		gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
   100 		gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    96 		const char *fingerprint = luaL_checkstring (L, 1);
   101 
    97 
   102 		if (lua_rawlen (L, 1) > 46) {
    98 		if (lua_rawlen (L, 1) > 46)
       
    99 			string2fingerprint (fingerprint, buffer);
   103 			string2fingerprint (fingerprint, buffer);
   100 		ssl = lm_ssl_new (buffer, NULL, NULL, NULL);
   104 			fingerprint = buffer;
       
   105 		} else
       
   106 			fingerprint = NULL;
       
   107 #endif
       
   108 		ssl = lm_ssl_new (fingerprint, NULL, NULL, NULL);
   101 	} else {
   109 	} else {
   102 		llm_callback_t *cb;
   110 		llm_callback_t *cb;
   103 		gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
   111 		const char *fingerprint = NULL;
   104 
   112 
   105 		if (args > 1) {
   113 		if (args > 1) {
   106 			const char *fingerprint = luaL_checkstring (L, 1);
   114 			fingerprint = luaL_checkstring (L, 1);
   107 			if (lua_rawlen (L, 1) > 46)
   115 #ifndef HAVE_LM_SHA256_FINGERPRINTS
       
   116 			gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
       
   117 
       
   118 			if (lua_rawlen (L, 1) > 46) {
   108 				string2fingerprint (fingerprint, buffer);
   119 				string2fingerprint (fingerprint, buffer);
       
   120 				fingerprint = buffer;
       
   121 			} else
       
   122 				fingerprint = NULL;
       
   123 #endif
   109 			luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
   124 			luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
   110 		} else
   125 		} else
   111 			luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected");
   126 			luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected");
   112 		
   127 		
   113 		cb = luaL_malloc (L, sizeof (llm_callback_t));
   128 		cb = luaL_malloc (L, sizeof (llm_callback_t));
   114 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   129 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   115 		cb->L = L;
   130 		cb->L = L;
   116 
   131 
   117 		ssl = lm_ssl_new ((args > 1) ? buffer : NULL, (LmSSLFunction)callback_lm_ssl,
   132 		ssl = lm_ssl_new (fingerprint, (LmSSLFunction)callback_lm_ssl,
   118 							cb, (GDestroyNotify)llm_callback_destroy);
   133 							cb, (GDestroyNotify)llm_callback_destroy);
   119 	}
   134 	}
   120 	bless_lm_ssl (L, ssl);
   135 	bless_lm_ssl (L, ssl);
   121 	lm_ssl_unref (ssl); // XXX
   136 	lm_ssl_unref (ssl); // XXX
   122 	D ("SSL %p created", ssl);
   137 	D ("SSL %p created", ssl);
   133 	bless_lm_ssl (L, lua_touserdata (L, 1));
   148 	bless_lm_ssl (L, lua_touserdata (L, 1));
   134 	return 1;
   149 	return 1;
   135 }
   150 }
   136 
   151 
   137 /// lm.ssl.supported
   152 /// lm.ssl.supported
   138 /// Indicates if SSL is supported by loudmouth library.
   153 /// Indicates if SSL is supported by loudmouth library and what kind of
   139 /// R: boolean
   154 /// ssl fingerprint is used.
       
   155 /// R: nil or string ("MD5" or "SHA256")
   140 static int supported_lm_ssl (lua_State *L)
   156 static int supported_lm_ssl (lua_State *L)
   141 {
   157 {
   142 	lua_pushboolean (L, lm_ssl_is_supported ());
   158 	if (lm_ssl_is_supported ()) {
       
   159 #ifdef HAVE_LM_SHA256_FINGERPRINTS
       
   160 		lua_pushliteral (L, "SHA256");
       
   161 #else
       
   162 		lua_pushliteral (L, "MD5");
       
   163 #endif
       
   164 	} else {
       
   165 		lua_pushnil (L);
       
   166 	}
   143 	return 1;
   167 	return 1;
   144 }
   168 }
   145 
   169 
   146 /// ssl:fingerprint
   170 /// ssl:fingerprint
   147 /// Returns fingerprint of remote server.
   171 /// Returns fingerprint of remote server.
   148 /// R: string or nil
   172 /// R: string or nil
   149 static int fingerprint_lm_ssl (lua_State *L)
   173 static int fingerprint_lm_ssl (lua_State *L)
   150 {
   174 {
   151 	char buffer[48];
       
   152 	llm_ssl_t *object = luaL_checklm_ssl (L, 1);
   175 	llm_ssl_t *object = luaL_checklm_ssl (L, 1);
   153 	const gchar *fingerprint = lm_ssl_get_fingerprint (object->ssl);
   176 	const gchar *fingerprint = lm_ssl_get_fingerprint (object->ssl);
   154 	if (fingerprint == NULL)
   177 	if (fingerprint == NULL)
   155 		lua_pushnil (L);
   178 		lua_pushnil (L);
   156 	else {
   179 	else {
       
   180 #ifdef HAVE_LM_SHA256_FINGERPRINTS
       
   181 		lua_pushstring (L, fingerprint);
       
   182 #else
       
   183 		char buffer[48];
   157 		snprintf (buffer, 48,
   184 		snprintf (buffer, 48,
   158 			  "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:"
   185 			  "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:"
   159 			  "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
   186 			  "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
   160 			  fingerprint[0], fingerprint[1], fingerprint[2], fingerprint[3],
   187 			  fingerprint[0], fingerprint[1], fingerprint[2], fingerprint[3],
   161 			  fingerprint[4], fingerprint[5], fingerprint[6], fingerprint[7],
   188 			  fingerprint[4], fingerprint[5], fingerprint[6], fingerprint[7],
   162 			  fingerprint[8], fingerprint[9], fingerprint[10], fingerprint[11],
   189 			  fingerprint[8], fingerprint[9], fingerprint[10], fingerprint[11],
   163 			  fingerprint[12], fingerprint[13], fingerprint[14], fingerprint[15]);
   190 			  fingerprint[12], fingerprint[13], fingerprint[14], fingerprint[15]);
   164 		lua_pushlstring (L, buffer, 47);
   191 		lua_pushlstring (L, buffer, 47);
       
   192 #endif
   165 	}
   193 	}
   166 	return 1;
   194 	return 1;
   167 }
   195 }
   168 
   196 
   169 /// ssl:tls
   197 /// ssl:tls