Bitcoin Forum
December 09, 2016, 09:47:01 AM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1] 2 »  All
  Print  
Author Topic: Paying to get RSA signing working in Javascript  (Read 4428 times)
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 07:46:20 AM
 #1

I hate Javascript, hate it with a passion

The person that solves this will get 70Bitcoins, and can (for another 120Bitcoins) continue on and develope this into an interface for GLBSE. As well as the bitcoin itself there will be major credit for this, and your name will be added to GLBSE's (soon to be created) contributors page.

I am attempting to use some Javascript libraries to sign base64 encoded data with an RSA prvate key, the signature is then also encoded base64 which is sent to a server, the server verifies it with the public key, and then I do some stuff.

The problem is the JavaScript lib's seem to be signing but the output is not correct.


Private key is:
Code:
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA9cbXaYIbd41nTHnMcs4KGtsc+N+k4n/yQUJSoEq2F7Tz742a
ZZZU+DZCQ7T/4ZHkmTtO2tYY877ptdIn7uJwGEbh6uwqEoO4Q9kYbbMyrsFCFozh
Q+Ro0goztGmqcXwn0p1K70Yay/4UbNoZUs86HgsuZyb7pLChzDoAWTp4gzLBxVq9
su9xENuYEd/5viKvwGY7EaKtWqLj4GaM7YPhINwWC+nGI+EobmeTKTxWcFj+/azw
1pv8iXNZ53mc6kVWCCNoEpqrVtV7OJ2rEQoVzfhmcA0ODMqGIg9SpsOOJLcN/8WB
B8Oj/cSQuvKotrkUw+4FEHxbv6NULqm102/hcwIDAQABAoIBAB4dNPuqg28MlmGa
cuCGOlq8Cjq8PFm8T5SLadBWFyWkeFcbxJU57ENVcp0VKl0bISVdTPYnUXtcIwqy
K05r1ExFMx0zvpvyI7u8d+/JCya6azyUGm3VKLrDoT4zzDyHd+GogCv9LP9drGcB
+iRZW8eemxrsfdzZ/5kkajKEpYt9iSRmsem2u5ylQb8eA4sfsi1lqU/3OUhf7NpG
tq4UFZMCtNmDMdsH97049aqO5PswIY4odN35qHYjn9r4FdSHHY0Cy6qU84dY6Y3z
40erWs4/0yFNSrloc6rXOCiuIz9L9pOVy/ovKWz3T9+lq7/sEBo4tzixI+uNQ00m
nzDyjRECgYEA+371ZGI4tSqd77ZbG+GQHJxm8P2TkGSZMeU0jieqAfyLSJkAxuXb
lTWXNCkmpkbzHHUzFMkjNhg8qYfOETdHnebmypgL/1V+lYxbAhgN/guP0a7GHo1v
fXSk2k+F6/sI4JGi072JF3y0jSBIH64Zy1rotksBuBqFI+onYxfZnWcCgYEA+i2p
b321Wk0fjM0FYzaQPOaqFQqKpFaY3PMcioLxByYXjLl99m+iJbTe0QUq72juoNmk
rJnK8vQs/1rDLO5ShMxcJ7tyOyOQosKYMBiXBELd224OQFLwxiIylWtAkZpe/Y9o
IGEEz5OX7PA/b6t5OIEo98G0pGNXvMiPcm+HSBUCgYAHpuM8fKiRCoznoDrBbGmA
FVokdvKocAchN4IT2S0ZBmAOg2k8o/28WncRRTdB0+LNuNsnBL+ng7FEzzb72xy4
ty/KdjnXOPizACQ8uhKi6iMkFBc/ErqTrJrfcY/3kBnDC2tS2e9z4X101cib/iYW
w9tUwI961IO8hgFQyIb8fQKBgQCOg3ZyqYSmhSqE1cHM66cs2iVnSRMDXMVFkEPg
74iIDv35Ty1fNeaDXk5TeYMk0HynKnQxUoaW8du0NyrZrIVbWuLgS1peL/tTjFCP
jWJxrfmd50q8/8Jiy9ZO4cLtbqfP/2JMl5fa+3PfKFiqr16lfoYs5K6XunOfegyl
Hr1MpQKBgFZ84EaudGufLx1Bo0+v+FcOFfbQxEmkkyJpQwoi6Dvmh/MTe3TNegaY
MBf7rm6l65ZqfBMAwRDjE/dhVwRNpd9jRzPVBVyWRZOJkd63n/ENPdIiUrFdvgIt
SFNKk00LvLk4VgWBtUbK9wdk8GQHBrd1mNXnni2x0u0VWLDnW5Zs
-----END RSA PRIVATE KEY-----

The test data being signed is(already base64 encoded):
Code:
eyJ1c2VyaWQiOiJcdTAwMGFkOTQ2ZWQ2MDFmMTNhNDZmNjU1YjUwODM4N2FmZTc2OWYzNTNiMzk1MmY3YTMzOWE4Mjc3ZTdmMTlkYTU4NGQ4XHUwMDBhIiwibm9uY2UiOjg2MTU2NjE5NTE2NX0=


We're using SHA512 as the padding digest.

Signature(base64 encoded) should be:

Code:
AZIigvzu0G2BG/RpDz05qZZctQvzTGNOiOJzziL1+eQEU42cwp/LPOv+6DwMgDIIf2/DAHD+pe7bsxIWftlr65z1bRTYdQ+K+VA0fC6m36muny3NLsKl4hrqIVVWQsTZwbUqJcOvYaNcYMaXr05hTMKC4XAkqKNSqLBOc41k7vU8V6duYwndal7rZiWvso+cH0HxffJszLREUbamMtNBapmhXJCsn2PafQ+RyFj5VnJPYe5MIp/d86yco2q1PC2jLyySxH2kma6d1dq/yxgFiTiABT80w7/9GPlE+cNFIuclEeeYZyo+3r1R7Lgqhnr98MGC7dGjrW5BK5zSRHjgBw==

What I keep getting is:
Code:
GSIoL87tBtgRv0aQ89OamWXLUL80xjTojic84i9fnkBFONnMKfyzzr/ug8DIAyCH9vwwBw/qXu27 MSFn7Za+uc9W0U2HUPivlQNHwupt+prp8tzS7CpeIa6iFVVkLE2cG1KiXDr2GjXGDGl69OYUzCgu FwJKijUqiwTnONZO71PFenbmMJ3Wpe62Ylr7KPnB9B8X3ybMy0RFG2pjLTQWqZoVyQrJ9j2n0Pkc hY+VZyT2HuTCKf3fOsnKNqtTwtoy8sksR9pJmundXav8sYBYk4gAU/NMO//Rj5RPnDRSLnJRHnmG cqPt69Uey4KoZ6/fDBgu3Ro61uQSuc0kR44A

Get the function sign_and_encode(data,private_key) to return the correct output, the fuction (and all it's dependencies) can be found here http://dev.glbse.com:4567/rsa.html  just view source. Time is very important, we need to move on ASAP.

If needs be you can use another Javscript RSA library if the one being used is simply broken.

Note:updated to remove linebreaks from the base64 endoded signature

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
1481276821
Hero Member
*
Offline Offline

Posts: 1481276821

View Profile Personal Message (Offline)

Ignore
1481276821
Reply with quote  #2

1481276821
Report to moderator
1481276821
Hero Member
*
Offline Offline

Posts: 1481276821

View Profile Personal Message (Offline)

Ignore
1481276821
Reply with quote  #2

1481276821
Report to moderator
There are several different types of Bitcoin clients. The most secure are full nodes like Bitcoin-Qt, but full nodes are more resource-heavy, and they must do a lengthy initial syncing process. As a result, lightweight clients with somewhat less security are commonly used.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481276821
Hero Member
*
Offline Offline

Posts: 1481276821

View Profile Personal Message (Offline)

Ignore
1481276821
Reply with quote  #2

1481276821
Report to moderator
1481276821
Hero Member
*
Offline Offline

Posts: 1481276821

View Profile Personal Message (Offline)

Ignore
1481276821
Reply with quote  #2

1481276821
Report to moderator
de4l
Member
**
Offline Offline

Activity: 83


View Profile
May 23, 2011, 08:18:13 AM
 #2


Signature(base64 encoded) should be:

Code:
AZIigvzu0G2BG/RpDz05qZZctQvzTGNOiOJzziL1+eQEU42cwp/LPOv+6DwM\ngDIIf2/DAHD+pe7bsxIWftlr65z1bRTYdQ+K+VA0fC6m36muny3NLsKl4hrq\nIVVWQsTZwbUqJcOvYaNcYMaXr05hTMKC4XAkqKNSqLBOc41k7vU8V6duYwnd\nal7rZiWvso+cH0HxffJszLREUbamMtNBapmhXJCsn2PafQ+RyFj5VnJPYe5M\nIp/d86yco2q1PC2jLyySxH2kma6d1dq/yxgFiTiABT80w7/9GPlE+cNFIucl\nEeeYZyo+3r1R7Lgqhnr98MGC7dGjrW5BK5zSRHjgBw==


I'm looking through everything but so far, this does not appear to be a valid base-64 string for me at least

ps. or is the implication that this was the (RSA encoded (base-64 encoded(data)) and not (base64(rsa(base64(data))))
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 08:28:28 AM
 #3

Sorry, that seems to be including linebreaks, here it is without the linebreaks(\n).

Code:
AZIigvzu0G2BG/RpDz05qZZctQvzTGNOiOJzziL1+eQEU42cwp/LPOv+6DwMgDIIf2/DAHD+pe7bsxIWftlr65z1bRTYdQ+K+VA0fC6m36muny3NLsKl4hrqIVVWQsTZwbUqJcOvYaNcYMaXr05hTMKC4XAkqKNSqLBOc41k7vU8V6duYwndal7rZiWvso+cH0HxffJszLREUbamMtNBapmhXJCsn2PafQ+RyFj5VnJPYe5MIp/d86yco2q1PC2jLyySxH2kma6d1dq/yxgFiTiABT80w7/9GPlE+cNFIuclEeeYZyo+3r1R7Lgqhnr98MGC7dGjrW5BK5zSRHjgBw==

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
Jere.Jones
Full Member
***
Offline Offline

Activity: 120


View Profile
May 23, 2011, 08:47:45 AM
 #4

What is your source for the expected output?

Jere
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 08:55:52 AM
 #5

What is your source for the expected output?

Jere

I've been using ruby's interface to openssl

If you've got ruby installed launch irb.

Code:
require 'openssl'
require 'base64'

privkey="privatekeytext"
data="my data string"
private_key = OpenSSL::PKey::RSA.new(privkey)
b64sig=Base64.encode64(private_key.sign( OpenSSL::Digest::SHA512.new, data))

I know it works as I've been using it on glbse since before we launched.

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 09:04:39 AM
 #6

You can actually just use the source from the page linked(it's a static page), if it works with that then it should just plain old work, so a link to a html page with all the js source in it would be fine(essentially a copy of the one I linked to).

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
weweritz
Newbie
*
Offline Offline

Activity: 2


View Profile
May 23, 2011, 09:23:52 AM
 #7

Funny thing, this.
The problem is in the rsa.signString function that returns the hex value of the signature. Javascript incorrectly discards the leading zero (1922282fceed0... instead of 01922282fceed0...) and when you convert this to binary and then base64encode it, naturally, everything goes wrong. With the extra zero, the output is as specified.

Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 09:28:42 AM
 #8

Funny thing, this.
The problem is in the rsa.signString function that returns the hex value of the signature. Javascript incorrectly discards the leading zero (1922282fceed0... instead of 01922282fceed0...) and when you convert this to binary and then base64encode it, naturally, everything goes wrong. With the extra zero, the output is as specified.



Nice, you found a bug(not my code). Do you know if a leading 0 needs to be added with all output or just this instance?

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
de4l
Member
**
Offline Offline

Activity: 83


View Profile
May 23, 2011, 09:38:17 AM
 #9

you could also just un-comment the base-64 output built into rsa.js and use that instead of the additional (lossy) conversion steps
weweritz
Newbie
*
Offline Offline

Activity: 2


View Profile
May 23, 2011, 09:39:52 AM
 #10

The hex that the stringSign function returns should be 512 characters long. If it is any shorter, prepend the number of zeroes that are missing;

something along the lines
Code:
while ( String( hSig ).length < 512 ) hSig = "0" + String( hSig );

should work.
mikegogulski
Sr. Member
****
Offline Offline

Activity: 360



View Profile WWW
May 23, 2011, 09:46:04 AM
 #11

I succeeded in getting "shitassfuck" as output from your test script! Cheesy

FREE ROSS ULBRICHT, allegedly one of the Dread Pirates Roberts of the Silk Road
Jere.Jones
Full Member
***
Offline Offline

Activity: 120


View Profile
May 23, 2011, 09:49:49 AM
 #12

The hex that the stringSign function returns should be 512 characters long. If it is any shorter, prepend the number of zeroes that are missing;

something along the lines
Code:
while ( String( hSig ).length < 512 ) hSig = "0" + String( hSig );

should work.
Unfortunately, that won't work because the bad version is 2 characters short.  A zero on the front and a 7 on the back.

There's a bug in bnToString(b) in jsbn.js that needs to be fixed.

Jere
arturh
Member
**
Offline Offline

Activity: 60



View Profile
May 23, 2011, 10:06:55 AM
 #13

Add needed zeroes to s:

s = new Array(257-s.length).join("0") + s;
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 10:18:01 AM
 #14

I succeeded in getting "shitassfuck" as output from your test script! Cheesy

Thats more than the stated requirements  Tongue

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
Jere.Jones
Full Member
***
Offline Offline

Activity: 120


View Profile
May 23, 2011, 10:20:35 AM
 #15

Here you go:  Replace your jsbn.js with the one located at http://jere.us/jsbn.zip and you'll be good to go.

It properly fixes the bnToString so that it outputs full hex bytes instead of skipping the first 4 bits when there isn't data for them.

Jere
Jere.Jones
Full Member
***
Offline Offline

Activity: 120


View Profile
May 23, 2011, 10:42:27 AM
 #16

I just put up an improved version.  You probably want to download it again.  The previous version had issues with IE.  This one works correctly in IE although doing RSA in IE is annoying. Smiley

http://jere.us/jsbn.zip

Jere
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 10:42:51 AM
 #17

I succeeded in getting "shitassfuck" as output from your test script! Cheesy

So to actually test if the whole thing properly works you would change

Code:
//sedData('test',{});

to

Code:
sendData("account/balance",{});

And the result from the server should be the balance(base64 encoded) if it works, otherwise it's a base64 encoded error(probably bad signature).

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
Nefario
Hero Member
*****
Offline Offline

Activity: 602


GLBSE Support support@glbse.com


View Profile WWW
May 23, 2011, 10:47:20 AM
 #18

Actually the update(above) can be seen in http://dev.glbse.com:4567/rsa.html

Jere.Jones, problem seems to persist. Am going to manually check now.

PGP key id at pgp.mit.edu 0xA68F4B7C

To get help and support for GLBSE please email support@glbse.com
mikegogulski
Sr. Member
****
Offline Offline

Activity: 360



View Profile WWW
May 23, 2011, 10:48:31 AM
 #19

I succeeded in getting "shitassfuck" as output from your test script! Cheesy

So to actually test if the whole thing properly works you would change

Code:
//sedData('test',{});

to

Code:
sendData("account/balance",{});

And the result from the server should be the balance(base64 encoded) if it works, otherwise it's a base64 encoded error(probably bad signature).


... and here I kinda liked the "shitassfuck" API Smiley

FREE ROSS ULBRICHT, allegedly one of the Dread Pirates Roberts of the Silk Road
graingert
Full Member
***
Offline Offline

Activity: 227


View Profile WWW
May 23, 2011, 10:52:28 AM
 #20

perhaps you should tell stanford to fix their library?

http://www-cs-students.stanford.edu/~tjw/jsbn/

 *Image Removed*
Pages: [1] 2 »  All
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!