-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·186 lines (158 loc) · 6.68 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<html lang="en-US" style="height: 100%;">
<head>
<!--
Code developed by Steffen Sanwald.
Based on https://github.com/openpgpjs/openpgpjs
https://www.stoerkind.de/SicheresMailformular
https://docs.hcaptcha.com/
-->
<meta charset="utf-8">
<!-- !!!make sure PGP key has no spaces to the left - otherwise openpgpjs will not work!!!-->
<meta name="pgp-pubkey" id="pgp-pubkey" content=
"-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: Alice's OpenPGP certificate
Comment: https://www.ietf.org/id/draft-bre-openpgp-samples-01.html
mDMEXEcE6RYJKwYBBAHaRw8BAQdArjWwk3FAqyiFbFBKT4TzXcVBqPTB3gmzlC/U
b7O1u120JkFsaWNlIExvdmVsYWNlIDxhbGljZUBvcGVucGdwLmV4YW1wbGU+iJAE
ExYIADgCGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AWIQTrhbtfozp14V6UTmPy
MVUMT0fjjgUCXaWfOgAKCRDyMVUMT0fjjukrAPoDnHBSogOmsHOsd9qGsiZpgRnO
dypvbm+QtXZqth9rvwD9HcDC0tC+PHAsO7OTh1S1TC9RiJsvawAfCPaQZoed8gK4
OARcRwTpEgorBgEEAZdVAQUBAQdAQv8GIa2rSTzgqbXCpDDYMiKRVitCsy203x3s
E9+eviIDAQgHiHgEGBYIACAWIQTrhbtfozp14V6UTmPyMVUMT0fjjgUCXEcE6QIb
DAAKCRDyMVUMT0fjjlnQAQDFHUs6TIcxrNTtEZFjUFm1M0PJ1Dng/cDW4xN80fsn
0QEA22Kr7VkCjeAEC08VSTeV+QFsmz55/lntWkwYWhmvOgE=
=iIGO
-----END PGP PUBLIC KEY BLOCK-----">
<script src="openpgpjs/dist/openpgp.js"></script>
<script src="js/check_input.js"></script>
<script src="js/lazy_load.js" ></script>
</head>
<body onload="add_lazyload_handlers();">
<form action="contact.php" method="post" role="form" id="myForm">
<label for="contactname">Name:</label><br>
<input type="text" id="contactname" placeholder="Max Mustermann" required><br>
<label for="email">Email:</label><br>
<input type="text" id="email" placeholder="max@mustermann.de" required><br>
<label for="subject">Subject:</label><br>
<input type="text" id="subject" placeholder="Your offer from last sunday" required><br>
<label for="content">Content:</label><br>
<input type="text" id="content" placeholder="Hey Max" required>
<!-- official dummy value for data-sitekey taken from https://docs.hcaptcha.com.
Can be used for debugging.
Replace value with the one from your hcaptcha instance -->
<!--<div class="h-captcha" data-sitekey="10000000-ffff-ffff-ffff-000000000001"></div>
use invisible captcha described in https://docs.hcaptcha.com/invisible
to execute hcaptcha via javascript-->
<div
id="hcaptcha"
class="h-captcha" data-sitekey="10000000-ffff-ffff-ffff-000000000001"
data-callback="after_capture_success"
data-size="invisible"
></div>
<input id="encryptedmessage" name="encryptedmessage" type="hidden" placeholder="">
<button id="submit-msg" name="submit-msg" value="Submit encrypted" onclick="validate(event);"></button>
<p id="result">Not yet</p>
</form>
<script>
function validate(event){
/*
Validate input before captcha is loaded.
*/
console.log("Validating input before captcha check");
event.preventDefault();
var result = is_form_input_valid(document.getElementById("myForm"));
if(!result[0]){
console.log("Form input failed:"+result[1]);
document.getElementById("result").innerText="Form input failed:"+result[2];//for easy debugging on Smartphone
return false;
}
if ( document.getElementById("pgp-pubkey").content == null){
console.log("public key not specified");
document.getElementById("result").innerText="public key not specified";//for easy debugging on Smartphone
return false;
}
if(is_hcaptcha_already_loaded()){
hcaptcha.execute();
}
else{
console.log("Hcaptcha lazy load was too slow or not triggered,yet. Please try again");
lazy_load_hcaptcha();
}
}
function after_capture_success(evt){
/*
Evaluates password strength as meta information and attaches it to hidden pw meta form input.
Decrypt it for testing via: gpg --output test.msg --decrypt test.msg.pgp
But first check captcha client-sided
*/
(async () => {
console.log("Capture solved successfully");
var hcaptchaVal = document.getElementsByName("h-captcha-response")[0].value;
if (hcaptchaVal === "") {
evt.preventDefault();
alert("Please complete the hCaptcha");
}
var form = document.getElementById("myForm");
var publicKeyArmored = document.getElementById("pgp-pubkey").content;
var form_jq=$(form);
var action = form_jq.attr('action');
var plaintext="Name:" + form.elements["contactname"].value + " Email:"+form.elements["email"].value +
" Subject:"+form.elements["subject"].value+ " Content:"+form.elements["content"].value;
await openpgp.initWorker({ path: 'openpgpjs/dist/openpgp.worker.js' }); // set the relative web worker path
const { data: encrypted } = await openpgp.encrypt({
message: openpgp.message.fromText(plaintext),
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys,
});
console.log("Raw msg:"+encrypted);
var trimmed=trim_pgp_message(encrypted);
console.log("Send msg:"+trimmed);
//check whether trimming somehow destroyed the content
if(encrypted.indexOf(trimmed)<0 || /^([A-Za-z0-9+/=\r\n]+)$/.test(trimmed)==false){
console.log("Trimming deleted pgp message content or contains not base64 characters");
document.getElementById("result").innerText="Trimming deleted pgp message content or contains not base64 characters";//for easy debugging on Smartphone
return false;
}
var transmitted_val="encryptedmessage="+trimmed+"&h-captcha-response="+hcaptchaVal;
// transfer data via AJAX POST request
$.ajax({
type: "POST",
url: action,
data: transmitted_val,
encode:false,
process:false,
success: function(msg) {
document.getElementById("result").innerText=msg;//for easy debugging on Smartphone
if (msg == 'Valid' || msg=="Invalid") { //if only one of these words is returned, debug is off.
if (msg == 'Valid') {
console.log("Submitted encrypted data successfully");
} else {
console.log("Submitted encrypted data failed");
}
}
else{//debug messages are enabled. JSON object returned
response=JSON.parse(msg);
if (response["result"] == 'Valid') {
console.log("Submitted encrypted data successfully (DEBUG ON)")
} else {
msg=escapeHtml(response["msg"]);
result=escapeHtml(response["result"]);
info=escapeHtml(response["info"]);
console.log("Submitted encrypted data failed (DEBUG ON)");
console.log(result);
console.log(info);
console.log(msg);
if(response["log"]!=null){
response["log"].forEach(function (item,index){
console.log("Log"+index+":"+escapeHtml(item));
})
}
}
}
}
});
})();
}
</script>
<script src="js/jquery.min.js"></script>
</body>
</html>