-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSigningOps.kt
More file actions
202 lines (180 loc) · 6.01 KB
/
SigningOps.kt
File metadata and controls
202 lines (180 loc) · 6.01 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2023-2026 European Commission
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.europa.ec.eudi.openid4vci.internal
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.crypto.impl.ECDSA
import com.nimbusds.jose.jwk.Curve
import com.nimbusds.jose.jwk.ECKey
import eu.europa.ec.eudi.openid4vci.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.security.PrivateKey
import java.security.SecureRandom
import java.security.Signature
import java.security.interfaces.ECPrivateKey
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@OptIn(ExperimentalContracts::class)
internal suspend fun <PUB, R> Signer<PUB>.use(block: suspend (SignOperation<PUB>) -> R): R {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
val signOp = acquire()
try {
return block(signOp)
} finally {
release(signOp)
}
}
@OptIn(ExperimentalContracts::class)
internal suspend fun <PUB, R> BatchSigner<PUB>.use(block: suspend (BatchSignOperation<PUB>) -> R): R {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
val signOps = authenticate()
try {
return block(signOps)
} finally {
release(signOps)
}
}
internal fun ByteArray.transcodeSignatureToConcat(alg: JWSAlgorithm): ByteArray {
if (!JWSAlgorithm.Family.EC.contains(alg)) {
return this
}
val outputLen = when (alg) {
JWSAlgorithm.ES256 -> 64
JWSAlgorithm.ES384 -> 96
JWSAlgorithm.ES512 -> 132
else -> error("Unsupported algorithm for JWS signature transcoding: ${alg.name}")
}
return ECDSA.transcodeSignatureToConcat(this, outputLen)
}
internal fun String.toJoseAlg(): JWSAlgorithm =
this.toJoseECAlg()
?: error("Unsupported algorithm for JWS signature: $this")
internal fun String.toJoseECAlg(): JWSAlgorithm? = when (this) {
"SHA256withECDSA" -> JWSAlgorithm.ES256
"SHA384withECDSA" -> JWSAlgorithm.ES384
"SHA512withECDSA" -> JWSAlgorithm.ES512
else -> null
}
internal fun SignFunction.Companion.forJavaPrivateKey(
javaSigningAlgorithm: String,
privateKey: PrivateKey,
secureRandom: SecureRandom?,
provider: String?,
): SignFunction =
SignFunction { input ->
withContext(Dispatchers.IO) {
val signature =
provider
?.let { Signature.getInstance(javaSigningAlgorithm, it) }
?: Signature.getInstance(javaSigningAlgorithm)
signature.run {
secureRandom
?.let { initSign(privateKey, it) }
?: initSign(privateKey)
update(input)
sign()
}
}
}
internal fun <PUB> Signer.Companion.fromEcPrivateKey(
signingAlgorithm: String,
privateKey: ECPrivateKey,
publicMaterial: PUB,
secureRandom: SecureRandom?,
provider: String?,
): Signer<PUB> = object : Signer<PUB> {
override val javaAlgorithm: String
get() = signingAlgorithm
override suspend fun acquire(): SignOperation<PUB> {
val sign = SignFunction.forJavaPrivateKey(signingAlgorithm, privateKey, secureRandom, provider)
return SignOperation(sign, publicMaterial)
}
override suspend fun release(signOperation: SignOperation<PUB>?) {
// Nothing to do for releasing
}
}
internal fun <PUB> BatchSigner.Companion.fromECPrivateKeys(
signingAlgorithm: String,
ecKeyPairs: Map<ECPrivateKey, PUB>,
secureRandom: SecureRandom?,
provider: String?,
): BatchSigner<PUB> = object : BatchSigner<PUB> {
override val javaAlgorithm: String
get() = signingAlgorithm
override suspend fun authenticate(): BatchSignOperation<PUB> {
val signOperations = ecKeyPairs.map {
val sign = SignFunction.forJavaPrivateKey(
signingAlgorithm,
it.key,
secureRandom,
provider,
)
SignOperation(sign, it.value)
}
return BatchSignOperation(signOperations)
}
override suspend fun release(signOps: BatchSignOperation<PUB>?) {
// Nothing to do for releasing
}
}
internal fun <KI> Signer.Companion.fromNimbusEcKey(
ecPrivateKey: ECKey,
keyInfo: KI,
secureRandom: SecureRandom?,
provider: String?,
): Signer<KI> {
require(ecPrivateKey.isPrivate)
val signatureAlgorithm = ecPrivateKey.curve.toJavaSigningAlg()
return fromEcPrivateKey(
signatureAlgorithm,
ecPrivateKey.toECPrivateKey(),
keyInfo,
secureRandom,
provider,
)
}
internal fun <PUB> BatchSigner.Companion.fromNimbusEcKeys(
ecKeyPairs: Map<ECKey, PUB>,
secureRandom: SecureRandom?,
provider: String?,
): BatchSigner<PUB> {
require(ecKeyPairs.isNotEmpty()) { "At least one EC key pair must be provided" }
ecKeyPairs.forEach {
require(it.key.isPrivate) { "All EC keys must be private keys" }
}
val signatureAlgorithm = ecKeyPairs.entries.first().key.curve.toJavaSigningAlg()
return fromECPrivateKeys(
signatureAlgorithm,
ecKeyPairs.map {
it.key.toECPrivateKey() to it.value
}.toMap(),
secureRandom,
provider,
)
}
internal fun Curve.toJavaSigningAlg(): String {
return when (this) {
Curve.P_256 -> "SHA256withECDSA"
Curve.P_384 -> "SHA384withECDSA"
Curve.P_521 -> "SHA512withECDSA"
else -> error("Unsupported algorithm")
}
}