@@ -47,24 +47,34 @@ def __init__(self, host: MultihostHost, fs: LinuxFileSystem, svc: SystemdService
4747 self .svc : SystemdServices = svc
4848 """Systemd utility to manage and interact with svc."""
4949
50- def initialize_card (self , label : str = "sc_test" , so_pin : str = "12345678" , user_pin : str = "123456" ) -> None :
50+ def initialize_card (
51+ self ,
52+ label : str = "sc_test" ,
53+ so_pin : str = "12345678" ,
54+ user_pin : str = "123456" ,
55+ reset : bool = True ,
56+ ) -> None :
5157 """
52- Initializes a SoftHSM token with the given label and PINs.
58+ Initialize a SoftHSM token with the given label and PINs.
5359
54- Cleans cache directories and prepares the token directory.
60+ When *reset* is ``True`` (default), existing token storage and OpenSC
61+ caches are removed first. Pass ``False`` to add a token alongside
62+ existing ones (multi-token / multi-card setup).
5563
5664 :param label: Token label, defaults to "sc_test"
5765 :type label: str, optional
5866 :param so_pin: Security Officer PIN, defaults to "12345678"
5967 :type so_pin: str, optional
6068 :param user_pin: User PIN, defaults to "123456"
6169 :type user_pin: str, optional
70+ :param reset: Remove existing tokens before initializing, defaults to True
71+ :type reset: bool, optional
6272 """
63- for path in self . OPENSC_CACHE_PATHS :
64- self .fs . rm ( path )
65-
66- self .fs .rm (self .TOKEN_STORAGE_PATH )
67- self .fs .mkdir_p (self .TOKEN_STORAGE_PATH )
73+ if reset :
74+ for path in self .OPENSC_CACHE_PATHS :
75+ self . fs . rm ( path )
76+ self .fs .rm (self .TOKEN_STORAGE_PATH )
77+ self .fs .mkdir_p (self .TOKEN_STORAGE_PATH )
6878
6979 args : CLIBuilderArgs = {
7080 "label" : (self .cli .option .VALUE , label ),
@@ -82,6 +92,8 @@ def add_cert(
8292 cert_id : str = "01" ,
8393 pin : str = "123456" ,
8494 private : bool | None = False ,
95+ token_label : str | None = None ,
96+ label : str | None = None ,
8597 ) -> None :
8698 """
8799 Adds a certificate or private key to the smart card.
@@ -94,6 +106,15 @@ def add_cert(
94106 :type pin: str, optional
95107 :param private: Whether the object is a private key. Defaults to False.
96108 :type private: bool, optional
109+ :param token_label: Label of the target token. When ``None`` (the
110+ default) ``pkcs11-tool`` writes to the first available token.
111+ Set this when multiple tokens exist to target a specific one.
112+ :type token_label: str | None, optional
113+ :param label: Label for the PKCS#11 object being written. Required
114+ when ``p11_child`` accesses the token directly (i.e. without
115+ ``virt_cacard``), because the response parser expects a
116+ non-empty label.
117+ :type label: str | None, optional
97118 """
98119 obj_type = "privkey" if private else "cert"
99120 args : CLIBuilderArgs = {
@@ -104,9 +125,20 @@ def add_cert(
104125 "type" : (self .cli .option .VALUE , obj_type ),
105126 "id" : (self .cli .option .VALUE , cert_id ),
106127 }
128+ if token_label is not None :
129+ args ["token-label" ] = (self .cli .option .VALUE , token_label )
130+ if label is not None :
131+ args ["label" ] = (self .cli .option .VALUE , label )
107132 self .host .conn .run (self .cli .command ("pkcs11-tool" , args ), env = {"SOFTHSM2_CONF" : self .SOFTHSM2_CONF_PATH })
108133
109- def add_key (self , key_path : str , key_id : str = "01" , pin : str = "123456" ) -> None :
134+ def add_key (
135+ self ,
136+ key_path : str ,
137+ key_id : str = "01" ,
138+ pin : str = "123456" ,
139+ token_label : str | None = None ,
140+ label : str | None = None ,
141+ ) -> None :
110142 """
111143 Adds a private key to the smart card.
112144
@@ -116,8 +148,12 @@ def add_key(self, key_path: str, key_id: str = "01", pin: str = "123456") -> Non
116148 :type key_id: str, optional
117149 :param pin: User PIN, defaults to "123456"
118150 :type pin: str, optional
151+ :param token_label: Label of the target token (see :meth:`add_cert`).
152+ :type token_label: str | None, optional
153+ :param label: Label for the PKCS#11 object (see :meth:`add_cert`).
154+ :type label: str | None, optional
119155 """
120- self .add_cert (cert_path = key_path , cert_id = key_id , pin = pin , private = True )
156+ self .add_cert (cert_path = key_path , cert_id = key_id , pin = pin , private = True , token_label = token_label , label = label )
121157
122158 def generate_cert (
123159 self ,
0 commit comments