x509big

package module
v0.0.0-...-e8ea46e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2021 License: BSD-3-Clause Imports: 5 Imported by: 1

README

x509big

This package provides a partial fork of x509 with support for large integers in the public exponent of RSA Public Keys. It also provides the necessary support to marshal a Private Key with a large public exponent into a PKCS1 Private Key for encoding as ASN1

This is useful for experimentation with unsual or known faulty RSA keys with public exponents larger than MaxUint64.

Features

  • func ParseBigPKCS1PublicKey(derBytes []byte) (*BigPublicKey, err error) returns a *BigPublicKey type which is defined below.

  • func ParseBigPKIXPublicKey(derBytes []byte) (*BigPublicKey, err error) returns a *BigPublicKey type which is defined below.

  • func ParseBigPKCS1PrivateKey(der []byte) (*BigPrivateKey, error) returns a *BigPrivateKey type which is defined below.

  • func MarshalPKCS1BigPrivateKey(key *BigPrivateKey) []byte returns a array of bytes suitable to encode with something like pem.EncodeToMemory()

  • func MarshalPKCS1BigPublicKey(key *BigPublicKey) []byte returns a array of bytes suitable to encode with something like pem.EncodeToMemory()

Types

type BigPublicKey struct {
  N *big.Int
  E *big.Int
}
type BigPrivateKey struct {
        PublicKey BigPublicKey  // public part.
        D         *big.Int      // private exponent
        Primes    []*big.Int    // prime factors of N, has >= 2 elements.

        // Precomputed contains precomputed values that speed up private
        // operations, if available.
        Precomputed PrecomputedValues
}

Examples

An example implementation of parsing a RSA Public Key with a large public exponent

func parsePublicRsaKey(keyBytes []byte) (*x509big.BigPublicKey, error) {
  key, err := x509big.ParseBigPKCS1PublicKey(keyBytes)
  if err != nil {
    return nil, fmt.Errorf("parsePublicRsaKey: failed to parse the DER key after decoding: %v", err)
  }

  return key, nil
}

An example of marshalling a BigPrivateKey type into an PEM private key.

func encodeDerToPem(der []byte, t string) string {
  p := pem.EncodeToMemory(
    &pem.Block{
      Type: t, 
      Bytes: der,
      },
    )
  return string(p)
}

func EncodeBigPrivateKey(priv *x509big.BigPrivateKey) string {
  privder := x509big.MarshalPKCS1BigPrivateKey(priv)
  fmt.Println(encodeDerToPem(privder, "RSA PRIVATE KEY"))
}

Install

Use go to install the library go get github.com/sourcekris/x509big

License

As this contains a great deal of code copied from the Go source it is licenced identically to the Go source itself - see the LICENSE file for details.

Authors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalPKCS1BigPrivateKey

func MarshalPKCS1BigPrivateKey(key *BigPrivateKey) []byte

MarshalPKCS1BigPrivateKey converts a big private key to ASN.1 DER encoded form. Taken from the standard library

func MarshalPKCS1BigPublicKey

func MarshalPKCS1BigPublicKey(key *BigPublicKey) []byte

MarshalPKCS1BigPublicKey converts a big public key to ASN.1 DER encoded form.

Types

type BigPrivateKey

type BigPrivateKey struct {
	PublicKey BigPublicKey // public part.
	D         *big.Int     // private exponent
	Primes    []*big.Int   // prime factors of N, has >= 2 elements.

	// Precomputed contains precomputed values that speed up private
	// operations, if available.
	Precomputed PrecomputedValues
}

BigPrivateKey is a big private key type

func ParseBigPKCS1PrivateKey

func ParseBigPKCS1PrivateKey(der []byte) (*BigPrivateKey, error)

ParseBigPKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.

type BigPublicKey

type BigPublicKey struct {
	N *big.Int
	E *big.Int
}

BigPublicKey is big.Int public key type

func ParseBigPKCS1PublicKey

func ParseBigPKCS1PublicKey(der []byte) (*BigPublicKey, error)

ParseBigPKCS1PublicKey parses an RSA public key in PKCS#1, ASN.1 DER form.

func ParseBigPKIXPublicKey

func ParseBigPKIXPublicKey(derBytes []byte) (*BigPublicKey, error)

ParseBigPKIXPublicKey parses a DER encoded public key. These values are typically found in PEM blocks with "BEGIN PUBLIC KEY".

type CRTValue

type CRTValue struct {
	Exp   *big.Int // D mod (prime-1).
	Coeff *big.Int // R·Coeff ≡ 1 mod Prime.
	R     *big.Int // product of primes prior to this (inc p and q).
}

CRTValue contains the precomputed Chinese remainder theorem values. directly taken from standard library rsa.CRTValues

type PrecomputedValues

type PrecomputedValues struct {
	Dp, Dq *big.Int // D mod (P-1) (or mod Q-1)
	Qinv   *big.Int // Q^-1 mod P

	// CRTValues is used for the 3rd and subsequent primes. Due to a
	// historical accident, the CRT for the first two primes is handled
	// differently in PKCS#1 and interoperability is sufficiently
	// important that we mirror this.
	CRTValues []CRTValue
}

PrecomputedValues is directly taken from standard library rsa.PrecomputedValues using big.Int types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL