-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPGpubkey.js
191 lines (164 loc) · 5.07 KB
/
PGpubkey.js
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
187
188
189
190
191
/* OpenPGP public key extraction
* Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
* version 1.1, check www.haneWIN.de for the latest version
* Modified by Philipp Hagemeister<[email protected]> for inclusion in keysheet
* This software is provided as-is, without express or implied warranty.
* Permission to use, copy, modify, distribute or sell this software, with or
* without fee, for any purpose and by any individual or organization, is hereby
* granted, provided that the above copyright notice and this paragraph appear
* in all copies. Distribution as a part of an application or binary must
* include the above copyright notice in the documentation and/or other materials
* provided with the application or distribution.
*/
function decodeUTF8(bytestring) {
var res = '';
for (var i=0;i < bytestring.length;i++) {
var b = bytestring.charCodeAt(i);
if ((b >= 0xc0) && (b < 0xe0) && (i + 1 < bytestring.length)) {
// Two-digit sequence
b = ((b & 0x1f) << 6) | (bytestring.charCodeAt(i+1) & 0x3f);
i++;
} else if ((b >= 0xe0) && (b < 0xf0) && (i + 2 < bytestring.length)) {
// Three-digit sequence
b = (
((b & 0x0f) << 12) |
((bytestring.charCodeAt(i+1) & 0x3f) << 6) |
(bytestring.charCodeAt(i+2) & 0x3f));
i += 2;
}
res += String.fromCharCode(b);
}
return res;
}
function s2hex(s)
{
var result = '';
for(var i=0; i<s.length; i++)
{
c = s.charCodeAt(i);
result += ((c<16) ? "0" : "") + c.toString(16);
}
return result;
}
function getPublicKey(text)
{
var res = {user_ids: []};
var i= text.indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----');
if(i == -1)
{
return false;
}
var a=text.indexOf('\n\n',i);
if(a>0) a += 2;
else
{
a = text.indexOf('\n\r\n', i);
if(a>0) a += 3;
}
var e=text.indexOf('\n=',i);
if(a>0 && e>0) text = text.slice(a,e);
else
{
return false;
}
var s=r2s(text);
for(var i=0; i < s.length;)
{
var tag = s.charCodeAt(i++);
if((tag&128) == 0) break;
if(tag&64)
{
tag&=63;
len=s.charCodeAt(i++);
if(len >191 && len <224) len=((len-192)<<8) + s.charCodeAt(i++);
else if(len==255) len = (s.charCodeAt(i++)<<24) + (s.charCodeAt(i++)<<16) + (s.charCodeAt(i++)<<8) + s.charCodeAt(i++);
else if(len>223 &&len<255) len = (1<<(len&0x1f));
}
else
{
len = tag&3;
tag = (tag>>2)&15;
if(len==0) len = s.charCodeAt(i++);
else if(len==1) len = (s.charCodeAt(i++)<<8) + s.charCodeAt(i++);
else if(len==2) len = (s.charCodeAt(i++)<<24) + (s.charCodeAt(i++)<<16) + (s.charCodeAt(i++)<<8) + s.charCodeAt(i++);
else len = s.length-1;
}
if(tag==6) // public key
{
var k = i;
var vers=s.charCodeAt(i++);
res.vers=vers;
var time=(s.charCodeAt(i++)<<24) + (s.charCodeAt(i++)<<16) + (s.charCodeAt(i++)<<8) + s.charCodeAt(i++);
if(vers==2 || vers==3) var valid=s.charCodeAt(i++)<<8 + s.charCodeAt(i++);
var algo=s.charCodeAt(i++);
if(algo == 1 || algo == 2)
{
var m = i;
var lm = Math.floor((s.charCodeAt(i)*256 + s.charCodeAt(i+1)+7)/8);
i+=lm+2;
var mod = s.substr(m,lm+2);
var le = Math.floor((s.charCodeAt(i)*256 + s.charCodeAt(i+1)+7)/8);
i+=le+2;
res.pkey=s2r(s.substr(m,lm+le+4));
res.type="RSA";
if(vers==3)
{
res.keyid=s2hex(mod.substr(mod.length-8, 8));
}
else if(vers==4)
{
var pkt = String.fromCharCode(0x99) + String.fromCharCode(len>>8)
+ String.fromCharCode(len&255)+s.substr(k, len);
var fp = str_sha1(pkt);
res.fp=s2hex(fp);
res.keyid=s2hex(fp.substr(fp.length-8,8));
}
else
{
res.keyid='';
}
}
else if((algo == 16 || algo == 20) && vers == 4)
{
var m = i;
var lp = Math.floor((s.charCodeAt(i)*256 + s.charCodeAt(i+1)+7)/8);
i+=lp+2;
var lg = Math.floor((s.charCodeAt(i)*256 + s.charCodeAt(i+1)+7)/8);
i+=lg+2;
var ly = Math.floor((s.charCodeAt(i)*256 + s.charCodeAt(i+1)+7)/8);
i+=ly+2;
res.pkey=s2r(s.substr(m,lp+lg+ly+6));
var pkt = String.fromCharCode(0x99) + String.fromCharCode(len>>8)
+ String.fromCharCode(len&255)+s.substr(k, len);
var fp = str_sha1(pkt);
res.fp_enc=s2hex(fp);
res.keyid=s2hex(fp.substr(fp.length-8,8));
res.type="ELGAMAL";
}
else if (algo == 17 && vers == 4) { // DSA
var pkt = String.fromCharCode(0x99) + String.fromCharCode(len>>8)
+ String.fromCharCode(len&255)+s.substr(k, len);
var fp = str_sha1(pkt);
res.fp=s2hex(fp);
i = k + len;
} else
{
i = k + len;
}
}
else if(tag==13) // user id
{
var uid = decodeUTF8(s.substr(i, len));
res.user_ids.push(uid);
if (!res.user) {
res.user = uid;
}
i+=len;
}
else
{
i+=len;
}
}
return res;
}