bovine.crypto.types
The aim of these two classes is to encapsulate the usage of public and private keys in the Fediverse. We will now touch on how these are used. This is not meant as an introduction to public key cryptography, but more as a refresher and explainer how things are intended to be used.
Signing a document
flowchart LR
A --> D[key_id]
A[CryptographicSecret] -->|sign| B[signature]
C[document] --> B
B --> E[message]
C --> E
D --> E
This diagram illustrates that given a document, one generates the signature
using a CryptographicSecret. The
triple (document, signature, key_id)
then forms the signed message.
Verifying a document
flowchart LR
A[message] -->|key_id| B[CryptographicIdentifier]
A -->|signature| C[controller]
B -->|verify| C
C -->|is by?| D
A --> D[document]
Given a signed message, i.e. a triple (document, signature, key_id)
, one can
verify its integrity by following the process shown in the diagram above.
First, one uses the key_id
to retrieve the CryptographicIdentifier,
e.g. in the Fediverse as attached to the actor profile. Then one uses this
identifier and the signature, to obtain the controller
, i.e. the person
who signed the message. If this matches the claimed authorship of the
document, all is well.
The methods from_public_key and from_multikey are meant to make importing CryptographicIdentifiers form an actor profile easier.
bovine.crypto.types
CryptographicIdentifier
dataclass
Represents a cryptographic identifier. The usage is: If an object is
signed by public_key
, then it is authored by controller
. In order
to discover which CryptographicIdentifier
to use, one resolves another
identifier key_id
, which yields either a Multikey or a publicKey object, which
can then be resolved into a CryptographicIdentifier.
One should never need to directly access the properties of this class, instead verify returns the controller, if and only if the signature is valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
controller
|
str
|
The URI of the actor that controls the public key |
required |
public_key
|
Ed25519PublicKey | RSAPublicKey | EllipticCurvePublicKey
|
Public key used to verify signatures |
required |
Source code in bovine/bovine/crypto/types.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
as_tuple
from_did_key
classmethod
from_did_key(did_key: str)
Creates a cryptographic identifier from a did:key The controller is then the did:key and the public key the encoded public key. See The did:key Method for details. Currently supported: Ed25519, RSA, P-256, and P-384 keys.
>>> identifier = CryptographicIdentifier.from_did_key("did:key:z6MkekwC6R9bj9ErToB7AiZJfyCSDhaZe1UxhDbCqJrhqpS5")
>>> identifier.controller
'did:key:z6MkekwC6R9bj9ErToB7AiZJfyCSDhaZe1UxhDbCqJrhqpS5'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
did_key
|
str
|
The did key |
required |
Source code in bovine/bovine/crypto/types.py
from_multikey
classmethod
from_multikey(multikey: dict)
Creates a CryptographicIdentifier from a Multikey, see
Example:
>>> identifier = CryptographicIdentifier.from_multikey({
... "id": "https://server.example/users/alice#ed25519-key",
... "type": "Multikey",
... "controller": "https://server.example/users/alice",
... "publicKeyMultibase": "z6MkrJVnaZkeFzdQyMZu1cgjg7k1pZZ6pvBQ7XJPt4swbTQ2"
... })
>>> identifier.controller
'https://server.example/users/alice'
Source code in bovine/bovine/crypto/types.py
from_pem
classmethod
Creates a CryptographicIdentifier from a pem encoded public key and the controller
>>> public_key_pem = '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA15vhFdK272bbGDzLtypo\n4Nn8mNFY3YSLnrAOX4zZKkNmWmgypgDP8qXjNiVsBf8f+Yk3tHDs58LMf8QDSP09\nA+zrlWBHN1rLELn0JBgqT9xj8WSobDIjOjFBAy4FKUko7k/IsYwTl/Vnx1tykhPR\n1UzbaNqN1yQSy0zGbIce/Xhqlzm6u+twyuHVCtbGPcPh7for5o0avKdMwhAXpWMr\nNoc9L2L/\n9h3UgoePgAvCE6HTPXEBPesUBlTULcRxMXIZJ7P6eMkb2pGUCDlVF4EN\nvcxZAG8Pb7HQp9nzVwK4OXZclKsH1YK0G8oBGTxnroBtq7cJbrJvqNMNOO5Yg3cu\n6QIDAQAB\n-----END PUBLIC KEY-----'
>>> identifier = CryptographicIdentifier.from_pem(public_key_pem, 'https://com.example/issuer/123')
>>> identifier.controller
'https://com.example/issuer/123'
Source code in bovine/bovine/crypto/types.py
from_public_key
classmethod
from_public_key(data: dict)
Creates a Cryptographic identifier from a publicKey object, example:
>>> public_key = {
... "id": "https://com.example/issuer/123#main-key",
... "owner": "https://com.example/issuer/123",
... "publicKeyPem": '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA15vhFdK272bbGDzLtypo\n4Nn8mNFY3YSLnrAOX4zZKkNmWmgypgDP8qXjNiVsBf8f+Yk3tHDs58LMf8QDSP09\nA+zrlWBHN1rLELn0JBgqT9xj8WSobDIjOjFBAy4FKUko7k/IsYwTl/Vnx1tykhPR\n1UzbaNqN1yQSy0zGbIce/Xhqlzm6u+twyuHVCtbGPcPh7for5o0avKdMwhAXpWMr\nNoc9L2L/\n9h3UgoePgAvCE6HTPXEBPesUBlTULcRxMXIZJ7P6eMkb2pGUCDlVF4EN\nvcxZAG8Pb7HQp9nzVwK4OXZclKsH1YK0G8oBGTxnroBtq7cJbrJvqNMNOO5Yg3cu\n6QIDAQAB\n-----END PUBLIC KEY-----'
... }
>>> identifier = CryptographicIdentifier.from_public_key(public_key)
>>> identifier.controller
'https://com.example/issuer/123'
Source code in bovine/bovine/crypto/types.py
from_tuple
classmethod
Creates a CryptographicIdentifier from a tuple
Parameters:
Name | Type | Description | Default |
---|---|---|---|
controller
|
str
|
The controller URI |
required |
multibase_public_key
|
str
|
The public key encoded using multibase/multicodex |
required |
Source code in bovine/bovine/crypto/types.py
verify
Verifies that signature
is a correct signature for the given message.
Warning: Interface might change, to enable specifying encoding of the signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str | bytes
|
The message string. |
required |
signature
|
str | bytes
|
The signature |
required |
Returns:
Type | Description |
---|---|
str | None
|
If the signature is valid the corresponding controller, otherwise null. |
Source code in bovine/bovine/crypto/types.py
verify_document
verify_document(document: dict)
Verifies that document has a valid signature according to FEP-8b32. We note that in order to verify a document signed using FEP-8b32, one will already need to parse it sufficiently to extract the controller, so the CryptographicIdentifier can be created.
Beware: Verification with P-386 keys might be broken.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
document
|
dict
|
The document to verify |
required |
Source code in bovine/bovine/crypto/types.py
CryptographicSecret
dataclass
Represents a cryptographic secret. Such a secret is composed of the private key and the URI that resolves to the material, one can construct the appropriate cryptographic identifier from.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key_id
|
str
|
The URI, where the corresponding public key and controller can be retrieved |
required |
private_key
|
Ed25519PrivateKey | RSAPrivateKey
|
The signing material |
required |
Source code in bovine/bovine/crypto/types.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
from_multibase
classmethod
Creates a CryptographicSecret from multibase encoded Ed25519 private key and key_id
from_pem
classmethod
Creates a CryptographicSecret from a PEM encoded private key
sign
Signs the message.
Currently only implemented for RSA: Uses PKCS1v15 padding and SHA256 hashes. Returns the signature as base64 encoded.
Warning: Interface might change, to enable specifying encoding of the signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str | bytes
|
The message to sign as UTF-8 encoded string. |
required |
Source code in bovine/bovine/crypto/types.py
sign_document
Signs the current document according to the procedure
outlined in FEP-8b32. We support signing with
eddsa-jcs-2022 / eddsa-rdfc-2022
and ecdsa-jcs-2019 / eddsa-edfc-2019.
The cryptosuite is chosen depending on the type of signing material
and the use_rdfc
parameter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
document
|
dict
|
The document to sign |
required |
use_rdfc
|
bool
|
Set to |
False
|
proof_purpose
|
Purpose of the proof |
'assertionMethod'
|