obadgen 0.2.3

Generates (aka "bakes") basic Open Badge annotated images (lib & CLI-tool).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
169
170
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!--
SPDX-FileCopyrightText: 2021 - 2023 Robin Vobruba <hoijui.quaero@gmail.com>

SPDX-License-Identifier: CC0-1.0
-->

# _O_pen-_Bad_ges _Gen_erator (`obadgen`)

[![License: AGPL-3.0-or-later](
    https://img.shields.io/badge/License-AGPL%203.0+-blue.svg)](
    LICENSE.txt)
[![REUSE status](
    https://api.reuse.software/badge/github.com/hoijui/obadgen)](
    https://api.reuse.software/info/github.com/hoijui/obadgen)
[![Repo](
    https://img.shields.io/badge/Repo-GitHub-555555&logo=github.svg)](
    https://github.com/hoijui/obadgen)
[![Package Releases](
    https://img.shields.io/crates/v/obadgen.svg)](
    https://crates.io/crates/obadgen)
[![Documentation Releases](
    https://docs.rs/obadgen/badge.svg)](
    https://docs.rs/obadgen)
[![Dependency Status](
    https://deps.rs/repo/github/hoijui/obadgen/status.svg)](
    https://deps.rs/repo/github/hoijui/obadgen)
[![Build Status](
    https://github.com/hoijui/obadgen/workflows/build/badge.svg)](
    https://github.com/hoijui/obadgen/actions)

[![In cooperation with Open Source Ecology Germany](
    https://raw.githubusercontent.com/osegermany/tiny-files/master/res/media/img/badge-oseg.svg)](
    https://opensourceecology.de)

`obadgen` is a CLI ([Command-line Interface](
https://en.wikipedia.org/wiki/Command-line_interface)) tool,
which helps in generating ["baking"](
https://github.com/mozilla/openbadges-backpack/wiki/Badge-Baking)
[Open Badge](https://openbadges.org/) 2.0 (latest release as of Nov. 2023)
annotated images.

## Usage

To use this tool, you need some prerequisites:

- This tool itsself
- an **input badge image** in SVG or PNG format
  ("unbaked"/without Open Badge meta-data)
- an Open Badge **Assertion file** in JSON-LD format;
  see [Assertion Content]#assertion-content
- (optional) if a [signed]
  https://github.com/mozilla/openbadges-specification/blob/master/Assertion/latest.md#signed
  instead of a [hosted]https://github.com/mozilla/openbadges-specification/blob/master/Assertion/latest.md#hosted
  asssertion is supplied,
  you need to supply a **DER encoded private key**,
  prefferably from an x.509 certificate.
  See [Certificate or Key-Pair]#certificate-or-key-pair
- (optional) [OpenSSL]https://www.openssl.org/'s CLI tool `openssl`,
  which allows you to [generate]#generate and [convert]#convert
  cryptographic certificates and keys.

## Assertion Content

In Open Badg 2.0,
the assertion JSON-LD content can either be:

- _hosted_, which means it has to be hosted under the URL under its ID,
  which should be a location under the control of the issuing party
- _signed_, which means it has to be cryptographically signed with a key
  (prefferably from an x.509 certificate)
  under the control of the issuing party,
  but it may be hosted/stored anywhere after that

Also see [Full Example](#full-example) further down.

### Assertion Examples

#### Hosted

[res/ob-ents/badge-assertion-simple.json](
res/ob-ents/badge-assertion-simple.json):

```json
{
  "@context": "https://w3id.org/openbadges/v2",
  "type": "Assertion",
  "id": "https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/badge-assertion-simple.json",
  "badge": "https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/badge-definition-simple.json",
  "recipient": {
    "type": "email",
    "identity": "sha256$488842626ec74a0468d90ea17dc4e11c2d0e8e54e45c5075fbd1d2e767f44249",
    "hashed": true
  },
  "verification": {
    "type": "HostedBadge"
  },
  "issuedOn": "2022-06-17T23:59:59Z",
  "expires": "2099-06-30T23:59:59Z"
}
```

#### Signed

[res/ob-ents/badge-assertion-with-key.json](
res/ob-ents/badge-assertion-with-key.json):

```json
{
  "@context": "https://w3id.org/openbadges/v2",
  "type": "Assertion",
  "id": "https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/badge-assertion-with-key.json",
  "badge": "https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/badge-definition-with-key.json",
  "recipient": {
    "type": "email",
    "identity": "sha256$a596420c9e6e6d4a7a552b2073a3b6c28807d808b37c97454ab4e419c9e5e4ad",
    "hashed": true,
    "salt": "dfvnk0923#%^&87t6iubasr"
  },
  "verification": {
    "type": "SignedBadge",
    "creator": "https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/issuer-with-key.json"
  },
  "issuedOn": "2022-06-17T23:59:59Z",
  "expires": "2099-06-30T23:59:59Z"
}
```

### Bakeing

Both the examples below create the file _baked-badge.svg_.

Example for a _hosted_ badge:

```shell
obadgen \
    --assertion assertion.json \
    --source-image raw-badge.svg \
    --baked baked-badge.svg
```

Example for a _signed_ badge:

```shell
obadgen \
    --assertion assertion.json \
    --signing-algorithm es256 \
    --key my_organization.x509_cert.priv_key.der \
    --source-image raw-badge.svg \
    --baked baked-badge.svg
```

### Full Example

Here we create a badge assertion,
which we then sign and bake into an SVG.
This SVG can thereafter be stored and verified anywhere.

```shell
PRIV_KEY="res/ob-ents/issuer-key.priv.der"
ALG="es256"
ISSUER_ID="https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/issuer-with-key.json"
BADGE_CLASS_ID="https://raw.githubusercontent.com/hoijui/obadgen/master/res/ob-ents/badge-definition-with-key.json"
ASSERTION_ID="https://some-domain.com/anywhere/does-not-even-have-to-exist/because-signed/badge-assertion-with-key.json"
EMAIL="recipient@email.com"
RECIPIENT_SALT="dfvnk097t6iubasr"
IDENTITY_HASH="sha256\$$(printf '%s%s' "$EMAIL" "$RECIPIENT_SALT" | sha256sum - | sed -e 's/ .*//')"
DATE_NOW="$(date --iso-8601=seconds)"
DATE_FUTURE="$(date --iso-8601=seconds --date="2099-12-30")"
ASSERTION_FILE_PATH="assertion.json"
IMG_EXT="svg"
#IMG_EXT="png"
SOURCE_IMAGE_BASE="res/media/img/test"

# Writing the badge asserion JSON-LD file
cat > "$ASSERTION_FILE_PATH" << EOF
{
  "@context": "https://w3id.org/openbadges/v2",
  "type": "Assertion",
  "id": "$ASSERTION_ID",
  "badge": "$BADGE_CLASS_ID",
  "recipient": {
    "type": "email",
    "identity": "$IDENTITY_HASH",
    "hashed": true,
    "salt": "$RECIPIENT_SALT"
  },
  "verification": {
    "type": "SignedBadge",
    "creator": "$ISSUER_ID"
  },
  "issuedOn": "$DATE_NOW",
  "expires": "$DATE_FUTURE"
}
EOF

# Signs and bakes the assertion to baked-badge.svg
obadgen \
    --assertion "$ASSERTION_FILE_PATH" \
    --signing-algorithm "$ALG" \
    --key "$PRIV_KEY" \
    --source-image "$SOURCE_IMAGE_BASE.$IMG_EXT" \
    --baked "baked-badge.$IMG_EXT"
```

## Certificate or Key-Pair

If you desicde to sign your badge (vs simply hosting it)
as your verification method,
you will need a private-key.

It could come from a simple public-private key-pair,
or prefferably from an [x.509 certificate](
https://en.wikipedia.org/wiki/X.509),
both of which can be generated with OpenSSL (shown below),
if you do not yet have it.

The private-key you supply to this tool **must** be in [DER format](
https://wiki.openssl.org/index.php/DER).
If yours is in any other format (most likely [PEM](
https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail)),
you can convert it to DER suing OpenSSL (shown below).

Currently supported key-types are:

- RS256 - RSASSA-PKCS1-v1_5 using SHA-256
- RS384 - RSASSA-PKCS1-v1_5 using SHA-384
- RS512 - RSASSA-PKCS1-v1_5 using SHA-512
- ES256 - ECDSA using P-256 and SHA-256
- ES384 - ECDSA using P-384 and SHA-384

This list is what the library we use ([`biscuit`](
https://crates.io/crates/biscuit))
supports for [JSON Web Signature (JWS))](
http://self-issued.info/docs/draft-ietf-jose-json-web-signature.html),
which is what Open Badge signing requires.

In the following sub-sections,
you find how to [generate](#generate) and [convert](#convert)
certificates and keys.

For all these examples we use a common base (`FILE_BASE`)
for the certificates and keys file names,
extended with verbose suffixes.
We also provide the algorithm name (`ALG`)
as it has to be supplied to this tool later on. \
NOTE: In cryptographic practice,
each private-key file also contains its corresponding public-key. \

So in any case, you will either have:

- key-pair "package":
  - private file: contains the _private-_ and the _public-key_
  - public file: contains the _public-key_
- certificate "package":
  - private file: contains the _private-_ and the _public-key_
  - public file: contains the certificate with all its meta-data properties,
    one of which also is its _public-key_

### Generate

#### Certificate (preffered)

Both of the two exampels below,
create these two files:

- `my_organization.x509_cert.priv_key.der` (private file)
- `my_organization.x509_cert.cert.pem` (public file)

##### ECDSA Certificate

Create a self-signed x.509 certificate in PEM format
using `ES256` (ECDSA) key types,
with its private-key in DER format:

```shell
FILE_BASE="my_organization"
ALG="es256"
openssl req \
    -new \
    -x509 \
    -subj "/C=DE/ST=Berlin/L=Berlin/O=OSEG/OU=SW-Dev/CN=ose-germany.de/emailAddress=open-badges-123@ose-germany.de" \
    -sha256 \
    -nodes \
    -pkeyopt ec_paramgen_curve:prime256v1 \
    -newkey ec \
    -keyform DER \
    -keyout "$FILE_BASE.x509_cert.priv_key.der" \
    -days 730 \
    -outform PEM \
    -out "$FILE_BASE.x509_cert.cert.pem"
```

##### RSA Certificate

Create a self-signed x.509 certificate in PEM format
using `RS256` (RSA) key types,
with its private-key in DER format:

```shell
FILE_BASE="my_organization"
ALG="rs256"
openssl req \
    -new \
    -x509 \
    -subj "/C=DE/ST=Berlin/L=Berlin/O=OSEG/OU=SW-Dev/CN=ose-germany.de/emailAddress=open-badges-123@ose-germany.de" \
    -sha256 \
    -nodes \
    -newkey rsa:4096 \
    -keyform DER \
    -keyout "$FILE_BASE.x509_cert.priv_key.der" \
    -days 730 \
    -outform PEM \
    -out "$FILE_BASE.x509_cert.cert.pem"
```

#### Key-Pair

Both of the two exampels below,
create these two files:

- `my_organization.priv.der` (private file)
- `my_organization.pub.pem` (public file)

##### ECDSA Pair

Create an `ES256` (ECDSA) key pair,
with the private-key in DER format
and the public-key in PEM format:

```shell
FILE_BASE="my_organization"
ALG="es256"
openssl genpkey \
    -algorithm RSA \
    -pkeyopt rsa_keygen_bits:4096 \
    -outform der \
    -out "$FILE_BASE.priv.der"
```

##### RSA Pair

Create an `RS256` (RSA) key pair,
with the private-key in DER format
and the public-key in PEM format:

```shell
FILE_BASE="my_organization"
ALG="rs256"
# TODO FIXME This is still the certificate, not a key-pair only!
openssl req \
    -x509 \
    -sha256 \
    -nodes \
    -newkey rsa:4096 \
    -keyform DER \
    -keyout "$FILE_BASE.x509_cert.priv_key.der" \
    -days 730 \
    -outform PEM \
    -out "$FILE_BASE.x509_cert.cert.pem"
```

### Convert

#### RSA encoding

Converts an RSA _private_-key in PEM format
into the DER format:

```shell
FILE_BASE="my_organization"
openssl rsa \
    -inform PEM \
    -in "$FILE_BASE.pem" \
    -outform DER \
    -out "$FILE_BASE.der"
```

Converts an RSA _public_-key in DER format
into the PEM format:

```shell
FILE_BASE="my_organization"
openssl rsa \
    -RSAPublicKey_in \
    -inform PEM \
    -in "$FILE_BASE.pem" \
    -outform DER \
    -RSAPublicKey_out \
    -out "$FILE_BASE.der"
```

#### ECDSA encoding

Converts an ECDSA _private_-key in PEM format
into the DER format:

```shell
FILE_BASE="my_organization"
openssl ec \
    -inform PEM \
    -in "$FILE_BASE.pem" \
    -outform DER \
    -out "$FILE_BASE.der"
```

Converts an ECDSA _public_-key in DER format
into the PEM format:

```shell
FILE_BASE="my_organization"
openssl ec \
    -pubin \
    -inform PEM \
    -in "$FILE_BASE.pem" \
    -outform DER \
    -pubout \
    -out "$FILE_BASE.der"
```

#### Extract

Extracts the public key from an RSA private key-pair file
(here one from an x.509 certificate, but can be any other)
into a separate file,
and reformats that files content to be pasted directly
into an Open Badge 2.0 _assertion.json_ file:

```shell
FILE_BASE="my_organization"
openssl rsa \
    -in "$FILE_BASE.x509_cert.priv_key.der" \
    -inform DER \
    -RSAPublicKey_out \
    -outform PEM \
    -out "$FILE_BASE.x509_cert.pub_key.pem"
sed -e 's/\n/\\n/' "$FILE_BASE.x509_cert.pub_key.pem"
```

Visualizes certificate info for human eyes:

```shell
openssl x509 \
    -in "$FILE_BASE.x509_cert.cert.pem" \
    -inform PEM \
    -text
```