...

Source file src/github.com/MicahParks/keyfunc/rsa.go

Documentation: github.com/MicahParks/keyfunc

     1  package keyfunc
     2  
     3  import (
     4  	"crypto/rsa"
     5  	"fmt"
     6  	"math/big"
     7  )
     8  
     9  const (
    10  	// ktyRSA is the key type (kty) in the JWT header for RSA.
    11  	ktyRSA = "RSA"
    12  )
    13  
    14  // RSA parses a jsonWebKey and turns it into an RSA public key.
    15  func (j *jsonWebKey) RSA() (publicKey *rsa.PublicKey, err error) {
    16  	if j.Exponent == "" || j.Modulus == "" {
    17  		return nil, fmt.Errorf("%w: %s", ErrMissingAssets, ktyRSA)
    18  	}
    19  
    20  	// Decode the exponent from Base64.
    21  	//
    22  	// According to RFC 7518, this is a Base64 URL unsigned integer.
    23  	// https://tools.ietf.org/html/rfc7518#section-6.3
    24  	exponent, err := base64urlTrailingPadding(j.Exponent)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	modulus, err := base64urlTrailingPadding(j.Modulus)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	publicKey = &rsa.PublicKey{}
    34  
    35  	// Turn the exponent into an integer.
    36  	//
    37  	// According to RFC 7517, these numbers are in big-endian format.
    38  	// https://tools.ietf.org/html/rfc7517#appendix-A.1
    39  	publicKey.E = int(big.NewInt(0).SetBytes(exponent).Uint64())
    40  	publicKey.N = big.NewInt(0).SetBytes(modulus)
    41  
    42  	return publicKey, nil
    43  }
    44  

View as plain text