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

Trying to replace openssl x509 -hash -noout with go #146

Open
andreas-kupries opened this issue Apr 28, 2021 · 1 comment
Open

Trying to replace openssl x509 -hash -noout with go #146

andreas-kupries opened this issue Apr 28, 2021 · 1 comment

Comments

@andreas-kupries
Copy link

I am looking to replace openssl x509 -hash -noout with go code.
Is that possible with this package ?
If yes, is there an example in the docs ? I failed to find anything.

I am aware of #112. It looks to be related, but not the same (It talks about the -subject_hash_old instead of -hash).

Further, do I understand correctly that this package requires a regular openssl.so in the environment it will then make uses of ?

@andreas-kupries
Copy link
Author

andreas-kupries commented Apr 28, 2021

Hm. Could adding

func (c *Certificate) GetSubjectNameHash() (uint32, error) {
	hash := C.X509_subject_name_hash(c.x)
	return uint32(hash), nil
}

to cert.go be enough ? (I am unsure about endianess)

Could then do

	pemBytes, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		return fmt.Errorf("unable to open certificate: %v", err)
	}

	block, _ := pem.Decode(pemBytes)
	if block == nil {
		return errors.New("failed to decode PEM")
	}

	cert, err := x509.ParseCertificate(block.Bytes)
	if err != nil {
		return fmt.Errorf("failed to parse certificate from PEM: %v", err)
	}

        hash, _ := cert.GetSubjectNameHash()
        asString := fmt.Sprintf("%08x\n", hash)
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant