Make MD2 autodetected (EJAB-1285)

This commit is contained in:
Evgeniy Khramtsov 2010-08-07 22:04:57 +10:00
parent 1ea09b09a2
commit d2d8a09b4a
6 changed files with 86 additions and 21 deletions

View File

@ -64,6 +64,11 @@ ifeq (@transient_supervisors@, false)
EFLAGS+=-DNO_TRANSIENT_SUPERVISORS
endif
ifeq (@md2@, true)
EFLAGS+=-DHAVE_MD2
ERLANG_CFLAGS += -DHAVE_MD2
endif
INSTALL_EPAM=
ifeq (@pam@, pam)
INSTALL_EPAM=install -m 750 $(O_USER) epam $(PBINDIR)

View File

@ -148,6 +148,9 @@ if test "$ENABLEUSER" != ""; then
AC_SUBST([INSTALLUSER], [$ENABLEUSER])
fi
AC_CHECK_HEADER(openssl/md2.h, md2=true, md2=false)
AC_SUBST(md2)
AC_CANONICAL_SYSTEM
#AC_DEFINE_UNQUOTED(CPU_VENDOR_OS, "$target")
#AC_SUBST(target_os)

View File

@ -276,25 +276,8 @@ feature_response(#iq{type = result,
sub_el = [{xmlelement, _, _, Els}]},
Host, From, Caps, [SubNode | SubNodes]) ->
BinaryNode = node_to_binary(Caps#caps.node, SubNode),
IsValid = case Caps#caps.hash of
"md2" ->
Caps#caps.version == make_disco_hash(Els, md2);
"md5" ->
Caps#caps.version == make_disco_hash(Els, md5);
"sha-1" ->
Caps#caps.version == make_disco_hash(Els, sha1);
"sha-224" ->
Caps#caps.version == make_disco_hash(Els, sha224);
"sha-256" ->
Caps#caps.version == make_disco_hash(Els, sha256);
"sha-384" ->
Caps#caps.version == make_disco_hash(Els, sha384);
"sha-512" ->
Caps#caps.version == make_disco_hash(Els, sha512);
_ ->
true
end,
if IsValid ->
case check_hash(Caps, Els) of
true ->
Features = lists:flatmap(
fun({xmlelement, "feature", FAttrs, _}) ->
[xml:get_attr_s("var", FAttrs)];
@ -304,7 +287,7 @@ feature_response(#iq{type = result,
mnesia:dirty_write(
#caps_features{node_pair = BinaryNode,
features = features_to_binary(Features)});
true ->
false ->
mnesia:dirty_write(#caps_features{node_pair = BinaryNode})
end,
feature_request(Host, From, Caps, SubNodes);
@ -349,6 +332,7 @@ make_my_disco_hash(Host) ->
""
end.
-ifdef(HAVE_MD2).
make_disco_hash(DiscoEls, Algo) ->
Concat = [concat_identities(DiscoEls),
concat_features(DiscoEls),
@ -370,6 +354,64 @@ make_disco_hash(DiscoEls, Algo) ->
sha:sha512(Concat)
end).
check_hash(Caps, Els) ->
case Caps#caps.hash of
"md2" ->
Caps#caps.version == make_disco_hash(Els, md2);
"md5" ->
Caps#caps.version == make_disco_hash(Els, md5);
"sha-1" ->
Caps#caps.version == make_disco_hash(Els, sha1);
"sha-224" ->
Caps#caps.version == make_disco_hash(Els, sha224);
"sha-256" ->
Caps#caps.version == make_disco_hash(Els, sha256);
"sha-384" ->
Caps#caps.version == make_disco_hash(Els, sha384);
"sha-512" ->
Caps#caps.version == make_disco_hash(Els, sha512);
_ ->
true
end.
-else.
make_disco_hash(DiscoEls, Algo) ->
Concat = [concat_identities(DiscoEls),
concat_features(DiscoEls),
concat_info(DiscoEls)],
base64:encode_to_string(
if Algo == md5 ->
crypto:md5(Concat);
Algo == sha1 ->
crypto:sha(Concat);
Algo == sha224 ->
sha:sha224(Concat);
Algo == sha256 ->
sha:sha256(Concat);
Algo == sha384 ->
sha:sha384(Concat);
Algo == sha512 ->
sha:sha512(Concat)
end).
check_hash(Caps, Els) ->
case Caps#caps.hash of
"md5" ->
Caps#caps.version == make_disco_hash(Els, md5);
"sha-1" ->
Caps#caps.version == make_disco_hash(Els, sha1);
"sha-224" ->
Caps#caps.version == make_disco_hash(Els, sha224);
"sha-256" ->
Caps#caps.version == make_disco_hash(Els, sha256);
"sha-384" ->
Caps#caps.version == make_disco_hash(Els, sha384);
"sha-512" ->
Caps#caps.version == make_disco_hash(Els, sha512);
_ ->
true
end.
-endif.
concat_features(Els) ->
lists:usort(
lists:flatmap(

View File

@ -28,7 +28,11 @@
-author('alexey@process-one.net').
-export([start/0, sha/1, sha1/1, sha224/1, sha256/1, sha384/1,
sha512/1, md2/1]).
sha512/1]).
-ifdef(HAVE_MD2).
-export([md2/1]).
-endif.
-include("ejabberd.hrl").
@ -80,8 +84,10 @@ sha384(Text) ->
sha512(Text) ->
erlang:port_control(?DRIVER, 512, Text).
-ifdef(HAVE_MD2).
md2(Text) ->
erlang:port_control(?DRIVER, 2, Text).
-endif.
driver_path() ->
Suffix = case os:type() of

View File

@ -30,6 +30,11 @@ ifdef debug
EFLAGS+=+debug_info +export_all
endif
ifeq (@md2@, true)
EFLAGS+=-DHAVE_MD2
ERLANG_CFLAGS += -DHAVE_MD2
endif
ERLSHLIBS = ../tls_drv.so ../sha_drv.so
OUTDIR = ..
SOURCES = $(wildcard *.erl)

View File

@ -20,7 +20,9 @@
#include <erl_driver.h>
#include <openssl/sha.h>
#ifdef HAVE_MD2
#include <openssl/md2.h>
#endif
static ErlDrvData sha_drv_start(ErlDrvPort port, char *buf)
{
@ -36,11 +38,13 @@ static int sha_drv_control(ErlDrvData handle,
ErlDrvBinary *b = NULL;
switch (command) {
#ifdef HAVE_MD2
case 2:
rlen = MD2_DIGEST_LENGTH;
b = driver_alloc_binary(rlen);
if (b) MD2((unsigned char*)buf, len, (unsigned char*)b->orig_bytes);
break;
#endif
case 224:
rlen = SHA224_DIGEST_LENGTH;
b = driver_alloc_binary(rlen);