Rust – Sequoia OpenPGP with Botan backend – wasm-bindgen fails

I’m trying to check if a Rust application making use of cryptography (i.e. Sequoia OpenPGP using crypto-botan as backend) can be turned into WebAssembly to be used in a frontend application.

This is my trivial lib.rs file, where I’m just trying to expose a function to return a public key:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>use sequoia_openpgp::{cert::{CertBuilder, CipherSuite}, serialize::MarshalInto};
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
pub fn generate_certificate(email: &str) -> String {
let certificate = CertBuilder::general_purpose(CipherSuite::RSA4k, Some(email))
.generate()
.unwrap()
.0;
let armored = &certificate.armored();
String::from_utf8(armored.to_vec().unwrap()).unwrap()
}
</code>
<code>use sequoia_openpgp::{cert::{CertBuilder, CipherSuite}, serialize::MarshalInto}; use wasm_bindgen::prelude::wasm_bindgen; #[wasm_bindgen] pub fn generate_certificate(email: &str) -> String { let certificate = CertBuilder::general_purpose(CipherSuite::RSA4k, Some(email)) .generate() .unwrap() .0; let armored = &certificate.armored(); String::from_utf8(armored.to_vec().unwrap()).unwrap() } </code>
use sequoia_openpgp::{cert::{CertBuilder, CipherSuite}, serialize::MarshalInto};
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
pub fn generate_certificate(email: &str) -> String {
    let certificate = CertBuilder::general_purpose(CipherSuite::RSA4k, Some(email))
        .generate()
        .unwrap()
        .0;
    let armored = &certificate.armored();
    String::from_utf8(armored.to_vec().unwrap()).unwrap()
}

And this is my Cargo.toml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[package]
name = "wasmtest"
version = "0.1.0"
edition = "2021"
resolver = "2"
[dependencies]
sequoia-openpgp = { version = "1.21", default-features = false, features = [
"crypto-botan",
] }
wasm-bindgen = "0.2.92"
[lib]
crate-type = ["cdylib", "rlib"]
[build-dependencies]
num_cpus = "1.16.0"
pkg-config = "0.3.30"
</code>
<code>[package] name = "wasmtest" version = "0.1.0" edition = "2021" resolver = "2" [dependencies] sequoia-openpgp = { version = "1.21", default-features = false, features = [ "crypto-botan", ] } wasm-bindgen = "0.2.92" [lib] crate-type = ["cdylib", "rlib"] [build-dependencies] num_cpus = "1.16.0" pkg-config = "0.3.30" </code>
[package]
name = "wasmtest"
version = "0.1.0"
edition = "2021"
resolver = "2"

[dependencies]
sequoia-openpgp = { version = "1.21", default-features = false, features = [
    "crypto-botan",
] }
wasm-bindgen = "0.2.92"

[lib]
crate-type = ["cdylib", "rlib"]

[build-dependencies]
num_cpus = "1.16.0"
pkg-config = "0.3.30"

Since Sequoia needs Botan as backend my project uses it as a git submodule (using version 3.5.0 as of now) and builds it in build.rs as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>use core::panic;
use std::{cmp::max, env, path::Path, process::Command};
fn main() {
env::set_var("RUST_BACKTRACE", "full");
build_botan();
}
fn build_botan() {
println!("cargo:rerun-if-changed=./botan/src/");
println!("cargo:rerun-if-changed=./botan/configure.py");
let out_dir = env::var("OUT_DIR").unwrap();
let botan_path = Path::new(&out_dir).join("botan_output");
let lib_path = botan_path.join("lib");
println!(
"cargo:rustc-link-search=native={}",
lib_path.to_string_lossy()
);
configure_botan(&botan_path);
let num_jobs = max(num_cpus::get() - 2, 1);
make_botan(num_jobs);
make_install_botan();
}
fn configure_botan(botan_path: &Path) {
let mut command = Command::new("python3");
let botan_prefix = botan_path.to_string_lossy().into_owned();
// Prepare configuration arguments
let mut config_args = vec![
"configure.py".to_string(),
// // wasm
// "--cc=emcc".to_string(),
"--cpu=wasm".to_string(),
"--os=emscripten".to_string(),
"--prefix".to_string(),
botan_prefix,
];
command.args(&config_args);
let status = command.current_dir("./botan").status().unwrap();
if !status.success() {
panic!();
}
}
fn make_botan(num_jobs: usize) {
#[cfg(unix)]
let status = Command::new("make")
.args(["--directory", "./botan", "-j", &num_jobs.to_string()])
.status()
.unwrap();
if !status.success() {
panic!();
}
}
fn make_install_botan() {
#[cfg(unix)]
let status = Command::new("make")
.args(["--directory", "./botan", "install"])
.status()
.unwrap();
if !status.success() {
panic!();
}
}
</code>
<code>use core::panic; use std::{cmp::max, env, path::Path, process::Command}; fn main() { env::set_var("RUST_BACKTRACE", "full"); build_botan(); } fn build_botan() { println!("cargo:rerun-if-changed=./botan/src/"); println!("cargo:rerun-if-changed=./botan/configure.py"); let out_dir = env::var("OUT_DIR").unwrap(); let botan_path = Path::new(&out_dir).join("botan_output"); let lib_path = botan_path.join("lib"); println!( "cargo:rustc-link-search=native={}", lib_path.to_string_lossy() ); configure_botan(&botan_path); let num_jobs = max(num_cpus::get() - 2, 1); make_botan(num_jobs); make_install_botan(); } fn configure_botan(botan_path: &Path) { let mut command = Command::new("python3"); let botan_prefix = botan_path.to_string_lossy().into_owned(); // Prepare configuration arguments let mut config_args = vec![ "configure.py".to_string(), // // wasm // "--cc=emcc".to_string(), "--cpu=wasm".to_string(), "--os=emscripten".to_string(), "--prefix".to_string(), botan_prefix, ]; command.args(&config_args); let status = command.current_dir("./botan").status().unwrap(); if !status.success() { panic!(); } } fn make_botan(num_jobs: usize) { #[cfg(unix)] let status = Command::new("make") .args(["--directory", "./botan", "-j", &num_jobs.to_string()]) .status() .unwrap(); if !status.success() { panic!(); } } fn make_install_botan() { #[cfg(unix)] let status = Command::new("make") .args(["--directory", "./botan", "install"]) .status() .unwrap(); if !status.success() { panic!(); } } </code>
use core::panic;
use std::{cmp::max, env, path::Path, process::Command};

fn main() {
    env::set_var("RUST_BACKTRACE", "full");
    build_botan();
}

fn build_botan() {
    println!("cargo:rerun-if-changed=./botan/src/");
    println!("cargo:rerun-if-changed=./botan/configure.py");

    let out_dir = env::var("OUT_DIR").unwrap();
    let botan_path = Path::new(&out_dir).join("botan_output");
    let lib_path = botan_path.join("lib");
    println!(
        "cargo:rustc-link-search=native={}",
        lib_path.to_string_lossy()
    );
    configure_botan(&botan_path);

    let num_jobs = max(num_cpus::get() - 2, 1);
    make_botan(num_jobs);
    make_install_botan();
}

fn configure_botan(botan_path: &Path) {
    let mut command = Command::new("python3");
    let botan_prefix = botan_path.to_string_lossy().into_owned();

    // Prepare configuration arguments
    let mut config_args = vec![
        "configure.py".to_string(),
        // // wasm
        // "--cc=emcc".to_string(),
        "--cpu=wasm".to_string(),
        "--os=emscripten".to_string(),
        "--prefix".to_string(),
        botan_prefix,
    ];

    command.args(&config_args);

    let status = command.current_dir("./botan").status().unwrap();
    if !status.success() {
        panic!();
    }
}

fn make_botan(num_jobs: usize) {
    #[cfg(unix)]
    let status = Command::new("make")
        .args(["--directory", "./botan", "-j", &num_jobs.to_string()])
        .status()
        .unwrap();

    if !status.success() {
        panic!();
    }
}

fn make_install_botan() {
    #[cfg(unix)]
    let status = Command::new("make")
        .args(["--directory", "./botan", "install"])
        .status()
        .unwrap();

    if !status.success() {
        panic!();
    }
}

In addition I also configured the default target in .cargo/config.toml as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[build]
target = "wasm32-unknown-unknown"
</code>
<code>[build] target = "wasm32-unknown-unknown" </code>
[build]
target = "wasm32-unknown-unknown"

If I try to run wasm-pack build --target web the build finishes correctly. But then when trying out in a HTML page:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE html>
<html>
<head>
<title>Home</title>
<body>
<h1>Wasm Test</h1>
<span id="result"></span>
</body>
<script type="module">
import init, {add, generate_certificate} from './pkg/wasmtest.js';
await init();
let result = generate_certificate("[email protected]");
document.getElementById("result").innerText = result;
</script>
</html>
</code>
<code><!DOCTYPE html> <html> <head> <title>Home</title> <body> <h1>Wasm Test</h1> <span id="result"></span> </body> <script type="module"> import init, {add, generate_certificate} from './pkg/wasmtest.js'; await init(); let result = generate_certificate("[email protected]"); document.getElementById("result").innerText = result; </script> </html> </code>
<!DOCTYPE html> 
<html>
<head>
    <title>Home</title>
    <body>
        <h1>Wasm Test</h1>
        <span id="result"></span>
    </body>
    <script type="module">
        import init, {add, generate_certificate} from './pkg/wasmtest.js';
        await init();
        let result = generate_certificate("[email protected]");
        document.getElementById("result").innerText = result;

    </script>   
</html>

The console shows error: Uncaught TypeError: Failed to resolve module specifier “env”.
I’ve been able to pinpoint this thanks to text which clearly identifies the culprit as Botan since there are a number of entries such as (import “env” “botan_mp_init” (func (;4;) (type 6))) in the wat file.
As far as I understood this means that Botan functions are undefined (i.e. could not be bound to the wasm somehow).
This led me to try and modify the build.rs so that Botan is built for wasm. Following the instructions in text I changed the option passed to the configure.py script:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let mut config_args = vec![
"configure.py".to_string(),
"--cpu=wasm".to_string(),
"--os=emscripten".to_string(),
"--prefix".to_string(),
botan_prefix,
];
</code>
<code>let mut config_args = vec![ "configure.py".to_string(), "--cpu=wasm".to_string(), "--os=emscripten".to_string(), "--prefix".to_string(), botan_prefix, ]; </code>
let mut config_args = vec![
        "configure.py".to_string(),
        "--cpu=wasm".to_string(),
        "--os=emscripten".to_string(),
        "--prefix".to_string(),
        botan_prefix,
    ];

However this makes me wonder whether Botan can be build for target wasm32-unknown-unknown.
Running again wasm-pack build --target web fails this time with error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>thread 'main' panicked at /Users/antonioumbertoaramini/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-wasm-interpreter-0.2.92/src/lib.rs:266:22:
non-i32 constant
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit status: 101
full command: "/Users/antonioumbertoaramini/.cargo/bin/wasm-bindgen" "/Users/antonioumbertoaramini/Documents/Projects/wasmtest/target/wasm32-unknown-unknown/release/wasmtest.wasm" "--out-dir" "/Users/antonioumbertoaramini/Documents/Projects/wasmtest/pkg" "--typescript" "--target" "web"
</code>
<code>thread 'main' panicked at /Users/antonioumbertoaramini/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-wasm-interpreter-0.2.92/src/lib.rs:266:22: non-i32 constant note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace Error: Running the wasm-bindgen CLI Caused by: Running the wasm-bindgen CLI Caused by: failed to execute `wasm-bindgen`: exited with exit status: 101 full command: "/Users/antonioumbertoaramini/.cargo/bin/wasm-bindgen" "/Users/antonioumbertoaramini/Documents/Projects/wasmtest/target/wasm32-unknown-unknown/release/wasmtest.wasm" "--out-dir" "/Users/antonioumbertoaramini/Documents/Projects/wasmtest/pkg" "--typescript" "--target" "web" </code>
thread 'main' panicked at /Users/antonioumbertoaramini/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-wasm-interpreter-0.2.92/src/lib.rs:266:22:
non-i32 constant
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit status: 101
full command: "/Users/antonioumbertoaramini/.cargo/bin/wasm-bindgen" "/Users/antonioumbertoaramini/Documents/Projects/wasmtest/target/wasm32-unknown-unknown/release/wasmtest.wasm" "--out-dir" "/Users/antonioumbertoaramini/Documents/Projects/wasmtest/pkg" "--typescript" "--target" "web"

which is really not saying much on what went wrong.

Any clue or hints to solve this would be welcome.

New contributor

Antonio Umberto Aramini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật