File: algorithms.go

package info (click to toggle)
golang-step-crypto 0.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,056 kB
  • sloc: sh: 53; makefile: 28
file content (85 lines) | stat: -rw-r--r-- 2,454 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package x509util

import (
	"crypto/x509"
	"strings"

	"github.com/pkg/errors"
)

// List of signature algorithms.
const (
	MD2WithRSA       = "MD2-RSA"
	MD5WithRSA       = "MD5-RSA"
	SHA1WithRSA      = "SHA1-RSA"
	SHA256WithRSA    = "SHA256-RSA"
	SHA384WithRSA    = "SHA384-RSA"
	SHA512WithRSA    = "SHA512-RSA"
	DSAWithSHA1      = "DSA-SHA1"
	DSAWithSHA256    = "DSA-SHA256"
	ECDSAWithSHA1    = "ECDSA-SHA1"
	ECDSAWithSHA256  = "ECDSA-SHA256"
	ECDSAWithSHA384  = "ECDSA-SHA384"
	ECDSAWithSHA512  = "ECDSA-SHA512"
	SHA256WithRSAPSS = "SHA256-RSAPSS"
	SHA384WithRSAPSS = "SHA384-RSAPSS"
	SHA512WithRSAPSS = "SHA512-RSAPSS"
	PureEd25519      = "Ed25519"
)

var signatureAlgorithmMapping = []struct {
	name  string
	value x509.SignatureAlgorithm
}{
	{"", x509.UnknownSignatureAlgorithm},
	{MD2WithRSA, x509.MD2WithRSA},
	{MD5WithRSA, x509.MD5WithRSA},
	{SHA1WithRSA, x509.SHA1WithRSA},
	{SHA256WithRSA, x509.SHA256WithRSA},
	{SHA384WithRSA, x509.SHA384WithRSA},
	{SHA512WithRSA, x509.SHA512WithRSA},
	{DSAWithSHA1, x509.DSAWithSHA1},
	{DSAWithSHA256, x509.DSAWithSHA256},
	{ECDSAWithSHA1, x509.ECDSAWithSHA1},
	{ECDSAWithSHA256, x509.ECDSAWithSHA256},
	{ECDSAWithSHA384, x509.ECDSAWithSHA384},
	{ECDSAWithSHA512, x509.ECDSAWithSHA512},
	{SHA256WithRSAPSS, x509.SHA256WithRSAPSS},
	{SHA384WithRSAPSS, x509.SHA384WithRSAPSS},
	{SHA512WithRSAPSS, x509.SHA512WithRSAPSS},
	{PureEd25519, x509.PureEd25519},
}

// SignatureAlgorithm is the JSON representation of the X509 signature algorithms
type SignatureAlgorithm x509.SignatureAlgorithm

// Set sets the signature algorithm in the given certificate.
func (s SignatureAlgorithm) Set(c *x509.Certificate) {
	c.SignatureAlgorithm = x509.SignatureAlgorithm(s)
}

// MarshalJSON implements the json.Marshaller interface.
func (s SignatureAlgorithm) MarshalJSON() ([]byte, error) {
	if s == SignatureAlgorithm(x509.UnknownSignatureAlgorithm) {
		return []byte(`""`), nil
	}
	return []byte(`"` + x509.SignatureAlgorithm(s).String() + `"`), nil
}

// UnmarshalJSON implements the json.Unmarshal interface and unmarshals and
// validates a string as a SignatureAlgorithm.
func (s *SignatureAlgorithm) UnmarshalJSON(data []byte) error {
	name, err := unmarshalString(data)
	if err != nil {
		return err
	}

	for _, m := range signatureAlgorithmMapping {
		if strings.EqualFold(name, m.name) {
			*s = SignatureAlgorithm(m.value)
			return nil
		}
	}

	return errors.Errorf("unsupported signatureAlgorithm %s", name)
}