Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mtxclient
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
mtxclient
Commits
0c1b84ed
Commit
0c1b84ed
authored
5 years ago
by
Nicolas Werner
Browse files
Options
Downloads
Patches
Plain Diff
Add encrypted file struct
parent
4d518dbb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/mtx/common.hpp
+41
-0
41 additions, 0 deletions
include/mtx/common.hpp
lib/structs/common.cpp
+40
-0
40 additions, 0 deletions
lib/structs/common.cpp
tests/crypto.cpp
+31
-0
31 additions, 0 deletions
tests/crypto.cpp
with
112 additions
and
0 deletions
include/mtx/common.hpp
+
41
−
0
View file @
0c1b84ed
...
@@ -54,5 +54,46 @@ from_json(const json &obj, DeviceKeys &res);
...
@@ -54,5 +54,46 @@ from_json(const json &obj, DeviceKeys &res);
void
void
to_json
(
json
&
obj
,
const
DeviceKeys
&
res
);
to_json
(
json
&
obj
,
const
DeviceKeys
&
res
);
struct
JWK
{
//! Required. Key type. Must be oct.
std
::
string
kty
;
//! Required. Key operations. Must at least contain encrypt and decrypt.
std
::
vector
<
std
::
string
>
key_ops
;
//! Required. Algorithm. Must be A256CTR.
std
::
string
alg
;
//! Required. The key, encoded as urlsafe unpadded base64.
std
::
string
k
;
//! Required. Extractable. Must be true. This is a W3C extension.
bool
ext
;
};
void
from_json
(
const
json
&
obj
,
JWK
&
res
);
void
to_json
(
json
&
obj
,
const
JWK
&
res
);
struct
EncryptedFile
{
//! Required. The URL to the file.
std
::
string
url
;
//! Required. A JSON Web Key object. (The encryption key)
JWK
key
;
//! Required. The Initialisation Vector used by AES-CTR, encoded as unpadded base64.
std
::
string
iv
;
//! Required. A map from an algorithm name to a hash of the ciphertext, encoded as unpadded
//! base64. Clients should support the SHA-256 hash, which uses the key sha256.
std
::
map
<
std
::
string
,
std
::
string
>
hashes
;
//! Required. Version of the encrypted attachments protocol. Must be v2.
std
::
string
v
;
};
void
from_json
(
const
json
&
obj
,
EncryptedFile
&
res
);
void
to_json
(
json
&
obj
,
const
EncryptedFile
&
res
);
}
// namespace crypto
}
// namespace crypto
}
// namespace mtx
}
// namespace mtx
This diff is collapsed.
Click to expand it.
lib/structs/common.cpp
+
40
−
0
View file @
0c1b84ed
...
@@ -44,5 +44,45 @@ to_json(json &obj, const DeviceKeys &res)
...
@@ -44,5 +44,45 @@ to_json(json &obj, const DeviceKeys &res)
if
(
!
res
.
unsigned_info
.
device_display_name
.
empty
())
if
(
!
res
.
unsigned_info
.
device_display_name
.
empty
())
obj
[
"unsigned"
]
=
res
.
unsigned_info
;
obj
[
"unsigned"
]
=
res
.
unsigned_info
;
}
}
void
from_json
(
const
json
&
obj
,
JWK
&
res
)
{
res
.
kty
=
obj
.
at
(
"kty"
).
get
<
std
::
string
>
();
res
.
key_ops
=
obj
.
at
(
"key_ops"
).
get
<
std
::
vector
<
std
::
string
>>
();
res
.
alg
=
obj
.
at
(
"alg"
).
get
<
std
::
string
>
();
res
.
k
=
obj
.
at
(
"k"
).
get
<
std
::
string
>
();
res
.
ext
=
obj
.
at
(
"ext"
).
get
<
bool
>
();
}
void
to_json
(
json
&
obj
,
const
JWK
&
res
)
{
obj
[
"kty"
]
=
res
.
kty
;
obj
[
"key_ops"
]
=
res
.
key_ops
;
obj
[
"alg"
]
=
res
.
alg
;
obj
[
"k"
]
=
res
.
k
;
obj
[
"ext"
]
=
res
.
ext
;
}
void
from_json
(
const
json
&
obj
,
EncryptedFile
&
res
)
{
res
.
url
=
obj
.
at
(
"url"
).
get
<
std
::
string
>
();
res
.
key
=
obj
.
at
(
"key"
).
get
<
JWK
>
();
res
.
iv
=
obj
.
at
(
"iv"
).
get
<
std
::
string
>
();
res
.
hashes
=
obj
.
at
(
"hashes"
).
get
<
std
::
map
<
std
::
string
,
std
::
string
>>
();
res
.
v
=
obj
.
at
(
"v"
).
get
<
std
::
string
>
();
}
void
to_json
(
json
&
obj
,
const
EncryptedFile
&
res
)
{
obj
[
"url"
]
=
res
.
url
;
obj
[
"key"
]
=
res
.
key
;
obj
[
"iv"
]
=
res
.
iv
;
obj
[
"hashes"
]
=
res
.
hashes
;
obj
[
"v"
]
=
res
.
v
;
}
}
}
}
}
This diff is collapsed.
Click to expand it.
tests/crypto.cpp
+
31
−
0
View file @
0c1b84ed
...
@@ -62,3 +62,34 @@ TEST(Crypto, DeviceKeys)
...
@@ -62,3 +62,34 @@ TEST(Crypto, DeviceKeys)
EXPECT_EQ
(
device2
.
unsigned_info
.
device_display_name
,
"Alice's mobile phone"
);
EXPECT_EQ
(
device2
.
unsigned_info
.
device_display_name
,
"Alice's mobile phone"
);
}
}
TEST
(
Crypto
,
EncryptedFile
)
{
json
j
=
R"({
"url": "mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe",
"v": "v2",
"key": {
"alg": "A256CTR",
"ext": true,
"k": "aWF6-32KGYaC3A_FEUCk1Bt0JA37zP0wrStgmdCaW-0",
"key_ops": ["encrypt","decrypt"],
"kty": "oct"
},
"iv": "w+sE15fzSc0AAAAAAAAAAA",
"hashes": {
"sha256": "fdSLu/YkRx3Wyh3KQabP3rd6+SFiKg5lsJZQHtkSAYA"
}})"
_json
;
EncryptedFile
file
=
j
;
// json j2 = file;
// EXPECT_EQ(j, j2);
EXPECT_EQ
(
file
.
v
,
"v2"
);
EXPECT_EQ
(
file
.
iv
,
"w+sE15fzSc0AAAAAAAAAAA"
);
EXPECT_EQ
(
file
.
hashes
.
at
(
"sha256"
),
"fdSLu/YkRx3Wyh3KQabP3rd6+SFiKg5lsJZQHtkSAYA"
);
EXPECT_EQ
(
file
.
key
.
alg
,
"A256CTR"
);
EXPECT_EQ
(
file
.
key
.
ext
,
true
);
EXPECT_EQ
(
file
.
key
.
k
,
"aWF6-32KGYaC3A_FEUCk1Bt0JA37zP0wrStgmdCaW-0"
);
EXPECT_EQ
(
file
.
key
.
key_ops
.
size
(),
2
);
EXPECT_EQ
(
file
.
key
.
kty
,
"oct"
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment