decode b32 first

This commit is contained in:
Özgür Kesim 2021-10-17 18:34:55 +02:00
parent 329039efb3
commit 9ce706684c
Signed by: oec
GPG Key ID: F136A7F922D018D7

View File

@ -2,6 +2,7 @@ package main
import (
"crypto/rsa"
"encoding/base32"
"encoding/binary"
"fmt"
"math/big"
@ -48,20 +49,28 @@ type EdDSASignature struct {
S [256 / 8]byte `json:"s"`
}
// following gnunet/src/util/crypto_rsa.c
// following gnunet/src/json/json_helper.c and gnunet/src/util/crypto_rsa.c
func (ep *EncodedRSAPublicKey) Decode() (p rsa.PublicKey, e error) {
buf := []byte(*ep)
var buf []byte
// 1. decode base32
_, e = base32.StdEncoding.Decode(buf, *ep)
if e != nil {
return
}
// 2. parse header
if len(buf) < 4 {
e = fmt.Errorf("byte array too small for RSA public key header")
return
}
modulus_length, public_exponent_length := binary.BigEndian.Uint16(buf[0:]), binary.BigEndian.Uint16(buf[2:])
if len(buf[4:]) != int(modulus_length)+int(public_exponent_length) {
e = fmt.Errorf("byte array has wrong size according to encoded length's for modulus and public exponent")
return
}
// 3. parse RSA public key
// BUG! This is most likely wrong.
// Consult _gcry_mpi_set_buffer from libgcrypt-1.9.4/mpi/mpicoder.c
buf = buf[4:]