cleaned up RS256 implementation. no functional changes

This commit is contained in:
Dave Grijalva 2014-07-05 15:50:46 -07:00
parent fbcb3e4b63
commit 37329b525d
2 changed files with 89 additions and 52 deletions

139
rs256.go
View File

@ -22,67 +22,104 @@ func (m *SigningMethodRS256) Alg() string {
return "RS256" return "RS256"
} }
func (m *SigningMethodRS256) Verify(signingString, signature string, key []byte) (err error) { func (m *SigningMethodRS256) Verify(signingString, signature string, key []byte) error {
// Key var err error
var sig []byte
if sig, err = DecodeSegment(signature); err == nil {
var block *pem.Block
if block, _ = pem.Decode(key); block != nil {
var parsedKey interface{}
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
parsedKey, err = x509.ParseCertificate(block.Bytes)
}
if err == nil {
if rsaKey, ok := parsedKey.(*rsa.PublicKey); ok {
hasher := sha256.New()
hasher.Write([]byte(signingString))
err = rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, hasher.Sum(nil), sig) // Decode the signature
} else if cert, ok := parsedKey.(*x509.Certificate); ok { var sig []byte
err = cert.CheckSignature(x509.SHA256WithRSA, []byte(signingString), sig) if sig, err = DecodeSegment(signature); err != nil {
} else { return err
err = errors.New("Key is not a valid RSA public key")
}
}
} else {
err = errors.New("Could not parse key data")
}
} }
return
// Parse public key
var rsaKey *rsa.PublicKey
if rsaKey, err = m.parsePublicKey(key); err != nil {
return err
}
// Create hasher
hasher := sha256.New()
hasher.Write([]byte(signingString))
// Verify the signature
return rsa.VerifyPKCS1v15(rsaKey, crypto.SHA256, hasher.Sum(nil), sig)
} }
// Implements the Sign method from SigningMethod // Implements the Sign method from SigningMethod
// For this signing method, must be PEM encoded PKCS1 or PKCS8 RSA private key // For this signing method, must be PEM encoded PKCS1 or PKCS8 RSA private key
func (m *SigningMethodRS256) Sign(signingString string, key []byte) (sig string, err error) { func (m *SigningMethodRS256) Sign(signingString string, key []byte) (string, error) {
var err error
// Key // Key
var rsaKey *rsa.PrivateKey var rsaKey *rsa.PrivateKey
if rsaKey, err = m.parsePrivateKey(key); err == nil { if rsaKey, err = m.parsePrivateKey(key); err != nil {
hasher := sha256.New() return "", err
hasher.Write([]byte(signingString))
var sigBytes []byte
if sigBytes, err = rsa.SignPKCS1v15(rand.Reader, rsaKey, crypto.SHA256, hasher.Sum(nil)); err == nil {
sig = EncodeSegment(sigBytes)
}
} }
return
}
func (m *SigningMethodRS256) parsePrivateKey(key []byte) (pkey *rsa.PrivateKey, err error) { // Create the hasher
var block *pem.Block hasher := sha256.New()
if block, _ = pem.Decode(key); block != nil { hasher.Write([]byte(signingString))
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil { // Sign the string and return the encoded bytes
if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil { if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, crypto.SHA256, hasher.Sum(nil)); err == nil {
return nil, err return EncodeSegment(sigBytes), nil
}
}
var ok bool
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
err = errors.New("Key is not a valid RSA private key")
}
} else { } else {
err = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key") return "", err
} }
return
}
// Parse PEM encoded PKCS1 or PKCS8 public key
func (m *SigningMethodRS256) parsePublicKey(key []byte) (*rsa.PublicKey, error) {
var err error
// Parse PEM block
var block *pem.Block
if block, _ = pem.Decode(key); block == nil {
return nil, errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
}
// Parse the key
var parsedKey interface{}
if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil {
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
return nil, err
} else {
parsedKey = cert.PublicKey
}
}
var pkey *rsa.PublicKey
var ok bool
if pkey, ok = parsedKey.(*rsa.PublicKey); !ok {
return nil, errors.New("Key is not a valid RSA public key")
}
return pkey, nil
}
// Parse PEM encoded PKCS1 or PKCS8 private key
func (m *SigningMethodRS256) parsePrivateKey(key []byte) (*rsa.PrivateKey, error) {
var err error
// Parse PEM block
var block *pem.Block
if block, _ = pem.Decode(key); block == nil {
return nil, errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key")
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
return nil, err
}
}
var pkey *rsa.PrivateKey
var ok bool
if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok {
return nil, errors.New("Key is not a valid RSA private key")
}
return pkey, nil
} }

View File

@ -61,7 +61,7 @@ func TestRS256Sign(t *testing.T) {
} }
} }
func TestKeyParsing(t *testing.T) { func TestRSAKeyParsing(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key") key, _ := ioutil.ReadFile("test/sample_key")
pubKey, _ := ioutil.ReadFile("test/sample_key.pub") pubKey, _ := ioutil.ReadFile("test/sample_key.pub")
badKey := []byte("All your base are belong to key") badKey := []byte("All your base are belong to key")