Unable to verify signature with certificate

664 views
Skip to first unread message

Gabriel Vincent Kho

unread,
Oct 9, 2015, 3:02:52 PM10/9/15
to golang-nuts
I'm creating a signature in javascript using a private key, and I want to verify it in my server using golang. However, I am unable to verify the signature.

Here is how I create the signature in golang using forge.js

     var md = forge.md.sha256.create();
     md.update(randomString, 'utf8');
     var signature = privateKey.sign(md);
     signature = forge.util.encode64(signature);
     formData.append("sig", signature);

I pass it over using multiform, and then on my golang server using the crypto library, I read it out and try to verify it this way

    h := sha256.New()
digest := h.Sum(nil)

signature, err := b64.StdEncoding.DecodeString(string(sig))
if err != nil {
fmt.Println("COULD NOT DECODE")
return
}
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return
}
pubkey := cert.PublicKey.(*rsa.PublicKey)
err = rsa.VerifyPKCS1v15(pubkey, crypto.SHA256, digest, signature)
if err != nil {
fmt.Println("COULD NOT VERIFY")
return
}
    
I keep failing to verify the signature. I'm kinda lost as to how to even debug this.

Joe Taber

unread,
Oct 9, 2015, 4:04:50 PM10/9/15
to golang-nuts
I would guess a good starting point for debugging would be to actually print the returned errors.

The 'error' type doesn't just indicate the presence of a problem, but it usually also contains details of the problem. E.g. change to `fmt.Println("cannot verify:", err)`.

I'm not familiar with the crypto library so maybe someone else could help you once you have these details.

Gabriel Vincent Kho

unread,
Oct 9, 2015, 5:14:24 PM10/9/15
to golang-nuts
Ya, I've printed those. The error is just "
   crypto/rsa: verification error". Not exactly the most helpful.

Charles Haynes

unread,
Oct 10, 2015, 2:17:35 AM10/10/15
to Gabriel Vincent Kho, golang-nuts
A couple of suggestions.

First, is it possible to walk through the code in the crypto library to see why it's failing to validate?

Second, is it possible to do an encode on the go side to see what the crypto library might be expecting? Do an a/b comparison of the same encode done in node and in Go and see how they are different. See if you can simplify the failing example until they both give the same result, then work forward to see where they differ.

Good luck.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Giulio Iotti

unread,
Oct 10, 2015, 10:14:30 AM10/10/15
to golang-nuts
On Saturday, October 10, 2015 at 12:14:24 AM UTC+3, Gabriel Vincent Kho wrote:
Ya, I've printed those. The error is just "
   crypto/rsa: verification error". Not exactly the most helpful.

Try to print the data before and after base64 encoding/decoding. Is it the same length? Same first and last bytes?

-- 
Giulio Iotti 

Dave Cheney

unread,
Oct 10, 2015, 10:31:08 AM10/10/15
to golang-nuts
Watch out for padding, some base64 impls generate it, others do not, some require it to decode the data, others do not. Some signing libraries consider the padding part of the signature, others do not.

Indrajeet Nandy

unread,
Dec 5, 2023, 1:38:13 AM12/5/23
to golang-nuts
Hi, how did you get about this? I am facing the same issue.
Reply all
Reply to author
Forward
0 new messages