1 /*
2  * This patch Copyright (C) 2010 by James Nobis - quel
3  * - quel NOSPAM quelrod NOSPAM net, and it is herby released to the general
4  * public under the follow terms:
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted.
7  *
8  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
9  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
12  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18  * SUCH DAMAGE.
19  *
20  * format specification
21  * http://www.hmailserver.com/forum/viewtopic.php?p=97515&sid=b2c1c6ba1e10c2f0654ca9421b2059e8#p97515
22  * inspiration from the generic sha-1 and md5
23  * Copyright (c) 2010 by Solar Designer
24  *
25  * JimF Feb, 2015: converted into a 'thin' format, hooked to dynamic_61
26  */
27 
28 #if AC_BUILT
29 #include "autoconfig.h"
30 #endif
31 #ifndef DYNAMIC_DISABLED
32 
33 #if FMT_EXTERNS_H
34 extern struct fmt_main fmt_hmailserver;
35 #elif FMT_REGISTERS_H
36 john_register_one(&fmt_hmailserver);
37 #else
38 
39 #include "sha2.h"
40 
41 #include "params.h"
42 #include "common.h"
43 #include "formats.h"
44 #include "dynamic.h"
45 
46 #define FORMAT_LABEL        "hMailServer"
47 #define FORMAT_NAME         ""
48 
49 #define ALGORITHM_NAME      "?" /* filled in by dynamic */
50 
51 #define BENCHMARK_COMMENT   ""
52 #define BENCHMARK_LENGTH    7
53 
54 // set PLAINTEXT_LENGTH to 0, so dyna will set this
55 #define PLAINTEXT_LENGTH	0
56 #define CIPHERTEXT_LENGTH   64
57 
58 #define BINARY_SIZE         32
59 #define DYNA_BINARY_SIZE	16
60 #define BINARY_ALIGN        4
61 #define SALT_SIZE           6
62 #define DYNA_SALT_SIZE		(sizeof(char*))
63 #define SALT_ALIGN          4
64 
65 #define MIN_KEYS_PER_CRYPT  1
66 #define MAX_KEYS_PER_CRYPT  1
67 
68 static struct fmt_tests hmailserver_tests[] = {
69     {"cc06fa688a64cdeea43d3c0fb761fede7e3ccf00a9daea9c79f7d458e06f88327f16dd", "password"},
70     {"fee4fd4446aebcb3332aa5c61845b7bcbe5a3126fedf51a6359663d61b87d4f6ee87df", "12345678"},
71     {"2d7b784370c488b6548394ba11513e159220c83e2458ed01d8c7cdadd6bf486b433703", "1234"},
72     {"0926aadc8d49682c3f091af2dbf7f16f1cc7130b8e6dc86978d3f1bef914ce0096d4b3", "0123456789ABCDE"},
73     {NULL}
74 };
75 
76 static char Conv_Buf[120];
77 static struct fmt_main *pDynamic;
78 static void hmailserver_init(struct fmt_main *self);
79 static void get_ptr();
80 
81 /* this function converts a 'native' phps signature string into a $dynamic_6$ syntax string */
Convert(char * Buf,char * ciphertext)82 static char *Convert(char *Buf, char *ciphertext)
83 {
84 	if (text_in_dynamic_format_already(pDynamic, ciphertext))
85 		return ciphertext;
86 
87 	snprintf(Buf, sizeof(Conv_Buf), "$dynamic_61$%s$%6.6s", &ciphertext[6], ciphertext);
88 	return Buf;
89 }
90 
our_split(char * ciphertext,int index,struct fmt_main * self)91 static char *our_split(char *ciphertext, int index, struct fmt_main *self)
92 {
93 	get_ptr();
94 	return pDynamic->methods.split(Convert(Conv_Buf, ciphertext), index, self);
95 }
96 
our_prepare(char * split_fields[10],struct fmt_main * self)97 static char *our_prepare(char *split_fields[10], struct fmt_main *self)
98 {
99 	get_ptr();
100 	return pDynamic->methods.prepare(split_fields, self);
101 }
102 
103 
hmailserver_valid(char * ciphertext,struct fmt_main * self)104 static int hmailserver_valid(char *ciphertext, struct fmt_main *self)
105 {
106 	int i;
107 
108 	if (!ciphertext)
109 		return 0;
110 
111 	get_ptr();
112 	i = strnlen(ciphertext, CIPHERTEXT_LENGTH + SALT_SIZE + 1);
113 
114 	if (i != CIPHERTEXT_LENGTH + SALT_SIZE)
115 		return pDynamic->methods.valid(ciphertext, pDynamic);
116 	return pDynamic->methods.valid(Convert(Conv_Buf, ciphertext), pDynamic);
117 }
118 
our_salt(char * ciphertext)119 static void * our_salt(char *ciphertext)
120 {
121 	get_ptr();
122 	return pDynamic->methods.salt(Convert(Conv_Buf, ciphertext));
123 }
124 
our_binary(char * ciphertext)125 static void * our_binary(char *ciphertext)
126 {
127 	get_ptr();
128 	return pDynamic->methods.binary(Convert(Conv_Buf, ciphertext));
129 }
130 
131 
132 struct fmt_main fmt_hmailserver =
133 {
134 	{
135 		// setup the labeling and stuff. NOTE the max and min crypts are set to 1
136 		// here, but will be reset within our init() function.
137 		FORMAT_LABEL, FORMAT_NAME, ALGORITHM_NAME, BENCHMARK_COMMENT, BENCHMARK_LENGTH,
138 		0, PLAINTEXT_LENGTH, DYNA_BINARY_SIZE, BINARY_ALIGN, DYNA_SALT_SIZE, SALT_ALIGN, 1, 1, FMT_CASE | FMT_8_BIT | FMT_DYNAMIC,
139 		{ NULL },
140 		{ NULL },
141 		hmailserver_tests
142 	},
143 	{
144 		/*  All we setup here, is the pointer to valid, and the pointer to init */
145 		/*  within the call to init, we will properly set this full object      */
146 		hmailserver_init,
147 		fmt_default_done,
148 		fmt_default_reset,
149 		our_prepare,
150 		hmailserver_valid,
151 		our_split
152 	}
153 };
154 
link_funcs()155 static void link_funcs()
156 {
157 	fmt_hmailserver.methods.salt   = our_salt;
158 	fmt_hmailserver.methods.binary = our_binary;
159 	fmt_hmailserver.methods.split = our_split;
160 	fmt_hmailserver.methods.prepare = our_prepare;
161 }
162 
hmailserver_init(struct fmt_main * self)163 static void hmailserver_init(struct fmt_main *self)
164 {
165 	if (self->private.initialized == 0) {
166 		get_ptr();
167 		pDynamic->methods.init(pDynamic);
168 		self->private.initialized = 1;
169 	}
170 }
171 
get_ptr()172 static void get_ptr()
173 {
174 	if (!pDynamic) {
175 		pDynamic = dynamic_THIN_FORMAT_LINK(&fmt_hmailserver, Convert(Conv_Buf, hmailserver_tests[0].ciphertext), "hmailserver", 0);
176 		link_funcs();
177 	}
178 }
179 
180 #endif /* plugin stanza */
181 
182 #endif /* DYNAMIC_DISABLED */
183