@@ -176,10 +176,25 @@ Key.prototype.createVerify = function (hashAlgo) {
176
176
var oldVerify = v . verify . bind ( v ) ;
177
177
var key = this . toBuffer ( 'pkcs8' ) ;
178
178
v . verify = function ( signature , fmt ) {
179
- if ( typeof ( signature ) === 'object' &&
180
- signature instanceof Signature )
179
+ if ( Signature . isSignature ( signature , [ 2 , 0 ] ) ) {
181
180
return ( oldVerify ( key , signature . toBuffer ( 'asn1' ) ) ) ;
182
- return ( oldVerify ( key , signature , fmt ) ) ;
181
+
182
+ } else if ( typeof ( signature ) === 'string' ||
183
+ Buffer . isBuffer ( signature ) ) {
184
+ return ( oldVerify ( key , signature , fmt ) ) ;
185
+
186
+ /*
187
+ * Avoid doing this on valid arguments, walking the prototype
188
+ * chain can be quite slow.
189
+ */
190
+ } else if ( Signature . isSignature ( signature , [ 1 , 0 ] ) ) {
191
+ throw ( new Error ( 'signature was created by too old ' +
192
+ 'a version of sshpk and cannot be verified' ) ) ;
193
+
194
+ } else {
195
+ throw ( new TypeError ( 'signature must be a string, ' +
196
+ 'Buffer, or Signature object' ) ) ;
197
+ }
183
198
} ;
184
199
return ( v ) ;
185
200
} ;
@@ -214,3 +229,32 @@ Key.parse = function (data, format, name) {
214
229
throw ( new KeyParseError ( name , format , e ) ) ;
215
230
}
216
231
} ;
232
+
233
+ Key . isKey = function ( obj , ver ) {
234
+ return ( utils . isCompatible ( obj , Key , ver ) ) ;
235
+ } ;
236
+
237
+ /*
238
+ * API versions for Key:
239
+ * [1,0] -- initial ver, may take Signature for createVerify or may not
240
+ * [1,1] -- added pkcs1, pkcs8 formats
241
+ * [1,2] -- added auto, ssh-private, openssh formats
242
+ * [1,3] -- added defaultHashAlgorithm
243
+ * [1,4] -- added ed support, createDH
244
+ * [1,5] -- first explicitly tagged version
245
+ */
246
+ Key . prototype . _sshpkApiVersion = [ 1 , 5 ] ;
247
+
248
+ Key . _oldVersionDetect = function ( obj ) {
249
+ assert . func ( obj . toBuffer ) ;
250
+ assert . func ( obj . fingerprint ) ;
251
+ if ( obj . createDH )
252
+ return ( [ 1 , 4 ] ) ;
253
+ if ( obj . defaultHashAlgorithm )
254
+ return ( [ 1 , 3 ] ) ;
255
+ if ( obj . formats [ 'auto' ] )
256
+ return ( [ 1 , 2 ] ) ;
257
+ if ( obj . formats [ 'pkcs1' ] )
258
+ return ( [ 1 , 1 ] ) ;
259
+ return ( [ 1 , 0 ] ) ;
260
+ } ;
0 commit comments