author | Michael Krelin <hacker@klever.net> | 2009-04-06 20:27:39 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2009-04-06 20:27:39 (UTC) |
commit | 51d8a8a4ac6ef6096c393fd602df34c6bf8f6366 (patch) (side-by-side diff) | |
tree | 8126f60fac6562b14c965e41d19983d81312638a | |
parent | de964540e5a58b3a9195c642ef7a0745ee3b2344 (diff) | |
download | iii-51d8a8a4ac6ef6096c393fd602df34c6bf8f6366.zip iii-51d8a8a4ac6ef6096c393fd602df34c6bf8f6366.tar.gz iii-51d8a8a4ac6ef6096c393fd602df34c6bf8f6366.tar.bz2 |
better nonce generation and session credentials verification
based on the patch from Chris Davies
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/eyefiservice.cc | 21 | ||||
-rw-r--r-- | src/eyetil.cc | 17 | ||||
-rw-r--r-- | src/eyetil.h | 1 |
6 files changed, 43 insertions, 7 deletions
@@ -1,9 +1,10 @@ Klever dissected: Michael 'hacker' Krelin <hacker@klever.net> Leonid Ivanov <kamel@klever.net> Thanks to: cdavies of Eye-Fi forums for integrity digest verification algorithm. See http://forums.eye.fi/viewtopic.php?f=4&t=270&p=4074#p4074 + and session nonce verification patch diff --git a/configure.ac b/configure.ac index 2e66fc4..515d465 100644 --- a/configure.ac +++ b/configure.ac @@ -2,32 +2,38 @@ AC_INIT([iii], [0.1], [iii-bugs@klever.net]) AC_CONFIG_SRCDIR([src/iiid.cc]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE([dist-bzip2]) AC_PROG_INSTALL AC_PROG_CXX AC_PROG_CC PKG_PROG_PKG_CONFIG AC_HEADER_STDC AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) PKG_CHECK_MODULES([MODULES],[gsoap++ openssl libconfuse],,[ AC_MSG_ERROR([one of the build dependencies isn't satisfied]) ]) +PKG_CHECK_MODULES([UUID],[uuid],[have_uuid=true],[have_uuid=false]) +AM_CONDITIONAL([HAVE_UUID],[$have_uuid]) +if $have_uuid ; then + AC_DEFINE([HAVE_LIBUUID],,[defined in presence of libuuid]) + AC_SUBST([UUID_UUID],[uuid]) +fi AC_PATH_PROG([SOAPCPP2],[soapcpp2],[false]) test "$SOAPCPP2" = "false" && AC_MSG_ERROR([no soapcpp2 tool, part of gsoap package, found.]) notfound=false AC_CHECK_HEADERS([archive.h],[ AC_CHECK_LIB([archive],[archive_read_new],,[notfound=true]) ],[notfound=true]) $notfound && AC_MSG_ERROR([no required libarchive library found. get one from http://people.freebsd.org/~kientzle/libarchive/]) notfound=false AC_LANG_PUSH([C++]) AC_CHECK_HEADERS([autosprintf.h],[ AC_CHECK_LIB([asprintf],[main],,[notfound=true]) ],[notfound=true]) $notfound && AC_MSG_ERROR([no autosprintf, part of gettext, found]) diff --git a/src/Makefile.am b/src/Makefile.am index 09f698e..b5b7d5c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,32 +1,32 @@ sbin_PROGRAMS=iiid noinst_HEADERS = \ eyefi.h \ eyekinfig.h eyetil.h \ eyefiworker.h AM_CPPFLAGS = ${CPPFLAGS_DEBUG} \ -DEYEKIN_CONF_DIR=\"${sysconfdir}/${PACKAGE}\" DEFAULT_INCLUDES = -I${top_builddir} -I${builddir} -I${srcdir} -INCLUDES = ${MODULES_CFLAGS} +INCLUDES = ${MODULES_CFLAGS} ${UUID_CFLAGS} iiid_SOURCES = iiid.cc \ eyekinfig.cc eyetil.cc \ eyefiservice.cc eyefiworker.cc nodist_iiid_SOURCES = \ ${builddir}/soapC.cpp ${builddir}/soapeyefiService.cpp \ COPYING.cc -iiid_LDADD = ${MODULES_LIBS} +iiid_LDADD = ${MODULES_LIBS} ${UUID_LIBS} COPYING.cc: ${top_srcdir}/COPYING echo "const char * COPYING = " >$@ || (rm $@;exit 1) sed -e 's/"/\\"/g' -e 's/^/\"/' -e 's/$$/\\n\"/' $< >>$@ || (rm $@;exit 1) echo ';' >>$@ || (rm $@;exit 1) ${srcdir}/eyefiservice.cc: ${builddir}/soapeyefiService.h ${srcdir}/iiid.cc: ${builddir}/eyefi.nsmap ${builddir}soapC.cpp ${builddir}/soapeyefiService.cpp ${builddir}/eyefi.nsmap ${builddir}/soapeyefiService.h: ${srcdir}/eyefi.h ${SOAPCPP2} -d${builddir} -S -L -a -i -w -x $< clean-local: rm -f soap{{H,Stub,eyefiService}.h,{C,eyefiService}.cpp} eyefi.nsmap COPYING.cc diff --git a/src/eyefiservice.cc b/src/eyefiservice.cc index d233a07..1a21c02 100644 --- a/src/eyefiservice.cc +++ b/src/eyefiservice.cc @@ -1,28 +1,31 @@ #include <cassert> #include <iostream> #include <fstream> #include <stdexcept> #include <iterator> #include <syslog.h> #include <sys/wait.h> #include <autosprintf.h> +#include <openssl/rand.h> #include "eyekinfig.h" #include "eyetil.h" #include "soapeyefiService.h" +static binary_t session_nonce; + static bool detached_child() { pid_t p = fork(); if(p<0) throw std::runtime_error("failed to fork()"); if(!p) { p = fork(); if(p<0) { syslog(LOG_ERR,"Failed to re-fork child process"); _exit(-1); } if(!p) { setsid(); for(int i=getdtablesize();i>=0;--i) close(i); int i=open("/dev/null",O_RDWR); assert(i==0); i = dup(i); assert(i==1); i = dup(i); assert(i==2); return true; @@ -33,63 +36,71 @@ static bool detached_child() { if(waitpid(p,&rc,0)<0) throw std::runtime_error("failed to waitpid()"); if(!WIFEXITED(rc)) throw std::runtime_error("error in forked process"); if(WEXITSTATUS(rc)) throw std::runtime_error("forked process signalled error"); return false; } int eyefiService::StartSession( std::string macaddress,std::string cnonce, int transfermode,long transfermodetimestamp, struct rns__StartSessionResponse &r ) { #ifndef NDEBUG syslog(LOG_DEBUG, "StartSession request from %s with cnonce=%s, transfermode=%d, transfermodetimestamp=%ld", macaddress.c_str(), cnonce.c_str(), transfermode, transfermodetimestamp ); #endif r.credential = binary_t(macaddress+cnonce+eyekinfig_t(macaddress).get_upload_key()).md5().hex(); - /* TODO: better nonce generator */ - time_t t = time(0); - r.snonce = binary_t(&t,sizeof(t)).md5().hex(); + + r.snonce = session_nonce.make_nonce().hex(); r.transfermode=transfermode; r.transfermodetimestamp=transfermodetimestamp; r.upsyncallowed=false; std::string cmd = eyekinfig_t(macaddress).get_on_start_session(); if(!cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); putenv( gnu::autosprintf("EYEFI_TRANSFERMODE=%d",transfermode) ); putenv( gnu::autosprintf("EYEFI_TRANSFERMODETIMESTAMP=%ld",transfermodetimestamp) ); char *argv[] = { (char*)"/bin/sh", (char*)"-c", (char*)cmd.c_str(), 0 }; execv("/bin/sh",argv); syslog(LOG_ERR,"Failed to execute '%s'",cmd.c_str()); _exit(-1); } } return SOAP_OK; } int eyefiService::GetPhotoStatus( std::string credential, std::string macaddress, std::string filename, long filesize, std::string filesignature, struct rns__GetPhotoStatusResponse &r ) { #ifndef NDEBUG syslog(LOG_DEBUG, - "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s", - macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str() ); + "GetPhotoStatus request from %s with credential=%s, filename=%s, filesize=%ld, filesignature=%s; session nonce=%s", + macaddress.c_str(), credential.c_str(), filename.c_str(), filesize, filesignature.c_str(), session_nonce.hex().c_str() ); +#endif + + std::string computed_credential = binary_t(macaddress+eyekinfig_t(macaddress).get_upload_key()+session_nonce.hex()).md5().hex(); + +#ifndef NDEBUG + syslog(LOG_DEBUG, " computed credential=%s", computed_credential.c_str()); #endif + + if (credential != computed_credential) throw std::runtime_error("card authentication failed"); + r.fileid = 1; r.offset = 0; return SOAP_OK; } int eyefiService::MarkLastPhotoInRoll( std::string macaddress, int mergedelta, struct rns__MarkLastPhotoInRollResponse &r ) { #ifndef NDEBUG syslog(LOG_DEBUG, "MarkLastPhotoInRoll request from %s with mergedelta=%d", macaddress.c_str(), mergedelta ); #endif std::string cmd = eyekinfig_t(macaddress).get_on_mark_last_photo_in_roll(); if(!cmd.empty()) { if(detached_child()) { putenv( gnu::autosprintf("EYEFI_MACADDRESS=%s",macaddress.c_str()) ); diff --git a/src/eyetil.cc b/src/eyetil.cc index fe816a6..7669cb6 100644 --- a/src/eyetil.cc +++ b/src/eyetil.cc @@ -1,50 +1,67 @@ #include <stdlib.h> #include <sys/stat.h> #include <syslog.h> #include <iostream> #include <cassert> #include <stdexcept> #include <algorithm> #include <numeric> #include <openssl/md5.h> #include "eyetil.h" +#include "config.h" +#ifdef HAVE_LIBUUID +# include <uuid/uuid.h> +#endif + binary_t& binary_t::from_hex(const std::string& h) { std::string::size_type hs = h.length(); if(hs&1) throw std::runtime_error("odd number of characters in hexadecimal number"); int rvs = hs>>1; resize(rvs); const unsigned char *hp = (const unsigned char*)h.data(); iterator oi=begin(); char t[3] = { 0,0,0 }; for(int i=0;i<rvs;++i) { t[0]=*(hp++); t[1]=*(hp++); *(oi++) = strtol(t,0,16); } return *this; } binary_t& binary_t::from_data(const void *d,size_t s) { resize(s); std::copy((const unsigned char*)d,(const unsigned char *)d+s, begin() ); return *this; } +binary_t& binary_t::make_nonce() { +#ifdef HAVE_LIBUUID + uuid_t uuid; + uuid_generate(uuid); + from_data((unsigned char*)uuid,sizeof(uuid)); +#else + resize(16); + std::generate_n(begin(),16,rand); +#endif /* HAVE_LIBUUID */ + return *this; +} + std::string binary_t::hex() const { std::string rv; rv.reserve((size()<<1)+1); char t[3] = {0,0,0}; for(const_iterator i=begin(),ie=end();i!=ie;++i) { int rc = snprintf(t,sizeof(t),"%02x",*i); assert(rc<sizeof(t)); rv += t; } return rv; } binary_t binary_t::md5() const { binary_t rv(MD5_DIGEST_LENGTH); if(!MD5( (const unsigned char*)&(front()),size(), diff --git a/src/eyetil.h b/src/eyetil.h index 378f703..d946e71 100644 --- a/src/eyetil.h +++ b/src/eyetil.h @@ -2,32 +2,33 @@ #define __EYETIL_H #include <vector> #include <string> #include <archive.h> #include <archive_entry.h> class binary_t : public std::vector<unsigned char> { public: binary_t() { } binary_t(size_type n) : std::vector<unsigned char>(n) { } binary_t(const std::string& h) { from_hex(h); } binary_t(const void *d,size_t s) { from_data(d,s); } binary_t& from_hex(const std::string& h); binary_t& from_data(const void *d,size_t s); + binary_t& make_nonce(); std::string hex() const; binary_t md5() const; }; class tmpdir_t { public: std::string dir; tmpdir_t(const std::string& dt); ~tmpdir_t(); std::string get_file(const std::string& f); }; class tarchive_t { |