Removed incorrect test on IPAddresses (was relying on IPaddresses encoded as text)
[gnutls.git] / lib / gnutls_pubkey.c
bloba8fc97581f09f1d33af797cc49b892d8538ba100
1 /*
2 * GnuTLS PKCS#11 support
3 * Copyright (C) 2010 Free Software Foundation
4 *
5 * Author: Nikos Mavrogiannopoulos
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA
23 #include <gnutls_int.h>
24 #include <pakchois/pakchois.h>
25 #include <gnutls/pkcs11.h>
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <gnutls_errors.h>
30 #include <gnutls_datum.h>
31 #include <pkcs11_int.h>
32 #include <gnutls/abstract.h>
33 #include <gnutls_pk.h>
34 #include <x509_int.h>
35 #include <openpgp/openpgp_int.h>
36 #include <pkcs11_int.h>
37 #include <gnutls_num.h>
38 #include <x509/common.h>
39 #include <x509_b64.h>
40 #include <abstract_int.h>
42 #define PK_PEM_HEADER "PUBLIC KEY"
45 struct gnutls_pubkey_st
47 gnutls_pk_algorithm_t pk_algorithm;
48 unsigned int bits; /* an indication of the security parameter */
50 /* the size of params depends on the public
51 * key algorithm
52 * RSA: [0] is modulus
53 * [1] is public exponent
54 * DSA: [0] is p
55 * [1] is q
56 * [2] is g
57 * [3] is public key
59 bigint_t params[MAX_PUBLIC_PARAMS_SIZE];
60 int params_size; /* holds the size of MPI params */
62 unsigned int key_usage; /* bits from GNUTLS_KEY_* */
65 static int pubkey_to_bits(gnutls_pk_algorithm_t pk, bigint_t* params, int params_size)
67 switch(pk)
69 case GNUTLS_PK_RSA:
70 return _gnutls_mpi_get_nbits(params[0]);
71 case GNUTLS_PK_DSA:
72 if (params_size < 3) return 0;
73 return _gnutls_mpi_get_nbits(params[3]);
74 default:
75 return 0;
79 /**
80 * gnutls_pubkey_get_pk_algorithm:
81 * @key: should contain a #gnutls_pubkey_t structure
82 * @bits: If set will return the number of bits of the parameters (may be NULL)
84 * This function will return the public key algorithm of a public
85 * key and if possible will return a number of bits that indicates
86 * the security parameter of the key.
88 * Returns: a member of the #gnutls_pk_algorithm_t enumeration on
89 * success, or a negative value on error.
90 **/
91 int
92 gnutls_pubkey_get_pk_algorithm (gnutls_pubkey_t key, unsigned int *bits)
94 if (bits)
95 *bits = key->bits;
97 return key->pk_algorithm;
101 * gnutls_pubkey_get_key_usage:
102 * @key: should contain a #gnutls_pubkey_t structure
103 * @usage: If set will return the number of bits of the parameters (may be NULL)
105 * This function will return the key usage of the public key.
107 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
108 * negative error value.
111 gnutls_pubkey_get_key_usage (gnutls_pubkey_t key, unsigned int *usage)
113 if (usage)
114 *usage = key->key_usage;
116 return 0;
120 * gnutls_pubkey_init:
121 * @key: The structure to be initialized
123 * This function will initialize an public key structure.
125 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
126 * negative error value.
129 gnutls_pubkey_init (gnutls_pubkey_t * key)
131 *key = gnutls_calloc (1, sizeof (struct gnutls_pubkey_st));
132 if (*key == NULL)
134 gnutls_assert ();
135 return GNUTLS_E_MEMORY_ERROR;
138 return 0;
142 * gnutls_pubkey_deinit:
143 * @key: The structure to be deinitialized
145 * This function will deinitialize a public key structure.
147 void
148 gnutls_pubkey_deinit (gnutls_pubkey_t key)
150 int i;
152 for (i = 0; i < key->params_size; i++)
154 _gnutls_mpi_release (&key->params[i]);
157 gnutls_free (key);
161 * gnutls_pubkey_import_x509:
162 * @key: The public key
163 * @crt: The certificate to be imported
164 * @flags: should be zero
166 * This function will import the given public key to the abstract
167 * #gnutls_pubkey_t structure.
169 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
170 * negative error value.
173 gnutls_pubkey_import_x509 (gnutls_pubkey_t key, gnutls_x509_crt_t crt,
174 unsigned int flags)
176 int ret;
178 key->pk_algorithm = gnutls_x509_crt_get_pk_algorithm (crt, &key->bits);
180 ret = gnutls_x509_crt_get_key_usage (crt, &key->key_usage, NULL);
181 if (ret < 0)
182 key->key_usage = 0;
184 key->params_size = sizeof (key->params) / sizeof (key->params[0]);
185 switch (key->pk_algorithm)
187 case GNUTLS_PK_RSA:
188 ret = _gnutls_x509_crt_get_mpis (crt, key->params, &key->params_size);
189 if (ret < 0)
191 gnutls_assert ();
192 return ret;
194 break;
195 case GNUTLS_PK_DSA:
196 ret = _gnutls_x509_crt_get_mpis (crt, key->params, &key->params_size);
197 if (ret < 0)
199 gnutls_assert ();
200 return ret;
203 break;
204 default:
205 gnutls_assert ();
206 return GNUTLS_E_INVALID_REQUEST;
209 return 0;
213 * gnutls_pubkey_import_privkey: Imports the public key from a private
214 * @key: The public key
215 * @pkey: The private key
216 * @usage: GNUTLS_KEY_* key usage flags.
217 * @flags: should be zero
219 * This function will import the given public key to the abstract
220 * #gnutls_pubkey_t structure.
222 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
223 * negative error value.
225 * Since: 2.12.0
228 gnutls_pubkey_import_privkey (gnutls_pubkey_t key, gnutls_privkey_t pkey,
229 unsigned int usage, unsigned int flags)
231 key->pk_algorithm = gnutls_privkey_get_pk_algorithm (pkey, &key->bits);
233 key->key_usage = usage;
235 key->params_size = sizeof (key->params) / sizeof (key->params[0]);
237 return _gnutls_privkey_get_public_mpis (pkey, key->params,
238 &key->params_size);
242 * gnutls_pubkey_get_preferred_hash_algorithm:
243 * @key: Holds the certificate
244 * @hash: The result of the call with the hash algorithm used for signature
245 * @mand: If non zero it means that the algorithm MUST use this hash. May be NULL.
247 * This function will read the certifcate and return the appropriate digest
248 * algorithm to use for signing with this certificate. Some certificates (i.e.
249 * DSA might not be able to sign without the preferred algorithm).
251 * Returns: the 0 if the hash algorithm is found. A negative value is
252 * returned on error.
254 * Since: 2.11.0
257 gnutls_pubkey_get_preferred_hash_algorithm (gnutls_pubkey_t key,
258 gnutls_digest_algorithm_t *
259 hash, unsigned int *mand)
261 int ret;
263 if (key == NULL)
265 gnutls_assert ();
266 return GNUTLS_E_INVALID_REQUEST;
269 ret = _gnutls_pk_get_hash_algorithm (key->pk_algorithm,
270 key->params, key->params_size,
271 hash, mand);
273 return ret;
278 * gnutls_pubkey_import_pkcs11: Imports a public key from a pkcs11 key
279 * @key: The public key
280 * @obj: The parameters to be imported
281 * @flags: should be zero
283 * This function will import the given public key to the abstract
284 * #gnutls_pubkey_t structure.
286 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
287 * negative error value.
290 gnutls_pubkey_import_pkcs11 (gnutls_pubkey_t key,
291 gnutls_pkcs11_obj_t obj, unsigned int flags)
293 int ret;
295 ret = gnutls_pkcs11_obj_get_type (obj);
296 if (ret != GNUTLS_PKCS11_OBJ_PUBKEY)
298 gnutls_assert ();
299 return GNUTLS_E_INVALID_REQUEST;
302 key->key_usage = obj->key_usage;
304 switch (obj->pk_algorithm)
306 case GNUTLS_PK_RSA:
307 ret = gnutls_pubkey_import_rsa_raw (key, &obj->pubkey[0],
308 &obj->pubkey[1]);
309 break;
310 case GNUTLS_PK_DSA:
311 ret = gnutls_pubkey_import_dsa_raw (key, &obj->pubkey[0],
312 &obj->pubkey[1],
313 &obj->pubkey[2], &obj->pubkey[3]);
314 break;
315 default:
316 gnutls_assert ();
317 return GNUTLS_E_UNIMPLEMENTED_FEATURE;
320 if (ret < 0)
322 gnutls_assert ();
323 return ret;
326 return 0;
329 #ifdef ENABLE_OPENPGP
331 * gnutls_pubkey_import_openpgp: Imports a public key from an openpgp key
332 * @key: The public key
333 * @crt: The certificate to be imported
334 * @flags: should be zero
336 * This function will import the given public key to the abstract
337 * #gnutls_pubkey_t structure. The subkey set as preferred will be
338 * imported or the master key otherwise.
340 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
341 * negative error value.
344 gnutls_pubkey_import_openpgp (gnutls_pubkey_t key,
345 gnutls_openpgp_crt_t crt,
346 unsigned int flags)
348 int ret, idx;
349 uint32_t kid32[2];
350 uint32_t *k;
351 uint8_t keyid[GNUTLS_OPENPGP_KEYID_SIZE];
353 ret = gnutls_openpgp_crt_get_preferred_key_id (crt, keyid);
354 if (ret == GNUTLS_E_OPENPGP_PREFERRED_KEY_ERROR)
356 key->pk_algorithm = gnutls_openpgp_crt_get_pk_algorithm (crt, &key->bits);
358 ret = gnutls_openpgp_crt_get_key_usage (crt, &key->key_usage);
359 if (ret < 0)
360 key->key_usage = 0;
362 k = NULL;
364 else
366 if (ret < 0)
368 gnutls_assert ();
369 return ret;
372 KEYID_IMPORT (kid32, keyid);
373 k = kid32;
375 idx = gnutls_openpgp_crt_get_subkey_idx (crt, keyid);
377 ret = gnutls_openpgp_crt_get_subkey_usage (crt, idx, &key->key_usage);
378 if (ret < 0)
379 key->key_usage = 0;
381 key->pk_algorithm = gnutls_openpgp_crt_get_subkey_pk_algorithm (crt, idx, NULL);
384 switch (key->pk_algorithm)
386 case GNUTLS_PK_RSA:
387 ret =
388 _gnutls_openpgp_crt_get_mpis (crt, k, key->params,
389 &key->params_size);
390 if (ret < 0)
392 gnutls_assert ();
393 return ret;
395 break;
396 case GNUTLS_PK_DSA:
397 ret =
398 _gnutls_openpgp_crt_get_mpis (crt, k, key->params,
399 &key->params_size);
400 if (ret < 0)
402 gnutls_assert ();
403 return ret;
405 break;
406 default:
407 gnutls_assert ();
408 return GNUTLS_E_INVALID_REQUEST;
411 return 0;
414 #endif
417 * gnutls_pubkey_export:
418 * @key: Holds the certificate
419 * @format: the format of output params. One of PEM or DER.
420 * @output_data: will contain a certificate PEM or DER encoded
421 * @output_data_size: holds the size of output_data (and will be
422 * replaced by the actual size of parameters)
424 * This function will export the certificate to DER or PEM format.
426 * If the buffer provided is not long enough to hold the output, then
427 * *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
428 * be returned.
430 * If the structure is PEM encoded, it will have a header
431 * of "BEGIN CERTIFICATE".
433 * Return value: In case of failure a negative value will be
434 * returned, and 0 on success.
437 gnutls_pubkey_export (gnutls_pubkey_t key,
438 gnutls_x509_crt_fmt_t format, void *output_data,
439 size_t * output_data_size)
441 int result;
442 ASN1_TYPE spk = ASN1_TYPE_EMPTY;
444 if (key == NULL)
446 gnutls_assert ();
447 return GNUTLS_E_INVALID_REQUEST;
450 if ((result = asn1_create_element
451 (_gnutls_get_pkix (), "PKIX1.SubjectPublicKeyInfo", &spk))
452 != ASN1_SUCCESS)
454 gnutls_assert ();
455 return _gnutls_asn2err (result);
458 result =
459 _gnutls_x509_encode_and_copy_PKI_params (spk, "",
460 key->pk_algorithm,
461 key->params, key->params_size);
462 if (result < 0)
464 gnutls_assert ();
465 goto cleanup;
468 result = _gnutls_x509_export_int_named (spk, "",
469 format, PK_PEM_HEADER,
470 output_data, output_data_size);
471 if (result < 0)
473 gnutls_assert ();
474 goto cleanup;
477 result = 0;
479 cleanup:
480 asn1_delete_structure (&spk);
482 return result;
487 * gnutls_pubkey_get_key_id:
488 * @key: Holds the public key
489 * @flags: should be 0 for now
490 * @output_data: will contain the key ID
491 * @output_data_size: holds the size of output_data (and will be
492 * replaced by the actual size of parameters)
494 * This function will return a unique ID the depends on the public
495 * key parameters. This ID can be used in checking whether a
496 * certificate corresponds to the given public key.
498 * If the buffer provided is not long enough to hold the output, then
499 * *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
500 * be returned. The output will normally be a SHA-1 hash output,
501 * which is 20 bytes.
503 * Return value: In case of failure a negative value will be
504 * returned, and 0 on success.
507 gnutls_pubkey_get_key_id (gnutls_pubkey_t key, unsigned int flags,
508 unsigned char *output_data,
509 size_t * output_data_size)
511 int ret = 0;
513 if (key == NULL)
515 gnutls_assert ();
516 return GNUTLS_E_INVALID_REQUEST;
519 ret =
520 _gnutls_get_key_id (key->pk_algorithm, key->params,
521 key->params_size, output_data, output_data_size);
522 if (ret < 0)
524 gnutls_assert ();
525 return ret;
528 return 0;
532 * gnutls_pubkey_get_pk_rsa_raw:
533 * @key: Holds the certificate
534 * @m: will hold the modulus
535 * @e: will hold the public exponent
537 * This function will export the RSA public key's parameters found in
538 * the given structure. The new parameters will be allocated using
539 * gnutls_malloc() and will be stored in the appropriate datum.
541 * Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
544 gnutls_pubkey_get_pk_rsa_raw (gnutls_pubkey_t key,
545 gnutls_datum_t * m, gnutls_datum_t * e)
547 int ret;
549 if (key == NULL)
551 gnutls_assert ();
552 return GNUTLS_E_INVALID_REQUEST;
555 if (key->pk_algorithm != GNUTLS_PK_RSA)
557 gnutls_assert ();
558 return GNUTLS_E_INVALID_REQUEST;
561 ret = _gnutls_mpi_dprint (key->params[0], m);
562 if (ret < 0)
564 gnutls_assert ();
565 return ret;
568 ret = _gnutls_mpi_dprint (key->params[1], e);
569 if (ret < 0)
571 gnutls_assert ();
572 _gnutls_free_datum (m);
573 return ret;
576 return 0;
580 * gnutls_pubkey_get_pk_dsa_raw:
581 * @key: Holds the public key
582 * @p: will hold the p
583 * @q: will hold the q
584 * @g: will hold the g
585 * @y: will hold the y
587 * This function will export the DSA public key's parameters found in
588 * the given certificate. The new parameters will be allocated using
589 * gnutls_malloc() and will be stored in the appropriate datum.
591 * Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
594 gnutls_pubkey_get_pk_dsa_raw (gnutls_pubkey_t key,
595 gnutls_datum_t * p, gnutls_datum_t * q,
596 gnutls_datum_t * g, gnutls_datum_t * y)
598 int ret;
600 if (key == NULL)
602 gnutls_assert ();
603 return GNUTLS_E_INVALID_REQUEST;
606 if (key->pk_algorithm != GNUTLS_PK_DSA)
608 gnutls_assert ();
609 return GNUTLS_E_INVALID_REQUEST;
612 /* P */
613 ret = _gnutls_mpi_dprint (key->params[0], p);
614 if (ret < 0)
616 gnutls_assert ();
617 return ret;
620 /* Q */
621 ret = _gnutls_mpi_dprint (key->params[1], q);
622 if (ret < 0)
624 gnutls_assert ();
625 _gnutls_free_datum (p);
626 return ret;
630 /* G */
631 ret = _gnutls_mpi_dprint (key->params[2], g);
632 if (ret < 0)
634 gnutls_assert ();
635 _gnutls_free_datum (p);
636 _gnutls_free_datum (q);
637 return ret;
641 /* Y */
642 ret = _gnutls_mpi_dprint (key->params[3], y);
643 if (ret < 0)
645 gnutls_assert ();
646 _gnutls_free_datum (p);
647 _gnutls_free_datum (g);
648 _gnutls_free_datum (q);
649 return ret;
652 return 0;
656 * gnutls_pubkey_import:
657 * @key: The structure to store the parsed public key.
658 * @data: The DER or PEM encoded certificate.
659 * @format: One of DER or PEM
661 * This function will convert the given DER or PEM encoded Public key
662 * to the native gnutls_pubkey_t format.The output will be stored * in @ key.
663 * If the Certificate is PEM encoded it should have a header of "PUBLIC KEY".
665 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
666 * negative error value.
669 gnutls_pubkey_import (gnutls_pubkey_t key,
670 const gnutls_datum_t * data,
671 gnutls_x509_crt_fmt_t format)
673 int result = 0, need_free = 0;
674 gnutls_datum_t _data;
675 ASN1_TYPE spk;
677 if (key == NULL)
679 gnutls_assert ();
680 return GNUTLS_E_INVALID_REQUEST;
683 _data.data = data->data;
684 _data.size = data->size;
686 /* If the Certificate is in PEM format then decode it
688 if (format == GNUTLS_X509_FMT_PEM)
690 opaque *out;
692 /* Try the first header */
693 result =
694 _gnutls_fbase64_decode (PK_PEM_HEADER, data->data, data->size, &out);
696 if (result <= 0)
698 if (result == 0)
699 result = GNUTLS_E_INTERNAL_ERROR;
700 gnutls_assert ();
701 return result;
704 _data.data = out;
705 _data.size = result;
707 need_free = 1;
710 if ((result = asn1_create_element
711 (_gnutls_get_pkix (), "PKIX1.SubjectPublicKeyInfo", &spk))
712 != ASN1_SUCCESS)
714 gnutls_assert ();
715 result = _gnutls_asn2err (result);
716 goto cleanup;
719 result = asn1_der_decoding (&spk, _data.data, _data.size, NULL);
720 if (result != ASN1_SUCCESS)
722 gnutls_assert ();
723 result = _gnutls_asn2err (result);
724 goto cleanup;
727 key->params_size = sizeof (key->params) / sizeof (key->params[0]);
728 result = _gnutls_get_asn_mpis (spk, "", key->params, &key->params_size);
729 if (result < 0)
731 gnutls_assert ();
732 goto cleanup;
735 /* this has already been called by get_asn_mpis() thus it cannot
736 * fail.
738 key->pk_algorithm = _gnutls_x509_get_pk_algorithm (spk, "", NULL);
739 key->bits = pubkey_to_bits(key->pk_algorithm, key->params, key->params_size);
741 result = 0;
743 cleanup:
744 asn1_delete_structure (&spk);
746 if (need_free)
747 _gnutls_free_datum (&_data);
748 return result;
752 * gnutls_x509_crt_set_pubkey:
753 * @crt: should contain a #gnutls_x509_crt_t structure
754 * @key: holds a public key
756 * This function will set the public parameters from the given public
757 * key to the request.
759 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
760 * negative error value.
763 gnutls_x509_crt_set_pubkey (gnutls_x509_crt_t crt, gnutls_pubkey_t key)
765 int result;
767 if (crt == NULL)
769 gnutls_assert ();
770 return GNUTLS_E_INVALID_REQUEST;
773 result = _gnutls_x509_encode_and_copy_PKI_params (crt->cert,
774 "tbsCertificate.subjectPublicKeyInfo",
775 key->pk_algorithm,
776 key->params,
777 key->params_size);
779 if (result < 0)
781 gnutls_assert ();
782 return result;
785 if (key->key_usage)
786 gnutls_x509_crt_set_key_usage (crt, key->key_usage);
788 return 0;
792 * gnutls_x509_crq_set_pubkey:
793 * @crq: should contain a #gnutls_x509_crq_t structure
794 * @key: holds a public key
796 * This function will set the public parameters from the given public
797 * key to the request.
799 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
800 * negative error value.
803 gnutls_x509_crq_set_pubkey (gnutls_x509_crq_t crq, gnutls_pubkey_t key)
805 int result;
807 if (crq == NULL)
809 gnutls_assert ();
810 return GNUTLS_E_INVALID_REQUEST;
813 result = _gnutls_x509_encode_and_copy_PKI_params
814 (crq->crq,
815 "certificationRequestInfo.subjectPKInfo",
816 key->pk_algorithm, key->params, key->params_size);
818 if (result < 0)
820 gnutls_assert ();
821 return result;
824 if (key->key_usage)
825 gnutls_x509_crq_set_key_usage (crq, key->key_usage);
827 return 0;
831 * gnutls_pubkey_set_key_usage:
832 * @key: a certificate of type #gnutls_x509_crt_t
833 * @usage: an ORed sequence of the GNUTLS_KEY_* elements.
835 * This function will set the key usage flags of the public key. This
836 * is only useful if the key is to be exported to a certificate or
837 * certificate request.
839 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
840 * negative error value.
843 gnutls_pubkey_set_key_usage (gnutls_pubkey_t key, unsigned int usage)
845 key->key_usage = usage;
847 return 0;
851 * gnutls_pubkey_import_pkcs11_url:
852 * @key: A key of type #gnutls_pubkey_t
853 * @url: A PKCS 11 url
854 * @flags: One of GNUTLS_PKCS11_OBJ_* flags
856 * This function will import a PKCS 11 certificate to a #gnutls_pubkey_t
857 * structure.
859 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
860 * negative error value.
864 gnutls_pubkey_import_pkcs11_url (gnutls_pubkey_t key, const char *url,
865 unsigned int flags)
867 gnutls_pkcs11_obj_t pcrt;
868 int ret;
870 ret = gnutls_pkcs11_obj_init (&pcrt);
871 if (ret < 0)
873 gnutls_assert ();
874 return ret;
877 ret = gnutls_pkcs11_obj_import_url (pcrt, url, flags);
878 if (ret < 0)
880 gnutls_assert ();
881 goto cleanup;
884 ret = gnutls_pubkey_import_pkcs11 (key, pcrt, 0);
885 if (ret < 0)
887 gnutls_assert ();
888 goto cleanup;
891 ret = 0;
892 cleanup:
894 gnutls_pkcs11_obj_deinit (pcrt);
896 return ret;
900 * gnutls_pubkey_import_rsa_raw:
901 * @key: Is a structure will hold the parameters
902 * @m: holds the modulus
903 * @e: holds the public exponent
905 * This function will replace the parameters in the given structure.
906 * The new parameters should be stored in the appropriate
907 * gnutls_datum.
909 * Returns: %GNUTLS_E_SUCCESS on success, or an negative error code.
912 gnutls_pubkey_import_rsa_raw (gnutls_pubkey_t key,
913 const gnutls_datum_t * m,
914 const gnutls_datum_t * e)
916 size_t siz = 0;
918 if (key == NULL)
920 gnutls_assert ();
921 return GNUTLS_E_INVALID_REQUEST;
924 siz = m->size;
925 if (_gnutls_mpi_scan_nz (&key->params[0], m->data, siz))
927 gnutls_assert ();
928 return GNUTLS_E_MPI_SCAN_FAILED;
931 siz = e->size;
932 if (_gnutls_mpi_scan_nz (&key->params[1], e->data, siz))
934 gnutls_assert ();
935 _gnutls_mpi_release (&key->params[0]);
936 return GNUTLS_E_MPI_SCAN_FAILED;
939 key->params_size = RSA_PUBLIC_PARAMS;
940 key->pk_algorithm = GNUTLS_PK_RSA;
941 key->bits = pubkey_to_bits(GNUTLS_PK_RSA, key->params, key->params_size);
943 return 0;
947 * gnutls_pubkey_import_dsa_raw:
948 * @key: The structure to store the parsed key
949 * @p: holds the p
950 * @q: holds the q
951 * @g: holds the g
952 * @y: holds the y
954 * This function will convert the given DSA raw parameters to the
955 * native #gnutls_pubkey_t format. The output will be stored
956 * in @key.
958 * Returns: On success, %GNUTLS_E_SUCCESS is returned, otherwise a
959 * negative error value.
962 gnutls_pubkey_import_dsa_raw (gnutls_pubkey_t key,
963 const gnutls_datum_t * p,
964 const gnutls_datum_t * q,
965 const gnutls_datum_t * g,
966 const gnutls_datum_t * y)
968 size_t siz = 0;
970 if (key == NULL)
972 gnutls_assert ();
973 return GNUTLS_E_INVALID_REQUEST;
976 siz = p->size;
977 if (_gnutls_mpi_scan_nz (&key->params[0], p->data, siz))
979 gnutls_assert ();
980 return GNUTLS_E_MPI_SCAN_FAILED;
983 siz = q->size;
984 if (_gnutls_mpi_scan_nz (&key->params[1], q->data, siz))
986 gnutls_assert ();
987 _gnutls_mpi_release (&key->params[0]);
988 return GNUTLS_E_MPI_SCAN_FAILED;
991 siz = g->size;
992 if (_gnutls_mpi_scan_nz (&key->params[2], g->data, siz))
994 gnutls_assert ();
995 _gnutls_mpi_release (&key->params[1]);
996 _gnutls_mpi_release (&key->params[0]);
997 return GNUTLS_E_MPI_SCAN_FAILED;
1000 siz = y->size;
1001 if (_gnutls_mpi_scan_nz (&key->params[3], y->data, siz))
1003 gnutls_assert ();
1004 _gnutls_mpi_release (&key->params[2]);
1005 _gnutls_mpi_release (&key->params[1]);
1006 _gnutls_mpi_release (&key->params[0]);
1007 return GNUTLS_E_MPI_SCAN_FAILED;
1010 key->params_size = DSA_PUBLIC_PARAMS;
1011 key->pk_algorithm = GNUTLS_PK_DSA;
1012 key->bits = pubkey_to_bits(GNUTLS_PK_DSA, key->params, key->params_size);
1014 return 0;
1019 * gnutls_pubkey_verify_data:
1020 * @pubkey: Holds the public key
1021 * @flags: should be 0 for now
1022 * @data: holds the data to be signed
1023 * @signature: contains the signature
1025 * This function will verify the given signed data, using the
1026 * parameters from the certificate.
1028 * Returns: In case of a verification failure
1029 * %GNUTLS_E_PK_SIG_VERIFY_FAILED is returned, and a positive code
1030 * on success.
1032 * Since: 2.12.0
1035 gnutls_pubkey_verify_data (gnutls_pubkey_t pubkey, unsigned int flags,
1036 const gnutls_datum_t * data,
1037 const gnutls_datum_t * signature)
1039 int ret;
1041 if (pubkey == NULL)
1043 gnutls_assert ();
1044 return GNUTLS_E_INVALID_REQUEST;
1047 ret = pubkey_verify_sig( data, NULL, signature, pubkey->pk_algorithm,
1048 pubkey->params, pubkey->params_size);
1049 if (ret < 0)
1051 gnutls_assert();
1054 return ret;
1059 * gnutls_pubkey_verify_hash:
1060 * @key: Holds the certificate
1061 * @flags: should be 0 for now
1062 * @hash: holds the hash digest to be verified
1063 * @signature: contains the signature
1065 * This function will verify the given signed digest, using the
1066 * parameters from the certificate.
1068 * Returns: In case of a verification failure %GNUTLS_E_PK_SIG_VERIFY_FAILED
1069 * is returned, and a positive code on success.
1072 gnutls_pubkey_verify_hash (gnutls_pubkey_t key, unsigned int flags,
1073 const gnutls_datum_t * hash,
1074 const gnutls_datum_t * signature)
1076 int ret;
1078 if (key == NULL)
1080 gnutls_assert ();
1081 return GNUTLS_E_INVALID_REQUEST;
1084 ret =
1085 pubkey_verify_sig (NULL, hash, signature, key->pk_algorithm,
1086 key->params, key->params_size);
1088 return ret;
1092 * gnutls_pubkey_get_verify_algorithm:
1093 * @key: Holds the certificate
1094 * @signature: contains the signature
1095 * @hash: The result of the call with the hash algorithm used for signature
1097 * This function will read the certifcate and the signed data to
1098 * determine the hash algorithm used to generate the signature.
1100 * Returns: the 0 if the hash algorithm is found. A negative value is
1101 * returned on error.
1104 gnutls_pubkey_get_verify_algorithm (gnutls_pubkey_t key,
1105 const gnutls_datum_t * signature,
1106 gnutls_digest_algorithm_t * hash)
1108 if (key == NULL)
1110 gnutls_assert ();
1111 return GNUTLS_E_INVALID_REQUEST;
1114 return _gnutls_x509_verify_algorithm ((gnutls_mac_algorithm_t *)
1115 hash, signature,
1116 key->pk_algorithm,
1117 key->params, key->params_size);