From 0ba5933c94a54997985701aa0f88f8cc6e572ba9 Mon Sep 17 00:00:00 2001 From: turnoffthiscomputer Date: Tue, 27 Aug 2024 11:49:26 +0200 Subject: [PATCH] refactor app code, dscSecret gen and mockpassport gen --- app/src/screens/MockDataScreen.tsx | 22 ++------ app/src/stores/userStore.ts | 9 +--- app/src/utils/nfcScanner.ts | 52 ------------------- .../passportData/genMockPassportData.ts | 12 +++-- common/src/utils/csca.ts | 7 ++- 5 files changed, 20 insertions(+), 82 deletions(-) diff --git a/app/src/screens/MockDataScreen.tsx b/app/src/screens/MockDataScreen.tsx index ffafd3ee..2bbd3522 100644 --- a/app/src/screens/MockDataScreen.tsx +++ b/app/src/screens/MockDataScreen.tsx @@ -5,7 +5,6 @@ import { bgGreen, textBlack } from '../utils/colors'; import useUserStore from '../stores/userStore'; import useNavigationStore from '../stores/navigationStore'; import CustomButton from '../components/CustomButton'; -import { mock_dsc_sha256_rsa_4096, mock_dsc_sha1_rsa_4096, mock_dsc_sha256_rsapss_4096 } from '../../../common/src/constants/mockCertificates'; import DatePicker from 'react-native-date-picker'; import { genMockPassportData } from '../../../common/scripts/passportData/genMockPassportData'; import { countryCodes } from '../../../common/src/constants/constants'; @@ -26,28 +25,13 @@ const MockDataScreen: React.FC = () => { const castDate = (date: Date) => { return (date.toISOString().slice(2, 4) + date.toISOString().slice(5, 7) + date.toISOString().slice(8, 10)).toString(); } - const getDSC = () => { - switch (signatureAlgorithm) { - case "rsa sha1": - return mock_dsc_sha1_rsa_4096 - case "rsa sha256": - return mock_dsc_sha256_rsa_4096 - case "rsapss sha256": - return mock_dsc_sha256_rsapss_4096 - default: - return mock_dsc_sha256_rsa_4096 - } - } + const handleGenerate = () => { - let mockPassportData = genMockPassportData(signatureAlgorithm as "rsa sha256" | "rsa sha1" | "rsapss sha256", nationality, castDate(dateOfBirthDatePicker), castDate(dateOfExpiryDatePicker)); - const dsc = getDSC() - console.log(mockPassportData) - console.log(dsc) - mockPassportData.dsc = dsc + const mockPassportData = genMockPassportData(signatureAlgorithm as "rsa sha256" | "rsa sha1" | "rsapss sha256", nationality, castDate(dateOfBirthDatePicker), castDate(dateOfExpiryDatePicker)); useUserStore.getState().registerPassportData(mockPassportData) useUserStore.getState().setRegistered(true); - const sigAlgName = getSignatureAlgorithm(dsc); + const sigAlgName = getSignatureAlgorithm(mockPassportData.dsc as string); const circuitName = getCircuitName("prove", sigAlgName.signatureAlgorithm, sigAlgName.hashFunction); downloadZkey(circuitName as any); useNavigationStore.getState().setSelectedTab("app"); diff --git a/app/src/stores/userStore.ts b/app/src/stores/userStore.ts index ff0555cd..2aee4fce 100644 --- a/app/src/stores/userStore.ts +++ b/app/src/stores/userStore.ts @@ -19,6 +19,7 @@ import { sendRegisterTransaction } from '../utils/transactions'; import { loadPassportData, loadSecret, loadSecretOrCreateIt, storePassportData } from '../utils/keychain'; import { ethers } from 'ethers'; import { isCommitmentRegistered } from '../utils/registration'; +import { generateDscSecret } from '../../../common/src/utils/csca'; interface UserState { @@ -28,7 +29,6 @@ interface UserState { registered: boolean passportData: PassportData secret: string - dscCertificate: any cscaProof: Proof | null localProof: Proof | null dscSecret: string | null @@ -57,7 +57,6 @@ const useUserStore = create((set, get) => ({ registered: false, passportData: mockPassportData_sha256_rsa_65537, secret: "", - dscCertificate: null, cscaProof: null, localProof: null, setRegistered: (registered: boolean) => { @@ -135,7 +134,6 @@ const useUserStore = create((set, get) => ({ console.log("registerCommitment") const { toast, - setStep, update: updateNavigationStore, } = useNavigationStore.getState(); const secret = await loadSecret() as string; @@ -161,10 +159,7 @@ const useUserStore = create((set, get) => ({ try { if (get().dscSecret === null) { - console.log("DSC secret is not set, generating a new one"); - const secretBytes = forge.random.getBytesSync(31); - dsc_secret = BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString(); - console.log('Generated secret:', dsc_secret.toString()); + dsc_secret = generateDscSecret(); get().setDscSecret(dsc_secret); } const inputs = generateCircuitInputsRegister( diff --git a/app/src/utils/nfcScanner.ts b/app/src/utils/nfcScanner.ts index 9781f1d1..91f62450 100644 --- a/app/src/utils/nfcScanner.ts +++ b/app/src/utils/nfcScanner.ts @@ -9,9 +9,6 @@ import { Buffer } from 'buffer'; import * as amplitude from '@amplitude/analytics-react-native'; import useUserStore from '../stores/userStore'; import useNavigationStore from '../stores/navigationStore'; -import { k_csca, k_dsc, max_cert_bytes, n_csca, n_dsc } from '../../../common/src/constants/constants'; -import { getCSCAInputs } from '../../../common/src/utils/csca'; -import { sendCSCARequest } from './cscaRequest'; import { getSignatureAlgorithm, getCircuitName } from '../../../common/src/utils/handleCertificate'; import { downloadZkey } from './zkeyDownload'; @@ -54,7 +51,6 @@ const scanAndroid = async (setModalProofStep: (modalProofStep: number) => void) passportNumber, dateOfBirth, dateOfExpiry, - dscCertificate } = useUserStore.getState() const { toast, setNfcSheetIsOpen } = useNavigationStore.getState(); setNfcSheetIsOpen(true); @@ -172,32 +168,9 @@ const handleResponseIOS = async ( photoBase64: "data:image/jpeg;base64," + parsed.passportPhoto, }; useUserStore.getState().registerPassportData(passportData) - - let secret = useUserStore.getState().dscSecret; - if (secret === null) { - // Finally, generate CSCA Inputs and request modal server - // Generate a cryptographically secure random secret of (31 bytes) - const secretBytes = forge.random.getBytesSync(31); - secret = BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString(); - console.log('Generated secret:', secret.toString()); - useUserStore.getState().setDscSecret(secret); - } const sigAlgName = getSignatureAlgorithm(pem); const circuitName = getCircuitName("prove", sigAlgName.signatureAlgorithm, sigAlgName.hashFunction); downloadZkey(circuitName as any); - // const inputs_csca = getCSCAInputs( - // secret as string, - // certificate, - // null, - // n_dsc, - // k_dsc, - // n_csca, - // k_csca, - // max_cert_bytes, - // false - // ); - - //sendCSCARequest(inputs_csca, setModalProofStep); useNavigationStore.getState().setSelectedTab("next"); } catch (e: any) { console.log('error during parsing:', e); @@ -280,33 +253,8 @@ const handleResponseAndroid = async ( console.log("documentSigningCertificate", documentSigningCertificate) useUserStore.getState().registerPassportData(passportData) - // Finally request the Modal server to verify the DSC certificate - const certificate = forge.pki.certificateFromPem(documentSigningCertificate); - useUserStore.getState().dscCertificate = certificate; - - let secret = useUserStore.getState().dscSecret; - if (secret === null) { - // Finally, generate CSCA Inputs and request modal server - // Generate a cryptographically secure random secret of (31 bytes) - const secretBytes = forge.random.getBytesSync(31); - secret = BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString(); - console.log('Generated secret:', secret.toString()); - useUserStore.getState().setDscSecret(secret); - } const sigAlgName = getSignatureAlgorithm(pem); const circuitName = getCircuitName("prove", sigAlgName.signatureAlgorithm, sigAlgName.hashFunction); downloadZkey(circuitName as any); - // const inputs_csca = getCSCAInputs( - // secret as string, - // certificate, - // null, - // n_dsc, - // k_dsc, - // n_csca, - // k_csca, - // max_cert_bytes, - // false - // ); - //sendCSCARequest(inputs_csca, setModalProofStep); useNavigationStore.getState().setSelectedTab("next"); }; diff --git a/common/scripts/passportData/genMockPassportData.ts b/common/scripts/passportData/genMockPassportData.ts index 866a9f9f..309f68a5 100644 --- a/common/scripts/passportData/genMockPassportData.ts +++ b/common/scripts/passportData/genMockPassportData.ts @@ -2,7 +2,7 @@ import { PassportData } from "../../src/utils/types"; import { hash, assembleEContent, formatAndConcatenateDataHashes, formatMrz, hexToDecimal } from "../../src/utils/utils"; import * as forge from 'node-forge'; import * as rs from 'jsrsasign'; -import { mock_dsc_key_sha1_rsa_4096, mock_dsc_key_sha256_rsa_4096, mock_dsc_key_sha256_rsapss_2048, mock_dsc_sha256_rsapss_2048 } from "../../src/constants/mockCertificates"; +import { mock_dsc_key_sha1_rsa_4096, mock_dsc_key_sha256_rsa_4096, mock_dsc_key_sha256_rsapss_2048, mock_dsc_key_sha256_rsapss_4096, mock_dsc_sha1_rsa_4096, mock_dsc_sha256_rsa_4096, mock_dsc_sha256_rsapss_2048, mock_dsc_sha256_rsapss_4096 } from "../../src/constants/mockCertificates"; import { sampleDataHashes_rsa_sha1, sampleDataHashes_rsa_sha256, sampleDataHashes_rsapss_sha256 } from "./sampleDataHashes"; export function genMockPassportData( @@ -18,6 +18,7 @@ export function genMockPassportData( let hashLen: number; let sampleDataHashes: [number, number[]][]; let privateKeyPem: string; + let dsc: string; switch (signatureType) { case 'rsa sha1': @@ -25,18 +26,21 @@ export function genMockPassportData( hashLen = 20; sampleDataHashes = sampleDataHashes_rsa_sha1; privateKeyPem = mock_dsc_key_sha1_rsa_4096; + dsc = mock_dsc_sha1_rsa_4096; break; case 'rsa sha256': signatureAlgorithm = 'sha256WithRSAEncryption'; hashLen = 32; sampleDataHashes = sampleDataHashes_rsa_sha256; privateKeyPem = mock_dsc_key_sha256_rsa_4096; + dsc = mock_dsc_sha256_rsa_4096; break; case 'rsapss sha256': signatureAlgorithm = 'sha256WithRSASSAPSS'; hashLen = 32; sampleDataHashes = sampleDataHashes_rsapss_sha256; - privateKeyPem = mock_dsc_key_sha256_rsapss_2048; + privateKeyPem = mock_dsc_key_sha256_rsapss_4096; + dsc = mock_dsc_sha256_rsapss_4096; break; } @@ -74,6 +78,7 @@ export function genMockPassportData( const signatureBytes = Array.from(signature, byte => byte < 128 ? byte : byte - 256); return { + dsc: dsc, mrz: mrz, signatureAlgorithm: signatureAlgorithm, pubKey: { @@ -85,4 +90,5 @@ export function genMockPassportData( encryptedDigest: signatureBytes, photoBase64: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIy3..." }; -} \ No newline at end of file +} + diff --git a/common/src/utils/csca.ts b/common/src/utils/csca.ts index 79a69c76..7bd50473 100644 --- a/common/src/utils/csca.ts +++ b/common/src/utils/csca.ts @@ -256,4 +256,9 @@ export const sendCSCARequest = async (inputs_csca: any): Promise => { } throw error; } -}; \ No newline at end of file +}; + +export const generateDscSecret = () => { + const secretBytes = forge.random.getBytesSync(31); + return BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString(); +} \ No newline at end of file