Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Certificate signing request generator modules in rust and golang #10

Merged
merged 12 commits into from
Jul 26, 2023
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
710 changes: 710 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[workspace]

members = [
"samples/csr-rust",
"samples/dns-rust",
"samples/hello-world-rust"
]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ build-opa-policy-wasm:
build-ebpf-xdp-prog:
cd samples/ebpf; make

build-csr-gen-rust-wasm:
cd samples/csr-rust; make

load-hello-world-rust-wasm:
sudo ./w3k load -file samples/hello-world-rust/target/wasm32-unknown-unknown/release/hello-world.wasm

Expand Down
5 changes: 5 additions & 0 deletions samples/csr-go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go-csr:
tinygo build -o csr-go.wasm \
-target=wasm \
-gc=leaking -panic=trap -scheduler=none \
-opt=z main.go
57 changes: 57 additions & 0 deletions samples/csr-go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"unsafe"
)

//export _debug
func _debug(m string) int32

//export gen_csr
func gen_csr(privPtr *uint32, privLen int) uint32 {

unsafePtr := unsafe.Pointer(privPtr)

priv := *(*[]byte)(unsafe.Pointer(&struct {
addr uintptr
len int
cap int
}{uintptr(unsafePtr), privLen, privLen}))

block, _ := pem.Decode(priv)

rsaPriv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
_debug("failed to parse priv key" + err.Error())
}

var csrTemplate = x509.CertificateRequest{
Subject: pkix.Name{},
SignatureAlgorithm: x509.SHA256WithRSA,
ExtraExtensions: []pkix.Extension{
{
Id: asn1.ObjectIdentifier{2, 5, 29, 19},
Critical: true,
},
},
}
csrCertificate, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, rsaPriv)
if err != nil {
_debug("failed to create certificate signing request" + err.Error())
}
csr := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST", Bytes: csrCertificate,
})

csrPtr := uint16(uintptr(unsafe.Pointer(&csr[0])))
csrLen := uint16(len(csr))

return (uint32(csrPtr) << uint32(16)) | uint32(csrLen)
}

func main() {}
3 changes: 3 additions & 0 deletions samples/csr-rust/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
# rustflags = ["-C", "target-feature=+multivalue"]
target = "wasm32-unknown-unknown"
Loading