Skip to content

Commit

Permalink
Merge pull request #21 from rarimo/feature/add_sha1_rsa
Browse files Browse the repository at this point in the history
Handle SHA1 with RSA, handle RSA as SHA256 with RSA
  • Loading branch information
artemskriabin authored Jun 25, 2024
2 parents 89c3d20 + 2b14e7c commit 0cdf321
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/service/api/handlers/create_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
SHA1 = "sha1"
SHA256 = "sha256"

SHA1withRSA = "SHA1withRSA"
SHA256withRSA = "SHA256withRSA"
SHA1withECDSA = "SHA1withECDSA"
SHA256withECDSA = "SHA256withECDSA"
Expand All @@ -49,6 +50,7 @@ const (
var algorithmsListMap = map[string]map[string]string{
"SHA1": {
"ECDSA": SHA1withECDSA,
"RSA": SHA1withRSA,
},
"SHA256": {
"RSA": SHA256withRSA,
Expand Down Expand Up @@ -120,7 +122,7 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) {
cfg := api.VerifierConfig(r)

switch algorithm {
case SHA1withECDSA:
case SHA1withRSA, SHA1withECDSA:
if err := verifier.VerifyGroth16(req.Data.ZKProof, cfg.VerificationKeys[SHA1]); err != nil {
log.WithError(err).Error("failed to verify Groth16")
ape.RenderErr(w, problems.BadRequest(err)...)
Expand Down Expand Up @@ -356,7 +358,7 @@ func validateSignedAttributes(signedAttributes, encapsulatedContent []byte, algo

d := make([]byte, 0)
switch algorithm {
case SHA1withECDSA:
case SHA1withRSA, SHA1withECDSA:
h := sha1.New()
h.Write(encapsulatedContent)
d = h.Sum(nil)
Expand Down Expand Up @@ -387,6 +389,10 @@ func signatureAlgorithm(passedAlgorithm string) string {
return SHA256withRSA
}

if passedAlgorithm == "RSA" {
return SHA256withRSA
}

if strings.Contains(strings.ToUpper(passedAlgorithm), "PSS") {
return "" // RSA-PSS is not currently supported
}
Expand Down Expand Up @@ -428,6 +434,16 @@ func verifySignature(req requests.CreateIdentityRequest, cert *x509.Certificate,
}

switch algo {
case SHA1withRSA:
pubKey := cert.PublicKey.(*rsa.PublicKey)

h := sha1.New()
h.Write(signedAttributes)
d := h.Sum(nil)

if err := rsa.VerifyPKCS1v15(pubKey, crypto.SHA1, d, signature); err != nil {
return errors.Wrap(err, "failed to verify SHA1 with RSA signature")
}
case SHA256withRSA:
pubKey := cert.PublicKey.(*rsa.PublicKey)

Expand Down

0 comments on commit 0cdf321

Please sign in to comment.