summaryrefslogtreecommitdiff
path: root/doc/rst/legacy/ssl_functions/pkfnc/index.rst
blob: b71487dd7a28c7666d1ac5a47a5c6f321614b429 (plain)
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
.. _mozilla_projects_nss_ssl_functions_pkfnc:

pkfnc
=====

.. container::

   .. note::

      -  This page is part of the :ref:`mozilla_projects_nss_ssl_functions_old_ssl_reference` that
         we are migrating into the format described in the `MDN Style
         Guide <https://developer.mozilla.org/en-US/docs/MDN/Guidelines>`__. If you are inclined to
         help with this migration, your help would be very much appreciated.

      -  Upgraded documentation may be found in the :ref:`mozilla_projects_nss_reference`

   .. rubric:: PKCS #11 Functions
      :name: PKCS_11_Functions

   --------------

.. _chapter_7_pkcs_11_functions:

`Chapter 7
PKCS #11 Functions <#chapter_7_pkcs_11_functions>`__
----------------------------------------------------

.. container::

   This chapter describes the core PKCS #11 functions that an application needs for communicating
   with cryptographic modules. In particular, these functions are used for obtaining certificates,
   keys, and passwords.

   |  ```PK11_FindCertFromNickname`` <#1035673>`__
   | ```PK11_FindKeyByAnyCert`` <#1026891>`__
   | ```PK11_GetSlotName`` <#1030779>`__
   | ```PK11_GetTokenName`` <#1026964>`__
   | ```PK11_IsHW`` <#1026762>`__
   | ```PK11_IsPresent`` <#1022948>`__
   | ```PK11_IsReadOnly`` <#1022991>`__
   | ```PK11_SetPasswordFunc`` <#1023128>`__

   .. rubric:: PK11_FindCertFromNickname
      :name: pk11_findcertfromnickname

   Finds a certificate from its nickname.

   .. rubric:: Syntax
      :name: syntax

   .. code::

      #include <pk11func.h>
      #include <certt.h>

   .. code::

      CERTCertificate *PK11_FindCertFromNickname(
         char *nickname,
         void *wincx);

   .. rubric:: Parameters
      :name: parameters

   This function has the following parameters:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to the nickname in the certificate    |
   |                                                 | database or to the nickname in the token.       |
   |    nickname                                     |                                                 |
   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to application data for the password  |
   |                                                 | callback function. This pointer is set with     |
   |    wincx                                        | :ref:`moz                                       |
   |                                                 | illa_projects_nss_ssl_functions_sslfnc#1088040` |
   |                                                 | during SSL configuration. To retrieve its       |
   |                                                 | current value, use                              |
   |                                                 | :ref:`mozi                                      |
   |                                                 | lla_projects_nss_ssl_functions_sslfnc#1123385`. |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns

   The function returns one of these values:

   -  If successful, a pointer to a certificate structure.
   -  If unsuccessful, ``NULL``.

   .. rubric:: Description
      :name: description

   A nickname is an alias for a certificate subject. There may be multiple certificates with the
   same subject, and hence the same nickname. This function will return the newest certificate that
   matches the subject, based on the NotBefore / NotAfter fields of the certificate. When you are
   finished with the certificate structure returned by ``PK11_FindCertFromNickname``, you must free
   it by calling ```CERT_DestroyCertificate`` <sslcrt.html#1050532>`__.

   The ``PK11_FindCertFromNickname`` function calls the password callback function set with
   ```PK11_SetPasswordFunc`` <#1023128>`__ and passes it the pointer specified by the ``wincx``
   parameter.

   .. rubric:: PK11_FindKeyByAnyCert
      :name: pk11_findkeybyanycert

   Finds the private key associated with a specified certificate in any available slot.

   .. rubric:: Syntax
      :name: syntax_2

   .. code::

      #include <pk11func.h>
      #include <certt.h>
      #include <keyt.h>

   .. code::

      SECKEYPrivateKey *PK11_FindKeyByAnyCert(
         CERTCertificate *cert,
         void *wincx);

   .. rubric:: Parameters
      :name: parameters_2

   This function has the following parameters:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a certificate structure in the     |
   |                                                 | certificate database.                           |
   |    cert                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to application data for the password  |
   |                                                 | callback function. This pointer is set with     |
   |    wincx                                        | :ref:`moz                                       |
   |                                                 | illa_projects_nss_ssl_functions_sslfnc#1088040` |
   |                                                 | during SSL configuration. To retrieve its       |
   |                                                 | current value, use                              |
   |                                                 | :ref:`mozi                                      |
   |                                                 | lla_projects_nss_ssl_functions_sslfnc#1123385`. |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns_2

   The function returns one of these values:

   -  If successful, a pointer to a private key structure.
   -  If unsuccessful, ``NULL``.

   .. rubric:: Description
      :name: description_2

   When you are finished with the private key structure returned by ``PK11_FindKeyByAnyCert``, you
   must free it by calling ```SECKEY_DestroyPrivateKey`` <sslkey.html#1051017>`__.

   The ``PK11_FindKeyByAnyCert`` function calls the password callback function set with
   ```PK11_SetPasswordFunc`` <#1023128>`__ and passes it the pointer specified by the ``wincx``
   parameter.

   .. rubric:: PK11_GetSlotName
      :name: pk11_getslotname

   Gets the name of a slot.

   .. rubric:: Syntax
      :name: syntax_3

   .. code::

      #include <pk11func.h>

   .. code::

      char *PK11_GetSlotName(PK11SlotInfo *slot);

   .. rubric:: Parameters
      :name: parameters_3

   This function has the following parameter:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a slot info structure.             |
   |                                                 |                                                 |
   |    slot                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns_3

   The function returns one of these values:

   -  If successful, a pointer to the name of the slot (a string).
   -  If unsuccessful, ``NULL``.

   .. rubric:: Description
      :name: description_3

   If the slot is freed, the string with the slot name may also be freed. If you want to preserve
   it, copy the string before freeing the slot. Do not try to free the string yourself.

   .. rubric:: PK11_GetTokenName
      :name: pk11_gettokenname

   Gets the name of the token.

   .. rubric:: Syntax
      :name: syntax_4

   .. code::

      #include <pk11func.h>

   .. code::

      char *PK11_GetTokenName(PK11SlotInfo *slot);

   .. rubric:: Parameters
      :name: parameters_4

   This function has the following parameter:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a slot info structure.             |
   |                                                 |                                                 |
   |    slot                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns_4

   The function returns one of these values:

   -  If successful, a pointer to the name of the token (a string).
   -  If unsuccessful, ``NULL``.

   .. rubric:: Description
      :name: description_4

   If the slot is freed, the string with the token name may also be freed. If you want to preserve
   it, copy the string before freeing the slot. Do not try to free the string yourself.

   .. rubric:: PK11_IsHW
      :name: pk11_ishw

   Finds out whether a slot is implemented in hardware or software.

   .. rubric:: Syntax
      :name: syntax_5

   .. code::

      #include <pk11func.h>
      #include <prtypes.h>

   .. code::

      PRBool PK11_IsHW(PK11SlotInfo *slot);

   .. rubric:: Parameters
      :name: parameters_5

   This function has the following parameter:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a slot info structure.             |
   |                                                 |                                                 |
   |    slot                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns_5

   The function returns one of these values:

   -  If the slot is implemented in hardware, ``PR_TRUE``.
   -  If the slot is implemented in software, ``PR_FALSE``.

   .. rubric:: PK11_IsPresent
      :name: pk11_ispresent

   Finds out whether the token for a slot is available.

   .. rubric:: Syntax
      :name: syntax_6

   .. code::

      #include <pk11func.h>
      #include <prtypes.h>

   .. code::

      PRBool PK11_IsPresent(PK11SlotInfo *slot);

   .. rubric:: Parameters
      :name: parameters_6

   This function has the following parameter:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a slot info structure.             |
   |                                                 |                                                 |
   |    slot                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns_6

   The function returns one of these values:

   -  If token is available, ``PR_TRUE``.
   -  If token is disabled or missing, ``PR_FALSE``.

   .. rubric:: PK11_IsReadOnly
      :name: pk11_isreadonly

   Finds out whether a slot is read-only.

   .. rubric:: Syntax
      :name: syntax_7

   .. code::

      #include <pk11func.h>
      #include <prtypes.h>

   .. code::

      PRBool PK11_IsReadOnly(PK11SlotInfo *slot);

   .. rubric:: Parameters
      :name: parameters_7

   This function has the following parameter:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a slot info structure.             |
   |                                                 |                                                 |
   |    slot                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Returns
      :name: returns_7

   The function returns one of these values:

   -  If slot is read-only, ``PR_TRUE``.
   -  Otherwise, ``PR_FALSE``.

   .. rubric:: PK11_SetPasswordFunc
      :name: pk11_setpasswordfunc

   Defines a callback function used by the NSS libraries whenever information protected by a
   password needs to be retrieved from the key or certificate databases.

   .. rubric:: Syntax
      :name: syntax_8

   .. code::

      #include <pk11func.h>
      #include <prtypes.h>

   .. code::

      void PK11_SetPasswordFunc(PK11PasswordFunc func);

   .. rubric:: Parameter
      :name: parameter

   This function has the following parameter:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to the callback function to set.      |
   |                                                 |                                                 |
   |    func                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+

   .. rubric:: Description
      :name: description_5

   During the course of an SSL operation, it may be necessary for the user to log in to a PKCS #11
   token (either a smart card or soft token) to access protected information, such as a private key.
   Such information is protected with a password that can be retrieved by calling an
   application-supplied callback function. The callback function is identified in a call to
   ``PK11_SetPasswordFunc`` that takes place during NSS initialization.

   The callback function set up by ``PK11_SetPasswordFunc`` has the following prototype:

   .. code::

      typedef char *(*PK11PasswordFunc)(
         PK11SlotInfo *slot,
         PRBool retry,
         void *arg);

   This callback function has the following parameters:

   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer to a slot info structure.             |
   |                                                 |                                                 |
   |    slot                                         |                                                 |
   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | Set to ``PR_TRUE`` if this is a retry. This     |
   |                                                 | implies that the callback has previously        |
   |    retry                                        | returned the wrong password.                    |
   +-------------------------------------------------+-------------------------------------------------+
   | .. code::                           | A pointer supplied by the application that can  |
   |                                                 | be used to pass state information. Can be       |
   |    arg                                          | ``NULL``.                                       |
   +-------------------------------------------------+-------------------------------------------------+

   This callback function returns one of these values:

   -  If successful, a pointer to the password. This memory must have been allocated with
      ```PR_Malloc`` <../../../../../nspr/reference/html/prmem2.html#21428>`__ or
      ```PL_strdup`` <../../../../../nspr/reference/html/plstr.html#21753>`__.
   -  If unsuccessful, returns ``NULL``.

   Many tokens keep track of the number of attempts to enter a password and do not allow further
   attempts after a certain point. Therefore, if the ``retry`` argument is ``PR_TRUE``, indicating
   that the password was tried and is wrong, the callback function should return ``NULL`` to
   indicate that it is unsuccessful, rather than attempting to return the same password again.
   Failing to terminate when the ``retry`` argument is ``PR_TRUE`` can result in an endless loop.

   Several functions in the NSS libraries use the password callback function to obtain the password
   before performing operations that involve the protected information. The third parameter to the
   password callback function is application-defined and can be used for any purpose. For example,
   Communicator uses the parameter to pass information about which window is associated with the
   modal dialog box requesting the password from the user. When NSS libraries call the password
   callback function, the value they pass in the third parameter is determined by
   :ref:`mozilla_projects_nss_ssl_functions_sslfnc#1088040`.

   .. rubric:: See Also
      :name: see_also

   For examples of password callback functions, see the samples in the
   :ref:`mozilla_projects_nss_nss_sample_code` directory.