Skip to content

Commit

Permalink
Merge pull request #34 from SparkiDev/add_examples
Browse files Browse the repository at this point in the history
Examples: added some useful examples
  • Loading branch information
dgarske authored Nov 4, 2024
2 parents a1ddeba + 6369e45 commit e561ff1
Show file tree
Hide file tree
Showing 17 changed files with 3,657 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ coverage.info
tests/pkcs11test
tests/pkcs11mtt
tests/pkcs11str
examples/add_aes_key
examples/add_hmac_key
examples/add_rsa_key
examples/add_rsa_key_file
examples/init_token
examples/mech_info
examples/obj_list
examples/slot_info
examples/token_info
store/wp11*
test/*
*.gcda
*.gcno
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ACLOCAL_AMFLAGS= -I m4

include src/include.am
include wolfpkcs11/include.am
include examples/include.am
include tests/include.am
include IDE/include.am

Expand Down
163 changes: 163 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Examples of using PKCS#11 API

These examples show how to do common operations with a PKCS#11 token.

## init_token

Initialize the token and set the passwords for the Security Officer (SO) and
User.

```
./examples/init_token
```

Steps (see pkcs11_init_token):
1. Initialize token with Security Officer's (SO) Pin and a label using
C_InitToken.
2. Open a session on the token with C_OpenSession.
3. Login to the token with SO's PIN using C_Login
4. Initialize the User's PIN with C_InitPin


## slot_info

Show the slot information.

```
./examples/slot_info
```

pkcs11_slot_info() uses C_GetSlotInfo to get the data structure and prints the
slot's information as text.

## token_info

Show the token information.

```
./examples/token_info
```

pkcs11_token_info() uses C_GetTokenInfo to get the data structure and prints the
token's information as text.

## mech_info

Show information on mechanism of slot.

```
./examples/mech_info
```

pkcs11_mechs_info() shows all mechanisms available for a slot. Calls
C_GetMechanismList to get all available mechanisms.

pkcs11_mech_info() retrieves the mechanism's info with C_GetMechanismInfo and
prints it.


## add_aes_key

Adds an AES key to the PKCS#11 Store. Key will only available for the session
unless a private identifier is supplied.

Store in session:

```
./examples/add_aes_key
```

Store on token:

```
./examples/add_aes_key -privId <label>
```

pkcs11_add_aes_key() uses C_CreateObject to store the key in the PKCS#11 store.
Key type attribute should CKK_AES if supported.

## add_hmac_key

Adds an HMAC key to the PKCS#11 Store. Key will only available for the
session unless a private identifier is supplied.

Store in session:

```
./examples/add_hmac_key
```

Store on token:

```
./examples/add_hmac_key -privId <label>
```

pkcs11_add_hmac_key() uses C_CreateObject to store the key in the PKCS#11 store.
Key type attribute is CKK_GENERIC_SECRET.

## add_rsa_key

Adds an RSA key to the PKCS#11 Store. Key will only available for the
session unless a private identifier is supplied.

Store in session:

```
./examples/add_rsa_key
```

Store on token:

```
./examples/add_rsa_key -privId <label>
```

pkcs11_add_rsa_key() uses C_CreateObject to store the key in the PKCS#11 store.


## add_rsa_key_file

Adds an RSA key from a DER encoded file to the PKCS#11 Store. Key will only
available for the session unless a private identifier is supplied.

Store in session:

```
./examples/add_rsa_key_file -rsa <filename>
```

Store on token:

```
./examples/add_rsa_key_file -privId <label> -rsa <filename>
```

load_rsa_key() loads the DER encoded RSA private key from file into an RsaKey
object.

pkcs11_add_rsa_key() uses C_CreateObject to store the key in the PKCS#11 store.

export_mp() is used get the data for each of the private key components.


## obj_list

Lists the objects stored on a token.


```
./examples/obj_list
```

pkcs11_objs_attr() finds all objects using C_FindObjectsInit(), C_FindObjects()
and C_FindObjectsFinal() and prints them.

pkcs11_obj_attr() uses C_GetAttributeValue() to get the attribute values of an
object that is identified by its handle.

pkcs11_key_attr() uses C_GetAttributeValue() to get common attributes of keys.

pkcs11_rsa_attr() uses C_GetAttributeValue() to get the public fields of an RSA
key.

Loading

0 comments on commit e561ff1

Please sign in to comment.