pixel-react 1.15.21 → 1.15.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/lib/_virtual/aesCipherSuites.js +4 -0
  2. package/lib/_virtual/aesCipherSuites.js.map +1 -0
  3. package/lib/_virtual/asn1-validator.js +4 -0
  4. package/lib/_virtual/asn1-validator.js.map +1 -0
  5. package/lib/_virtual/asn1.js +4 -0
  6. package/lib/_virtual/asn1.js.map +1 -0
  7. package/lib/_virtual/cipherModes.js +4 -0
  8. package/lib/_virtual/cipherModes.js.map +1 -0
  9. package/lib/_virtual/hmac.js +4 -0
  10. package/lib/_virtual/hmac.js.map +1 -0
  11. package/lib/_virtual/index11.js +2 -2
  12. package/lib/_virtual/index12.js +2 -2
  13. package/lib/_virtual/index13.js +2 -2
  14. package/lib/_virtual/index14.js +4 -0
  15. package/lib/_virtual/index14.js.map +1 -0
  16. package/lib/_virtual/index2.js +5 -3
  17. package/lib/_virtual/index2.js.map +1 -1
  18. package/lib/_virtual/index3.js +4 -2
  19. package/lib/_virtual/index3.js.map +1 -1
  20. package/lib/_virtual/index4.js +2 -4
  21. package/lib/_virtual/index4.js.map +1 -1
  22. package/lib/_virtual/index5.js +4 -2
  23. package/lib/_virtual/index5.js.map +1 -1
  24. package/lib/_virtual/index6.js +2 -2
  25. package/lib/_virtual/index7.js +2 -6
  26. package/lib/_virtual/index7.js.map +1 -1
  27. package/lib/_virtual/index8.js +6 -2
  28. package/lib/_virtual/index8.js.map +1 -1
  29. package/lib/_virtual/index9.js +2 -2
  30. package/lib/_virtual/md5.js +4 -0
  31. package/lib/_virtual/md5.js.map +1 -0
  32. package/lib/_virtual/mgf1.js +4 -0
  33. package/lib/_virtual/mgf1.js.map +1 -0
  34. package/lib/_virtual/oids.js +4 -0
  35. package/lib/_virtual/oids.js.map +1 -0
  36. package/lib/_virtual/pem.js +4 -0
  37. package/lib/_virtual/pem.js.map +1 -0
  38. package/lib/_virtual/pkcs1.js +4 -0
  39. package/lib/_virtual/pkcs1.js.map +1 -0
  40. package/lib/_virtual/pkcs12.js +4 -0
  41. package/lib/_virtual/pkcs12.js.map +1 -0
  42. package/lib/_virtual/pkcs7.js +4 -0
  43. package/lib/_virtual/pkcs7.js.map +1 -0
  44. package/lib/_virtual/pkcs7asn1.js +4 -0
  45. package/lib/_virtual/pkcs7asn1.js.map +1 -0
  46. package/lib/_virtual/pki.js +4 -0
  47. package/lib/_virtual/pki.js.map +1 -0
  48. package/lib/_virtual/prime.js +4 -0
  49. package/lib/_virtual/prime.js.map +1 -0
  50. package/lib/_virtual/prng.js +4 -0
  51. package/lib/_virtual/prng.js.map +1 -0
  52. package/lib/_virtual/pss.js +4 -0
  53. package/lib/_virtual/pss.js.map +1 -0
  54. package/lib/_virtual/random.js +4 -0
  55. package/lib/_virtual/random.js.map +1 -0
  56. package/lib/_virtual/sha1.js +4 -0
  57. package/lib/_virtual/sha1.js.map +1 -0
  58. package/lib/_virtual/sha256.js +4 -0
  59. package/lib/_virtual/sha256.js.map +1 -0
  60. package/lib/_virtual/sha512.js +4 -0
  61. package/lib/_virtual/sha512.js.map +1 -0
  62. package/lib/_virtual/ssh.js +4 -0
  63. package/lib/_virtual/ssh.js.map +1 -0
  64. package/lib/_virtual/util.js +4 -0
  65. package/lib/_virtual/util.js.map +1 -0
  66. package/lib/_virtual/x509.js +4 -0
  67. package/lib/_virtual/x509.js.map +1 -0
  68. package/lib/assets/icons/testify_icon.svg.js +6 -0
  69. package/lib/assets/icons/testify_icon.svg.js.map +1 -0
  70. package/lib/assets/icons/testify_loader.svg.js +6 -0
  71. package/lib/assets/icons/testify_loader.svg.js.map +1 -0
  72. package/lib/assets/icons/testify_logo_name.svg.js +6 -0
  73. package/lib/assets/icons/testify_logo_name.svg.js.map +1 -0
  74. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js +3 -1
  75. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js.map +1 -1
  76. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js +2 -9
  77. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js.map +1 -1
  78. package/lib/components/Charts/DashboardDonutChart/types.d.ts +1 -1
  79. package/lib/components/ConditionalDropdown/ConditionalDropdown.js +2 -9
  80. package/lib/components/ConditionalDropdown/ConditionalDropdown.js.map +1 -1
  81. package/lib/components/Editor/Editor.js +37 -17
  82. package/lib/components/Editor/Editor.js.map +1 -1
  83. package/lib/components/Icon/iconList.js +6 -0
  84. package/lib/components/Icon/iconList.js.map +1 -1
  85. package/lib/components/MultiSelect/MultiSelect.js +9 -10
  86. package/lib/components/MultiSelect/MultiSelect.js.map +1 -1
  87. package/lib/components/PhoneInput/PhoneInput.js +1 -0
  88. package/lib/components/PhoneInput/PhoneInput.js.map +1 -1
  89. package/lib/components/SessionDropdown/SessionDropdown.js +4 -2
  90. package/lib/components/SessionDropdown/SessionDropdown.js.map +1 -1
  91. package/lib/index.cjs +27423 -379
  92. package/lib/index.cjs.map +1 -1
  93. package/lib/index.d.ts +1 -1
  94. package/lib/node_modules/classnames/index.js +1 -1
  95. package/lib/node_modules/input-format/modules/react/Input.js +1 -1
  96. package/lib/node_modules/js-beautify/js/index.js +1 -1
  97. package/lib/node_modules/js-beautify/js/src/html/beautifier.js +1 -1
  98. package/lib/node_modules/js-beautify/js/src/html/index.js +1 -1
  99. package/lib/node_modules/js-beautify/js/src/html/options.js +1 -1
  100. package/lib/node_modules/js-beautify/js/src/html/tokenizer.js +1 -1
  101. package/lib/node_modules/js-beautify/js/src/index.js +1 -1
  102. package/lib/node_modules/js-beautify/js/src/javascript/beautifier.js +1 -1
  103. package/lib/node_modules/js-beautify/js/src/javascript/index.js +1 -1
  104. package/lib/node_modules/js-beautify/js/src/javascript/options.js +1 -1
  105. package/lib/node_modules/js-beautify/js/src/javascript/tokenizer.js +1 -1
  106. package/lib/node_modules/node-forge/lib/aes.js +1014 -0
  107. package/lib/node_modules/node-forge/lib/aes.js.map +1 -0
  108. package/lib/node_modules/node-forge/lib/aesCipherSuites.js +286 -0
  109. package/lib/node_modules/node-forge/lib/aesCipherSuites.js.map +1 -0
  110. package/lib/node_modules/node-forge/lib/asn1-validator.js +100 -0
  111. package/lib/node_modules/node-forge/lib/asn1-validator.js.map +1 -0
  112. package/lib/node_modules/node-forge/lib/asn1.js +1379 -0
  113. package/lib/node_modules/node-forge/lib/asn1.js.map +1 -0
  114. package/lib/node_modules/node-forge/lib/baseN.js +181 -0
  115. package/lib/node_modules/node-forge/lib/baseN.js.map +1 -0
  116. package/lib/node_modules/node-forge/lib/cipher.js +236 -0
  117. package/lib/node_modules/node-forge/lib/cipher.js.map +1 -0
  118. package/lib/node_modules/node-forge/lib/cipherModes.js +936 -0
  119. package/lib/node_modules/node-forge/lib/cipherModes.js.map +1 -0
  120. package/lib/node_modules/node-forge/lib/des.js +467 -0
  121. package/lib/node_modules/node-forge/lib/des.js.map +1 -0
  122. package/lib/node_modules/node-forge/lib/ed25519.js +1108 -0
  123. package/lib/node_modules/node-forge/lib/ed25519.js.map +1 -0
  124. package/lib/node_modules/node-forge/lib/forge.js +23 -0
  125. package/lib/node_modules/node-forge/lib/forge.js.map +1 -0
  126. package/lib/node_modules/node-forge/lib/hmac.js +158 -0
  127. package/lib/node_modules/node-forge/lib/hmac.js.map +1 -0
  128. package/lib/node_modules/node-forge/lib/index.js +70 -0
  129. package/lib/node_modules/node-forge/lib/index.js.map +1 -0
  130. package/lib/node_modules/node-forge/lib/jsbn.js +1481 -0
  131. package/lib/node_modules/node-forge/lib/jsbn.js.map +1 -0
  132. package/lib/node_modules/node-forge/lib/kem.js +178 -0
  133. package/lib/node_modules/node-forge/lib/kem.js.map +1 -0
  134. package/lib/node_modules/node-forge/lib/log.js +325 -0
  135. package/lib/node_modules/node-forge/lib/log.js.map +1 -0
  136. package/lib/node_modules/node-forge/lib/md.all.js +28 -0
  137. package/lib/node_modules/node-forge/lib/md.all.js.map +1 -0
  138. package/lib/node_modules/node-forge/lib/md.js +22 -0
  139. package/lib/node_modules/node-forge/lib/md.js.map +1 -0
  140. package/lib/node_modules/node-forge/lib/md5.js +288 -0
  141. package/lib/node_modules/node-forge/lib/md5.js.map +1 -0
  142. package/lib/node_modules/node-forge/lib/mgf.js +24 -0
  143. package/lib/node_modules/node-forge/lib/mgf.js.map +1 -0
  144. package/lib/node_modules/node-forge/lib/mgf1.js +68 -0
  145. package/lib/node_modules/node-forge/lib/mgf1.js.map +1 -0
  146. package/lib/node_modules/node-forge/lib/oids.js +185 -0
  147. package/lib/node_modules/node-forge/lib/oids.js.map +1 -0
  148. package/lib/node_modules/node-forge/lib/pbe.js +966 -0
  149. package/lib/node_modules/node-forge/lib/pbe.js.map +1 -0
  150. package/lib/node_modules/node-forge/lib/pbkdf2.js +209 -0
  151. package/lib/node_modules/node-forge/lib/pbkdf2.js.map +1 -0
  152. package/lib/node_modules/node-forge/lib/pem.js +250 -0
  153. package/lib/node_modules/node-forge/lib/pem.js.map +1 -0
  154. package/lib/node_modules/node-forge/lib/pkcs1.js +273 -0
  155. package/lib/node_modules/node-forge/lib/pkcs1.js.map +1 -0
  156. package/lib/node_modules/node-forge/lib/pkcs12.js +980 -0
  157. package/lib/node_modules/node-forge/lib/pkcs12.js.map +1 -0
  158. package/lib/node_modules/node-forge/lib/pkcs7.js +1073 -0
  159. package/lib/node_modules/node-forge/lib/pkcs7.js.map +1 -0
  160. package/lib/node_modules/node-forge/lib/pkcs7asn1.js +415 -0
  161. package/lib/node_modules/node-forge/lib/pkcs7asn1.js.map +1 -0
  162. package/lib/node_modules/node-forge/lib/pki.js +125 -0
  163. package/lib/node_modules/node-forge/lib/pki.js.map +1 -0
  164. package/lib/node_modules/node-forge/lib/prime.js +297 -0
  165. package/lib/node_modules/node-forge/lib/prime.js.map +1 -0
  166. package/lib/node_modules/node-forge/lib/prng.js +433 -0
  167. package/lib/node_modules/node-forge/lib/prng.js.map +1 -0
  168. package/lib/node_modules/node-forge/lib/pss.js +246 -0
  169. package/lib/node_modules/node-forge/lib/pss.js.map +1 -0
  170. package/lib/node_modules/node-forge/lib/random.js +191 -0
  171. package/lib/node_modules/node-forge/lib/random.js.map +1 -0
  172. package/lib/node_modules/node-forge/lib/rc2.js +382 -0
  173. package/lib/node_modules/node-forge/lib/rc2.js.map +1 -0
  174. package/lib/node_modules/node-forge/lib/rsa.js +1815 -0
  175. package/lib/node_modules/node-forge/lib/rsa.js.map +1 -0
  176. package/lib/node_modules/node-forge/lib/sha1.js +325 -0
  177. package/lib/node_modules/node-forge/lib/sha1.js.map +1 -0
  178. package/lib/node_modules/node-forge/lib/sha256.js +306 -0
  179. package/lib/node_modules/node-forge/lib/sha256.js.map +1 -0
  180. package/lib/node_modules/node-forge/lib/sha512.js +479 -0
  181. package/lib/node_modules/node-forge/lib/sha512.js.map +1 -0
  182. package/lib/node_modules/node-forge/lib/ssh.js +244 -0
  183. package/lib/node_modules/node-forge/lib/ssh.js.map +1 -0
  184. package/lib/node_modules/node-forge/lib/tls.js +4207 -0
  185. package/lib/node_modules/node-forge/lib/tls.js.map +1 -0
  186. package/lib/node_modules/node-forge/lib/util.js +2565 -0
  187. package/lib/node_modules/node-forge/lib/util.js.map +1 -0
  188. package/lib/node_modules/node-forge/lib/x509.js +2986 -0
  189. package/lib/node_modules/node-forge/lib/x509.js.map +1 -0
  190. package/lib/node_modules/prop-types/index.js +1 -1
  191. package/lib/node_modules/react-async-script/lib/esm/async-script-loader.js +1 -1
  192. package/lib/node_modules/react-google-recaptcha/lib/esm/recaptcha.js +1 -1
  193. package/lib/node_modules/react-is/index.js +1 -1
  194. package/lib/node_modules/react-phone-number-input/modules/CountryIcon.js +1 -1
  195. package/lib/node_modules/react-phone-number-input/modules/CountrySelect.js +1 -1
  196. package/lib/node_modules/react-phone-number-input/modules/Flag.js +1 -1
  197. package/lib/node_modules/react-phone-number-input/modules/InputBasic.js +1 -1
  198. package/lib/node_modules/react-phone-number-input/modules/InputSmart.js +1 -1
  199. package/lib/node_modules/react-phone-number-input/modules/InternationalIcon.js +1 -1
  200. package/lib/node_modules/react-phone-number-input/modules/PhoneInputWithCountry.js +1 -1
  201. package/lib/node_modules/react-phone-number-input/modules/PropTypes.js +1 -1
  202. package/lib/node_modules/scheduler/index.js +1 -1
  203. package/lib/node_modules/use-context-selector/dist/index.js +1 -1
  204. package/lib/styles.css +1 -1
  205. package/lib/styles.css.map +1 -1
  206. package/lib/utils/getEncryptedData/getEncryptedData.js +10 -13
  207. package/lib/utils/getEncryptedData/getEncryptedData.js.map +1 -1
  208. package/package.json +3 -1
@@ -0,0 +1,1815 @@
1
+ import { __require as requireForge } from './forge.js';
2
+ import { __require as requireAsn1 } from './asn1.js';
3
+ import { __require as requireJsbn } from './jsbn.js';
4
+ import { __require as requireOids } from './oids.js';
5
+ import { __require as requirePkcs1 } from './pkcs1.js';
6
+ import { __require as requirePrime } from './prime.js';
7
+ import { __require as requireRandom } from './random.js';
8
+ import { __require as requireUtil } from './util.js';
9
+ import require$$4 from 'crypto';
10
+
11
+ /**
12
+ * Javascript implementation of basic RSA algorithms.
13
+ *
14
+ * @author Dave Longley
15
+ *
16
+ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
17
+ *
18
+ * The only algorithm currently supported for PKI is RSA.
19
+ *
20
+ * An RSA key is often stored in ASN.1 DER format. The SubjectPublicKeyInfo
21
+ * ASN.1 structure is composed of an algorithm of type AlgorithmIdentifier
22
+ * and a subjectPublicKey of type bit string.
23
+ *
24
+ * The AlgorithmIdentifier contains an Object Identifier (OID) and parameters
25
+ * for the algorithm, if any. In the case of RSA, there aren't any.
26
+ *
27
+ * SubjectPublicKeyInfo ::= SEQUENCE {
28
+ * algorithm AlgorithmIdentifier,
29
+ * subjectPublicKey BIT STRING
30
+ * }
31
+ *
32
+ * AlgorithmIdentifer ::= SEQUENCE {
33
+ * algorithm OBJECT IDENTIFIER,
34
+ * parameters ANY DEFINED BY algorithm OPTIONAL
35
+ * }
36
+ *
37
+ * For an RSA public key, the subjectPublicKey is:
38
+ *
39
+ * RSAPublicKey ::= SEQUENCE {
40
+ * modulus INTEGER, -- n
41
+ * publicExponent INTEGER -- e
42
+ * }
43
+ *
44
+ * PrivateKeyInfo ::= SEQUENCE {
45
+ * version Version,
46
+ * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
47
+ * privateKey PrivateKey,
48
+ * attributes [0] IMPLICIT Attributes OPTIONAL
49
+ * }
50
+ *
51
+ * Version ::= INTEGER
52
+ * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
53
+ * PrivateKey ::= OCTET STRING
54
+ * Attributes ::= SET OF Attribute
55
+ *
56
+ * An RSA private key as the following structure:
57
+ *
58
+ * RSAPrivateKey ::= SEQUENCE {
59
+ * version Version,
60
+ * modulus INTEGER, -- n
61
+ * publicExponent INTEGER, -- e
62
+ * privateExponent INTEGER, -- d
63
+ * prime1 INTEGER, -- p
64
+ * prime2 INTEGER, -- q
65
+ * exponent1 INTEGER, -- d mod (p-1)
66
+ * exponent2 INTEGER, -- d mod (q-1)
67
+ * coefficient INTEGER -- (inverse of q) mod p
68
+ * }
69
+ *
70
+ * Version ::= INTEGER
71
+ *
72
+ * The OID for the RSA key algorithm is: 1.2.840.113549.1.1.1
73
+ */
74
+ var rsa;
75
+ var hasRequiredRsa;
76
+ function requireRsa() {
77
+ if (hasRequiredRsa) return rsa;
78
+ hasRequiredRsa = 1;
79
+ var forge = requireForge();
80
+ requireAsn1();
81
+ requireJsbn();
82
+ requireOids();
83
+ requirePkcs1();
84
+ requirePrime();
85
+ requireRandom();
86
+ requireUtil();
87
+ if (typeof BigInteger === 'undefined') {
88
+ var BigInteger = forge.jsbn.BigInteger;
89
+ }
90
+ var _crypto = forge.util.isNodejs ? require$$4 : null;
91
+
92
+ // shortcut for asn.1 API
93
+ var asn1 = forge.asn1;
94
+
95
+ // shortcut for util API
96
+ var util = forge.util;
97
+
98
+ /*
99
+ * RSA encryption and decryption, see RFC 2313.
100
+ */
101
+ forge.pki = forge.pki || {};
102
+ rsa = forge.pki.rsa = forge.rsa = forge.rsa || {};
103
+ var pki = forge.pki;
104
+
105
+ // for finding primes, which are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
106
+ var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
107
+
108
+ // validator for a PrivateKeyInfo structure
109
+ var privateKeyValidator = {
110
+ // PrivateKeyInfo
111
+ name: 'PrivateKeyInfo',
112
+ tagClass: asn1.Class.UNIVERSAL,
113
+ type: asn1.Type.SEQUENCE,
114
+ constructed: true,
115
+ value: [{
116
+ // Version (INTEGER)
117
+ name: 'PrivateKeyInfo.version',
118
+ tagClass: asn1.Class.UNIVERSAL,
119
+ type: asn1.Type.INTEGER,
120
+ constructed: false,
121
+ capture: 'privateKeyVersion'
122
+ }, {
123
+ // privateKeyAlgorithm
124
+ name: 'PrivateKeyInfo.privateKeyAlgorithm',
125
+ tagClass: asn1.Class.UNIVERSAL,
126
+ type: asn1.Type.SEQUENCE,
127
+ constructed: true,
128
+ value: [{
129
+ name: 'AlgorithmIdentifier.algorithm',
130
+ tagClass: asn1.Class.UNIVERSAL,
131
+ type: asn1.Type.OID,
132
+ constructed: false,
133
+ capture: 'privateKeyOid'
134
+ }]
135
+ }, {
136
+ // PrivateKey
137
+ name: 'PrivateKeyInfo',
138
+ tagClass: asn1.Class.UNIVERSAL,
139
+ type: asn1.Type.OCTETSTRING,
140
+ constructed: false,
141
+ capture: 'privateKey'
142
+ }]
143
+ };
144
+
145
+ // validator for an RSA private key
146
+ var rsaPrivateKeyValidator = {
147
+ // RSAPrivateKey
148
+ name: 'RSAPrivateKey',
149
+ tagClass: asn1.Class.UNIVERSAL,
150
+ type: asn1.Type.SEQUENCE,
151
+ constructed: true,
152
+ value: [{
153
+ // Version (INTEGER)
154
+ name: 'RSAPrivateKey.version',
155
+ tagClass: asn1.Class.UNIVERSAL,
156
+ type: asn1.Type.INTEGER,
157
+ constructed: false,
158
+ capture: 'privateKeyVersion'
159
+ }, {
160
+ // modulus (n)
161
+ name: 'RSAPrivateKey.modulus',
162
+ tagClass: asn1.Class.UNIVERSAL,
163
+ type: asn1.Type.INTEGER,
164
+ constructed: false,
165
+ capture: 'privateKeyModulus'
166
+ }, {
167
+ // publicExponent (e)
168
+ name: 'RSAPrivateKey.publicExponent',
169
+ tagClass: asn1.Class.UNIVERSAL,
170
+ type: asn1.Type.INTEGER,
171
+ constructed: false,
172
+ capture: 'privateKeyPublicExponent'
173
+ }, {
174
+ // privateExponent (d)
175
+ name: 'RSAPrivateKey.privateExponent',
176
+ tagClass: asn1.Class.UNIVERSAL,
177
+ type: asn1.Type.INTEGER,
178
+ constructed: false,
179
+ capture: 'privateKeyPrivateExponent'
180
+ }, {
181
+ // prime1 (p)
182
+ name: 'RSAPrivateKey.prime1',
183
+ tagClass: asn1.Class.UNIVERSAL,
184
+ type: asn1.Type.INTEGER,
185
+ constructed: false,
186
+ capture: 'privateKeyPrime1'
187
+ }, {
188
+ // prime2 (q)
189
+ name: 'RSAPrivateKey.prime2',
190
+ tagClass: asn1.Class.UNIVERSAL,
191
+ type: asn1.Type.INTEGER,
192
+ constructed: false,
193
+ capture: 'privateKeyPrime2'
194
+ }, {
195
+ // exponent1 (d mod (p-1))
196
+ name: 'RSAPrivateKey.exponent1',
197
+ tagClass: asn1.Class.UNIVERSAL,
198
+ type: asn1.Type.INTEGER,
199
+ constructed: false,
200
+ capture: 'privateKeyExponent1'
201
+ }, {
202
+ // exponent2 (d mod (q-1))
203
+ name: 'RSAPrivateKey.exponent2',
204
+ tagClass: asn1.Class.UNIVERSAL,
205
+ type: asn1.Type.INTEGER,
206
+ constructed: false,
207
+ capture: 'privateKeyExponent2'
208
+ }, {
209
+ // coefficient ((inverse of q) mod p)
210
+ name: 'RSAPrivateKey.coefficient',
211
+ tagClass: asn1.Class.UNIVERSAL,
212
+ type: asn1.Type.INTEGER,
213
+ constructed: false,
214
+ capture: 'privateKeyCoefficient'
215
+ }]
216
+ };
217
+
218
+ // validator for an RSA public key
219
+ var rsaPublicKeyValidator = {
220
+ // RSAPublicKey
221
+ name: 'RSAPublicKey',
222
+ tagClass: asn1.Class.UNIVERSAL,
223
+ type: asn1.Type.SEQUENCE,
224
+ constructed: true,
225
+ value: [{
226
+ // modulus (n)
227
+ name: 'RSAPublicKey.modulus',
228
+ tagClass: asn1.Class.UNIVERSAL,
229
+ type: asn1.Type.INTEGER,
230
+ constructed: false,
231
+ capture: 'publicKeyModulus'
232
+ }, {
233
+ // publicExponent (e)
234
+ name: 'RSAPublicKey.exponent',
235
+ tagClass: asn1.Class.UNIVERSAL,
236
+ type: asn1.Type.INTEGER,
237
+ constructed: false,
238
+ capture: 'publicKeyExponent'
239
+ }]
240
+ };
241
+
242
+ // validator for an SubjectPublicKeyInfo structure
243
+ // Note: Currently only works with an RSA public key
244
+ var publicKeyValidator = forge.pki.rsa.publicKeyValidator = {
245
+ name: 'SubjectPublicKeyInfo',
246
+ tagClass: asn1.Class.UNIVERSAL,
247
+ type: asn1.Type.SEQUENCE,
248
+ constructed: true,
249
+ captureAsn1: 'subjectPublicKeyInfo',
250
+ value: [{
251
+ name: 'SubjectPublicKeyInfo.AlgorithmIdentifier',
252
+ tagClass: asn1.Class.UNIVERSAL,
253
+ type: asn1.Type.SEQUENCE,
254
+ constructed: true,
255
+ value: [{
256
+ name: 'AlgorithmIdentifier.algorithm',
257
+ tagClass: asn1.Class.UNIVERSAL,
258
+ type: asn1.Type.OID,
259
+ constructed: false,
260
+ capture: 'publicKeyOid'
261
+ }]
262
+ }, {
263
+ // subjectPublicKey
264
+ name: 'SubjectPublicKeyInfo.subjectPublicKey',
265
+ tagClass: asn1.Class.UNIVERSAL,
266
+ type: asn1.Type.BITSTRING,
267
+ constructed: false,
268
+ value: [{
269
+ // RSAPublicKey
270
+ name: 'SubjectPublicKeyInfo.subjectPublicKey.RSAPublicKey',
271
+ tagClass: asn1.Class.UNIVERSAL,
272
+ type: asn1.Type.SEQUENCE,
273
+ constructed: true,
274
+ optional: true,
275
+ captureAsn1: 'rsaPublicKey'
276
+ }]
277
+ }]
278
+ };
279
+
280
+ // validator for a DigestInfo structure
281
+ var digestInfoValidator = {
282
+ name: 'DigestInfo',
283
+ tagClass: asn1.Class.UNIVERSAL,
284
+ type: asn1.Type.SEQUENCE,
285
+ constructed: true,
286
+ value: [{
287
+ name: 'DigestInfo.DigestAlgorithm',
288
+ tagClass: asn1.Class.UNIVERSAL,
289
+ type: asn1.Type.SEQUENCE,
290
+ constructed: true,
291
+ value: [{
292
+ name: 'DigestInfo.DigestAlgorithm.algorithmIdentifier',
293
+ tagClass: asn1.Class.UNIVERSAL,
294
+ type: asn1.Type.OID,
295
+ constructed: false,
296
+ capture: 'algorithmIdentifier'
297
+ }, {
298
+ // NULL paramters
299
+ name: 'DigestInfo.DigestAlgorithm.parameters',
300
+ tagClass: asn1.Class.UNIVERSAL,
301
+ type: asn1.Type.NULL,
302
+ // captured only to check existence for md2 and md5
303
+ capture: 'parameters',
304
+ optional: true,
305
+ constructed: false
306
+ }]
307
+ }, {
308
+ // digest
309
+ name: 'DigestInfo.digest',
310
+ tagClass: asn1.Class.UNIVERSAL,
311
+ type: asn1.Type.OCTETSTRING,
312
+ constructed: false,
313
+ capture: 'digest'
314
+ }]
315
+ };
316
+
317
+ /**
318
+ * Wrap digest in DigestInfo object.
319
+ *
320
+ * This function implements EMSA-PKCS1-v1_5-ENCODE as per RFC 3447.
321
+ *
322
+ * DigestInfo ::= SEQUENCE {
323
+ * digestAlgorithm DigestAlgorithmIdentifier,
324
+ * digest Digest
325
+ * }
326
+ *
327
+ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
328
+ * Digest ::= OCTET STRING
329
+ *
330
+ * @param md the message digest object with the hash to sign.
331
+ *
332
+ * @return the encoded message (ready for RSA encrytion)
333
+ */
334
+ var emsaPkcs1v15encode = function (md) {
335
+ // get the oid for the algorithm
336
+ var oid;
337
+ if (md.algorithm in pki.oids) {
338
+ oid = pki.oids[md.algorithm];
339
+ } else {
340
+ var error = new Error('Unknown message digest algorithm.');
341
+ error.algorithm = md.algorithm;
342
+ throw error;
343
+ }
344
+ var oidBytes = asn1.oidToDer(oid).getBytes();
345
+
346
+ // create the digest info
347
+ var digestInfo = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
348
+ var digestAlgorithm = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
349
+ digestAlgorithm.value.push(asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, oidBytes));
350
+ digestAlgorithm.value.push(asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, ''));
351
+ var digest = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, md.digest().getBytes());
352
+ digestInfo.value.push(digestAlgorithm);
353
+ digestInfo.value.push(digest);
354
+
355
+ // encode digest info
356
+ return asn1.toDer(digestInfo).getBytes();
357
+ };
358
+
359
+ /**
360
+ * Performs x^c mod n (RSA encryption or decryption operation).
361
+ *
362
+ * @param x the number to raise and mod.
363
+ * @param key the key to use.
364
+ * @param pub true if the key is public, false if private.
365
+ *
366
+ * @return the result of x^c mod n.
367
+ */
368
+ var _modPow = function (x, key, pub) {
369
+ if (pub) {
370
+ return x.modPow(key.e, key.n);
371
+ }
372
+ if (!key.p || !key.q) {
373
+ // allow calculation without CRT params (slow)
374
+ return x.modPow(key.d, key.n);
375
+ }
376
+
377
+ // pre-compute dP, dQ, and qInv if necessary
378
+ if (!key.dP) {
379
+ key.dP = key.d.mod(key.p.subtract(BigInteger.ONE));
380
+ }
381
+ if (!key.dQ) {
382
+ key.dQ = key.d.mod(key.q.subtract(BigInteger.ONE));
383
+ }
384
+ if (!key.qInv) {
385
+ key.qInv = key.q.modInverse(key.p);
386
+ }
387
+
388
+ /* Chinese remainder theorem (CRT) states:
389
+ Suppose n1, n2, ..., nk are positive integers which are pairwise
390
+ coprime (n1 and n2 have no common factors other than 1). For any
391
+ integers x1, x2, ..., xk there exists an integer x solving the
392
+ system of simultaneous congruences (where ~= means modularly
393
+ congruent so a ~= b mod n means a mod n = b mod n):
394
+ x ~= x1 mod n1
395
+ x ~= x2 mod n2
396
+ ...
397
+ x ~= xk mod nk
398
+ This system of congruences has a single simultaneous solution x
399
+ between 0 and n - 1. Furthermore, each xk solution and x itself
400
+ is congruent modulo the product n = n1*n2*...*nk.
401
+ So x1 mod n = x2 mod n = xk mod n = x mod n.
402
+ The single simultaneous solution x can be solved with the following
403
+ equation:
404
+ x = sum(xi*ri*si) mod n where ri = n/ni and si = ri^-1 mod ni.
405
+ Where x is less than n, xi = x mod ni.
406
+ For RSA we are only concerned with k = 2. The modulus n = pq, where
407
+ p and q are coprime. The RSA decryption algorithm is:
408
+ y = x^d mod n
409
+ Given the above:
410
+ x1 = x^d mod p
411
+ r1 = n/p = q
412
+ s1 = q^-1 mod p
413
+ x2 = x^d mod q
414
+ r2 = n/q = p
415
+ s2 = p^-1 mod q
416
+ So y = (x1r1s1 + x2r2s2) mod n
417
+ = ((x^d mod p)q(q^-1 mod p) + (x^d mod q)p(p^-1 mod q)) mod n
418
+ According to Fermat's Little Theorem, if the modulus P is prime,
419
+ for any integer A not evenly divisible by P, A^(P-1) ~= 1 mod P.
420
+ Since A is not divisible by P it follows that if:
421
+ N ~= M mod (P - 1), then A^N mod P = A^M mod P. Therefore:
422
+ A^N mod P = A^(M mod (P - 1)) mod P. (The latter takes less effort
423
+ to calculate). In order to calculate x^d mod p more quickly the
424
+ exponent d mod (p - 1) is stored in the RSA private key (the same
425
+ is done for x^d mod q). These values are referred to as dP and dQ
426
+ respectively. Therefore we now have:
427
+ y = ((x^dP mod p)q(q^-1 mod p) + (x^dQ mod q)p(p^-1 mod q)) mod n
428
+ Since we'll be reducing x^dP by modulo p (same for q) we can also
429
+ reduce x by p (and q respectively) before hand. Therefore, let
430
+ xp = ((x mod p)^dP mod p), and
431
+ xq = ((x mod q)^dQ mod q), yielding:
432
+ y = (xp*q*(q^-1 mod p) + xq*p*(p^-1 mod q)) mod n
433
+ This can be further reduced to a simple algorithm that only
434
+ requires 1 inverse (the q inverse is used) to be used and stored.
435
+ The algorithm is called Garner's algorithm. If qInv is the
436
+ inverse of q, we simply calculate:
437
+ y = (qInv*(xp - xq) mod p) * q + xq
438
+ However, there are two further complications. First, we need to
439
+ ensure that xp > xq to prevent signed BigIntegers from being used
440
+ so we add p until this is true (since we will be mod'ing with
441
+ p anyway). Then, there is a known timing attack on algorithms
442
+ using the CRT. To mitigate this risk, "cryptographic blinding"
443
+ should be used. This requires simply generating a random number r
444
+ between 0 and n-1 and its inverse and multiplying x by r^e before
445
+ calculating y and then multiplying y by r^-1 afterwards. Note that
446
+ r must be coprime with n (gcd(r, n) === 1) in order to have an
447
+ inverse.
448
+ */
449
+
450
+ // cryptographic blinding
451
+ var r;
452
+ do {
453
+ r = new BigInteger(forge.util.bytesToHex(forge.random.getBytes(key.n.bitLength() / 8)), 16);
454
+ } while (r.compareTo(key.n) >= 0 || !r.gcd(key.n).equals(BigInteger.ONE));
455
+ x = x.multiply(r.modPow(key.e, key.n)).mod(key.n);
456
+
457
+ // calculate xp and xq
458
+ var xp = x.mod(key.p).modPow(key.dP, key.p);
459
+ var xq = x.mod(key.q).modPow(key.dQ, key.q);
460
+
461
+ // xp must be larger than xq to avoid signed bit usage
462
+ while (xp.compareTo(xq) < 0) {
463
+ xp = xp.add(key.p);
464
+ }
465
+
466
+ // do last step
467
+ var y = xp.subtract(xq).multiply(key.qInv).mod(key.p).multiply(key.q).add(xq);
468
+
469
+ // remove effect of random for cryptographic blinding
470
+ y = y.multiply(r.modInverse(key.n)).mod(key.n);
471
+ return y;
472
+ };
473
+
474
+ /**
475
+ * NOTE: THIS METHOD IS DEPRECATED, use 'sign' on a private key object or
476
+ * 'encrypt' on a public key object instead.
477
+ *
478
+ * Performs RSA encryption.
479
+ *
480
+ * The parameter bt controls whether to put padding bytes before the
481
+ * message passed in. Set bt to either true or false to disable padding
482
+ * completely (in order to handle e.g. EMSA-PSS encoding seperately before),
483
+ * signaling whether the encryption operation is a public key operation
484
+ * (i.e. encrypting data) or not, i.e. private key operation (data signing).
485
+ *
486
+ * For PKCS#1 v1.5 padding pass in the block type to use, i.e. either 0x01
487
+ * (for signing) or 0x02 (for encryption). The key operation mode (private
488
+ * or public) is derived from this flag in that case).
489
+ *
490
+ * @param m the message to encrypt as a byte string.
491
+ * @param key the RSA key to use.
492
+ * @param bt for PKCS#1 v1.5 padding, the block type to use
493
+ * (0x01 for private key, 0x02 for public),
494
+ * to disable padding: true = public key, false = private key.
495
+ *
496
+ * @return the encrypted bytes as a string.
497
+ */
498
+ pki.rsa.encrypt = function (m, key, bt) {
499
+ var pub = bt;
500
+ var eb;
501
+
502
+ // get the length of the modulus in bytes
503
+ var k = Math.ceil(key.n.bitLength() / 8);
504
+ if (bt !== false && bt !== true) {
505
+ // legacy, default to PKCS#1 v1.5 padding
506
+ pub = bt === 0x02;
507
+ eb = _encodePkcs1_v1_5(m, key, bt);
508
+ } else {
509
+ eb = forge.util.createBuffer();
510
+ eb.putBytes(m);
511
+ }
512
+
513
+ // load encryption block as big integer 'x'
514
+ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
515
+ var x = new BigInteger(eb.toHex(), 16);
516
+
517
+ // do RSA encryption
518
+ var y = _modPow(x, key, pub);
519
+
520
+ // convert y into the encrypted data byte string, if y is shorter in
521
+ // bytes than k, then prepend zero bytes to fill up ed
522
+ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
523
+ var yhex = y.toString(16);
524
+ var ed = forge.util.createBuffer();
525
+ var zeros = k - Math.ceil(yhex.length / 2);
526
+ while (zeros > 0) {
527
+ ed.putByte(0x00);
528
+ --zeros;
529
+ }
530
+ ed.putBytes(forge.util.hexToBytes(yhex));
531
+ return ed.getBytes();
532
+ };
533
+
534
+ /**
535
+ * NOTE: THIS METHOD IS DEPRECATED, use 'decrypt' on a private key object or
536
+ * 'verify' on a public key object instead.
537
+ *
538
+ * Performs RSA decryption.
539
+ *
540
+ * The parameter ml controls whether to apply PKCS#1 v1.5 padding
541
+ * or not. Set ml = false to disable padding removal completely
542
+ * (in order to handle e.g. EMSA-PSS later on) and simply pass back
543
+ * the RSA encryption block.
544
+ *
545
+ * @param ed the encrypted data to decrypt in as a byte string.
546
+ * @param key the RSA key to use.
547
+ * @param pub true for a public key operation, false for private.
548
+ * @param ml the message length, if known, false to disable padding.
549
+ *
550
+ * @return the decrypted message as a byte string.
551
+ */
552
+ pki.rsa.decrypt = function (ed, key, pub, ml) {
553
+ // get the length of the modulus in bytes
554
+ var k = Math.ceil(key.n.bitLength() / 8);
555
+
556
+ // error if the length of the encrypted data ED is not k
557
+ if (ed.length !== k) {
558
+ var error = new Error('Encrypted message length is invalid.');
559
+ error.length = ed.length;
560
+ error.expected = k;
561
+ throw error;
562
+ }
563
+
564
+ // convert encrypted data into a big integer
565
+ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
566
+ var y = new BigInteger(forge.util.createBuffer(ed).toHex(), 16);
567
+
568
+ // y must be less than the modulus or it wasn't the result of
569
+ // a previous mod operation (encryption) using that modulus
570
+ if (y.compareTo(key.n) >= 0) {
571
+ throw new Error('Encrypted message is invalid.');
572
+ }
573
+
574
+ // do RSA decryption
575
+ var x = _modPow(y, key, pub);
576
+
577
+ // create the encryption block, if x is shorter in bytes than k, then
578
+ // prepend zero bytes to fill up eb
579
+ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
580
+ var xhex = x.toString(16);
581
+ var eb = forge.util.createBuffer();
582
+ var zeros = k - Math.ceil(xhex.length / 2);
583
+ while (zeros > 0) {
584
+ eb.putByte(0x00);
585
+ --zeros;
586
+ }
587
+ eb.putBytes(forge.util.hexToBytes(xhex));
588
+ if (ml !== false) {
589
+ // legacy, default to PKCS#1 v1.5 padding
590
+ return _decodePkcs1_v1_5(eb.getBytes(), key, pub);
591
+ }
592
+
593
+ // return message
594
+ return eb.getBytes();
595
+ };
596
+
597
+ /**
598
+ * Creates an RSA key-pair generation state object. It is used to allow
599
+ * key-generation to be performed in steps. It also allows for a UI to
600
+ * display progress updates.
601
+ *
602
+ * @param bits the size for the private key in bits, defaults to 2048.
603
+ * @param e the public exponent to use, defaults to 65537 (0x10001).
604
+ * @param [options] the options to use.
605
+ * prng a custom crypto-secure pseudo-random number generator to use,
606
+ * that must define "getBytesSync".
607
+ * algorithm the algorithm to use (default: 'PRIMEINC').
608
+ *
609
+ * @return the state object to use to generate the key-pair.
610
+ */
611
+ pki.rsa.createKeyPairGenerationState = function (bits, e, options) {
612
+ // TODO: migrate step-based prime generation code to forge.prime
613
+
614
+ // set default bits
615
+ if (typeof bits === 'string') {
616
+ bits = parseInt(bits, 10);
617
+ }
618
+ bits = bits || 2048;
619
+
620
+ // create prng with api that matches BigInteger secure random
621
+ options = options || {};
622
+ var prng = options.prng || forge.random;
623
+ var rng = {
624
+ // x is an array to fill with bytes
625
+ nextBytes: function (x) {
626
+ var b = prng.getBytesSync(x.length);
627
+ for (var i = 0; i < x.length; ++i) {
628
+ x[i] = b.charCodeAt(i);
629
+ }
630
+ }
631
+ };
632
+ var algorithm = options.algorithm || 'PRIMEINC';
633
+
634
+ // create PRIMEINC algorithm state
635
+ var rval;
636
+ if (algorithm === 'PRIMEINC') {
637
+ rval = {
638
+ algorithm: algorithm,
639
+ state: 0,
640
+ bits: bits,
641
+ rng: rng,
642
+ eInt: e || 65537,
643
+ e: new BigInteger(null),
644
+ p: null,
645
+ q: null,
646
+ qBits: bits >> 1,
647
+ pBits: bits - (bits >> 1),
648
+ pqState: 0,
649
+ num: null,
650
+ keys: null
651
+ };
652
+ rval.e.fromInt(rval.eInt);
653
+ } else {
654
+ throw new Error('Invalid key generation algorithm: ' + algorithm);
655
+ }
656
+ return rval;
657
+ };
658
+
659
+ /**
660
+ * Attempts to runs the key-generation algorithm for at most n seconds
661
+ * (approximately) using the given state. When key-generation has completed,
662
+ * the keys will be stored in state.keys.
663
+ *
664
+ * To use this function to update a UI while generating a key or to prevent
665
+ * causing browser lockups/warnings, set "n" to a value other than 0. A
666
+ * simple pattern for generating a key and showing a progress indicator is:
667
+ *
668
+ * var state = pki.rsa.createKeyPairGenerationState(2048);
669
+ * var step = function() {
670
+ * // step key-generation, run algorithm for 100 ms, repeat
671
+ * if(!forge.pki.rsa.stepKeyPairGenerationState(state, 100)) {
672
+ * setTimeout(step, 1);
673
+ * } else {
674
+ * // key-generation complete
675
+ * // TODO: turn off progress indicator here
676
+ * // TODO: use the generated key-pair in "state.keys"
677
+ * }
678
+ * };
679
+ * // TODO: turn on progress indicator here
680
+ * setTimeout(step, 0);
681
+ *
682
+ * @param state the state to use.
683
+ * @param n the maximum number of milliseconds to run the algorithm for, 0
684
+ * to run the algorithm to completion.
685
+ *
686
+ * @return true if the key-generation completed, false if not.
687
+ */
688
+ pki.rsa.stepKeyPairGenerationState = function (state, n) {
689
+ // set default algorithm if not set
690
+ if (!('algorithm' in state)) {
691
+ state.algorithm = 'PRIMEINC';
692
+ }
693
+
694
+ // TODO: migrate step-based prime generation code to forge.prime
695
+ // TODO: abstract as PRIMEINC algorithm
696
+
697
+ // do key generation (based on Tom Wu's rsa.js, see jsbn.js license)
698
+ // with some minor optimizations and designed to run in steps
699
+
700
+ // local state vars
701
+ var THIRTY = new BigInteger(null);
702
+ THIRTY.fromInt(30);
703
+ var deltaIdx = 0;
704
+ var op_or = function (x, y) {
705
+ return x | y;
706
+ };
707
+
708
+ // keep stepping until time limit is reached or done
709
+ var t1 = +new Date();
710
+ var t2;
711
+ var total = 0;
712
+ while (state.keys === null && (n <= 0 || total < n)) {
713
+ // generate p or q
714
+ if (state.state === 0) {
715
+ /* Note: All primes are of the form:
716
+ 30k+i, for i < 30 and gcd(30, i)=1, where there are 8 values for i
717
+ When we generate a random number, we always align it at 30k + 1. Each
718
+ time the number is determined not to be prime we add to get to the
719
+ next 'i', eg: if the number was at 30k + 1 we add 6. */
720
+ var bits = state.p === null ? state.pBits : state.qBits;
721
+ var bits1 = bits - 1;
722
+
723
+ // get a random number
724
+ if (state.pqState === 0) {
725
+ state.num = new BigInteger(bits, state.rng);
726
+ // force MSB set
727
+ if (!state.num.testBit(bits1)) {
728
+ state.num.bitwiseTo(BigInteger.ONE.shiftLeft(bits1), op_or, state.num);
729
+ }
730
+ // align number on 30k+1 boundary
731
+ state.num.dAddOffset(31 - state.num.mod(THIRTY).byteValue(), 0);
732
+ deltaIdx = 0;
733
+ ++state.pqState;
734
+ } else if (state.pqState === 1) {
735
+ // try to make the number a prime
736
+ if (state.num.bitLength() > bits) {
737
+ // overflow, try again
738
+ state.pqState = 0;
739
+ // do primality test
740
+ } else if (state.num.isProbablePrime(_getMillerRabinTests(state.num.bitLength()))) {
741
+ ++state.pqState;
742
+ } else {
743
+ // get next potential prime
744
+ state.num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
745
+ }
746
+ } else if (state.pqState === 2) {
747
+ // ensure number is coprime with e
748
+ state.pqState = state.num.subtract(BigInteger.ONE).gcd(state.e).compareTo(BigInteger.ONE) === 0 ? 3 : 0;
749
+ } else if (state.pqState === 3) {
750
+ // store p or q
751
+ state.pqState = 0;
752
+ if (state.p === null) {
753
+ state.p = state.num;
754
+ } else {
755
+ state.q = state.num;
756
+ }
757
+
758
+ // advance state if both p and q are ready
759
+ if (state.p !== null && state.q !== null) {
760
+ ++state.state;
761
+ }
762
+ state.num = null;
763
+ }
764
+ } else if (state.state === 1) {
765
+ // ensure p is larger than q (swap them if not)
766
+ if (state.p.compareTo(state.q) < 0) {
767
+ state.num = state.p;
768
+ state.p = state.q;
769
+ state.q = state.num;
770
+ }
771
+ ++state.state;
772
+ } else if (state.state === 2) {
773
+ // compute phi: (p - 1)(q - 1) (Euler's totient function)
774
+ state.p1 = state.p.subtract(BigInteger.ONE);
775
+ state.q1 = state.q.subtract(BigInteger.ONE);
776
+ state.phi = state.p1.multiply(state.q1);
777
+ ++state.state;
778
+ } else if (state.state === 3) {
779
+ // ensure e and phi are coprime
780
+ if (state.phi.gcd(state.e).compareTo(BigInteger.ONE) === 0) {
781
+ // phi and e are coprime, advance
782
+ ++state.state;
783
+ } else {
784
+ // phi and e aren't coprime, so generate a new p and q
785
+ state.p = null;
786
+ state.q = null;
787
+ state.state = 0;
788
+ }
789
+ } else if (state.state === 4) {
790
+ // create n, ensure n is has the right number of bits
791
+ state.n = state.p.multiply(state.q);
792
+
793
+ // ensure n is right number of bits
794
+ if (state.n.bitLength() === state.bits) {
795
+ // success, advance
796
+ ++state.state;
797
+ } else {
798
+ // failed, get new q
799
+ state.q = null;
800
+ state.state = 0;
801
+ }
802
+ } else if (state.state === 5) {
803
+ // set keys
804
+ var d = state.e.modInverse(state.phi);
805
+ state.keys = {
806
+ privateKey: pki.rsa.setPrivateKey(state.n, state.e, d, state.p, state.q, d.mod(state.p1), d.mod(state.q1), state.q.modInverse(state.p)),
807
+ publicKey: pki.rsa.setPublicKey(state.n, state.e)
808
+ };
809
+ }
810
+
811
+ // update timing
812
+ t2 = +new Date();
813
+ total += t2 - t1;
814
+ t1 = t2;
815
+ }
816
+ return state.keys !== null;
817
+ };
818
+
819
+ /**
820
+ * Generates an RSA public-private key pair in a single call.
821
+ *
822
+ * To generate a key-pair in steps (to allow for progress updates and to
823
+ * prevent blocking or warnings in slow browsers) then use the key-pair
824
+ * generation state functions.
825
+ *
826
+ * To generate a key-pair asynchronously (either through web-workers, if
827
+ * available, or by breaking up the work on the main thread), pass a
828
+ * callback function.
829
+ *
830
+ * @param [bits] the size for the private key in bits, defaults to 2048.
831
+ * @param [e] the public exponent to use, defaults to 65537.
832
+ * @param [options] options for key-pair generation, if given then 'bits'
833
+ * and 'e' must *not* be given:
834
+ * bits the size for the private key in bits, (default: 2048).
835
+ * e the public exponent to use, (default: 65537 (0x10001)).
836
+ * workerScript the worker script URL.
837
+ * workers the number of web workers (if supported) to use,
838
+ * (default: 2).
839
+ * workLoad the size of the work load, ie: number of possible prime
840
+ * numbers for each web worker to check per work assignment,
841
+ * (default: 100).
842
+ * prng a custom crypto-secure pseudo-random number generator to use,
843
+ * that must define "getBytesSync". Disables use of native APIs.
844
+ * algorithm the algorithm to use (default: 'PRIMEINC').
845
+ * @param [callback(err, keypair)] called once the operation completes.
846
+ *
847
+ * @return an object with privateKey and publicKey properties.
848
+ */
849
+ pki.rsa.generateKeyPair = function (bits, e, options, callback) {
850
+ // (bits), (options), (callback)
851
+ if (arguments.length === 1) {
852
+ if (typeof bits === 'object') {
853
+ options = bits;
854
+ bits = undefined;
855
+ } else if (typeof bits === 'function') {
856
+ callback = bits;
857
+ bits = undefined;
858
+ }
859
+ } else if (arguments.length === 2) {
860
+ // (bits, e), (bits, options), (bits, callback), (options, callback)
861
+ if (typeof bits === 'number') {
862
+ if (typeof e === 'function') {
863
+ callback = e;
864
+ e = undefined;
865
+ } else if (typeof e !== 'number') {
866
+ options = e;
867
+ e = undefined;
868
+ }
869
+ } else {
870
+ options = bits;
871
+ callback = e;
872
+ bits = undefined;
873
+ e = undefined;
874
+ }
875
+ } else if (arguments.length === 3) {
876
+ // (bits, e, options), (bits, e, callback), (bits, options, callback)
877
+ if (typeof e === 'number') {
878
+ if (typeof options === 'function') {
879
+ callback = options;
880
+ options = undefined;
881
+ }
882
+ } else {
883
+ callback = options;
884
+ options = e;
885
+ e = undefined;
886
+ }
887
+ }
888
+ options = options || {};
889
+ if (bits === undefined) {
890
+ bits = options.bits || 2048;
891
+ }
892
+ if (e === undefined) {
893
+ e = options.e || 0x10001;
894
+ }
895
+
896
+ // use native code if permitted, available, and parameters are acceptable
897
+ if (!forge.options.usePureJavaScript && !options.prng && bits >= 256 && bits <= 16384 && (e === 0x10001 || e === 3)) {
898
+ if (callback) {
899
+ // try native async
900
+ if (_detectNodeCrypto('generateKeyPair')) {
901
+ return _crypto.generateKeyPair('rsa', {
902
+ modulusLength: bits,
903
+ publicExponent: e,
904
+ publicKeyEncoding: {
905
+ type: 'spki',
906
+ format: 'pem'
907
+ },
908
+ privateKeyEncoding: {
909
+ type: 'pkcs8',
910
+ format: 'pem'
911
+ }
912
+ }, function (err, pub, priv) {
913
+ if (err) {
914
+ return callback(err);
915
+ }
916
+ callback(null, {
917
+ privateKey: pki.privateKeyFromPem(priv),
918
+ publicKey: pki.publicKeyFromPem(pub)
919
+ });
920
+ });
921
+ }
922
+ if (_detectSubtleCrypto('generateKey') && _detectSubtleCrypto('exportKey')) {
923
+ // use standard native generateKey
924
+ return util.globalScope.crypto.subtle.generateKey({
925
+ name: 'RSASSA-PKCS1-v1_5',
926
+ modulusLength: bits,
927
+ publicExponent: _intToUint8Array(e),
928
+ hash: {
929
+ name: 'SHA-256'
930
+ }
931
+ }, true /* key can be exported*/, ['sign', 'verify']).then(function (pair) {
932
+ return util.globalScope.crypto.subtle.exportKey('pkcs8', pair.privateKey);
933
+ // avoiding catch(function(err) {...}) to support IE <= 8
934
+ }).then(undefined, function (err) {
935
+ callback(err);
936
+ }).then(function (pkcs8) {
937
+ if (pkcs8) {
938
+ var privateKey = pki.privateKeyFromAsn1(asn1.fromDer(forge.util.createBuffer(pkcs8)));
939
+ callback(null, {
940
+ privateKey: privateKey,
941
+ publicKey: pki.setRsaPublicKey(privateKey.n, privateKey.e)
942
+ });
943
+ }
944
+ });
945
+ }
946
+ if (_detectSubtleMsCrypto('generateKey') && _detectSubtleMsCrypto('exportKey')) {
947
+ var genOp = util.globalScope.msCrypto.subtle.generateKey({
948
+ name: 'RSASSA-PKCS1-v1_5',
949
+ modulusLength: bits,
950
+ publicExponent: _intToUint8Array(e),
951
+ hash: {
952
+ name: 'SHA-256'
953
+ }
954
+ }, true /* key can be exported*/, ['sign', 'verify']);
955
+ genOp.oncomplete = function (e) {
956
+ var pair = e.target.result;
957
+ var exportOp = util.globalScope.msCrypto.subtle.exportKey('pkcs8', pair.privateKey);
958
+ exportOp.oncomplete = function (e) {
959
+ var pkcs8 = e.target.result;
960
+ var privateKey = pki.privateKeyFromAsn1(asn1.fromDer(forge.util.createBuffer(pkcs8)));
961
+ callback(null, {
962
+ privateKey: privateKey,
963
+ publicKey: pki.setRsaPublicKey(privateKey.n, privateKey.e)
964
+ });
965
+ };
966
+ exportOp.onerror = function (err) {
967
+ callback(err);
968
+ };
969
+ };
970
+ genOp.onerror = function (err) {
971
+ callback(err);
972
+ };
973
+ return;
974
+ }
975
+ } else {
976
+ // try native sync
977
+ if (_detectNodeCrypto('generateKeyPairSync')) {
978
+ var keypair = _crypto.generateKeyPairSync('rsa', {
979
+ modulusLength: bits,
980
+ publicExponent: e,
981
+ publicKeyEncoding: {
982
+ type: 'spki',
983
+ format: 'pem'
984
+ },
985
+ privateKeyEncoding: {
986
+ type: 'pkcs8',
987
+ format: 'pem'
988
+ }
989
+ });
990
+ return {
991
+ privateKey: pki.privateKeyFromPem(keypair.privateKey),
992
+ publicKey: pki.publicKeyFromPem(keypair.publicKey)
993
+ };
994
+ }
995
+ }
996
+ }
997
+
998
+ // use JavaScript implementation
999
+ var state = pki.rsa.createKeyPairGenerationState(bits, e, options);
1000
+ if (!callback) {
1001
+ pki.rsa.stepKeyPairGenerationState(state, 0);
1002
+ return state.keys;
1003
+ }
1004
+ _generateKeyPair(state, options, callback);
1005
+ };
1006
+
1007
+ /**
1008
+ * Sets an RSA public key from BigIntegers modulus and exponent.
1009
+ *
1010
+ * @param n the modulus.
1011
+ * @param e the exponent.
1012
+ *
1013
+ * @return the public key.
1014
+ */
1015
+ pki.setRsaPublicKey = pki.rsa.setPublicKey = function (n, e) {
1016
+ var key = {
1017
+ n: n,
1018
+ e: e
1019
+ };
1020
+
1021
+ /**
1022
+ * Encrypts the given data with this public key. Newer applications
1023
+ * should use the 'RSA-OAEP' decryption scheme, 'RSAES-PKCS1-V1_5' is for
1024
+ * legacy applications.
1025
+ *
1026
+ * @param data the byte string to encrypt.
1027
+ * @param scheme the encryption scheme to use:
1028
+ * 'RSAES-PKCS1-V1_5' (default),
1029
+ * 'RSA-OAEP',
1030
+ * 'RAW', 'NONE', or null to perform raw RSA encryption,
1031
+ * an object with an 'encode' property set to a function
1032
+ * with the signature 'function(data, key)' that returns
1033
+ * a binary-encoded string representing the encoded data.
1034
+ * @param schemeOptions any scheme-specific options.
1035
+ *
1036
+ * @return the encrypted byte string.
1037
+ */
1038
+ key.encrypt = function (data, scheme, schemeOptions) {
1039
+ if (typeof scheme === 'string') {
1040
+ scheme = scheme.toUpperCase();
1041
+ } else if (scheme === undefined) {
1042
+ scheme = 'RSAES-PKCS1-V1_5';
1043
+ }
1044
+ if (scheme === 'RSAES-PKCS1-V1_5') {
1045
+ scheme = {
1046
+ encode: function (m, key, pub) {
1047
+ return _encodePkcs1_v1_5(m, key, 0x02).getBytes();
1048
+ }
1049
+ };
1050
+ } else if (scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') {
1051
+ scheme = {
1052
+ encode: function (m, key) {
1053
+ return forge.pkcs1.encode_rsa_oaep(key, m, schemeOptions);
1054
+ }
1055
+ };
1056
+ } else if (['RAW', 'NONE', 'NULL', null].indexOf(scheme) !== -1) {
1057
+ scheme = {
1058
+ encode: function (e) {
1059
+ return e;
1060
+ }
1061
+ };
1062
+ } else if (typeof scheme === 'string') {
1063
+ throw new Error('Unsupported encryption scheme: "' + scheme + '".');
1064
+ }
1065
+
1066
+ // do scheme-based encoding then rsa encryption
1067
+ var e = scheme.encode(data, key, true);
1068
+ return pki.rsa.encrypt(e, key, true);
1069
+ };
1070
+
1071
+ /**
1072
+ * Verifies the given signature against the given digest.
1073
+ *
1074
+ * PKCS#1 supports multiple (currently two) signature schemes:
1075
+ * RSASSA-PKCS1-V1_5 and RSASSA-PSS.
1076
+ *
1077
+ * By default this implementation uses the "old scheme", i.e.
1078
+ * RSASSA-PKCS1-V1_5, in which case once RSA-decrypted, the
1079
+ * signature is an OCTET STRING that holds a DigestInfo.
1080
+ *
1081
+ * DigestInfo ::= SEQUENCE {
1082
+ * digestAlgorithm DigestAlgorithmIdentifier,
1083
+ * digest Digest
1084
+ * }
1085
+ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1086
+ * Digest ::= OCTET STRING
1087
+ *
1088
+ * To perform PSS signature verification, provide an instance
1089
+ * of Forge PSS object as the scheme parameter.
1090
+ *
1091
+ * @param digest the message digest hash to compare against the signature,
1092
+ * as a binary-encoded string.
1093
+ * @param signature the signature to verify, as a binary-encoded string.
1094
+ * @param scheme signature verification scheme to use:
1095
+ * 'RSASSA-PKCS1-V1_5' or undefined for RSASSA PKCS#1 v1.5,
1096
+ * a Forge PSS object for RSASSA-PSS,
1097
+ * 'NONE' or null for none, DigestInfo will not be expected, but
1098
+ * PKCS#1 v1.5 padding will still be used.
1099
+ * @param options optional verify options
1100
+ * _parseAllDigestBytes testing flag to control parsing of all
1101
+ * digest bytes. Unsupported and not for general usage.
1102
+ * (default: true)
1103
+ *
1104
+ * @return true if the signature was verified, false if not.
1105
+ */
1106
+ key.verify = function (digest, signature, scheme, options) {
1107
+ if (typeof scheme === 'string') {
1108
+ scheme = scheme.toUpperCase();
1109
+ } else if (scheme === undefined) {
1110
+ scheme = 'RSASSA-PKCS1-V1_5';
1111
+ }
1112
+ if (options === undefined) {
1113
+ options = {
1114
+ _parseAllDigestBytes: true
1115
+ };
1116
+ }
1117
+ if (!('_parseAllDigestBytes' in options)) {
1118
+ options._parseAllDigestBytes = true;
1119
+ }
1120
+ if (scheme === 'RSASSA-PKCS1-V1_5') {
1121
+ scheme = {
1122
+ verify: function (digest, d) {
1123
+ // remove padding
1124
+ d = _decodePkcs1_v1_5(d, key, true);
1125
+ // d is ASN.1 BER-encoded DigestInfo
1126
+ var obj = asn1.fromDer(d, {
1127
+ parseAllBytes: options._parseAllDigestBytes
1128
+ });
1129
+
1130
+ // validate DigestInfo
1131
+ var capture = {};
1132
+ var errors = [];
1133
+ if (!asn1.validate(obj, digestInfoValidator, capture, errors)) {
1134
+ var error = new Error('ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 ' + 'DigestInfo value.');
1135
+ error.errors = errors;
1136
+ throw error;
1137
+ }
1138
+ // check hash algorithm identifier
1139
+ // see PKCS1-v1-5DigestAlgorithms in RFC 8017
1140
+ // FIXME: add support to vaidator for strict value choices
1141
+ var oid = asn1.derToOid(capture.algorithmIdentifier);
1142
+ if (!(oid === forge.oids.md2 || oid === forge.oids.md5 || oid === forge.oids.sha1 || oid === forge.oids.sha224 || oid === forge.oids.sha256 || oid === forge.oids.sha384 || oid === forge.oids.sha512 || oid === forge.oids['sha512-224'] || oid === forge.oids['sha512-256'])) {
1143
+ var error = new Error('Unknown RSASSA-PKCS1-v1_5 DigestAlgorithm identifier.');
1144
+ error.oid = oid;
1145
+ throw error;
1146
+ }
1147
+
1148
+ // special check for md2 and md5 that NULL parameters exist
1149
+ if (oid === forge.oids.md2 || oid === forge.oids.md5) {
1150
+ if (!('parameters' in capture)) {
1151
+ throw new Error('ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 ' + 'DigestInfo value. ' + 'Missing algorithm identifer NULL parameters.');
1152
+ }
1153
+ }
1154
+
1155
+ // compare the given digest to the decrypted one
1156
+ return digest === capture.digest;
1157
+ }
1158
+ };
1159
+ } else if (scheme === 'NONE' || scheme === 'NULL' || scheme === null) {
1160
+ scheme = {
1161
+ verify: function (digest, d) {
1162
+ // remove padding
1163
+ d = _decodePkcs1_v1_5(d, key, true);
1164
+ return digest === d;
1165
+ }
1166
+ };
1167
+ }
1168
+
1169
+ // do rsa decryption w/o any decoding, then verify -- which does decoding
1170
+ var d = pki.rsa.decrypt(signature, key, true, false);
1171
+ return scheme.verify(digest, d, key.n.bitLength());
1172
+ };
1173
+ return key;
1174
+ };
1175
+
1176
+ /**
1177
+ * Sets an RSA private key from BigIntegers modulus, exponent, primes,
1178
+ * prime exponents, and modular multiplicative inverse.
1179
+ *
1180
+ * @param n the modulus.
1181
+ * @param e the public exponent.
1182
+ * @param d the private exponent ((inverse of e) mod n).
1183
+ * @param p the first prime.
1184
+ * @param q the second prime.
1185
+ * @param dP exponent1 (d mod (p-1)).
1186
+ * @param dQ exponent2 (d mod (q-1)).
1187
+ * @param qInv ((inverse of q) mod p)
1188
+ *
1189
+ * @return the private key.
1190
+ */
1191
+ pki.setRsaPrivateKey = pki.rsa.setPrivateKey = function (n, e, d, p, q, dP, dQ, qInv) {
1192
+ var key = {
1193
+ n: n,
1194
+ e: e,
1195
+ d: d,
1196
+ p: p,
1197
+ q: q,
1198
+ dP: dP,
1199
+ dQ: dQ,
1200
+ qInv: qInv
1201
+ };
1202
+
1203
+ /**
1204
+ * Decrypts the given data with this private key. The decryption scheme
1205
+ * must match the one used to encrypt the data.
1206
+ *
1207
+ * @param data the byte string to decrypt.
1208
+ * @param scheme the decryption scheme to use:
1209
+ * 'RSAES-PKCS1-V1_5' (default),
1210
+ * 'RSA-OAEP',
1211
+ * 'RAW', 'NONE', or null to perform raw RSA decryption.
1212
+ * @param schemeOptions any scheme-specific options.
1213
+ *
1214
+ * @return the decrypted byte string.
1215
+ */
1216
+ key.decrypt = function (data, scheme, schemeOptions) {
1217
+ if (typeof scheme === 'string') {
1218
+ scheme = scheme.toUpperCase();
1219
+ } else if (scheme === undefined) {
1220
+ scheme = 'RSAES-PKCS1-V1_5';
1221
+ }
1222
+
1223
+ // do rsa decryption w/o any decoding
1224
+ var d = pki.rsa.decrypt(data, key, false, false);
1225
+ if (scheme === 'RSAES-PKCS1-V1_5') {
1226
+ scheme = {
1227
+ decode: _decodePkcs1_v1_5
1228
+ };
1229
+ } else if (scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') {
1230
+ scheme = {
1231
+ decode: function (d, key) {
1232
+ return forge.pkcs1.decode_rsa_oaep(key, d, schemeOptions);
1233
+ }
1234
+ };
1235
+ } else if (['RAW', 'NONE', 'NULL', null].indexOf(scheme) !== -1) {
1236
+ scheme = {
1237
+ decode: function (d) {
1238
+ return d;
1239
+ }
1240
+ };
1241
+ } else {
1242
+ throw new Error('Unsupported encryption scheme: "' + scheme + '".');
1243
+ }
1244
+
1245
+ // decode according to scheme
1246
+ return scheme.decode(d, key, false);
1247
+ };
1248
+
1249
+ /**
1250
+ * Signs the given digest, producing a signature.
1251
+ *
1252
+ * PKCS#1 supports multiple (currently two) signature schemes:
1253
+ * RSASSA-PKCS1-V1_5 and RSASSA-PSS.
1254
+ *
1255
+ * By default this implementation uses the "old scheme", i.e.
1256
+ * RSASSA-PKCS1-V1_5. In order to generate a PSS signature, provide
1257
+ * an instance of Forge PSS object as the scheme parameter.
1258
+ *
1259
+ * @param md the message digest object with the hash to sign.
1260
+ * @param scheme the signature scheme to use:
1261
+ * 'RSASSA-PKCS1-V1_5' or undefined for RSASSA PKCS#1 v1.5,
1262
+ * a Forge PSS object for RSASSA-PSS,
1263
+ * 'NONE' or null for none, DigestInfo will not be used but
1264
+ * PKCS#1 v1.5 padding will still be used.
1265
+ *
1266
+ * @return the signature as a byte string.
1267
+ */
1268
+ key.sign = function (md, scheme) {
1269
+ /* Note: The internal implementation of RSA operations is being
1270
+ transitioned away from a PKCS#1 v1.5 hard-coded scheme. Some legacy
1271
+ code like the use of an encoding block identifier 'bt' will eventually
1272
+ be removed. */
1273
+
1274
+ // private key operation
1275
+ var bt = false;
1276
+ if (typeof scheme === 'string') {
1277
+ scheme = scheme.toUpperCase();
1278
+ }
1279
+ if (scheme === undefined || scheme === 'RSASSA-PKCS1-V1_5') {
1280
+ scheme = {
1281
+ encode: emsaPkcs1v15encode
1282
+ };
1283
+ bt = 0x01;
1284
+ } else if (scheme === 'NONE' || scheme === 'NULL' || scheme === null) {
1285
+ scheme = {
1286
+ encode: function () {
1287
+ return md;
1288
+ }
1289
+ };
1290
+ bt = 0x01;
1291
+ }
1292
+
1293
+ // encode and then encrypt
1294
+ var d = scheme.encode(md, key.n.bitLength());
1295
+ return pki.rsa.encrypt(d, key, bt);
1296
+ };
1297
+ return key;
1298
+ };
1299
+
1300
+ /**
1301
+ * Wraps an RSAPrivateKey ASN.1 object in an ASN.1 PrivateKeyInfo object.
1302
+ *
1303
+ * @param rsaKey the ASN.1 RSAPrivateKey.
1304
+ *
1305
+ * @return the ASN.1 PrivateKeyInfo.
1306
+ */
1307
+ pki.wrapRsaPrivateKey = function (rsaKey) {
1308
+ // PrivateKeyInfo
1309
+ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
1310
+ // version (0)
1311
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, asn1.integerToDer(0).getBytes()),
1312
+ // privateKeyAlgorithm
1313
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, asn1.oidToDer(pki.oids.rsaEncryption).getBytes()), asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')]),
1314
+ // PrivateKey
1315
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, asn1.toDer(rsaKey).getBytes())]);
1316
+ };
1317
+
1318
+ /**
1319
+ * Converts a private key from an ASN.1 object.
1320
+ *
1321
+ * @param obj the ASN.1 representation of a PrivateKeyInfo containing an
1322
+ * RSAPrivateKey or an RSAPrivateKey.
1323
+ *
1324
+ * @return the private key.
1325
+ */
1326
+ pki.privateKeyFromAsn1 = function (obj) {
1327
+ // get PrivateKeyInfo
1328
+ var capture = {};
1329
+ var errors = [];
1330
+ if (asn1.validate(obj, privateKeyValidator, capture, errors)) {
1331
+ obj = asn1.fromDer(forge.util.createBuffer(capture.privateKey));
1332
+ }
1333
+
1334
+ // get RSAPrivateKey
1335
+ capture = {};
1336
+ errors = [];
1337
+ if (!asn1.validate(obj, rsaPrivateKeyValidator, capture, errors)) {
1338
+ var error = new Error('Cannot read private key. ' + 'ASN.1 object does not contain an RSAPrivateKey.');
1339
+ error.errors = errors;
1340
+ throw error;
1341
+ }
1342
+
1343
+ // Note: Version is currently ignored.
1344
+ // capture.privateKeyVersion
1345
+ // FIXME: inefficient, get a BigInteger that uses byte strings
1346
+ var n, e, d, p, q, dP, dQ, qInv;
1347
+ n = forge.util.createBuffer(capture.privateKeyModulus).toHex();
1348
+ e = forge.util.createBuffer(capture.privateKeyPublicExponent).toHex();
1349
+ d = forge.util.createBuffer(capture.privateKeyPrivateExponent).toHex();
1350
+ p = forge.util.createBuffer(capture.privateKeyPrime1).toHex();
1351
+ q = forge.util.createBuffer(capture.privateKeyPrime2).toHex();
1352
+ dP = forge.util.createBuffer(capture.privateKeyExponent1).toHex();
1353
+ dQ = forge.util.createBuffer(capture.privateKeyExponent2).toHex();
1354
+ qInv = forge.util.createBuffer(capture.privateKeyCoefficient).toHex();
1355
+
1356
+ // set private key
1357
+ return pki.setRsaPrivateKey(new BigInteger(n, 16), new BigInteger(e, 16), new BigInteger(d, 16), new BigInteger(p, 16), new BigInteger(q, 16), new BigInteger(dP, 16), new BigInteger(dQ, 16), new BigInteger(qInv, 16));
1358
+ };
1359
+
1360
+ /**
1361
+ * Converts a private key to an ASN.1 RSAPrivateKey.
1362
+ *
1363
+ * @param key the private key.
1364
+ *
1365
+ * @return the ASN.1 representation of an RSAPrivateKey.
1366
+ */
1367
+ pki.privateKeyToAsn1 = pki.privateKeyToRSAPrivateKey = function (key) {
1368
+ // RSAPrivateKey
1369
+ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
1370
+ // version (0 = only 2 primes, 1 multiple primes)
1371
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, asn1.integerToDer(0).getBytes()),
1372
+ // modulus (n)
1373
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.n)),
1374
+ // publicExponent (e)
1375
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.e)),
1376
+ // privateExponent (d)
1377
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.d)),
1378
+ // privateKeyPrime1 (p)
1379
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.p)),
1380
+ // privateKeyPrime2 (q)
1381
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.q)),
1382
+ // privateKeyExponent1 (dP)
1383
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.dP)),
1384
+ // privateKeyExponent2 (dQ)
1385
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.dQ)),
1386
+ // coefficient (qInv)
1387
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.qInv))]);
1388
+ };
1389
+
1390
+ /**
1391
+ * Converts a public key from an ASN.1 SubjectPublicKeyInfo or RSAPublicKey.
1392
+ *
1393
+ * @param obj the asn1 representation of a SubjectPublicKeyInfo or RSAPublicKey.
1394
+ *
1395
+ * @return the public key.
1396
+ */
1397
+ pki.publicKeyFromAsn1 = function (obj) {
1398
+ // get SubjectPublicKeyInfo
1399
+ var capture = {};
1400
+ var errors = [];
1401
+ if (asn1.validate(obj, publicKeyValidator, capture, errors)) {
1402
+ // get oid
1403
+ var oid = asn1.derToOid(capture.publicKeyOid);
1404
+ if (oid !== pki.oids.rsaEncryption) {
1405
+ var error = new Error('Cannot read public key. Unknown OID.');
1406
+ error.oid = oid;
1407
+ throw error;
1408
+ }
1409
+ obj = capture.rsaPublicKey;
1410
+ }
1411
+
1412
+ // get RSA params
1413
+ errors = [];
1414
+ if (!asn1.validate(obj, rsaPublicKeyValidator, capture, errors)) {
1415
+ var error = new Error('Cannot read public key. ' + 'ASN.1 object does not contain an RSAPublicKey.');
1416
+ error.errors = errors;
1417
+ throw error;
1418
+ }
1419
+
1420
+ // FIXME: inefficient, get a BigInteger that uses byte strings
1421
+ var n = forge.util.createBuffer(capture.publicKeyModulus).toHex();
1422
+ var e = forge.util.createBuffer(capture.publicKeyExponent).toHex();
1423
+
1424
+ // set public key
1425
+ return pki.setRsaPublicKey(new BigInteger(n, 16), new BigInteger(e, 16));
1426
+ };
1427
+
1428
+ /**
1429
+ * Converts a public key to an ASN.1 SubjectPublicKeyInfo.
1430
+ *
1431
+ * @param key the public key.
1432
+ *
1433
+ * @return the asn1 representation of a SubjectPublicKeyInfo.
1434
+ */
1435
+ pki.publicKeyToAsn1 = pki.publicKeyToSubjectPublicKeyInfo = function (key) {
1436
+ // SubjectPublicKeyInfo
1437
+ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
1438
+ // AlgorithmIdentifier
1439
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
1440
+ // algorithm
1441
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, asn1.oidToDer(pki.oids.rsaEncryption).getBytes()),
1442
+ // parameters (null)
1443
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')]),
1444
+ // subjectPublicKey
1445
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false, [pki.publicKeyToRSAPublicKey(key)])]);
1446
+ };
1447
+
1448
+ /**
1449
+ * Converts a public key to an ASN.1 RSAPublicKey.
1450
+ *
1451
+ * @param key the public key.
1452
+ *
1453
+ * @return the asn1 representation of a RSAPublicKey.
1454
+ */
1455
+ pki.publicKeyToRSAPublicKey = function (key) {
1456
+ // RSAPublicKey
1457
+ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
1458
+ // modulus (n)
1459
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.n)),
1460
+ // publicExponent (e)
1461
+ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, _bnToBytes(key.e))]);
1462
+ };
1463
+
1464
+ /**
1465
+ * Encodes a message using PKCS#1 v1.5 padding.
1466
+ *
1467
+ * @param m the message to encode.
1468
+ * @param key the RSA key to use.
1469
+ * @param bt the block type to use, i.e. either 0x01 (for signing) or 0x02
1470
+ * (for encryption).
1471
+ *
1472
+ * @return the padded byte buffer.
1473
+ */
1474
+ function _encodePkcs1_v1_5(m, key, bt) {
1475
+ var eb = forge.util.createBuffer();
1476
+
1477
+ // get the length of the modulus in bytes
1478
+ var k = Math.ceil(key.n.bitLength() / 8);
1479
+
1480
+ /* use PKCS#1 v1.5 padding */
1481
+ if (m.length > k - 11) {
1482
+ var error = new Error('Message is too long for PKCS#1 v1.5 padding.');
1483
+ error.length = m.length;
1484
+ error.max = k - 11;
1485
+ throw error;
1486
+ }
1487
+
1488
+ /* A block type BT, a padding string PS, and the data D shall be
1489
+ formatted into an octet string EB, the encryption block:
1490
+ EB = 00 || BT || PS || 00 || D
1491
+ The block type BT shall be a single octet indicating the structure of
1492
+ the encryption block. For this version of the document it shall have
1493
+ value 00, 01, or 02. For a private-key operation, the block type
1494
+ shall be 00 or 01. For a public-key operation, it shall be 02.
1495
+ The padding string PS shall consist of k-3-||D|| octets. For block
1496
+ type 00, the octets shall have value 00; for block type 01, they
1497
+ shall have value FF; and for block type 02, they shall be
1498
+ pseudorandomly generated and nonzero. This makes the length of the
1499
+ encryption block EB equal to k. */
1500
+
1501
+ // build the encryption block
1502
+ eb.putByte(0x00);
1503
+ eb.putByte(bt);
1504
+
1505
+ // create the padding
1506
+ var padNum = k - 3 - m.length;
1507
+ var padByte;
1508
+ // private key op
1509
+ if (bt === 0x00 || bt === 0x01) {
1510
+ padByte = bt === 0x00 ? 0x00 : 0xFF;
1511
+ for (var i = 0; i < padNum; ++i) {
1512
+ eb.putByte(padByte);
1513
+ }
1514
+ } else {
1515
+ // public key op
1516
+ // pad with random non-zero values
1517
+ while (padNum > 0) {
1518
+ var numZeros = 0;
1519
+ var padBytes = forge.random.getBytes(padNum);
1520
+ for (var i = 0; i < padNum; ++i) {
1521
+ padByte = padBytes.charCodeAt(i);
1522
+ if (padByte === 0) {
1523
+ ++numZeros;
1524
+ } else {
1525
+ eb.putByte(padByte);
1526
+ }
1527
+ }
1528
+ padNum = numZeros;
1529
+ }
1530
+ }
1531
+
1532
+ // zero followed by message
1533
+ eb.putByte(0x00);
1534
+ eb.putBytes(m);
1535
+ return eb;
1536
+ }
1537
+
1538
+ /**
1539
+ * Decodes a message using PKCS#1 v1.5 padding.
1540
+ *
1541
+ * @param em the message to decode.
1542
+ * @param key the RSA key to use.
1543
+ * @param pub true if the key is a public key, false if it is private.
1544
+ * @param ml the message length, if specified.
1545
+ *
1546
+ * @return the decoded bytes.
1547
+ */
1548
+ function _decodePkcs1_v1_5(em, key, pub, ml) {
1549
+ // get the length of the modulus in bytes
1550
+ var k = Math.ceil(key.n.bitLength() / 8);
1551
+
1552
+ /* It is an error if any of the following conditions occurs:
1553
+ 1. The encryption block EB cannot be parsed unambiguously.
1554
+ 2. The padding string PS consists of fewer than eight octets
1555
+ or is inconsisent with the block type BT.
1556
+ 3. The decryption process is a public-key operation and the block
1557
+ type BT is not 00 or 01, or the decryption process is a
1558
+ private-key operation and the block type is not 02.
1559
+ */
1560
+
1561
+ // parse the encryption block
1562
+ var eb = forge.util.createBuffer(em);
1563
+ var first = eb.getByte();
1564
+ var bt = eb.getByte();
1565
+ if (first !== 0x00 || pub && bt !== 0x00 && bt !== 0x01 || !pub && bt != 0x02 || pub && bt === 0x00 && typeof ml === 'undefined') {
1566
+ throw new Error('Encryption block is invalid.');
1567
+ }
1568
+ var padNum = 0;
1569
+ if (bt === 0x00) {
1570
+ // check all padding bytes for 0x00
1571
+ padNum = k - 3 - ml;
1572
+ for (var i = 0; i < padNum; ++i) {
1573
+ if (eb.getByte() !== 0x00) {
1574
+ throw new Error('Encryption block is invalid.');
1575
+ }
1576
+ }
1577
+ } else if (bt === 0x01) {
1578
+ // find the first byte that isn't 0xFF, should be after all padding
1579
+ padNum = 0;
1580
+ while (eb.length() > 1) {
1581
+ if (eb.getByte() !== 0xFF) {
1582
+ --eb.read;
1583
+ break;
1584
+ }
1585
+ ++padNum;
1586
+ }
1587
+ } else if (bt === 0x02) {
1588
+ // look for 0x00 byte
1589
+ padNum = 0;
1590
+ while (eb.length() > 1) {
1591
+ if (eb.getByte() === 0x00) {
1592
+ --eb.read;
1593
+ break;
1594
+ }
1595
+ ++padNum;
1596
+ }
1597
+ }
1598
+
1599
+ // zero must be 0x00 and padNum must be (k - 3 - message length)
1600
+ var zero = eb.getByte();
1601
+ if (zero !== 0x00 || padNum !== k - 3 - eb.length()) {
1602
+ throw new Error('Encryption block is invalid.');
1603
+ }
1604
+ return eb.getBytes();
1605
+ }
1606
+
1607
+ /**
1608
+ * Runs the key-generation algorithm asynchronously, either in the background
1609
+ * via Web Workers, or using the main thread and setImmediate.
1610
+ *
1611
+ * @param state the key-pair generation state.
1612
+ * @param [options] options for key-pair generation:
1613
+ * workerScript the worker script URL.
1614
+ * workers the number of web workers (if supported) to use,
1615
+ * (default: 2, -1 to use estimated cores minus one).
1616
+ * workLoad the size of the work load, ie: number of possible prime
1617
+ * numbers for each web worker to check per work assignment,
1618
+ * (default: 100).
1619
+ * @param callback(err, keypair) called once the operation completes.
1620
+ */
1621
+ function _generateKeyPair(state, options, callback) {
1622
+ if (typeof options === 'function') {
1623
+ callback = options;
1624
+ options = {};
1625
+ }
1626
+ options = options || {};
1627
+ var opts = {
1628
+ algorithm: {
1629
+ name: options.algorithm || 'PRIMEINC',
1630
+ options: {
1631
+ workers: options.workers || 2,
1632
+ workLoad: options.workLoad || 100,
1633
+ workerScript: options.workerScript
1634
+ }
1635
+ }
1636
+ };
1637
+ if ('prng' in options) {
1638
+ opts.prng = options.prng;
1639
+ }
1640
+ generate();
1641
+ function generate() {
1642
+ // find p and then q (done in series to simplify)
1643
+ getPrime(state.pBits, function (err, num) {
1644
+ if (err) {
1645
+ return callback(err);
1646
+ }
1647
+ state.p = num;
1648
+ if (state.q !== null) {
1649
+ return finish(err, state.q);
1650
+ }
1651
+ getPrime(state.qBits, finish);
1652
+ });
1653
+ }
1654
+ function getPrime(bits, callback) {
1655
+ forge.prime.generateProbablePrime(bits, opts, callback);
1656
+ }
1657
+ function finish(err, num) {
1658
+ if (err) {
1659
+ return callback(err);
1660
+ }
1661
+
1662
+ // set q
1663
+ state.q = num;
1664
+
1665
+ // ensure p is larger than q (swap them if not)
1666
+ if (state.p.compareTo(state.q) < 0) {
1667
+ var tmp = state.p;
1668
+ state.p = state.q;
1669
+ state.q = tmp;
1670
+ }
1671
+
1672
+ // ensure p is coprime with e
1673
+ if (state.p.subtract(BigInteger.ONE).gcd(state.e).compareTo(BigInteger.ONE) !== 0) {
1674
+ state.p = null;
1675
+ generate();
1676
+ return;
1677
+ }
1678
+
1679
+ // ensure q is coprime with e
1680
+ if (state.q.subtract(BigInteger.ONE).gcd(state.e).compareTo(BigInteger.ONE) !== 0) {
1681
+ state.q = null;
1682
+ getPrime(state.qBits, finish);
1683
+ return;
1684
+ }
1685
+
1686
+ // compute phi: (p - 1)(q - 1) (Euler's totient function)
1687
+ state.p1 = state.p.subtract(BigInteger.ONE);
1688
+ state.q1 = state.q.subtract(BigInteger.ONE);
1689
+ state.phi = state.p1.multiply(state.q1);
1690
+
1691
+ // ensure e and phi are coprime
1692
+ if (state.phi.gcd(state.e).compareTo(BigInteger.ONE) !== 0) {
1693
+ // phi and e aren't coprime, so generate a new p and q
1694
+ state.p = state.q = null;
1695
+ generate();
1696
+ return;
1697
+ }
1698
+
1699
+ // create n, ensure n is has the right number of bits
1700
+ state.n = state.p.multiply(state.q);
1701
+ if (state.n.bitLength() !== state.bits) {
1702
+ // failed, get new q
1703
+ state.q = null;
1704
+ getPrime(state.qBits, finish);
1705
+ return;
1706
+ }
1707
+
1708
+ // set keys
1709
+ var d = state.e.modInverse(state.phi);
1710
+ state.keys = {
1711
+ privateKey: pki.rsa.setPrivateKey(state.n, state.e, d, state.p, state.q, d.mod(state.p1), d.mod(state.q1), state.q.modInverse(state.p)),
1712
+ publicKey: pki.rsa.setPublicKey(state.n, state.e)
1713
+ };
1714
+ callback(null, state.keys);
1715
+ }
1716
+ }
1717
+
1718
+ /**
1719
+ * Converts a positive BigInteger into 2's-complement big-endian bytes.
1720
+ *
1721
+ * @param b the big integer to convert.
1722
+ *
1723
+ * @return the bytes.
1724
+ */
1725
+ function _bnToBytes(b) {
1726
+ // prepend 0x00 if first byte >= 0x80
1727
+ var hex = b.toString(16);
1728
+ if (hex[0] >= '8') {
1729
+ hex = '00' + hex;
1730
+ }
1731
+ var bytes = forge.util.hexToBytes(hex);
1732
+
1733
+ // ensure integer is minimally-encoded
1734
+ if (bytes.length > 1 && (
1735
+ // leading 0x00 for positive integer
1736
+ bytes.charCodeAt(0) === 0 && (bytes.charCodeAt(1) & 0x80) === 0 ||
1737
+ // leading 0xFF for negative integer
1738
+ bytes.charCodeAt(0) === 0xFF && (bytes.charCodeAt(1) & 0x80) === 0x80)) {
1739
+ return bytes.substr(1);
1740
+ }
1741
+ return bytes;
1742
+ }
1743
+
1744
+ /**
1745
+ * Returns the required number of Miller-Rabin tests to generate a
1746
+ * prime with an error probability of (1/2)^80.
1747
+ *
1748
+ * See Handbook of Applied Cryptography Chapter 4, Table 4.4.
1749
+ *
1750
+ * @param bits the bit size.
1751
+ *
1752
+ * @return the required number of iterations.
1753
+ */
1754
+ function _getMillerRabinTests(bits) {
1755
+ if (bits <= 100) return 27;
1756
+ if (bits <= 150) return 18;
1757
+ if (bits <= 200) return 15;
1758
+ if (bits <= 250) return 12;
1759
+ if (bits <= 300) return 9;
1760
+ if (bits <= 350) return 8;
1761
+ if (bits <= 400) return 7;
1762
+ if (bits <= 500) return 6;
1763
+ if (bits <= 600) return 5;
1764
+ if (bits <= 800) return 4;
1765
+ if (bits <= 1250) return 3;
1766
+ return 2;
1767
+ }
1768
+
1769
+ /**
1770
+ * Performs feature detection on the Node crypto interface.
1771
+ *
1772
+ * @param fn the feature (function) to detect.
1773
+ *
1774
+ * @return true if detected, false if not.
1775
+ */
1776
+ function _detectNodeCrypto(fn) {
1777
+ return forge.util.isNodejs && typeof _crypto[fn] === 'function';
1778
+ }
1779
+
1780
+ /**
1781
+ * Performs feature detection on the SubtleCrypto interface.
1782
+ *
1783
+ * @param fn the feature (function) to detect.
1784
+ *
1785
+ * @return true if detected, false if not.
1786
+ */
1787
+ function _detectSubtleCrypto(fn) {
1788
+ return typeof util.globalScope !== 'undefined' && typeof util.globalScope.crypto === 'object' && typeof util.globalScope.crypto.subtle === 'object' && typeof util.globalScope.crypto.subtle[fn] === 'function';
1789
+ }
1790
+
1791
+ /**
1792
+ * Performs feature detection on the deprecated Microsoft Internet Explorer
1793
+ * outdated SubtleCrypto interface. This function should only be used after
1794
+ * checking for the modern, standard SubtleCrypto interface.
1795
+ *
1796
+ * @param fn the feature (function) to detect.
1797
+ *
1798
+ * @return true if detected, false if not.
1799
+ */
1800
+ function _detectSubtleMsCrypto(fn) {
1801
+ return typeof util.globalScope !== 'undefined' && typeof util.globalScope.msCrypto === 'object' && typeof util.globalScope.msCrypto.subtle === 'object' && typeof util.globalScope.msCrypto.subtle[fn] === 'function';
1802
+ }
1803
+ function _intToUint8Array(x) {
1804
+ var bytes = forge.util.hexToBytes(x.toString(16));
1805
+ var buffer = new Uint8Array(bytes.length);
1806
+ for (var i = 0; i < bytes.length; ++i) {
1807
+ buffer[i] = bytes.charCodeAt(i);
1808
+ }
1809
+ return buffer;
1810
+ }
1811
+ return rsa;
1812
+ }
1813
+
1814
+ export { requireRsa as __require };
1815
+ //# sourceMappingURL=rsa.js.map