2 * GnuTLS PKCS#11 support
3 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
4 * Copyright (C) 2012 Nikos Mavrogiannopoulos
6 * Author: Nikos Mavrogiannopoulos
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
22 #include <gnutls_int.h>
23 #include <gnutls/pkcs11.h>
26 #include <gnutls_errors.h>
27 #include <gnutls_datum.h>
28 #include <pkcs11_int.h>
29 #include <gnutls/abstract.h>
30 #include <gnutls_pk.h>
32 #include <openpgp/openpgp_int.h>
33 #include <openpgp/gnutls_openpgp.h>
34 #include <gnutls_sig.h>
35 #include <abstract_int.h>
38 * gnutls_privkey_get_type:
39 * @key: should contain a #gnutls_privkey_t structure
41 * This function will return the type of the private key. This is
42 * actually the type of the subsystem used to set this private key.
44 * Returns: a member of the #gnutls_privkey_type_t enumeration on
45 * success, or a negative error code on error.
50 gnutls_privkey_get_type (gnutls_privkey_t key
)
56 * gnutls_privkey_get_pk_algorithm:
57 * @key: should contain a #gnutls_privkey_t structure
58 * @bits: If set will return the number of bits of the parameters (may be NULL)
60 * This function will return the public key algorithm of a private
61 * key and if possible will return a number of bits that indicates
62 * the security parameter of the key.
64 * Returns: a member of the #gnutls_pk_algorithm_t enumeration on
65 * success, or a negative error code on error.
70 gnutls_privkey_get_pk_algorithm (gnutls_privkey_t key
, unsigned int *bits
)
75 case GNUTLS_PRIVKEY_OPENPGP
:
76 return gnutls_openpgp_privkey_get_pk_algorithm (key
->key
.openpgp
, bits
);
79 case GNUTLS_PRIVKEY_PKCS11
:
80 return gnutls_pkcs11_privkey_get_pk_algorithm (key
->key
.pkcs11
, bits
);
82 case GNUTLS_PRIVKEY_X509
:
84 *bits
= _gnutls_mpi_get_nbits (key
->key
.x509
->params
.params
[0]);
85 return gnutls_x509_privkey_get_pk_algorithm (key
->key
.x509
);
86 case GNUTLS_PRIVKEY_EXT
:
89 return key
->pk_algorithm
;
92 return GNUTLS_E_INVALID_REQUEST
;
98 privkey_to_pubkey (gnutls_pk_algorithm_t pk
,
99 const gnutls_pk_params_st
* priv
,
100 gnutls_pk_params_st
* pub
)
107 pub
->params
[0] = _gnutls_mpi_copy (priv
->params
[0]);
108 pub
->params
[1] = _gnutls_mpi_copy (priv
->params
[1]);
110 pub
->params_nr
= RSA_PUBLIC_PARAMS
;
112 if (pub
->params
[0] == NULL
|| pub
->params
[1] == NULL
)
115 ret
= GNUTLS_E_MEMORY_ERROR
;
121 pub
->params
[0] = _gnutls_mpi_copy (priv
->params
[0]);
122 pub
->params
[1] = _gnutls_mpi_copy (priv
->params
[1]);
123 pub
->params
[2] = _gnutls_mpi_copy (priv
->params
[2]);
124 pub
->params
[3] = _gnutls_mpi_copy (priv
->params
[3]);
126 pub
->params_nr
= DSA_PUBLIC_PARAMS
;
128 if (pub
->params
[0] == NULL
|| pub
->params
[1] == NULL
||
129 pub
->params
[2] == NULL
|| pub
->params
[3] == NULL
)
132 ret
= GNUTLS_E_MEMORY_ERROR
;
138 pub
->params
[0] = _gnutls_mpi_copy (priv
->params
[0]);
139 pub
->params
[1] = _gnutls_mpi_copy (priv
->params
[1]);
140 pub
->params
[2] = _gnutls_mpi_copy (priv
->params
[2]);
141 pub
->params
[3] = _gnutls_mpi_copy (priv
->params
[3]);
142 pub
->params
[4] = _gnutls_mpi_copy (priv
->params
[4]);
143 pub
->params
[5] = _gnutls_mpi_copy (priv
->params
[5]);
144 pub
->params
[6] = _gnutls_mpi_copy (priv
->params
[6]);
145 pub
->params
[7] = _gnutls_mpi_copy (priv
->params
[7]);
147 pub
->params_nr
= ECC_PUBLIC_PARAMS
;
148 pub
->flags
= priv
->flags
;
150 if (pub
->params
[0] == NULL
|| pub
->params
[1] == NULL
||
151 pub
->params
[2] == NULL
|| pub
->params
[3] == NULL
||
152 pub
->params
[4] == NULL
|| pub
->params
[5] == NULL
||
153 pub
->params
[6] == NULL
|| pub
->params
[7] == NULL
)
156 ret
= GNUTLS_E_MEMORY_ERROR
;
163 return GNUTLS_E_INVALID_REQUEST
;
168 gnutls_pk_params_release(pub
);
173 /* Returns the public key of the private key (if possible)
176 _gnutls_privkey_get_public_mpis (gnutls_privkey_t key
,
177 gnutls_pk_params_st
* params
)
180 gnutls_pk_algorithm_t pk
= gnutls_privkey_get_pk_algorithm (key
, NULL
);
184 #ifdef ENABLE_OPENPGP
185 case GNUTLS_PRIVKEY_OPENPGP
:
187 gnutls_pk_params_st tmp_params
;
189 uint8_t keyid
[GNUTLS_OPENPGP_KEYID_SIZE
];
192 gnutls_openpgp_privkey_get_preferred_key_id (key
->key
.openpgp
,
196 KEYID_IMPORT (kid
, keyid
);
197 ret
= _gnutls_openpgp_privkey_get_mpis (key
->key
.openpgp
, kid
,
201 ret
= _gnutls_openpgp_privkey_get_mpis (key
->key
.openpgp
, NULL
,
210 ret
= privkey_to_pubkey (pk
,
214 gnutls_pk_params_release(&tmp_params
);
219 case GNUTLS_PRIVKEY_X509
:
220 ret
= privkey_to_pubkey (pk
,
221 &key
->key
.x509
->params
,
226 return GNUTLS_E_INVALID_REQUEST
;
233 * gnutls_privkey_init:
234 * @key: The structure to be initialized
236 * This function will initialize an private key structure.
238 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
239 * negative error value.
244 gnutls_privkey_init (gnutls_privkey_t
* key
)
246 *key
= gnutls_calloc (1, sizeof (struct gnutls_privkey_st
));
250 return GNUTLS_E_MEMORY_ERROR
;
257 * gnutls_privkey_deinit:
258 * @key: The structure to be deinitialized
260 * This function will deinitialize a private key structure.
265 gnutls_privkey_deinit (gnutls_privkey_t key
)
267 if (key
== NULL
) return;
269 if (key
->flags
& GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
|| key
->flags
& GNUTLS_PRIVKEY_IMPORT_COPY
)
272 #ifdef ENABLE_OPENPGP
273 case GNUTLS_PRIVKEY_OPENPGP
:
274 gnutls_openpgp_privkey_deinit (key
->key
.openpgp
);
278 case GNUTLS_PRIVKEY_PKCS11
:
279 gnutls_pkcs11_privkey_deinit (key
->key
.pkcs11
);
282 case GNUTLS_PRIVKEY_X509
:
283 gnutls_x509_privkey_deinit (key
->key
.x509
);
285 case GNUTLS_PRIVKEY_EXT
:
286 if (key
->key
.ext
.deinit_func
!= NULL
)
287 key
->key
.ext
.deinit_func(key
, key
->key
.ext
.userdata
);
295 /* will fail if the private key contains an actual key.
297 static int check_if_clean(gnutls_privkey_t key
)
300 return GNUTLS_E_INVALID_REQUEST
;
308 * gnutls_privkey_import_pkcs11:
309 * @pkey: The private key
310 * @key: The private key to be imported
311 * @flags: Flags for the import
313 * This function will import the given private key to the abstract
314 * #gnutls_privkey_t structure.
316 * The #gnutls_pkcs11_privkey_t object must not be deallocated
317 * during the lifetime of this structure.
319 * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
320 * and %GNUTLS_PRIVKEY_IMPORT_COPY.
322 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
323 * negative error value.
328 gnutls_privkey_import_pkcs11 (gnutls_privkey_t pkey
,
329 gnutls_pkcs11_privkey_t key
, unsigned int flags
)
333 ret
= check_if_clean(pkey
);
340 if (flags
& GNUTLS_PRIVKEY_IMPORT_COPY
)
341 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
343 pkey
->key
.pkcs11
= key
;
344 pkey
->type
= GNUTLS_PRIVKEY_PKCS11
;
345 pkey
->pk_algorithm
= gnutls_pkcs11_privkey_get_pk_algorithm (key
, NULL
);
349 gnutls_pkcs11_privkey_set_pin_function(key
, pkey
->pin
.cb
, pkey
->pin
.data
);
355 * gnutls_privkey_import_pkcs11_url:
356 * @key: A key of type #gnutls_pubkey_t
357 * @url: A PKCS 11 url
359 * This function will import a PKCS 11 private key to a #gnutls_private_key_t
362 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
363 * negative error value.
368 gnutls_privkey_import_pkcs11_url (gnutls_privkey_t key
, const char *url
)
370 gnutls_pkcs11_privkey_t pkey
;
373 ret
= gnutls_pkcs11_privkey_init (&pkey
);
381 gnutls_pkcs11_privkey_set_pin_function(pkey
, key
->pin
.cb
, key
->pin
.data
);
383 ret
= gnutls_pkcs11_privkey_import_url (pkey
, url
, 0);
390 ret
= gnutls_privkey_import_pkcs11 (key
, pkey
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
400 gnutls_pkcs11_privkey_deinit (pkey
);
405 #endif /* ENABLE_PKCS11 */
408 * gnutls_privkey_import_ext:
409 * @pkey: The private key
410 * @pk: The public key algorithm
411 * @userdata: private data to be provided to the callbacks
412 * @sign_func: callback for signature operations
413 * @decrypt_func: callback for decryption operations
414 * @flags: Flags for the import
416 * This function will associate the given callbacks with the
417 * #gnutls_privkey_t structure. At least one of the two callbacks
420 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
421 * negative error value.
426 gnutls_privkey_import_ext (gnutls_privkey_t pkey
,
427 gnutls_pk_algorithm_t pk
,
429 gnutls_privkey_sign_func sign_func
,
430 gnutls_privkey_decrypt_func decrypt_func
,
433 return gnutls_privkey_import_ext2( pkey
, pk
, userdata
, sign_func
, decrypt_func
,
438 * gnutls_privkey_import_ext2:
439 * @pkey: The private key
440 * @pk: The public key algorithm
441 * @userdata: private data to be provided to the callbacks
442 * @sign_func: callback for signature operations
443 * @decrypt_func: callback for decryption operations
444 * @deinit_func: a deinitialization function
445 * @flags: Flags for the import
447 * This function will associate the given callbacks with the
448 * #gnutls_privkey_t structure. At least one of the two callbacks
449 * must be non-null. If a deinitialization function is provided
450 * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE.
452 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
453 * negative error value.
458 gnutls_privkey_import_ext2 (gnutls_privkey_t pkey
,
459 gnutls_pk_algorithm_t pk
,
461 gnutls_privkey_sign_func sign_func
,
462 gnutls_privkey_decrypt_func decrypt_func
,
463 gnutls_privkey_deinit_func deinit_func
,
468 ret
= check_if_clean(pkey
);
475 if (sign_func
== NULL
&& decrypt_func
== NULL
)
476 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
478 pkey
->key
.ext
.sign_func
= sign_func
;
479 pkey
->key
.ext
.decrypt_func
= decrypt_func
;
480 pkey
->key
.ext
.deinit_func
= deinit_func
;
481 pkey
->key
.ext
.userdata
= userdata
;
482 pkey
->type
= GNUTLS_PRIVKEY_EXT
;
483 pkey
->pk_algorithm
= pk
;
486 /* Ensure gnutls_privkey_deinit() calls the deinit_func */
488 pkey
->flags
|= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
;
494 * gnutls_privkey_import_x509:
495 * @pkey: The private key
496 * @key: The private key to be imported
497 * @flags: Flags for the import
499 * This function will import the given private key to the abstract
500 * #gnutls_privkey_t structure.
502 * The #gnutls_x509_privkey_t object must not be deallocated
503 * during the lifetime of this structure.
505 * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
506 * and %GNUTLS_PRIVKEY_IMPORT_COPY.
508 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
509 * negative error value.
514 gnutls_privkey_import_x509 (gnutls_privkey_t pkey
,
515 gnutls_x509_privkey_t key
, unsigned int flags
)
519 ret
= check_if_clean(pkey
);
526 if (flags
& GNUTLS_PRIVKEY_IMPORT_COPY
)
528 ret
= gnutls_x509_privkey_init(&pkey
->key
.x509
);
530 return gnutls_assert_val(ret
);
532 ret
= gnutls_x509_privkey_cpy(pkey
->key
.x509
, key
);
535 gnutls_x509_privkey_deinit(pkey
->key
.x509
);
536 return gnutls_assert_val(ret
);
540 pkey
->key
.x509
= key
;
542 pkey
->type
= GNUTLS_PRIVKEY_X509
;
543 pkey
->pk_algorithm
= gnutls_x509_privkey_get_pk_algorithm (key
);
549 #ifdef ENABLE_OPENPGP
551 * gnutls_privkey_import_openpgp:
552 * @pkey: The private key
553 * @key: The private key to be imported
554 * @flags: Flags for the import
556 * This function will import the given private key to the abstract
557 * #gnutls_privkey_t structure.
559 * The #gnutls_openpgp_privkey_t object must not be deallocated
560 * during the lifetime of this structure. The subkey set as
561 * preferred will be used, or the master key otherwise.
563 * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
564 * and %GNUTLS_PRIVKEY_IMPORT_COPY.
566 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
567 * negative error value.
572 gnutls_privkey_import_openpgp (gnutls_privkey_t pkey
,
573 gnutls_openpgp_privkey_t key
,
577 uint8_t keyid
[GNUTLS_OPENPGP_KEYID_SIZE
];
579 ret
= check_if_clean(pkey
);
586 if (flags
& GNUTLS_PRIVKEY_IMPORT_COPY
)
588 ret
= gnutls_openpgp_privkey_init(&pkey
->key
.openpgp
);
590 return gnutls_assert_val(ret
);
592 ret
= _gnutls_openpgp_privkey_cpy(pkey
->key
.openpgp
, key
);
595 gnutls_openpgp_privkey_deinit(pkey
->key
.openpgp
);
596 return gnutls_assert_val(ret
);
600 pkey
->key
.openpgp
= key
;
602 pkey
->type
= GNUTLS_PRIVKEY_OPENPGP
;
604 ret
= gnutls_openpgp_privkey_get_preferred_key_id (key
, keyid
);
605 if (ret
== GNUTLS_E_OPENPGP_PREFERRED_KEY_ERROR
)
607 pkey
->pk_algorithm
= gnutls_openpgp_privkey_get_pk_algorithm(key
, NULL
);
612 return gnutls_assert_val(ret
);
614 idx
= gnutls_openpgp_privkey_get_subkey_idx (key
, keyid
);
616 pkey
->pk_algorithm
= gnutls_openpgp_privkey_get_subkey_pk_algorithm (key
, idx
, NULL
);
625 * gnutls_privkey_import_openpgp_raw:
626 * @pkey: The private key
627 * @data: The private key data to be imported
628 * @format: The format of the private key
629 * @keyid: The key id to use (optional)
630 * @password: A password (optional)
632 * This function will import the given private key to the abstract
633 * #gnutls_privkey_t structure.
635 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
636 * negative error value.
640 int gnutls_privkey_import_openpgp_raw (gnutls_privkey_t pkey
,
641 const gnutls_datum_t
* data
,
642 gnutls_openpgp_crt_fmt_t format
,
643 const gnutls_openpgp_keyid_t keyid
,
644 const char* password
)
646 gnutls_openpgp_privkey_t xpriv
;
649 ret
= gnutls_openpgp_privkey_init(&xpriv
);
651 return gnutls_assert_val(ret
);
653 ret
= gnutls_openpgp_privkey_import(xpriv
, data
, format
, password
, 0);
662 ret
= gnutls_openpgp_privkey_set_preferred_key_id(xpriv
, keyid
);
670 ret
= gnutls_privkey_import_openpgp(pkey
, xpriv
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
680 gnutls_openpgp_privkey_deinit(xpriv
);
687 * gnutls_privkey_sign_data:
688 * @signer: Holds the key
689 * @hash: should be a digest algorithm
690 * @flags: should be 0 for now
691 * @data: holds the data to be signed
692 * @signature: will contain the signature allocate with gnutls_malloc()
694 * This function will sign the given data using a signature algorithm
695 * supported by the private key. Signature algorithms are always used
696 * together with a hash functions. Different hash functions may be
697 * used for the RSA algorithm, but only the SHA family for the DSA keys.
699 * You may use gnutls_pubkey_get_preferred_hash_algorithm() to determine
700 * the hash algorithm.
702 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
703 * negative error value.
708 gnutls_privkey_sign_data (gnutls_privkey_t signer
,
709 gnutls_digest_algorithm_t hash
,
711 const gnutls_datum_t
* data
,
712 gnutls_datum_t
* signature
)
715 gnutls_datum_t digest
;
717 ret
= pk_hash_data (signer
->pk_algorithm
, hash
, NULL
, data
, &digest
);
724 ret
= pk_prepare_hash (signer
->pk_algorithm
, hash
, &digest
);
731 ret
= _gnutls_privkey_sign_hash (signer
, &digest
, signature
);
732 _gnutls_free_datum (&digest
);
743 _gnutls_free_datum (&digest
);
748 * gnutls_privkey_sign_hash:
749 * @signer: Holds the signer's key
750 * @hash_algo: The hash algorithm used
751 * @flags: zero for now
752 * @hash_data: holds the data to be signed
753 * @signature: will contain newly allocated signature
755 * This function will sign the given hashed data using a signature algorithm
756 * supported by the private key. Signature algorithms are always used
757 * together with a hash functions. Different hash functions may be
758 * used for the RSA algorithm, but only SHA-XXX for the DSA keys.
760 * You may use gnutls_pubkey_get_preferred_hash_algorithm() to determine
761 * the hash algorithm.
763 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
764 * negative error value.
769 gnutls_privkey_sign_hash (gnutls_privkey_t signer
,
770 gnutls_digest_algorithm_t hash_algo
,
772 const gnutls_datum_t
* hash_data
,
773 gnutls_datum_t
* signature
)
776 gnutls_datum_t digest
;
778 digest
.data
= gnutls_malloc (hash_data
->size
);
779 if (digest
.data
== NULL
)
782 return GNUTLS_E_MEMORY_ERROR
;
784 digest
.size
= hash_data
->size
;
785 memcpy (digest
.data
, hash_data
->data
, digest
.size
);
787 ret
= pk_prepare_hash (signer
->pk_algorithm
, hash_algo
, &digest
);
794 ret
= _gnutls_privkey_sign_hash (signer
, &digest
, signature
);
804 _gnutls_free_datum (&digest
);
809 * _gnutls_privkey_sign_hash:
810 * @key: Holds the key
811 * @data: holds the data to be signed
812 * @signature: will contain the signature allocate with gnutls_malloc()
814 * This function will sign the given data using a signature algorithm
815 * supported by the private key.
817 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
818 * negative error value.
821 _gnutls_privkey_sign_hash (gnutls_privkey_t key
,
822 const gnutls_datum_t
* hash
,
823 gnutls_datum_t
* signature
)
827 #ifdef ENABLE_OPENPGP
828 case GNUTLS_PRIVKEY_OPENPGP
:
829 return gnutls_openpgp_privkey_sign_hash (key
->key
.openpgp
,
833 case GNUTLS_PRIVKEY_PKCS11
:
834 return _gnutls_pkcs11_privkey_sign_hash (key
->key
.pkcs11
,
837 case GNUTLS_PRIVKEY_X509
:
838 return _gnutls_pk_sign (key
->key
.x509
->pk_algorithm
,
839 signature
, hash
, &key
->key
.x509
->params
);
840 case GNUTLS_PRIVKEY_EXT
:
841 if (key
->key
.ext
.sign_func
== NULL
)
842 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
843 return key
->key
.ext
.sign_func(key
, key
->key
.ext
.userdata
, hash
, signature
);
846 return GNUTLS_E_INVALID_REQUEST
;
851 * gnutls_privkey_decrypt_data:
852 * @key: Holds the key
853 * @flags: zero for now
854 * @ciphertext: holds the data to be decrypted
855 * @plaintext: will contain the decrypted data, allocated with gnutls_malloc()
857 * This function will decrypt the given data using the algorithm
858 * supported by the private key.
860 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
861 * negative error value.
866 gnutls_privkey_decrypt_data (gnutls_privkey_t key
,
868 const gnutls_datum_t
* ciphertext
,
869 gnutls_datum_t
* plaintext
)
873 #ifdef ENABLE_OPENPGP
874 case GNUTLS_PRIVKEY_OPENPGP
:
875 return _gnutls_openpgp_privkey_decrypt_data (key
->key
.openpgp
, flags
,
876 ciphertext
, plaintext
);
878 case GNUTLS_PRIVKEY_X509
:
879 return _gnutls_pk_decrypt (key
->pk_algorithm
, plaintext
, ciphertext
,
880 &key
->key
.x509
->params
);
882 case GNUTLS_PRIVKEY_PKCS11
:
883 return _gnutls_pkcs11_privkey_decrypt_data (key
->key
.pkcs11
,
885 ciphertext
, plaintext
);
887 case GNUTLS_PRIVKEY_EXT
:
888 if (key
->key
.ext
.decrypt_func
== NULL
)
889 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
891 return key
->key
.ext
.decrypt_func(key
, key
->key
.ext
.userdata
, ciphertext
, plaintext
);
894 return GNUTLS_E_INVALID_REQUEST
;
899 * gnutls_privkey_import_x509_raw:
900 * @pkey: The private key
901 * @data: The private key data to be imported
902 * @format: The format of the private key
903 * @password: A password (optional)
904 * @flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t
906 * This function will import the given private key to the abstract
907 * #gnutls_privkey_t structure.
909 * The supported formats are basic unencrypted key, PKCS #8, PKCS #12,
910 * and the openssl format.
912 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
913 * negative error value.
917 int gnutls_privkey_import_x509_raw (gnutls_privkey_t pkey
,
918 const gnutls_datum_t
* data
,
919 gnutls_x509_crt_fmt_t format
,
920 const char* password
, unsigned int flags
)
922 gnutls_x509_privkey_t xpriv
;
925 ret
= gnutls_x509_privkey_init(&xpriv
);
927 return gnutls_assert_val(ret
);
929 ret
= gnutls_x509_privkey_import2(xpriv
, data
, format
, password
, flags
);
936 ret
= gnutls_privkey_import_x509(pkey
, xpriv
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
946 gnutls_x509_privkey_deinit(xpriv
);
952 * gnutls_privkey_import_url:
953 * @key: A key of type #gnutls_privkey_t
954 * @url: A PKCS 11 url
955 * @flags: should be zero
957 * This function will import a PKCS11 or TPM URL as a
958 * private key. The supported URL types can be checked
959 * using gnutls_url_is_supported().
961 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
962 * negative error value.
967 gnutls_privkey_import_url (gnutls_privkey_t key
, const char *url
, unsigned int flags
)
970 if (strstr(url
, "pkcs11:") != NULL
)
971 return gnutls_privkey_import_pkcs11_url(key
, url
);
974 if (strstr(url
, "tpmkey:") != NULL
)
975 return gnutls_privkey_import_tpm_url(key
, url
, NULL
, NULL
, 0);
977 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST
);
981 * gnutls_privkey_set_pin_function:
982 * @key: A key of type #gnutls_privkey_t
984 * @userdata: data associated with the callback
986 * This function will set a callback function to be used when
987 * required to access the object. This function overrides any other
988 * global PIN functions.
990 * Note that this function must be called right after initialization
996 void gnutls_privkey_set_pin_function (gnutls_privkey_t key
,
997 gnutls_pin_callback_t fn
, void *userdata
)
1000 key
->pin
.data
= userdata
;