-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1036 from vector-im/cross-signing/self-sign
Allow to sign own device once MSK is trusted
- Loading branch information
Showing
9 changed files
with
219 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2016-2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { PkSigning } from "@matrix-org/olm"; | ||
import anotherjson from "another-json"; | ||
import type * as OlmNamespace from "@matrix-org/olm"; | ||
type Olm = typeof OlmNamespace; | ||
|
||
export interface IObject { | ||
unsigned?: object; | ||
signatures?: ISignatures; | ||
} | ||
|
||
export interface ISignatures { | ||
[entity: string]: { | ||
[keyId: string]: string; | ||
}; | ||
} | ||
|
||
export interface ISigned { | ||
signatures?: ISignatures; | ||
} | ||
|
||
// from matrix-js-sdk | ||
/** | ||
* Sign a JSON object using public key cryptography | ||
* @param obj - Object to sign. The object will be modified to include | ||
* the new signature | ||
* @param key - the signing object or the private key | ||
* seed | ||
* @param userId - The user ID who owns the signing key | ||
* @param pubKey - The public key (ignored if key is a seed) | ||
* @returns the signature for the object | ||
*/ | ||
export function pkSign(olmUtil: Olm, obj: object & IObject, key: Uint8Array | PkSigning, userId: string, pubKey: string): string { | ||
let createdKey = false; | ||
if (key instanceof Uint8Array) { | ||
const keyObj = new olmUtil.PkSigning(); | ||
pubKey = keyObj.init_with_seed(key); | ||
key = keyObj; | ||
createdKey = true; | ||
} | ||
const sigs = obj.signatures || {}; | ||
delete obj.signatures; | ||
const unsigned = obj.unsigned; | ||
if (obj.unsigned) delete obj.unsigned; | ||
try { | ||
const mysigs = sigs[userId] || {}; | ||
sigs[userId] = mysigs; | ||
|
||
return (mysigs["ed25519:" + pubKey] = key.sign(anotherjson.stringify(obj))); | ||
} finally { | ||
obj.signatures = sigs; | ||
if (unsigned) obj.unsigned = unsigned; | ||
if (createdKey) { | ||
key.free(); | ||
} | ||
} | ||
} |
Oops, something went wrong.