Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove usage of secp256r1 and demos #1043

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ lazy val V = new {
val zioPreludeVersion = "1.0.0-RC24"

val apollo = "1.2.14"
val bouncyCastle = "1.78.1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidentally noticed it was not used by anything


val jsonSchemaValidator = "1.3.2" // scala-steward:off //TODO 1.3.2 need to fix:
// [error] org.hyperledger.identus.pollux.core.model.schema.AnoncredSchemaTypeSpec
// [error] org.hyperledger.identus.pollux.core.model.schema.CredentialSchemaSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import org.hyperledger.identus.pollux.core.repository.*
import org.hyperledger.identus.pollux.core.service.serdes.*
import org.hyperledger.identus.pollux.vc.jwt.*
import org.hyperledger.identus.shared.models.{WalletAccessContext, WalletId}
import org.hyperledger.identus.shared.crypto.KmpSecp256k1KeyOps
import zio.*

import java.security.*
import java.time.Instant
import java.util.UUID

Expand Down Expand Up @@ -42,16 +41,16 @@ trait PresentationServiceSpecHelper {
CredentialRepositoryInMemory.layer
) ++ defaultWalletLayer

def createIssuer(did: DID) = {
val keyGen = KeyPairGenerator.getInstance("EC")
keyGen.initialize(Curve.P_256.toECParameterSpec)
val keyPair = keyGen.generateKeyPair()
val privateKey = keyPair.getPrivate
val publicKey = keyPair.getPublic
def createIssuer(did: DID): Issuer = {

val keyPair = KmpSecp256k1KeyOps.generateKeyPair
val javaSKey = keyPair.privateKey.toJavaPrivateKey
val javaPKey = keyPair.publicKey.toJavaPublicKey

Issuer(
did = did,
signer = ES256Signer(privateKey),
publicKey = publicKey
signer = ES256KSigner(javaSKey),
publicKey = javaPKey
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ trait Signer {

}

class ES256Signer(privateKey: PrivateKey) extends Signer {
val algorithm: JwtECDSAAlgorithm = JwtAlgorithm.ES256
private val provider = BouncyCastleProviderSingleton.getInstance
Security.addProvider(provider)

override def encode(claim: Json): JWT = JWT(JwtCirce.encode(claim, privateKey, algorithm))

override def generateProofForJson(payload: Json, pk: PublicKey): Task[Proof] = {
EddsaJcs2022ProofGenerator.generateProof(payload, privateKey, pk)
}

}

// works with java 7, 8, 11 & bouncycastle provider
// https://connect2id.com/products/nimbus-jose-jwt/jca-algorithm-support#alg-support-table
class ES256KSigner(privateKey: PrivateKey) extends Signer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object JWTVerification {
// https://github.com/decentralized-identity/did-jwt/blob/8b3655097a1382934cabdf774d580e6731a636b1/src/JWT.ts#L146
val SUPPORT_PUBLIC_KEY_TYPES: Map[String, Set[String]] = Map(
"ES256K" -> Set("EcdsaSecp256k1VerificationKey2019", "JsonWebKey2020"),
"ES256" -> Set("ES256") // TODO: Only use valid type (added just for compatibility in the Demo code)
// Add support for other key types here
)

def validateAlgorithm(jwt: JWT): Validation[String, Unit] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import java.security.PublicKey
import java.time.temporal.TemporalAmount
import java.time.{Clock, Instant, OffsetDateTime, ZoneId}
import scala.util.Try
import com.nimbusds.jwt.SignedJWT
import scala.util.Failure

opaque type DID = String
object DID {
Expand Down Expand Up @@ -711,9 +713,15 @@ object JwtCredential {
def encodeJwt(payload: JwtCredentialPayload, issuer: Issuer): JWT = issuer.signer.encode(payload.asJson)

def decodeJwt(jwt: JWT, publicKey: PublicKey): Try[JwtCredentialPayload] = {
JwtCirce
.decodeRaw(jwt.value, publicKey, options = JwtOptions(expiration = false, notBefore = false))
.flatMap(decode[JwtCredentialPayload](_).toTry)
val signedJWT = SignedJWT.parse(jwt.value)
val verifier = JWTVerification.toECDSAVerifier(publicKey)

val isSignatureValid = signedJWT.verify(verifier)

if isSignatureValid then
val claimsSet = signedJWT.getJWTClaimsSet.toString
decode[JwtCredentialPayload](claimsSet).toTry
else Failure(Exception(s"Invalid JWT signature for: ${JWT.value}"))
}

def decodeJwt(jwt: JWT): IO[String, JwtCredentialPayload] = {
Expand All @@ -731,7 +739,9 @@ object JwtCredential {
}

def validateEncodedJwt(jwt: JWT, publicKey: PublicKey): Boolean =
JwtCirce.isValid(jwt.value, publicKey, JwtOptions(expiration = false, notBefore = false))
val signedJWT = SignedJWT.parse(jwt.value)
val verifier = JWTVerification.toECDSAVerifier(publicKey)
signedJWT.verify(verifier)

def validateEncodedJWT(
jwt: JWT,
Expand Down

This file was deleted.

Loading
Loading