Anope IRC Services  Version 2.0
enc_old.cpp
Go to the documentation of this file.
1 /* Include file for high-level encryption routines.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include "module.h"
13 #include "modules/encryption.h"
14 
15 static ServiceReference<Encryption::Provider> md5("Encryption::Provider", "md5");
16 
18 {
19  public:
20  OldMD5Provider(Module *creator) : Encryption::Provider(creator, "oldmd5") { }
21 
23  {
24  if (md5)
25  return md5->CreateContext(iv);
26  return NULL;
27  }
28 
30  {
31  if (md5)
32  return md5->GetDefaultIV();
33  return Encryption::IV(static_cast<const uint32_t *>(NULL), 0);
34  }
35 };
36 
37 class EOld : public Module
38 {
40 
41  inline static char XTOI(char c) { return c > 9 ? c - 'A' + 10 : c - '0'; }
42 
43  public:
44  EOld(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, ENCRYPTION | VENDOR),
45  oldmd5provider(this)
46  {
47 
48  ModuleManager::LoadModule("enc_md5", User::Find(creator));
49  if (!md5)
50  throw ModuleException("Unable to find md5 reference");
51 
52  }
53 
55  {
56  if (!md5)
57  return EVENT_CONTINUE;
58 
59  Encryption::Context *context = md5->CreateContext();
60  context->Update(reinterpret_cast<const unsigned char *>(src.c_str()), src.length());
61  context->Finalize();
62 
63  Encryption::Hash hash = context->GetFinalizedHash();
64 
65  char digest[32], digest2[16];
66  memset(digest, 0, sizeof(digest));
67  if (hash.second > sizeof(digest))
68  throw CoreException("Hash too large");
69  memcpy(digest, hash.first, hash.second);
70 
71  for (int i = 0; i < 32; i += 2)
72  digest2[i / 2] = XTOI(digest[i]) << 4 | XTOI(digest[i + 1]);
73 
74  Anope::string buf = "oldmd5:" + Anope::Hex(digest2, sizeof(digest2));
75 
76  Log(LOG_DEBUG_2) << "(enc_old) hashed password from [" << src << "] to [" << buf << "]";
77  dest = buf;
78  delete context;
79  return EVENT_ALLOW;
80  }
81 
83  {
84  const NickAlias *na = NickAlias::Find(req->GetAccount());
85  if (na == NULL)
86  return;
87  NickCore *nc = na->nc;
88 
89  size_t pos = nc->pass.find(':');
90  if (pos == Anope::string::npos)
91  return;
92  Anope::string hash_method(nc->pass.begin(), nc->pass.begin() + pos);
93  if (!hash_method.equals_cs("oldmd5"))
94  return;
95 
96  Anope::string buf;
97  this->OnEncrypt(req->GetPassword(), buf);
98  if (nc->pass.equals_cs(buf))
99  {
100  /* if we are NOT the first module in the list,
101  * we want to re-encrypt the pass with the new encryption
102  */
104  Anope::Encrypt(req->GetPassword(), nc->pass);
105  req->Success(this);
106  }
107  }
108 };
109 
Serialize::Reference< NickCore > nc
Definition: account.h:47
virtual Hash GetFinalizedHash()=0
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
bool equals_cs(const char *_str) const
Definition: anope.h:74
virtual void Finalize()=0
static char XTOI(char c)
Definition: enc_old.cpp:41
void OnCheckAuthentication(User *, IdentifyRequest *req) anope_override
Definition: enc_old.cpp:82
Definition: users.h:34
Definition: enc_old.cpp:37
Anope::string pass
Definition: account.h:115
CoreExport string Hex(const string &data)
Definition: misc.cpp:546
static ServiceReference< Encryption::Provider > md5("Encryption::Provider","md5")
std::pair< const unsigned char *, size_t > Hash
Definition: encryption.h:15
Encryption::IV GetDefaultIV() anope_override
Definition: enc_old.cpp:29
static const size_type npos
Definition: anope.h:44
OldMD5Provider oldmd5provider
Definition: enc_old.cpp:39
EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) anope_override
Definition: enc_old.cpp:54
std::pair< const uint32_t *, size_t > IV
Definition: encryption.h:16
#define anope_override
Definition: services.h:56
virtual void Update(const unsigned char *data, size_t len)=0
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
static Module * FindFirstOf(ModType type)
OldMD5Provider(Module *creator)
Definition: enc_old.cpp:20
static User * Find(const Anope::string &name, bool nick_only=false)
Definition: users.cpp:815
static ModuleReturn LoadModule(const Anope::string &modname, User *u)
Encryption::Context * CreateContext(Encryption::IV *iv) anope_override
Definition: enc_old.cpp:22
CoreExport void Encrypt(const Anope::string &src, Anope::string &dest)
Definition: misc.cpp:511
Definition: logger.h:53
EOld(const Anope::string &modname, const Anope::string &creator)
Definition: enc_old.cpp:44
iterator begin()
Definition: anope.h:282
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
Provider(Module *creator, const Anope::string &sname)
Definition: encryption.h:30