fix: 修复生成的证书无序列号的问题

main
Zaeyee 2024-09-02 11:44:43 +08:00
parent 5848155bba
commit dc1486a77e
4 changed files with 67 additions and 52 deletions

85
src-tauri/Cargo.lock generated
View File

@ -491,7 +491,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331" checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331"
dependencies = [ dependencies = [
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -501,7 +501,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f"
dependencies = [ dependencies = [
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -525,7 +525,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"strsim", "strsim",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -536,7 +536,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [ dependencies = [
"darling_core", "darling_core",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -559,7 +559,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rustc_version", "rustc_version",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -801,7 +801,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -1290,9 +1290,9 @@ dependencies = [
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.4.0" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c" checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown 0.14.5", "hashbrown 0.14.5",
@ -1728,7 +1728,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -1913,7 +1913,7 @@ dependencies = [
"phf_shared 0.11.2", "phf_shared 0.11.2",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -1943,6 +1943,20 @@ dependencies = [
"siphasher", "siphasher",
] ]
[[package]]
name = "phiz-pay-tool"
version = "0.1.0"
dependencies = [
"chrono",
"openssl",
"rand 0.8.5",
"serde",
"serde_json",
"tauri",
"tauri-build",
"thiserror",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.14" version = "0.2.14"
@ -1968,7 +1982,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42cf17e9a1800f5f396bc67d193dc9411b59012a5876445ef450d449881e1016" checksum = "42cf17e9a1800f5f396bc67d193dc9411b59012a5876445ef450d449881e1016"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"indexmap 2.4.0", "indexmap 2.5.0",
"quick-xml", "quick-xml",
"serde", "serde",
"time", "time",
@ -2357,7 +2371,7 @@ checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -2366,7 +2380,7 @@ version = "1.0.127"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
dependencies = [ dependencies = [
"indexmap 2.4.0", "indexmap 2.5.0",
"itoa 1.0.11", "itoa 1.0.11",
"memchr", "memchr",
"ryu", "ryu",
@ -2381,7 +2395,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -2403,7 +2417,7 @@ dependencies = [
"chrono", "chrono",
"hex", "hex",
"indexmap 1.9.3", "indexmap 1.9.3",
"indexmap 2.4.0", "indexmap 2.5.0",
"serde", "serde",
"serde_derive", "serde_derive",
"serde_json", "serde_json",
@ -2420,7 +2434,7 @@ dependencies = [
"darling", "darling",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -2442,7 +2456,7 @@ checksum = "772ee033c0916d670af7860b6e1ef7d658a4629a6d0b4c8c3e67f09b3765b75d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -2596,9 +2610,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.76" version = "2.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2686,7 +2700,7 @@ checksum = "f4e16beb8b2ac17db28eab8bca40e62dbfbb34c0fcdc6d9826b11b7b5d047dfd"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -2756,19 +2770,6 @@ dependencies = [
"windows 0.39.0", "windows 0.39.0",
] ]
[[package]]
name = "tauri-app"
version = "0.1.0"
dependencies = [
"chrono",
"openssl",
"serde",
"serde_json",
"tauri",
"tauri-build",
"thiserror",
]
[[package]] [[package]]
name = "tauri-build" name = "tauri-build"
version = "1.5.4" version = "1.5.4"
@ -2956,7 +2957,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -3017,9 +3018,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.39.3" version = "1.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5" checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -3074,7 +3075,7 @@ version = "0.19.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
dependencies = [ dependencies = [
"indexmap 2.4.0", "indexmap 2.5.0",
"serde", "serde",
"serde_spanned", "serde_spanned",
"toml_datetime", "toml_datetime",
@ -3087,7 +3088,7 @@ version = "0.22.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d"
dependencies = [ dependencies = [
"indexmap 2.4.0", "indexmap 2.5.0",
"serde", "serde",
"serde_spanned", "serde_spanned",
"toml_datetime", "toml_datetime",
@ -3113,7 +3114,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]
[[package]] [[package]]
@ -3309,7 +3310,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -3343,7 +3344,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -3947,5 +3948,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.77",
] ]

View File

@ -1,5 +1,5 @@
[package] [package]
name = "tauri-app" name = "phiz-pay-tool"
version = "0.1.0" version = "0.1.0"
description = "A Tauri App" description = "A Tauri App"
authors = ["you"] authors = ["you"]
@ -11,12 +11,13 @@ edition = "2021"
tauri-build = { version = "1", features = [] } tauri-build = { version = "1", features = [] }
[dependencies] [dependencies]
tauri = { version = "1", features = [ "path-all", "dialog-all", "shell-open"] } tauri = { version = "1", features = ["path-all", "dialog-all", "shell-open"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
openssl = "0.10.66" openssl = "0.10.66"
thiserror = "1.0.63" thiserror = "1.0.63"
chrono = "0.4.38" chrono = "0.4.38"
rand = "0.8.5"
[features] [features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!

View File

@ -2,14 +2,16 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use openssl::{ use openssl::{
asn1::Asn1Time, asn1::{Asn1Integer, Asn1Time},
bn::BigNum,
hash::MessageDigest,
pkey::PKey, pkey::PKey,
rsa::Rsa, rsa::Rsa,
x509::{X509Builder, X509NameBuilder}, x509::{X509Builder, X509NameBuilder},
}; };
use rand::{thread_rng, Rng};
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
// create the error type that represents all errors possible in our program // create the error type that represents all errors possible in our program
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
@ -46,18 +48,29 @@ fn gen_rsa_pair(merchant_id: &str, email: &str, save_path: &str) -> Result<(), E
let x509_name = x509_name_builder.build(); let x509_name = x509_name_builder.build();
let mut x509_builder: X509Builder = X509Builder::new()?; let mut x509_builder: X509Builder = X509Builder::new()?;
let mut rng = thread_rng();
let mut bytes = [0u8; 20];
rng.fill(&mut bytes);
let bn: BigNum = BigNum::from_slice(&bytes)?;
let serial_number = Asn1Integer::from_bn(&bn)?;
x509_builder.set_serial_number(&serial_number)?;
x509_builder.set_version(2)?;
x509_builder.set_subject_name(&x509_name)?; x509_builder.set_subject_name(&x509_name)?;
x509_builder.set_issuer_name(&x509_name)?; x509_builder.set_issuer_name(&x509_name)?;
x509_builder.set_pubkey(&private_key)?; x509_builder.set_pubkey(&private_key)?;
x509_builder.set_not_before(&Asn1Time::days_from_now(0).unwrap())?; let not_before = Asn1Time::days_from_now(0)?;
x509_builder.set_not_after(&Asn1Time::days_from_now(1095).unwrap())?; let not_after = Asn1Time::days_from_now(1095)?;
x509_builder.sign(&private_key, openssl::hash::MessageDigest::sha256())?; x509_builder.set_not_before(&not_before)?;
x509_builder.set_not_after(&not_after)?;
x509_builder.sign(&private_key, MessageDigest::sha256())?;
let x509 = x509_builder.build(); let x509 = x509_builder.build();
// 将私钥转换为 PEM 格式 // 将私钥转换为 PEM 格式
let private_key_pem = private_key.private_key_to_pem_pkcs8()?; let private_key_pem = private_key.private_key_to_pem_pkcs8()?;
// 保存私钥到文件 // 保存私钥到文件
let private_key_path = PathBuf::from(format!("{}/{}_private_key.pem", save_path, merchant_id)); let private_key_path = format!("{}/{}_private_key.pem", save_path, merchant_id);
let mut private_key_file = File::create(&private_key_path)?; let mut private_key_file = File::create(&private_key_path)?;
private_key_file.write_all(&private_key_pem)?; private_key_file.write_all(&private_key_pem)?;

View File

@ -4,8 +4,8 @@ import { invoke } from '@tauri-apps/api/tauri'
import { message, open } from '@tauri-apps/api/dialog' import { message, open } from '@tauri-apps/api/dialog'
import { downloadDir } from '@tauri-apps/api/path' import { downloadDir } from '@tauri-apps/api/path'
const merchantId = ref('P80029767866') const merchantId = ref('')
const email = ref('zaeyee@163.com') const email = ref('')
async function genPrivateKey() { async function genPrivateKey() {
try { try {