github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/smime/signaturealgorithm.go (about)

     1  package smime
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/x509"
     6  	"encoding/asn1"
     7  )
     8  
     9  var (
    10  	oidSignatureRSA             = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
    11  	oidSignatureMD2WithRSA      = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 2}
    12  	oidSignatureMD5WithRSA      = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4}
    13  	oidSignatureSHA1WithRSA     = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
    14  	oidSignatureSHA256WithRSA   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
    15  	oidSignatureSHA384WithRSA   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
    16  	oidSignatureSHA512WithRSA   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}
    17  	oidSignatureDSAWithSHA1     = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3}
    18  	oidSignatureDSAWithSHA256   = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 4, 3, 2}
    19  	oidSignatureECDSAWithSHA1   = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1}
    20  	oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
    21  	oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
    22  	oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
    23  
    24  	//TODO: add the algorithms in RFC 3370 & RFC 5754
    25  	oidDigestSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
    26  )
    27  
    28  /*
    29  type SignatureAlgorithm int
    30  
    31  func (s SignatureAlgorithm) hashType() (crypto.Hash, error) {
    32  	for _, details := range signatureAlgorithmDetails {
    33  		if s == details.algo {
    34  			return details.hash
    35  		}
    36  	}
    37  	return crypto.Hash(0), errors.New("unknown signature algorithm")
    38  }
    39  */
    40  
    41  var signatureAlgorithmDetails = []struct {
    42  	algo x509.SignatureAlgorithm
    43  	oid  asn1.ObjectIdentifier
    44  	hash crypto.Hash
    45  }{
    46  	// {x509.PlainRSA, oidSignatureRSA, x509.RSA, crypto.Hash(0)},
    47  	{x509.MD2WithRSA, oidSignatureMD2WithRSA, crypto.Hash(0)},
    48  	{x509.MD5WithRSA, oidSignatureMD5WithRSA, crypto.MD5},
    49  	{x509.SHA1WithRSA, oidSignatureSHA1WithRSA, crypto.SHA1},
    50  	{x509.SHA256WithRSA, oidSignatureSHA256WithRSA, crypto.SHA256},
    51  	{x509.SHA384WithRSA, oidSignatureSHA384WithRSA, crypto.SHA384},
    52  	{x509.SHA512WithRSA, oidSignatureSHA512WithRSA, crypto.SHA512},
    53  	{x509.DSAWithSHA1, oidSignatureDSAWithSHA1, crypto.SHA1},
    54  	{x509.DSAWithSHA256, oidSignatureDSAWithSHA256, crypto.SHA256},
    55  	{x509.ECDSAWithSHA1, oidSignatureECDSAWithSHA1, crypto.SHA1},
    56  	{x509.ECDSAWithSHA256, oidSignatureECDSAWithSHA256, crypto.SHA256},
    57  	{x509.ECDSAWithSHA384, oidSignatureECDSAWithSHA384, crypto.SHA384},
    58  	{x509.ECDSAWithSHA512, oidSignatureECDSAWithSHA512, crypto.SHA512},
    59  }
    60  
    61  func getSignatureAlgorithmFromOID(oid asn1.ObjectIdentifier) x509.SignatureAlgorithm {
    62  	for _, details := range signatureAlgorithmDetails {
    63  		if oid.Equal(details.oid) {
    64  			return details.algo
    65  		}
    66  	}
    67  	return x509.UnknownSignatureAlgorithm
    68  }
    69  
    70  func getDigestHashType(oid asn1.ObjectIdentifier) crypto.Hash {
    71  	//TODO: properly select digest
    72  
    73  	for _, details := range signatureAlgorithmDetails {
    74  		if oid.Equal(details.oid) {
    75  			return details.hash
    76  		}
    77  	}
    78  
    79  	if oid.Equal(oidDigestSHA1) {
    80  		return crypto.SHA1
    81  	}
    82  
    83  	return crypto.Hash(0)
    84  }