Skip to content

Instantly share code, notes, and snippets.

@antzucaro
Created December 7, 2019 16:51
Show Gist options
  • Save antzucaro/9344ee8492b8ef868b63d1991a61a9c7 to your computer and use it in GitHub Desktop.
Save antzucaro/9344ee8492b8ef868b63d1991a61a9c7 to your computer and use it in GitHub Desktop.
/*
This yeields the following:
12 bytes written
<nil>
could not decode data
*/
package d0
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
// D0BlindIDKeyGen is the default location standalone verification binary.
const D0BlindIDKeyGen = "/usr/local/bin/crypto-keygen-standalone"
// D0BlindIDPubKey is the default location of the d0 public key.
const D0BlindIDPubKey = "/home/ant/key_0.d0pk"
// VerifyResult is the result of a d0_blind_id verification
type VerifyResult struct {
IDFP string
CAStatus bool
}
// Verify checks if the given request data is verified via the d0_blind_id library
// via its command line executable.
func Verify(keygen, pubkey, signature, queryString, data string) (*VerifyResult, error) {
if signature == "" {
return nil, fmt.Errorf("missing signature")
}
var input string
if data == "" {
input = queryString
} else {
input = fmt.Sprintf("%s\x00%s", data, queryString)
}
// Create the data file for processing.
dr, dw, err := os.Pipe()
if err != nil {
return nil, err
}
drFd := fmt.Sprintf("/proc/self/fd/%d", dr.Fd())
defer dr.Close()
go func() {
defer dw.Close()
n, err := dw.WriteString(input)
fmt.Printf("%d bytes written\n", n)
fmt.Println(err)
}()
// Create the signature file for processing.
sFile, err := ioutil.TempFile("", "d0verify_s.*")
if err != nil {
return nil, fmt.Errorf("unable to create temporary signature file for d0 verification")
}
sData64, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
return nil, err
}
sFile.Write([]byte(sData64))
sFile.Close()
defer os.Remove(sFile.Name())
cmd := exec.Command(keygen, "-p", pubkey, "-d", drFd, "-s", sFile.Name())
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(output))
return nil, err
}
parts := strings.Split(string(output), "\n")
if len(parts) < 2 {
return nil, fmt.Errorf("unexpected output format from %s", keygen)
}
caStatus := parts[0] == "1"
result := VerifyResult{IDFP: parts[1], CAStatus: caStatus}
return &result, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment