Skip to content

Commit

Permalink
Uniffi changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkulic committed Oct 6, 2023
1 parent 397b82f commit 0d7c53c
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 190 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ features = ['derive']
[dependencies.askar-crypto]
version = '0.2'
features = ['std']
git = 'https://github.com/hyperledger/aries-askar'
rev = '4f29d43d584c4a1f1f982c4511824421aeccd2db'

[dependencies.uuid]
version = "0.8"
Expand Down
16 changes: 16 additions & 0 deletions src/secrets/resolvers/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use askar_crypto::alg::ed25519::Ed25519KeyPair;
use askar_crypto::alg::k256::K256KeyPair;
use askar_crypto::alg::p256::P256KeyPair;
use askar_crypto::alg::x25519::X25519KeyPair;
use askar_crypto::alg::AnyKey;
use askar_crypto::alg::AnyKeyCreate;
use askar_crypto::buffer::SecretBytes;
use askar_crypto::jwk::FromJwk;
use askar_crypto::kdf::ecdh_1pu::Ecdh1PU;
Expand All @@ -18,6 +20,7 @@ use askar_crypto::sign::KeySign;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::sync::Arc;

pub struct ExampleKMS {
known_secrets: Vec<Secret>,
Expand Down Expand Up @@ -253,6 +256,19 @@ pub enum SecretMaterial {
Base58 { private_key_base58: String },
}

impl Secret {
pub fn to_key(&self) -> Result<Arc<AnyKey>> {
// Box::new(KeyT(self.as_key_pair().unwrap()))
let any_key = match self.as_key_pair()? {
KnownKeyPair::Ed25519(key) => Arc::<AnyKey>::from_key(key),
KnownKeyPair::X25519(key) => Arc::<AnyKey>::from_key(key),
KnownKeyPair::P256(key) => Arc::<AnyKey>::from_key(key),
KnownKeyPair::K256(key) => Arc::<AnyKey>::from_key(key),
};
Ok(any_key)
}
}

impl AsKnownKeyPair for Secret {
fn key_alg(&self) -> KnownKeyAlg {
match (&self.type_, &self.secret_material) {
Expand Down
1 change: 1 addition & 0 deletions uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ futures = { version = "0.3.17", features = ["thread-pool"] }
num_cpus = "1.8.0"
async-trait = '0.1'
serde_json = '1.0'
aries-askar = "0.2"

[dev-dependencies.tokio]
version = '1.9'
Expand Down
48 changes: 41 additions & 7 deletions uniffi/src/didcomm.udl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ typedef string JsonValue;
// 1. MAIN DIDCOMM INTERFACE

interface DIDComm {
constructor(DIDResolver did_resolver, SecretsResolver secret_resolver);
constructor(DIDResolver did_resolver, KeyManagementService kms);

ErrorCode pack_plaintext([ByRef] Message msg, OnPackPlaintextResult cb);
ErrorCode pack_signed([ByRef] Message msg, string sign_by, OnPackSignedResult cb);
Expand Down Expand Up @@ -168,14 +168,17 @@ interface ExampleDIDResolver {

// 5. SECRETS RESOLVER

callback interface SecretsResolver {
ErrorCode get_secret(string secretid, OnGetSecretResult cb); // should be in camel case
callback interface KeyManagementService {
ErrorCode get_key_alg(string secretid, OnGetKeyAlgResult cb); // should be in camel case
ErrorCode find_secrets(sequence<string> secretids, OnFindSecretsResult cb); // should be in camel case
ErrorCode create_signature(string secretid, bytes message, KnownSignatureType? sigtype, OnSecretBytesResult cb);
ErrorCode derive_aes_key_using_ecdh_1pu(KidOrJwkAdapted epk, KidOrJwkAdapted sk, KidOrJwkAdapted rk, bytes alg, bytes apu, bytes apv, bytes tag, boolean recive, OnSecretBytesResult cb);
ErrorCode derive_aes_key_using_ecdh_es(KidOrJwkAdapted epk, KidOrJwkAdapted rk, bytes alg, bytes apu, bytes apv, boolean recive, OnSecretBytesResult cb);
};

interface OnGetSecretResult {
interface OnGetKeyAlgResult {
[Throws=ErrorKind]
void success(Secret? result);
void success(KnownKeyAlg result);

[Throws=ErrorKind]
void error(ErrorKind err, string msg);
Expand All @@ -189,6 +192,14 @@ interface OnFindSecretsResult {
void error(ErrorKind err, string msg);
};

interface OnSecretBytesResult {
[Throws=ErrorKind]
void success(bytes result);

[Throws=ErrorKind]
void error(ErrorKind err, string msg);
};

dictionary Secret {
string id;
SecretType type_;
Expand All @@ -212,10 +223,33 @@ enum SecretType {
"Other",
};

interface ExampleSecretsResolver {
enum KnownKeyAlg {
"Ed25519",
"X25519",
"P256",
"K256",
"Unsupported",
};

enum KnownSignatureType {
"EdDSA",
"ES256",
"ES256K",
};

[Enum]
interface KidOrJwkAdapted {
Kid(string kid);
Jwk(string jwk);
};

interface ExampleKMS {
constructor(sequence<Secret> known_secrets);
ErrorCode get_secret(string secret_id, OnGetSecretResult cb);
ErrorCode get_key_alg(string secret_id, OnGetKeyAlgResult cb);
ErrorCode find_secrets(sequence<string> secret_ids, OnFindSecretsResult cb);
ErrorCode create_signature(string secretid, bytes message, KnownSignatureType? sigtype, OnSecretBytesResult cb);
ErrorCode derive_aes_key_using_ecdh_1pu(KidOrJwkAdapted epk, KidOrJwkAdapted sk, KidOrJwkAdapted rk, bytes alg, bytes apu, bytes apv, bytes tag, boolean recive, OnSecretBytesResult cb);
ErrorCode derive_aes_key_using_ecdh_es(KidOrJwkAdapted epk, KidOrJwkAdapted rk, bytes alg, bytes apu, bytes apv, boolean recive, OnSecretBytesResult cb);
};


Expand Down
19 changes: 6 additions & 13 deletions uniffi/src/didcomm/from_prior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::DIDComm;

use crate::common::{ErrorCode, EXECUTOR};
use crate::did_resolver_adapter::DIDResolverAdapter;
use crate::secrets_resolver_adapter::SecretsResolverAdapter;
use crate::kms_adapter::KeyManagementServiceAdapter;

pub trait OnFromPriorPackResult: Sync + Send {
fn success(&self, from_prior_jwt: String, kid: String);
Expand All @@ -25,12 +25,9 @@ impl DIDComm {
) -> ErrorCode {
let msg = msg.clone();
let did_resolver = DIDResolverAdapter::new(self.did_resolver.clone());
let secret_resolver = SecretsResolverAdapter::new(self.secret_resolver.clone());
let kms = KeyManagementServiceAdapter::new(self.kms.clone());

let future = async move {
msg.pack(issuer_kid.as_deref(), &did_resolver, &secret_resolver)
.await
};
let future = async move { msg.pack(issuer_kid.as_deref(), &did_resolver, &kms).await };
EXECUTOR.spawn_ok(async move {
match future.await {
Ok((from_prior_jwt, kid)) => cb.success(from_prior_jwt, kid),
Expand Down Expand Up @@ -66,7 +63,7 @@ mod tests {

use crate::{
test_helper::{create_did_resolver, get_ok, FromPriorPackResult, FromPriorUnpackResult},
DIDComm, ExampleSecretsResolver,
DIDComm, ExampleKMS,
};
use didcomm_core::test_vectors::{
ALICE_DID, CHARLIE_DID, CHARLIE_ROTATED_TO_ALICE_SECRETS, CHARLIE_SECRET_AUTH_KEY_ED25519,
Expand All @@ -80,9 +77,7 @@ mod tests {

DIDComm::new(
create_did_resolver(),
Box::new(ExampleSecretsResolver::new(
CHARLIE_ROTATED_TO_ALICE_SECRETS.clone(),
)),
Box::new(ExampleKMS::new(CHARLIE_ROTATED_TO_ALICE_SECRETS.clone())),
)
.pack_from_prior(
&from_prior,
Expand All @@ -99,9 +94,7 @@ mod tests {
let (cb, receiver) = FromPriorPackResult::new();
let did_comm = DIDComm::new(
create_did_resolver(),
Box::new(ExampleSecretsResolver::new(
CHARLIE_ROTATED_TO_ALICE_SECRETS.clone(),
)),
Box::new(ExampleKMS::new(CHARLIE_ROTATED_TO_ALICE_SECRETS.clone())),
);

let from_prior = FromPrior::build(CHARLIE_DID.into(), ALICE_DID.into())
Expand Down
11 changes: 4 additions & 7 deletions uniffi/src/didcomm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ pub use unpack::OnUnpackResult;

use std::sync::Arc;

use crate::{DIDResolver, SecretsResolver};
use crate::{DIDResolver, KeyManagementService};

pub struct DIDComm {
did_resolver: Arc<Box<dyn DIDResolver>>,
secret_resolver: Arc<Box<dyn SecretsResolver>>,
kms: Arc<Box<dyn KeyManagementService>>,
}

impl DIDComm {
pub fn new(
did_resolver: Box<dyn DIDResolver>,
secret_resolver: Box<dyn SecretsResolver>,
) -> Self {
pub fn new(did_resolver: Box<dyn DIDResolver>, kms: Box<dyn KeyManagementService>) -> Self {
DIDComm {
did_resolver: Arc::new(did_resolver),
secret_resolver: Arc::new(secret_resolver),
kms: Arc::new(kms),
}
}
}
20 changes: 9 additions & 11 deletions uniffi/src/didcomm/pack_encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use didcomm_core::{Message, PackEncryptedMetadata, PackEncryptedOptions};

use crate::common::{ErrorCode, EXECUTOR};
use crate::did_resolver_adapter::DIDResolverAdapter;
use crate::secrets::secrets_resolver_adapter::SecretsResolverAdapter;
use crate::secrets::kms_adapter::KeyManagementServiceAdapter;
use crate::DIDComm;

pub trait OnPackEncryptedResult: Sync + Send {
Expand All @@ -24,15 +24,15 @@ impl DIDComm {
let msg = msg.clone();
let options = options.clone();
let did_resolver = DIDResolverAdapter::new(self.did_resolver.clone());
let secret_resolver = SecretsResolverAdapter::new(self.secret_resolver.clone());
let kms = KeyManagementServiceAdapter::new(self.kms.clone());

let future = async move {
msg.pack_encrypted(
&to,
from.as_deref(),
sign_by.as_deref(),
&did_resolver,
&secret_resolver,
&kms,
&options,
)
.await
Expand All @@ -50,9 +50,7 @@ impl DIDComm {

#[cfg(test)]
mod tests {
use crate::test_helper::{
create_did_resolver, create_secrets_resolver, get_error, get_ok, PackResult,
};
use crate::test_helper::{create_did_resolver, create_kms, get_error, get_ok, PackResult};
use crate::DIDComm;
use didcomm_core::error::ErrorKind;
use didcomm_core::test_vectors::{ALICE_DID, BOB_DID, MESSAGE_SIMPLE};
Expand All @@ -63,7 +61,7 @@ mod tests {
async fn pack_encrypted_works() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_encrypted(
DIDComm::new(create_did_resolver(), create_kms()).pack_encrypted(
&MESSAGE_SIMPLE,
String::from(BOB_DID),
Some(String::from(ALICE_DID)),
Expand All @@ -89,7 +87,7 @@ mod tests {

let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_encrypted(
DIDComm::new(create_did_resolver(), create_kms()).pack_encrypted(
&msg,
String::from("did:unknown:bob"),
Some(String::from(ALICE_DID)),
Expand All @@ -106,7 +104,7 @@ mod tests {
async fn pack_encrypted_works_did_url_not_found() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_encrypted(
DIDComm::new(create_did_resolver(), create_kms()).pack_encrypted(
&MESSAGE_SIMPLE,
String::from(format!("{}#unknown-fragment", BOB_DID)),
Some(String::from(ALICE_DID)),
Expand All @@ -123,7 +121,7 @@ mod tests {
async fn pack_encrypted_works_secret_not_found() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_encrypted(
DIDComm::new(create_did_resolver(), create_kms()).pack_encrypted(
&MESSAGE_SIMPLE,
String::from(BOB_DID),
Some(String::from(format!(
Expand All @@ -143,7 +141,7 @@ mod tests {
async fn pack_encrypted_works_illegal_argument() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_encrypted(
DIDComm::new(create_did_resolver(), create_kms()).pack_encrypted(
&MESSAGE_SIMPLE,
String::from("not-a-did"),
Some(String::from(ALICE_DID)),
Expand Down
5 changes: 2 additions & 3 deletions uniffi/src/didcomm/pack_plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ mod tests {

use crate::DIDComm;

use crate::test_helper::{create_did_resolver, create_secrets_resolver, get_ok, PackResult};
use crate::test_helper::{create_did_resolver, create_kms, get_ok, PackResult};
use didcomm_core::test_vectors::MESSAGE_SIMPLE;

#[tokio::test]
async fn pack_plaintext_works() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver())
.pack_plaintext(&MESSAGE_SIMPLE, cb);
DIDComm::new(create_did_resolver(), create_kms()).pack_plaintext(&MESSAGE_SIMPLE, cb);

let res = get_ok(receiver).await;
assert!(res.contains("body"));
Expand Down
23 changes: 9 additions & 14 deletions uniffi/src/didcomm/pack_signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use didcomm_core::{error::ErrorKind, PackSignedMetadata};

use crate::common::{ErrorCode, EXECUTOR};
use crate::did_resolver_adapter::DIDResolverAdapter;
use crate::secrets::secrets_resolver_adapter::SecretsResolverAdapter;
use crate::secrets::kms_adapter::KeyManagementServiceAdapter;
use crate::DIDComm;

pub trait OnPackSignedResult: Sync + Send {
Expand All @@ -20,12 +20,9 @@ impl DIDComm {
) -> ErrorCode {
let msg = msg.clone();
let did_resolver = DIDResolverAdapter::new(self.did_resolver.clone());
let secret_resolver = SecretsResolverAdapter::new(self.secret_resolver.clone());
let kms = KeyManagementServiceAdapter::new(self.kms.clone());

let future = async move {
msg.pack_signed(&sign_by, &did_resolver, &secret_resolver)
.await
};
let future = async move { msg.pack_signed(&sign_by, &did_resolver, &kms).await };

EXECUTOR.spawn_ok(async move {
match future.await {
Expand All @@ -44,9 +41,7 @@ mod tests {
use didcomm_core::Message;
use serde_json::json;

use crate::test_helper::{
create_did_resolver, create_secrets_resolver, get_error, get_ok, PackResult,
};
use crate::test_helper::{create_did_resolver, create_kms, get_error, get_ok, PackResult};
use crate::DIDComm;

use didcomm_core::test_vectors::{ALICE_DID, MESSAGE_SIMPLE};
Expand All @@ -55,7 +50,7 @@ mod tests {
async fn pack_signed_works() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_signed(
DIDComm::new(create_did_resolver(), create_kms()).pack_signed(
&MESSAGE_SIMPLE,
String::from(ALICE_DID),
cb,
Expand All @@ -78,7 +73,7 @@ mod tests {

let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_signed(
DIDComm::new(create_did_resolver(), create_kms()).pack_signed(
&msg,
String::from("did:unknown:alice"),
cb,
Expand All @@ -92,7 +87,7 @@ mod tests {
async fn pack_signed_works_did_url_not_found() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_signed(
DIDComm::new(create_did_resolver(), create_kms()).pack_signed(
&MESSAGE_SIMPLE,
String::from(format!("{}#unknown-fragment", ALICE_DID)),
cb,
Expand All @@ -106,7 +101,7 @@ mod tests {
async fn pack_signed_works_secret_not_found() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_signed(
DIDComm::new(create_did_resolver(), create_kms()).pack_signed(
&MESSAGE_SIMPLE,
String::from(format!("{}#key-not-in-secrets-1", ALICE_DID)),
cb,
Expand All @@ -120,7 +115,7 @@ mod tests {
async fn pack_signed_works_illegal_argument() {
let (cb, receiver) = PackResult::new();

DIDComm::new(create_did_resolver(), create_secrets_resolver()).pack_signed(
DIDComm::new(create_did_resolver(), create_kms()).pack_signed(
&MESSAGE_SIMPLE,
String::from("not-a-did"),
cb,
Expand Down
Loading

0 comments on commit 0d7c53c

Please sign in to comment.