-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmechanism.go
More file actions
142 lines (128 loc) · 4.11 KB
/
Copy pathmechanism.go
File metadata and controls
142 lines (128 loc) · 4.11 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"encoding/asn1"
"fmt"
"github.com/miekg/pkcs11"
)
const (
AlgRSAPKCS1SHA256 = "RSASSA_PKCS1_V1_5_SHA_256"
AlgRSAPKCS1SHA384 = "RSASSA_PKCS1_V1_5_SHA_384"
AlgRSAPKCS1SHA512 = "RSASSA_PKCS1_V1_5_SHA_512"
AlgRSAPSSSHA256 = "RSASSA_PSS_SHA_256"
AlgRSAPSSSHA384 = "RSASSA_PSS_SHA_384"
AlgRSAPSSSHA512 = "RSASSA_PSS_SHA_512"
AlgECDSASHA256 = "ECDSA_SHA_256"
AlgECDSASHA384 = "ECDSA_SHA_384"
AlgECDSASHA512 = "ECDSA_SHA_512"
)
const (
sha256DigestLen = 32
sha384DigestLen = 48
sha512DigestLen = 64
)
// mechanismToAlgorithm maps a PKCS#11 mechanism to an Infisical signing algorithm.
func mechanismToAlgorithm(mechanism uint) (string, error) {
switch mechanism {
case pkcs11.CKM_SHA256_RSA_PKCS:
return AlgRSAPKCS1SHA256, nil
case pkcs11.CKM_SHA384_RSA_PKCS:
return AlgRSAPKCS1SHA384, nil
case pkcs11.CKM_SHA512_RSA_PKCS:
return AlgRSAPKCS1SHA512, nil
case pkcs11.CKM_SHA256_RSA_PKCS_PSS:
return AlgRSAPSSSHA256, nil
case pkcs11.CKM_SHA384_RSA_PKCS_PSS:
return AlgRSAPSSSHA384, nil
case pkcs11.CKM_SHA512_RSA_PKCS_PSS:
return AlgRSAPSSSHA512, nil
case pkcs11.CKM_ECDSA_SHA256:
return AlgECDSASHA256, nil
case pkcs11.CKM_ECDSA_SHA384:
return AlgECDSASHA384, nil
case pkcs11.CKM_ECDSA_SHA512:
return AlgECDSASHA512, nil
case pkcs11.CKM_ECDSA:
return AlgECDSASHA256, nil
case pkcs11.CKM_RSA_PKCS:
return "", fmt.Errorf("CKM_RSA_PKCS requires DigestInfo parsing")
default:
return "", fmt.Errorf("unsupported mechanism: 0x%08x", mechanism)
}
}
// DigestInfo is the DER-encoded structure per PKCS#1 v1.5:
//
// DigestInfo ::= SEQUENCE {
// digestAlgorithm AlgorithmIdentifier,
// digest OCTET STRING
// }
type digestInfo struct {
DigestAlgorithm asn1.RawValue
Digest []byte
}
var (
oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
oidSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
oidSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
)
type algorithmIdentifier struct {
Algorithm asn1.ObjectIdentifier
Parameters asn1.RawValue `asn1:"optional"`
}
// parseDigestInfo extracts the signing algorithm from a DER-encoded DigestInfo
// structure (used with CKM_RSA_PKCS raw signing).
func parseDigestInfo(data []byte) (algorithm string, digest []byte, err error) {
var di digestInfo
rest, err := asn1.Unmarshal(data, &di)
if err != nil {
return "", nil, fmt.Errorf("failed to parse DigestInfo: %w", err)
}
if len(rest) > 0 {
return "", nil, fmt.Errorf("trailing data after DigestInfo (%d bytes)", len(rest))
}
var algID algorithmIdentifier
if _, err := asn1.Unmarshal(di.DigestAlgorithm.FullBytes, &algID); err != nil {
return "", nil, fmt.Errorf("failed to parse AlgorithmIdentifier: %w", err)
}
switch {
case algID.Algorithm.Equal(oidSHA256):
if len(di.Digest) != sha256DigestLen {
return "", nil, fmt.Errorf("SHA-256 DigestInfo has wrong digest length: got %d, want %d", len(di.Digest), sha256DigestLen)
}
return AlgRSAPKCS1SHA256, di.Digest, nil
case algID.Algorithm.Equal(oidSHA384):
if len(di.Digest) != sha384DigestLen {
return "", nil, fmt.Errorf("SHA-384 DigestInfo has wrong digest length: got %d, want %d", len(di.Digest), sha384DigestLen)
}
return AlgRSAPKCS1SHA384, di.Digest, nil
case algID.Algorithm.Equal(oidSHA512):
if len(di.Digest) != sha512DigestLen {
return "", nil, fmt.Errorf("SHA-512 DigestInfo has wrong digest length: got %d, want %d", len(di.Digest), sha512DigestLen)
}
return AlgRSAPKCS1SHA512, di.Digest, nil
default:
return "", nil, fmt.Errorf("unsupported hash OID in DigestInfo: %v", algID.Algorithm)
}
}
func isPSSMechanism(mech uint) bool {
switch mech {
case pkcs11.CKM_SHA256_RSA_PKCS_PSS, pkcs11.CKM_SHA384_RSA_PKCS_PSS, pkcs11.CKM_SHA512_RSA_PKCS_PSS:
return true
default:
return false
}
}
var rsaMechanisms = []uint{
pkcs11.CKM_RSA_PKCS,
pkcs11.CKM_SHA256_RSA_PKCS,
pkcs11.CKM_SHA384_RSA_PKCS,
pkcs11.CKM_SHA512_RSA_PKCS,
pkcs11.CKM_SHA256_RSA_PKCS_PSS,
pkcs11.CKM_SHA384_RSA_PKCS_PSS,
pkcs11.CKM_SHA512_RSA_PKCS_PSS,
}
var ecMechanisms = []uint{
pkcs11.CKM_ECDSA,
pkcs11.CKM_ECDSA_SHA256,
pkcs11.CKM_ECDSA_SHA384,
pkcs11.CKM_ECDSA_SHA512,
}