forked from russellhaering/goxmldsig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from KshitizFinbox/feature/uidai-okyc-validation
feat: validations for UIDAI OKYC
- Loading branch information
Showing
7 changed files
with
46 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,38 @@ | ||
# goxmldsig | ||
|
||
![Build Status](https://github.com/russellhaering/goxmldsig/actions/workflows/test.yml/badge.svg?branch=main) | ||
[![GoDoc](https://godoc.org/github.com/russellhaering/goxmldsig?status.svg)](https://godoc.org/github.com/russellhaering/goxmldsig) | ||
|
||
XML Digital Signatures implemented in pure Go. | ||
|
||
## Installation | ||
|
||
Install `goxmldsig` using `go get`: | ||
|
||
``` | ||
$ go get github.com/russellhaering/goxmldsig | ||
``` | ||
|
||
## Usage | ||
|
||
Include the [`types.Signature`](https://pkg.go.dev/github.com/russellhaering/goxmldsig/types#Signature) struct from this package in your application messages. | ||
|
||
```go | ||
import ( | ||
sigtypes "github.com/russellhaering/goxmldsig/types" | ||
) | ||
|
||
type AppHdr struct { | ||
... | ||
Signature *sigtypes.Signature | ||
} | ||
``` | ||
|
||
### Signing | ||
# I've spent days trying to wrap my mind over UIDAI's broken documentation over 3 languages (.net, Java, Python and now GoLang) to no good. Please don't waste yours. Use this snippet to validate Aadhaar Mobile and Email. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/beevik/etree" | ||
"github.com/russellhaering/goxmldsig" | ||
) | ||
|
||
func main() { | ||
// Generate a key and self-signed certificate for signing | ||
randomKeyStore := dsig.RandomKeyStoreForTest() | ||
ctx := dsig.NewDefaultSigningContext(randomKeyStore) | ||
elementToSign := &etree.Element{ | ||
Tag: "ExampleElement", | ||
} | ||
elementToSign.CreateAttr("ID", "id1234") | ||
|
||
// Sign the element | ||
signedElement, err := ctx.SignEnveloped(elementToSign) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Serialize the signed element. It is important not to modify the element | ||
// after it has been signed - even pretty-printing the XML will invalidate | ||
// the signature. | ||
doc := etree.NewDocument() | ||
doc.SetRoot(signedElement) | ||
str, err := doc.WriteToString() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
println(str) | ||
type PersonalInfoValidation struct { | ||
MobileMatch bool `json:"mobile_match"` | ||
EmailMatch bool `json:"email_match"` | ||
} | ||
``` | ||
|
||
### Signature Validation | ||
|
||
```go | ||
// Validate an element against a root certificate | ||
func validate(root *x509.Certificate, el *etree.Element) { | ||
// Construct a signing context with one or more roots of trust. | ||
ctx := dsig.NewDefaultValidationContext(&dsig.MemoryX509CertificateStore{ | ||
Roots: []*x509.Certificate{root}, | ||
}) | ||
|
||
// It is important to only use the returned validated element. | ||
// See: https://www.w3.org/TR/xmldsig-bestpractices/#check-what-is-signed | ||
validated, err := ctx.Validate(el) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
doc := etree.NewDocument() | ||
doc.SetRoot(validated) | ||
str, err := doc.WriteToString() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
println(str) | ||
func validatePersonalInfo(el *etree.Element, actualMobile, actualEmail, shareCode string, aadhaarLastDigit int) PersonalInfoValidation { | ||
mobile := el.FindElement("//Poi").SelectAttrValue("m", "default-mobile") | ||
email := el.FindElement("//Poi").SelectAttrValue("e", "default-email") | ||
|
||
if aadhaarLastDigit == 0 { | ||
aadhaarLastDigit = 1 | ||
} | ||
var response PersonalInfoValidation | ||
|
||
if mobile != "default-mobile" { | ||
lastInput := actualMobile + shareCode | ||
for i := 0; i < aadhaarLastDigit; i++ { | ||
h := sha256.Sum256([]byte(lastInput)) | ||
lastInput = hex.EncodeToString(h[:]) | ||
} | ||
response.MobileMatch = lastInput == mobile | ||
} | ||
|
||
if email != "default-email" { | ||
lastInput := actualEmail + shareCode | ||
for i := 0; i < aadhaarLastDigit; i++ { | ||
h := sha256.Sum256([]byte(lastInput)) | ||
lastInput = hex.EncodeToString(h[:]) | ||
} | ||
response.EmailMatch = lastInput == email | ||
} | ||
|
||
return response | ||
} | ||
``` | ||
|
||
## Limitations | ||
|
||
This library was created in order to [implement SAML 2.0](https://github.com/russellhaering/gosaml2) | ||
without needing to execute a command line tool to create and validate signatures. It currently | ||
only implements the subset of relevant standards needed to support that implementation, but | ||
I hope to make it more complete over time. Contributions are welcome. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/russellhaering/goxmldsig | ||
module github.com/finbox-in/goxmldsig | ||
|
||
go 1.15 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters