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

Replace source of pubKey to CSR #1761

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

* Fixed bug with changing joining community/create community screens with required field.

* Fixed bug with displaying incorrect default settings tab.
* Fixed bug with displaying incorrect default settings tab.

* Replaced source of publicKey in sendMessage saga to CSR
6 changes: 6 additions & 0 deletions packages/identity/src/extractPubKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export const keyFromCertificate = (certificate: Certificate | CertificationReque
return Buffer.from(certificate.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex).toString('base64')
}

export const pubKeyFromCsr = (csr: string) => {
const parsedCsr = parseCertificationRequest(csr)
const derBuffer = Buffer.from(parsedCsr.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex).toString('base64')
return derBuffer
}

export const keyObjectFromString = async (pubKeyString: string, crypto: SubtleCrypto | null): Promise<CryptoKey> => {
if (!crypto) throw new NoCryptoEngineError()
let keyArray = new ArrayBuffer(0)
Expand Down
3 changes: 2 additions & 1 deletion packages/identity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
keyFromCertificate,
keyObjectFromString,
extractPubKeyString,
pubKeyFromCsr,
} from './extractPubKey'
import { verifyUserCert } from './verifyUserCertificate'
import { verifySignature } from './verification'
Expand Down Expand Up @@ -36,7 +37,7 @@ import {

export { createRootCA }
export type { RootCA }
export { extractPubKey, parseCertificate, keyFromCertificate, keyObjectFromString, extractPubKeyString }
export { extractPubKey, parseCertificate, keyFromCertificate, keyObjectFromString, extractPubKeyString, pubKeyFromCsr }
export { verifyUserCert }
export { verifySignature }
export { sign }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setupCrypto, keyFromCertificate, loadPrivateKey, parseCertificate, sign } from '@quiet/identity'
import { setupCrypto, keyFromCertificate, loadPrivateKey, parseCertificate, sign, pubKeyFromCsr } from '@quiet/identity'
import { type Store } from '../../store.types'
import { getFactory } from '../../..'
import { prepareStore, reducers } from '../../../utils/tests/prepareStore'
Expand Down Expand Up @@ -74,8 +74,7 @@ describe('sendMessageSaga', () => {
.withReducer(reducer)
.withState(store.getState())
.provide([
[call.fn(parseCertificate), 'certificate'],
[call.fn(keyFromCertificate), 'publicKey'],
[call.fn(pubKeyFromCsr), 'publicKey'],
[call.fn(loadPrivateKey), 'privateKey'],
[call.fn(sign), jest.fn() as unknown as ArrayBuffer],
[call.fn(arrayBufferToString), 'signature'],
Expand Down Expand Up @@ -113,8 +112,7 @@ describe('sendMessageSaga', () => {
.withReducer(reducer)
.withState(store.getState())
.provide([
[call.fn(parseCertificate), 'certificate'],
[call.fn(keyFromCertificate), 'publicKey'],
[call.fn(pubKeyFromCsr), 'publicKey'],
[call.fn(loadPrivateKey), 'privateKey'],
[call.fn(sign), jest.fn() as unknown as ArrayBuffer],
[call.fn(arrayBufferToString), 'signature'],
Expand Down Expand Up @@ -165,8 +163,7 @@ describe('sendMessageSaga', () => {
.withReducer(reducer)
.withState(store.getState())
.provide([
[call.fn(parseCertificate), 'certificate'],
[call.fn(keyFromCertificate), 'publicKey'],
[call.fn(pubKeyFromCsr), 'publicKey'],
[call.fn(loadPrivateKey), 'privateKey'],
[call.fn(sign), jest.fn() as unknown as ArrayBuffer],
[call.fn(arrayBufferToString), 'signature'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Socket, applyEmitParams } from '../../../types'
import { type PayloadAction } from '@reduxjs/toolkit'
import { keyFromCertificate, parseCertificate, sign, loadPrivateKey } from '@quiet/identity'
import { sign, loadPrivateKey, pubKeyFromCsr } from '@quiet/identity'
import { call, select, apply, put } from 'typed-redux-saga'
import { arrayBufferToString } from 'pvutils'
import { config } from '../../users/const/certFieldTypes'
Expand All @@ -15,12 +15,9 @@ export function* sendMessageSaga(
action: PayloadAction<ReturnType<typeof messagesActions.sendMessage>['payload']>
): Generator {
const identity = yield* select(identitySelectors.currentIdentity)
if (!identity?.userCsr || !identity.userCertificate) return
if (!identity?.userCsr) return

const certificate = identity.userCertificate

const parsedCertificate = yield* call(parseCertificate, certificate)
const pubKey = yield* call(keyFromCertificate, parsedCertificate)
const pubKey = yield* call(pubKeyFromCsr, identity.userCsr.userCsr)
const keyObject = yield* call(loadPrivateKey, identity.userCsr.userKey, config.signAlg)
const signatureArrayBuffer = yield* call(sign, action.payload.message, keyObject)
const signature = yield* call(arrayBufferToString, signatureArrayBuffer)
Expand Down