@@ -14,6 +14,27 @@ function readUInt32BE(buf, offset) {
1414 + buf [ offset ] ;
1515}
1616
17+ function convertToMpint ( buf ) {
18+ let idx = 0 ;
19+ let length = buf . length ;
20+ while ( buf [ idx ] === 0x00 ) {
21+ ++ idx ;
22+ -- length ;
23+ }
24+ let newBuf ;
25+ if ( buf [ idx ] & 0x80 ) {
26+ newBuf = Buffer . allocUnsafe ( 1 + length ) ;
27+ newBuf [ 0 ] = 0 ;
28+ buf . copy ( newBuf , 1 , idx ) ;
29+ buf = newBuf ;
30+ } else if ( length !== buf . length ) {
31+ newBuf = Buffer . allocUnsafe ( length ) ;
32+ buf . copy ( newBuf , 0 , idx ) ;
33+ buf = newBuf ;
34+ }
35+ return buf ;
36+ }
37+
1738function bufferCopy ( src , dest , srcStart , srcEnd , destStart ) {
1839 if ( ! destStart )
1940 destStart = 0 ;
@@ -163,6 +184,7 @@ const utilBufferParser = makeBufferParser();
163184module . exports = {
164185 bufferCopy,
165186 bufferSlice,
187+ convertToMpint,
166188 FastBuffer,
167189 bufferFill : ( buf , value , start , end ) => {
168190 return TypedArrayFill . call ( buf , value , start , end ) ;
@@ -291,8 +313,8 @@ module.exports = {
291313 // total) like OpenSSH (and possibly others) are expecting
292314 const asnReader = new Ber . Reader ( signature ) ;
293315 asnReader . readSequence ( ) ;
294- let r = asnReader . readString ( Ber . Integer , true ) ;
295- let s = asnReader . readString ( Ber . Integer , true ) ;
316+ let r = convertToMpint ( asnReader . readString ( Ber . Integer , true ) ) ;
317+ let s = convertToMpint ( asnReader . readString ( Ber . Integer , true ) ) ;
296318 let rOffset = 0 ;
297319 let sOffset = 0 ;
298320 if ( r . length < 20 ) {
0 commit comments