...

Source file src/github.com/letsencrypt/boulder/goodkey/weak.go

Documentation: github.com/letsencrypt/boulder/goodkey

     1  package goodkey
     2  
     3  // This file defines a basic method for testing if a given RSA public key is on one of
     4  // the Debian weak key lists and is therefore considered compromised. Instead of
     5  // directly loading the hash suffixes from the individual lists we flatten them all
     6  // into a single JSON list using cmd/weak-key-flatten for ease of use.
     7  
     8  import (
     9  	"crypto/rsa"
    10  	"crypto/sha1"
    11  	"encoding/hex"
    12  	"encoding/json"
    13  	"fmt"
    14  	"os"
    15  )
    16  
    17  type truncatedHash [10]byte
    18  
    19  type WeakRSAKeys struct {
    20  	suffixes map[truncatedHash]struct{}
    21  }
    22  
    23  func LoadWeakRSASuffixes(path string) (*WeakRSAKeys, error) {
    24  	f, err := os.ReadFile(path)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	var suffixList []string
    30  	err = json.Unmarshal(f, &suffixList)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	wk := &WeakRSAKeys{suffixes: make(map[truncatedHash]struct{})}
    36  	for _, suffix := range suffixList {
    37  		err := wk.addSuffix(suffix)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  	}
    42  	return wk, nil
    43  }
    44  
    45  func (wk *WeakRSAKeys) addSuffix(str string) error {
    46  	var suffix truncatedHash
    47  	decoded, err := hex.DecodeString(str)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	if len(decoded) != 10 {
    52  		return fmt.Errorf("unexpected suffix length of %d", len(decoded))
    53  	}
    54  	copy(suffix[:], decoded)
    55  	wk.suffixes[suffix] = struct{}{}
    56  	return nil
    57  }
    58  
    59  func (wk *WeakRSAKeys) Known(key *rsa.PublicKey) bool {
    60  	// Hash input is in the format "Modulus={upper-case hex of modulus}\n"
    61  	hash := sha1.Sum([]byte(fmt.Sprintf("Modulus=%X\n", key.N.Bytes())))
    62  	var suffix truncatedHash
    63  	copy(suffix[:], hash[10:])
    64  	_, present := wk.suffixes[suffix]
    65  	return present
    66  }
    67  

View as plain text