1 /* pubkey.c - pubkey dispatcher
2 * Copyright (C) 1998,1999,2000,2002,2003 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 static gpg_err_code_t
pubkey_decrypt (int algo
, gcry_mpi_t
*result
, gcry_mpi_t
*data
, gcry_mpi_t
*skey
, int flags
);
34 static gpg_err_code_t
pubkey_sign (int algo
, gcry_mpi_t
*resarr
, gcry_mpi_t hash
, gcry_mpi_t
*skey
);
35 static gpg_err_code_t
pubkey_verify (int algo
, gcry_mpi_t hash
, gcry_mpi_t
*data
, gcry_mpi_t
*pkey
,
36 int (*cmp
) (void *, gcry_mpi_t
), void *opaque
);
38 /* This is the list of the default public-key ciphers included in
40 static struct pubkey_table_entry
42 gcry_pk_spec_t
*pubkey
;
43 unsigned int algorithm
;
47 { &pubkey_spec_rsa
, GCRY_PK_RSA
},
50 { &pubkey_spec_elg
, GCRY_PK_ELG
},
53 { &pubkey_spec_dsa
, GCRY_PK_DSA
},
58 /* List of registered ciphers. */
59 static gcry_module_t pubkeys_registered
;
61 /* This is the lock protecting PUBKEYS_REGISTERED. */
62 static ath_mutex_t pubkeys_registered_lock
;
64 /* Flag to check wether the default pubkeys have already been
66 static int default_pubkeys_registered
;
68 /* Convenient macro for registering the default digests. */
69 #define REGISTER_DEFAULT_PUBKEYS \
72 ath_mutex_lock (&pubkeys_registered_lock); \
73 if (! default_pubkeys_registered) \
75 gcry_pk_register_default (); \
76 default_pubkeys_registered = 1; \
78 ath_mutex_unlock (&pubkeys_registered_lock); \
82 /* These dummy functions are used in case a cipher implementation
83 refuses to provide it's own functions. */
86 dummy_generate (int algorithm
, unsigned int nbits
, unsigned long dummy
,
87 gcry_mpi_t
*skey
, gcry_mpi_t
**retfactors
)
89 log_bug ("no generate() for %d\n", algorithm
);
90 return GPG_ERR_PUBKEY_ALGO
;
94 dummy_check_secret_key (int algorithm
, gcry_mpi_t
*skey
)
96 log_bug ("no check_secret_key() for %d\n", algorithm
);
97 return GPG_ERR_PUBKEY_ALGO
;
100 static gpg_err_code_t
101 dummy_encrypt (int algorithm
, gcry_mpi_t
*resarr
, gcry_mpi_t data
, gcry_mpi_t
*pkey
, int flags
)
103 log_bug ("no encrypt() for %d\n", algorithm
);
104 return GPG_ERR_PUBKEY_ALGO
;
107 static gpg_err_code_t
108 dummy_decrypt (int algorithm
, gcry_mpi_t
*result
, gcry_mpi_t
*data
, gcry_mpi_t
*skey
, int flags
)
110 log_bug ("no decrypt() for %d\n", algorithm
);
111 return GPG_ERR_PUBKEY_ALGO
;
114 static gpg_err_code_t
115 dummy_sign (int algorithm
, gcry_mpi_t
*resarr
, gcry_mpi_t data
, gcry_mpi_t
*skey
)
117 log_bug ("no sign() for %d\n", algorithm
);
118 return GPG_ERR_PUBKEY_ALGO
;
121 static gpg_err_code_t
122 dummy_verify (int algorithm
, gcry_mpi_t hash
, gcry_mpi_t
*data
, gcry_mpi_t
*pkey
,
123 int (*cmp
) (void *, gcry_mpi_t
), void *opaquev
)
125 log_bug ("no verify() for %d\n", algorithm
);
126 return GPG_ERR_PUBKEY_ALGO
;
130 dummy_get_nbits (int algorithm
, gcry_mpi_t
*pkey
)
132 log_bug ("no get_nbits() for %d\n", algorithm
);
136 /* Internal function. Register all the pubkeys included in
137 PUBKEY_TABLE. Returns zero on success or an error code. */
139 gcry_pk_register_default (void)
141 gpg_err_code_t err
= 0;
144 for (i
= 0; (! err
) && pubkey_table
[i
].pubkey
; i
++)
146 #define pubkey_use_dummy(func) \
147 if (! pubkey_table[i].pubkey->func) \
148 pubkey_table[i].pubkey->func = dummy_##func;
150 pubkey_use_dummy (generate
);
151 pubkey_use_dummy (check_secret_key
);
152 pubkey_use_dummy (encrypt
);
153 pubkey_use_dummy (decrypt
);
154 pubkey_use_dummy (sign
);
155 pubkey_use_dummy (verify
);
156 pubkey_use_dummy (get_nbits
);
158 err
= _gcry_module_add (&pubkeys_registered
,
159 pubkey_table
[i
].algorithm
,
160 (void *) pubkey_table
[i
].pubkey
, NULL
);
167 /* Internal callback function. Used via _gcry_module_lookup. */
169 gcry_pk_lookup_func_name (void *spec
, void *data
)
171 gcry_pk_spec_t
*pubkey
= (gcry_pk_spec_t
*) spec
;
172 char *name
= (char *) data
;
173 char **sexp_names
= pubkey
->sexp_names
;
174 int ret
= stricmp (name
, pubkey
->name
);
176 while (ret
&& *sexp_names
)
177 ret
= stricmp (name
, *sexp_names
++);
182 /* Internal function. Lookup a pubkey entry by it's name. */
184 gcry_pk_lookup_name (const char *name
)
186 gcry_module_t pubkey
;
188 pubkey
= _gcry_module_lookup (pubkeys_registered
, (void *) name
,
189 gcry_pk_lookup_func_name
);
194 /* Register a new pubkey module whose specification can be found in
195 PUBKEY. On success, a new algorithm ID is stored in ALGORITHM_ID
196 and a pointer representhing this module is stored in MODULE. */
198 gcry_pk_register (gcry_pk_spec_t
*pubkey
,
199 unsigned int *algorithm_id
,
200 gcry_module_t
*module
)
202 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
205 ath_mutex_lock (&pubkeys_registered_lock
);
206 err
= _gcry_module_add (&pubkeys_registered
, 0,
207 (void *) pubkey
, &mod
);
208 ath_mutex_unlock (&pubkeys_registered_lock
);
213 *algorithm_id
= mod
->mod_id
;
219 /* Unregister the pubkey identified by ID, which must have been
220 registered with gcry_pk_register. */
222 gcry_pk_unregister (gcry_module_t module
)
224 ath_mutex_lock (&pubkeys_registered_lock
);
225 _gcry_module_release (module
);
226 ath_mutex_unlock (&pubkeys_registered_lock
);
230 release_mpi_array (gcry_mpi_t
*array
)
232 for (; *array
; array
++)
240 * Map a string to the pubkey algo
243 gcry_pk_map_name (const char *string
)
245 gcry_module_t pubkey
;
248 REGISTER_DEFAULT_PUBKEYS
;
250 ath_mutex_lock (&pubkeys_registered_lock
);
251 pubkey
= gcry_pk_lookup_name (string
);
254 algorithm
= pubkey
->mod_id
;
255 _gcry_module_release (pubkey
);
257 ath_mutex_unlock (&pubkeys_registered_lock
);
264 * Map a pubkey algo to a string
267 gcry_pk_algo_name (int algorithm
)
269 const char *name
= NULL
;
270 gcry_module_t pubkey
;
272 REGISTER_DEFAULT_PUBKEYS
;
274 ath_mutex_lock (&pubkeys_registered_lock
);
275 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
278 name
= ((gcry_pk_spec_t
*) pubkey
->spec
)->name
;
279 _gcry_module_release (pubkey
);
281 ath_mutex_unlock (&pubkeys_registered_lock
);
288 disable_pubkey_algo (int algorithm
)
290 gcry_module_t pubkey
;
292 ath_mutex_lock (&pubkeys_registered_lock
);
293 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
296 if (! (pubkey
-> flags
& FLAG_MODULE_DISABLED
))
297 pubkey
->flags
|= FLAG_MODULE_DISABLED
;
298 _gcry_module_release (pubkey
);
300 ath_mutex_unlock (&pubkeys_registered_lock
);
305 * a use of 0 means: don't care
307 static gpg_err_code_t
308 check_pubkey_algo (int algorithm
, unsigned use
)
310 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
311 gcry_pk_spec_t
*pubkey
;
312 gcry_module_t module
;
314 REGISTER_DEFAULT_PUBKEYS
;
316 ath_mutex_lock (&pubkeys_registered_lock
);
317 module
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
320 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
322 if (((use
& GCRY_PK_USAGE_SIGN
)
323 && (! (pubkey
->use
& GCRY_PK_USAGE_SIGN
)))
324 || ((use
& GCRY_PK_USAGE_ENCR
)
325 && (! (pubkey
->use
& GCRY_PK_USAGE_ENCR
))))
326 err
= GPG_ERR_WRONG_PUBKEY_ALGO
;
327 else if (module
->flags
& FLAG_MODULE_DISABLED
)
328 err
= GPG_ERR_PUBKEY_ALGO
;
329 _gcry_module_release (module
);
332 err
= GPG_ERR_PUBKEY_ALGO
;
333 ath_mutex_unlock (&pubkeys_registered_lock
);
340 * Return the number of public key material numbers
343 pubkey_get_npkey (int algorithm
)
345 gcry_module_t pubkey
;
348 REGISTER_DEFAULT_PUBKEYS
;
350 ath_mutex_lock (&pubkeys_registered_lock
);
351 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
354 npkey
= strlen (((gcry_pk_spec_t
*) pubkey
->spec
)->elements_pkey
);
355 _gcry_module_release (pubkey
);
357 ath_mutex_unlock (&pubkeys_registered_lock
);
363 * Return the number of secret key material numbers
366 pubkey_get_nskey (int algorithm
)
368 gcry_module_t pubkey
;
371 REGISTER_DEFAULT_PUBKEYS
;
373 ath_mutex_lock (&pubkeys_registered_lock
);
374 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
377 nskey
= strlen (((gcry_pk_spec_t
*) pubkey
->spec
)->elements_skey
);
378 _gcry_module_release (pubkey
);
380 ath_mutex_unlock (&pubkeys_registered_lock
);
386 * Return the number of signature material numbers
389 pubkey_get_nsig (int algorithm
)
391 gcry_module_t pubkey
;
394 REGISTER_DEFAULT_PUBKEYS
;
396 ath_mutex_lock (&pubkeys_registered_lock
);
397 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
400 nsig
= strlen (((gcry_pk_spec_t
*) pubkey
->spec
)->elements_sig
);
401 _gcry_module_release (pubkey
);
403 ath_mutex_unlock (&pubkeys_registered_lock
);
409 * Return the number of encryption material numbers
412 pubkey_get_nenc (int algorithm
)
414 gcry_module_t pubkey
;
417 REGISTER_DEFAULT_PUBKEYS
;
419 ath_mutex_lock (&pubkeys_registered_lock
);
420 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
423 nenc
= strlen (((gcry_pk_spec_t
*) pubkey
->spec
)->elements_enc
);
424 _gcry_module_release (pubkey
);
426 ath_mutex_unlock (&pubkeys_registered_lock
);
432 static gpg_err_code_t
433 pubkey_generate (int algorithm
, unsigned int nbits
, unsigned long use_e
,
434 gcry_mpi_t
*skey
, gcry_mpi_t
**retfactors
)
436 gpg_err_code_t err
= GPG_ERR_PUBKEY_ALGO
;
437 gcry_module_t pubkey
;
439 REGISTER_DEFAULT_PUBKEYS
;
441 ath_mutex_lock (&pubkeys_registered_lock
);
442 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
445 err
= (*((gcry_pk_spec_t
*) pubkey
->spec
)->generate
) (algorithm
, nbits
, use_e
, skey
,
447 _gcry_module_release (pubkey
);
449 ath_mutex_unlock (&pubkeys_registered_lock
);
454 static gpg_err_code_t
455 pubkey_check_secret_key (int algorithm
, gcry_mpi_t
*skey
)
457 gpg_err_code_t err
= GPG_ERR_PUBKEY_ALGO
;
458 gcry_module_t pubkey
;
460 REGISTER_DEFAULT_PUBKEYS
;
462 ath_mutex_lock (&pubkeys_registered_lock
);
463 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
466 err
= (*((gcry_pk_spec_t
*) pubkey
->spec
)->check_secret_key
) (algorithm
, skey
);
467 _gcry_module_release (pubkey
);
469 ath_mutex_unlock (&pubkeys_registered_lock
);
476 * This is the interface to the public key encryption. Encrypt DATA
477 * with PKEY and put it into RESARR which should be an array of MPIs
478 * of size PUBKEY_MAX_NENC (or less if the algorithm allows this -
479 * check with pubkey_get_nenc() )
481 static gpg_err_code_t
482 pubkey_encrypt (int algorithm
, gcry_mpi_t
*resarr
, gcry_mpi_t data
, gcry_mpi_t
*pkey
,
485 gcry_pk_spec_t
*pubkey
;
486 gcry_module_t module
;
492 log_debug ("pubkey_encrypt: algo=%d\n", algorithm
);
493 for(i
= 0; i
< pubkey_get_npkey (algorithm
); i
++)
494 log_mpidump (" pkey:", pkey
[i
]);
495 log_mpidump (" data:", data
);
498 ath_mutex_lock (&pubkeys_registered_lock
);
499 module
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
502 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
503 rc
= (*pubkey
->encrypt
) (algorithm
, resarr
, data
, pkey
, flags
);
504 _gcry_module_release (module
);
507 rc
= GPG_ERR_PUBKEY_ALGO
;
510 ath_mutex_unlock (&pubkeys_registered_lock
);
512 if (!rc
&& DBG_CIPHER
)
514 for(i
= 0; i
< pubkey_get_nenc (algorithm
); i
++)
515 log_mpidump(" encr:", resarr
[i
] );
522 * This is the interface to the public key decryption.
523 * ALGO gives the algorithm to use and this implicitly determines
524 * the size of the arrays.
525 * result is a pointer to a mpi variable which will receive a
526 * newly allocated mpi or NULL in case of an error.
528 static gpg_err_code_t
529 pubkey_decrypt (int algorithm
, gcry_mpi_t
*result
, gcry_mpi_t
*data
, gcry_mpi_t
*skey
,
532 gcry_pk_spec_t
*pubkey
;
533 gcry_module_t module
;
537 *result
= NULL
; /* so the caller can always do a mpi_free */
540 log_debug ("pubkey_decrypt: algo=%d\n", algorithm
);
541 for(i
= 0; i
< pubkey_get_nskey (algorithm
); i
++)
542 log_mpidump (" skey:", skey
[i
]);
543 for(i
= 0; i
< pubkey_get_nenc (algorithm
); i
++)
544 log_mpidump (" data:", data
[i
]);
547 ath_mutex_lock (&pubkeys_registered_lock
);
548 module
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
551 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
552 rc
= (*pubkey
->decrypt
) (algorithm
, result
, data
, skey
, flags
);
553 _gcry_module_release (module
);
557 rc
= GPG_ERR_PUBKEY_ALGO
;
560 ath_mutex_unlock (&pubkeys_registered_lock
);
562 if (! rc
&& DBG_CIPHER
)
563 log_mpidump (" plain:", *result
);
570 * This is the interface to the public key signing.
571 * Sign data with skey and put the result into resarr which
572 * should be an array of MPIs of size PUBKEY_MAX_NSIG (or less if the
573 * algorithm allows this - check with pubkey_get_nsig() )
575 static gpg_err_code_t
576 pubkey_sign (int algorithm
, gcry_mpi_t
*resarr
, gcry_mpi_t data
, gcry_mpi_t
*skey
)
578 gcry_pk_spec_t
*pubkey
;
579 gcry_module_t module
;
585 log_debug ("pubkey_sign: algo=%d\n", algorithm
);
586 for(i
= 0; i
< pubkey_get_nskey (algorithm
); i
++)
587 log_mpidump (" skey:", skey
[i
]);
588 log_mpidump(" data:", data
);
591 ath_mutex_lock (&pubkeys_registered_lock
);
592 module
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
595 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
596 rc
= (*pubkey
->sign
) (algorithm
, resarr
, data
, skey
);
597 _gcry_module_release (module
);
601 rc
= GPG_ERR_PUBKEY_ALGO
;
604 ath_mutex_unlock (&pubkeys_registered_lock
);
606 if (! rc
&& DBG_CIPHER
)
607 for (i
= 0; i
< pubkey_get_nsig (algorithm
); i
++)
608 log_mpidump (" sig:", resarr
[i
]);
614 * Verify a public key signature.
615 * Return 0 if the signature is good
617 static gpg_err_code_t
618 pubkey_verify (int algorithm
, gcry_mpi_t hash
, gcry_mpi_t
*data
, gcry_mpi_t
*pkey
,
619 int (*cmp
)(void *, gcry_mpi_t
), void *opaquev
)
621 gcry_pk_spec_t
*pubkey
;
622 gcry_module_t module
;
628 log_debug ("pubkey_verify: algo=%d\n", algorithm
);
629 for (i
= 0; i
< pubkey_get_npkey (algorithm
); i
++)
630 log_mpidump (" pkey:", pkey
[i
]);
631 for (i
= 0; i
< pubkey_get_nsig (algorithm
); i
++)
632 log_mpidump (" sig:", data
[i
]);
633 log_mpidump (" hash:", hash
);
636 ath_mutex_lock (&pubkeys_registered_lock
);
637 module
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
640 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
641 rc
= (*pubkey
->verify
) (algorithm
, hash
, data
, pkey
, cmp
, opaquev
);
642 _gcry_module_release (module
);
646 rc
= GPG_ERR_PUBKEY_ALGO
;
649 ath_mutex_unlock (&pubkeys_registered_lock
);
653 /* Internal function. */
654 static gpg_err_code_t
655 sexp_elements_extract (gcry_sexp_t key_sexp
, const char *element_names
,
656 gcry_mpi_t
*elements
)
658 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
663 for (name
= element_names
, index
= 0; *name
&& (! err
); name
++, index
++)
665 list
= gcry_sexp_find_token (key_sexp
, name
, 1);
667 err
= GPG_ERR_NO_OBJ
;
670 elements
[index
] = gcry_sexp_nth_mpi (list
, 1, GCRYMPI_FMT_USG
);
671 gcry_sexp_release (list
);
672 if (! elements
[index
])
673 err
= GPG_ERR_INV_OBJ
;
678 for (i
= 0; i
< index
; i
++)
680 gcry_free (elements
[i
]);
686 * Convert a S-Exp with either a private or a public key to our
687 * internal format. Currently we do only support the following
695 * Provide a SE with the first element be either "private-key" or
696 * or "public-key". the followed by a list with its first element
697 * be one of the above algorithm identifiers and the following
698 * elements are pairs with parameter-id and value.
699 * NOTE: we look through the list to find a list beginning with
700 * "private-key" or "public-key" - the first one found is used.
702 * FIXME: Allow for encrypted secret keys here.
704 * Returns: A pointer to an allocated array of MPIs if the return value is
705 * zero; the caller has to release this array.
707 * Example of a DSA public key:
716 * The <mpi> are expected to be in GCRYMPI_FMT_USG
718 static gpg_err_code_t
719 sexp_to_key (gcry_sexp_t sexp
, int want_private
, gcry_mpi_t
**retarray
,
720 gcry_module_t
*retalgo
)
722 gcry_sexp_t list
, l2
;
727 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
728 gcry_module_t module
;
729 gcry_pk_spec_t
*pubkey
;
731 /* check that the first element is valid */
732 list
= gcry_sexp_find_token( sexp
, want_private
? "private-key"
735 return GPG_ERR_INV_OBJ
; /* Does not contain a public- or private-key object */
736 l2
= gcry_sexp_cadr( list
);
737 gcry_sexp_release ( list
);
739 name
= gcry_sexp_nth_data( list
, 0, &n
);
741 gcry_sexp_release ( list
);
742 return GPG_ERR_INV_OBJ
; /* invalid structure of object */
746 char *name_terminated
= gcry_xmalloc (n
+ 1);
747 strncpy (name_terminated
, name
, n
);
748 name_terminated
[n
] = 0;
750 ath_mutex_lock (&pubkeys_registered_lock
);
751 module
= gcry_pk_lookup_name (name_terminated
);
752 ath_mutex_unlock (&pubkeys_registered_lock
);
754 free (name_terminated
);
759 gcry_sexp_release (list
);
760 return GPG_ERR_PUBKEY_ALGO
; /* unknown algorithm */
763 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
765 elems
= want_private
? pubkey
->elements_skey
: pubkey
->elements_pkey
;
766 array
= gcry_calloc (strlen (elems
) + 1, sizeof (*array
));
768 err
= gpg_err_code_from_errno (errno
);
770 err
= sexp_elements_extract (list
, elems
, array
);
773 gcry_sexp_release (list
);
780 ath_mutex_lock (&pubkeys_registered_lock
);
781 _gcry_module_release (module
);
782 ath_mutex_unlock (&pubkeys_registered_lock
);
793 static gpg_err_code_t
794 sexp_to_sig (gcry_sexp_t sexp
, gcry_mpi_t
**retarray
,
795 gcry_module_t
*retalgo
)
797 gcry_sexp_t list
, l2
;
802 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
803 gcry_module_t module
;
804 gcry_pk_spec_t
*pubkey
;
806 /* check that the first element is valid */
807 list
= gcry_sexp_find_token( sexp
, "sig-val" , 0 );
809 return GPG_ERR_INV_OBJ
; /* Does not contain a signature value object */
810 l2
= gcry_sexp_nth (list
, 1);
813 gcry_sexp_release (list
);
814 return GPG_ERR_NO_OBJ
; /* no cadr for the sig object */
816 name
= gcry_sexp_nth_data( l2
, 0, &n
);
818 gcry_sexp_release ( list
);
819 gcry_sexp_release ( l2
);
820 return GPG_ERR_INV_OBJ
; /* invalid structure of object */
822 else if (n
== 5 && (! memcmp (name
, "flags", 5))) {
823 /* Skip flags, since they are not used but just here for the
824 sake of consisten S-expressions. */
825 gcry_sexp_release (l2
);
826 l2
= gcry_sexp_nth (list
, 2);
829 gcry_sexp_release (list
);
830 return GPG_ERR_INV_OBJ
;
832 name
= gcry_sexp_nth_data (l2
, 0, &n
);
836 char *name_terminated
= gcry_xmalloc (n
+ 1);
837 strncpy (name_terminated
, name
, n
);
838 name_terminated
[n
] = 0;
840 ath_mutex_lock (&pubkeys_registered_lock
);
841 module
= gcry_pk_lookup_name (name_terminated
);
842 ath_mutex_unlock (&pubkeys_registered_lock
);
844 free (name_terminated
);
849 gcry_sexp_release (l2
);
850 gcry_sexp_release (list
);
851 return GPG_ERR_PUBKEY_ALGO
; /* unknown algorithm */
854 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
856 elems
= pubkey
->elements_sig
;
857 array
= gcry_calloc (strlen (elems
) + 1 , sizeof (*array
));
859 err
= gpg_err_code_from_errno (errno
);
862 err
= sexp_elements_extract (list
, elems
, array
);
864 gcry_sexp_release (l2
);
865 gcry_sexp_release (list
);
869 ath_mutex_lock (&pubkeys_registered_lock
);
870 _gcry_module_release (module
);
871 ath_mutex_unlock (&pubkeys_registered_lock
);
887 * Take sexp and return an array of MPI as used for our internal decrypt
892 * (<param_name1> <mpi>)
894 * (<param_namen> <mpi>)
896 * RET_MODERN is set to true when at least an empty flags list has been found.
898 static gpg_err_code_t
899 sexp_to_enc (gcry_sexp_t sexp
, gcry_mpi_t
**retarray
, gcry_module_t
*retalgo
,
900 int *ret_modern
, int *ret_want_pkcs1
, int *flags
)
902 gcry_sexp_t list
= NULL
, l2
= NULL
;
903 gcry_pk_spec_t
*pubkey
= NULL
;
904 gcry_module_t module
= NULL
;
907 int parsed_flags
= 0;
909 gcry_mpi_t
*array
= NULL
;
910 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
915 /* check that the first element is valid */
916 list
= gcry_sexp_find_token (sexp
, "enc-val" , 0);
918 err
= GPG_ERR_INV_OBJ
; /* Does not contain an encrypted value object */
922 l2
= gcry_sexp_nth (list
, 1);
924 err
= GPG_ERR_NO_OBJ
; /* no cdr for the data object */
929 /* Extract the name of the algorithm. */
930 name
= gcry_sexp_nth_data (l2
, 0, &n
);
932 err
= GPG_ERR_INV_OBJ
; /* invalid structure of object */
935 if ((! err
) && (n
== 5) && (! memcmp (name
, "flags", 5)))
937 /* There is a flags element - process it */
942 for (i
= gcry_sexp_length (l2
) - 1; i
> 0 && (! err
); i
--)
944 s
= gcry_sexp_nth_data (l2
, i
, &n
);
946 ; /* not a data element - ignore */
947 else if (n
== 3 && ! memcmp (s
, "raw", 3))
948 ; /* just a dummy because it is the default */
949 else if (n
== 5 && ! memcmp (s
, "pkcs1", 5))
951 else if (n
== 11 && ! memcmp (s
, "no-blinding", 11))
952 parsed_flags
|= PUBKEY_FLAG_NO_BLINDING
;
954 err
= GPG_ERR_INV_FLAG
;
960 /* Get the next which has the actual data */
961 gcry_sexp_release (l2
);
962 l2
= gcry_sexp_nth (list
, 2);
964 err
= GPG_ERR_NO_OBJ
; /* no cdr for the data object */
969 name
= gcry_sexp_nth_data (l2
, 0, &n
);
971 err
= GPG_ERR_INV_OBJ
; /* invalid structure of object */
974 gcry_sexp_release (list
);
982 char *name_terminated
= gcry_xmalloc (n
+ 1);
983 strncpy (name_terminated
, name
, n
);
984 name_terminated
[n
] = 0;
986 ath_mutex_lock (&pubkeys_registered_lock
);
987 module
= gcry_pk_lookup_name (name_terminated
);
988 ath_mutex_unlock (&pubkeys_registered_lock
);
990 free (name_terminated
);
993 err
= GPG_ERR_PUBKEY_ALGO
; /* unknown algorithm */
995 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
1000 elems
= pubkey
->elements_enc
;
1001 array
= gcry_calloc (strlen (elems
) + 1, sizeof (*array
));
1003 err
= gpg_err_code_from_errno (errno
);
1007 err
= sexp_elements_extract (list
, elems
, array
);
1010 gcry_sexp_release (list
);
1012 gcry_sexp_release (l2
);
1016 ath_mutex_lock (&pubkeys_registered_lock
);
1017 _gcry_module_release (module
);
1018 ath_mutex_unlock (&pubkeys_registered_lock
);
1026 *flags
= parsed_flags
;
1032 /* Take the hash value and convert into an MPI, suitable for for
1033 passing to the low level functions. We currently support the
1034 old style way of passing just a MPI and the modern interface which
1035 allows to pass flags so that we can choose between raw and pkcs1
1036 padding - may be more padding options later.
1042 [(hash <algo> <value>)]
1046 Either the VALUE or the HASH element must be present for use
1047 with signatures. VALUE is used for encryption.
1049 NBITS is the length of the key in bits.
1052 static gpg_err_code_t
1053 sexp_data_to_mpi (gcry_sexp_t input
, unsigned int nbits
, gcry_mpi_t
*ret_mpi
,
1054 int for_encryption
, int *flags
)
1056 gpg_err_code_t rc
= 0;
1057 gcry_sexp_t ldata
, lhash
, lvalue
;
1061 int is_raw
= 0, is_pkcs1
= 0, unknown_flag
=0;
1062 int parsed_flags
= 0, dummy_flags
;
1065 flags
= &dummy_flags
;
1068 ldata
= gcry_sexp_find_token (input
, "data", 0);
1070 { /* assume old style */
1071 *ret_mpi
= gcry_sexp_nth_mpi (input
, 0, 0);
1072 return *ret_mpi
? GPG_ERR_NO_ERROR
: GPG_ERR_INV_OBJ
;
1075 /* see whether there is a flags object */
1077 gcry_sexp_t lflags
= gcry_sexp_find_token (ldata
, "flags", 0);
1079 { /* parse the flags list. */
1080 for (i
=gcry_sexp_length (lflags
)-1; i
> 0; i
--)
1082 s
= gcry_sexp_nth_data (lflags
, i
, &n
);
1084 ; /* not a data element*/
1085 else if ( n
== 3 && !memcmp (s
, "raw", 3))
1087 else if ( n
== 5 && !memcmp (s
, "pkcs1", 5))
1089 else if (n
== 11 && ! memcmp (s
, "no-blinding", 11))
1090 parsed_flags
|= PUBKEY_FLAG_NO_BLINDING
;
1094 gcry_sexp_release (lflags
);
1098 if (!is_pkcs1
&& !is_raw
)
1099 is_raw
= 1; /* default to raw */
1101 /* Get HASH or MPI */
1102 lhash
= gcry_sexp_find_token (ldata
, "hash", 0);
1103 lvalue
= lhash
? NULL
: gcry_sexp_find_token (ldata
, "value", 0);
1105 if (!(!lhash
^ !lvalue
))
1106 rc
= GPG_ERR_INV_OBJ
; /* none or both given */
1107 else if (unknown_flag
)
1108 rc
= GPG_ERR_INV_FLAG
;
1109 else if (is_raw
&& is_pkcs1
&& !for_encryption
)
1110 rc
= GPG_ERR_CONFLICT
;
1111 else if (is_raw
&& lvalue
)
1113 *ret_mpi
= gcry_sexp_nth_mpi (lvalue
, 1, 0);
1115 rc
= GPG_ERR_INV_OBJ
;
1117 else if (is_pkcs1
&& lvalue
&& for_encryption
)
1118 { /* create pkcs#1 block type 2 padding */
1119 unsigned char *frame
= NULL
;
1120 size_t nframe
= (nbits
+7) / 8;
1125 if ( !(value
=gcry_sexp_nth_data (lvalue
, 1, &valuelen
)) || !valuelen
)
1126 rc
= GPG_ERR_INV_OBJ
;
1127 else if (valuelen
+ 7 > nframe
|| !nframe
)
1129 /* Can't encode a VALUELEN value in a NFRAME bytes frame. */
1130 rc
= GPG_ERR_TOO_SHORT
; /* the key is too short */
1132 else if ( !(frame
= gcry_malloc_secure (nframe
)))
1133 rc
= gpg_err_code_from_errno (errno
);
1138 frame
[n
++] = 2; /* block type */
1139 i
= nframe
- 3 - valuelen
;
1141 p
= gcry_random_bytes_secure (i
, GCRY_STRONG_RANDOM
);
1142 /* replace zero bytes by new values*/
1148 /* count the zero bytes */
1149 for (j
=k
=0; j
< i
; j
++)
1155 break; /* okay: no (more) zero bytes */
1157 k
+= k
/128; /* better get some more */
1158 pp
= gcry_random_bytes_secure (k
, GCRY_STRONG_RANDOM
);
1159 for (j
=0; j
< i
&& k
; j
++)
1166 memcpy (frame
+n
, p
, i
);
1171 memcpy (frame
+n
, value
, valuelen
);
1173 assert (n
== nframe
);
1175 /* FIXME, error checking? */
1176 gcry_mpi_scan (ret_mpi
, GCRYMPI_FMT_USG
, frame
, &nframe
);
1181 else if (is_pkcs1
&& lhash
&& !for_encryption
)
1182 { /* create pkcs#1 block type 1 padding */
1183 if (gcry_sexp_length (lhash
) != 3)
1184 rc
= GPG_ERR_INV_OBJ
;
1185 else if ( !(s
=gcry_sexp_nth_data (lhash
, 1, &n
)) || !n
)
1186 rc
= GPG_ERR_INV_OBJ
;
1189 static struct { const char *name
; int algo
; } hashnames
[] =
1190 { { "sha1", GCRY_MD_SHA1
},
1191 { "md5", GCRY_MD_MD5
},
1192 { "rmd160", GCRY_MD_RMD160
},
1193 { "sha256", GCRY_MD_SHA256
},
1194 { "sha384", GCRY_MD_SHA384
},
1195 { "sha512", GCRY_MD_SHA512
},
1196 { "md2", GCRY_MD_MD2
},
1197 { "md4", GCRY_MD_MD4
},
1198 { "tiger", GCRY_MD_TIGER
},
1199 { "haval", GCRY_MD_HAVAL
},
1205 size_t nframe
= (nbits
+7) / 8;
1208 size_t asnlen
, dlen
;
1210 for (i
=0; hashnames
[i
].name
; i
++)
1212 if ( strlen (hashnames
[i
].name
) == n
1213 && !memcmp (hashnames
[i
].name
, s
, n
))
1217 algo
= hashnames
[i
].algo
;
1219 dlen
= gcry_md_get_algo_dlen (algo
);
1221 if (!hashnames
[i
].name
)
1222 rc
= GPG_ERR_DIGEST_ALGO
;
1223 else if ( !(value
=gcry_sexp_nth_data (lhash
, 2, &valuelen
))
1225 rc
= GPG_ERR_INV_OBJ
;
1226 else if (gcry_md_algo_info (algo
, GCRYCTL_GET_ASNOID
, asn
, &asnlen
))
1227 rc
= GPG_ERR_NOT_IMPLEMENTED
; /* we don't have all of the above algos */
1228 else if ( valuelen
!= dlen
)
1230 /* hash value does not match the length of digest for
1232 rc
= GPG_ERR_CONFLICT
;
1234 else if( !dlen
|| dlen
+ asnlen
+ 4 > nframe
)
1236 /* can't encode an DLEN byte digest MD into a NFRAME byte frame */
1237 rc
= GPG_ERR_TOO_SHORT
;
1239 else if ( !(frame
= gcry_malloc (nframe
)) )
1240 rc
= gpg_err_code_from_errno (errno
);
1242 { /* assemble the pkcs#1 block type 1 */
1245 frame
[n
++] = 1; /* block type */
1246 i
= nframe
- valuelen
- asnlen
- 3 ;
1248 memset (frame
+n
, 0xff, i
);
1251 memcpy (frame
+n
, asn
, asnlen
);
1253 memcpy (frame
+n
, value
, valuelen
);
1255 assert (n
== nframe
);
1257 /* convert it into an MPI, FIXME: error checking? */
1258 gcry_mpi_scan (ret_mpi
, GCRYMPI_FMT_USG
, frame
, &nframe
);
1265 rc
= GPG_ERR_CONFLICT
;
1267 gcry_sexp_release (ldata
);
1268 gcry_sexp_release (lhash
);
1269 gcry_sexp_release (lvalue
);
1272 *flags
= parsed_flags
;
1279 Do a PK encrypt operation
1281 Caller has to provide a public key as the SEXP pkey and data as a
1282 SEXP with just one MPI in it. Alternativly S_DATA might be a
1283 complex S-Expression, similar to the one used for signature
1284 verification. This provides a flag which allows to handle PKCS#1
1285 block type 2 padding. The function returns a a sexp which may be
1286 passed to to pk_decrypt.
1288 Returns: 0 or an errorcode.
1290 s_data = See comment for sexp_data_to_mpi
1291 s_pkey = <key-as-defined-in-sexp_to_key>
1294 (<param_name1> <mpi>)
1296 (<param_namen> <mpi>)
1301 gcry_pk_encrypt (gcry_sexp_t
*r_ciph
, gcry_sexp_t s_data
, gcry_sexp_t s_pkey
)
1303 gcry_mpi_t
*pkey
= NULL
, data
= NULL
, *ciph
= NULL
;
1304 const char *algo_name
, *algo_elems
;
1307 gcry_pk_spec_t
*pubkey
= NULL
;
1308 gcry_module_t module
= NULL
;
1310 REGISTER_DEFAULT_PUBKEYS
;
1314 rc
= sexp_to_key (s_pkey
, 0, &pkey
, &module
);
1318 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
1319 algo_name
= pubkey
->name
;
1320 algo_elems
= pubkey
->elements_enc
;
1322 /* get the stuff we want to encrypt */
1323 rc
= sexp_data_to_mpi (s_data
, gcry_pk_get_nbits (s_pkey
), &data
, 1,
1329 /* Now we can encrypt data to ciph */
1330 ciph
= gcry_xcalloc (strlen (algo_elems
) + 1, sizeof (*ciph
));
1331 rc
= pubkey_encrypt (module
->mod_id
, ciph
, data
, pkey
, flags
);
1335 /* We did it. Now build the return list */
1340 size_t nelem
= strlen (algo_elems
);
1341 size_t needed
= 19 + strlen (algo_name
) + (nelem
* 5);
1343 if (flags
& PUBKEY_FLAG_NO_BLINDING
)
1346 /* Build the string. */
1347 string
= p
= gcry_xmalloc (needed
);
1348 p
= stpcpy ( p
, "(enc-val(flags" );
1349 if (flags
& PUBKEY_FLAG_NO_BLINDING
)
1350 p
= stpcpy (p
, " no-blinding");
1351 p
= stpcpy (p
, ")(");
1352 p
= stpcpy ( p
, algo_name
);
1353 for(i
=0; algo_elems
[i
]; i
++ ) {
1355 *p
++ = algo_elems
[i
];
1356 p
= stpcpy ( p
, "%m)" );
1360 /* and now the ugly part: we don't have a function to
1361 * pass an array to a format string, so we have to do it this way :-(
1366 void **arg_list
= malloc (sizeof (void *) * nelem
);
1369 for (i
= 0; i
< nelem
; i
++)
1370 arg_list
[i
] = &ciph
[i
];
1372 rc
= gcry_sexp_build_array (r_ciph
, NULL
, string
, arg_list
);
1377 rc
= gpg_err_code_from_errno (errno
);
1387 release_mpi_array (ciph
);
1393 ath_mutex_lock (&pubkeys_registered_lock
);
1394 _gcry_module_release (module
);
1395 ath_mutex_unlock (&pubkeys_registered_lock
);
1398 return gpg_error (rc
);
1402 * Do a PK decrypt operation
1404 * Caller has to provide a secret key as the SEXP skey and data in a
1405 * format as created by gcry_pk_encrypt. For historic reasons the
1406 * function returns simply an MPI as an S-expression part; this is
1407 * deprecated and the new method should be used which returns a real
1408 * S-expressionl this is selected by adding at least an empty flags
1411 * Returns: 0 or an errorcode.
1416 * (<param_name1> <mpi>)
1418 * (<param_namen> <mpi>)
1420 * s_skey = <key-as-defined-in-sexp_to_key>
1421 * r_plain= Either an incomplete S-expression without the parentheses
1422 * or if the flags list is used (even if empty) a real S-expression:
1425 gcry_pk_decrypt (gcry_sexp_t
*r_plain
, gcry_sexp_t s_data
, gcry_sexp_t s_skey
)
1427 gcry_mpi_t
*skey
= NULL
, *data
= NULL
, plain
= NULL
;
1428 int modern
, want_pkcs1
, flags
;
1430 gcry_module_t module_enc
= NULL
, module_key
= NULL
;
1431 gcry_pk_spec_t
*pubkey
= NULL
;
1433 REGISTER_DEFAULT_PUBKEYS
;
1436 rc
= sexp_to_key (s_skey
, 1, &skey
, &module_key
);
1439 rc
= sexp_to_enc (s_data
, &data
, &module_enc
, &modern
, &want_pkcs1
, &flags
);
1443 if (module_key
->mod_id
!= module_enc
->mod_id
)
1444 rc
= GPG_ERR_CONFLICT
; /* key algo does not match data algo */
1446 pubkey
= (gcry_pk_spec_t
*) module_key
->spec
;
1450 rc
= pubkey_decrypt (module_key
->mod_id
, &plain
, data
, skey
, flags
);
1456 if (gcry_sexp_build (r_plain
, NULL
, "%m", plain
))
1461 if (gcry_sexp_build (r_plain
, NULL
, "(value %m)", plain
))
1468 release_mpi_array (skey
);
1477 release_mpi_array (data
);
1481 if (module_key
|| module_enc
)
1483 ath_mutex_lock (&pubkeys_registered_lock
);
1485 _gcry_module_release (module_key
);
1487 _gcry_module_release (module_enc
);
1488 ath_mutex_unlock (&pubkeys_registered_lock
);
1491 return gpg_error (rc
);
1497 * Create a signature.
1499 * Caller has to provide a secret key as the SEXP skey and data
1500 * expressed as a SEXP list hash with only one element which should
1501 * instantly be available as a MPI. Alternatively the structure given
1502 * below may be used for S_HASH, it provides the abiliy to pass flags
1503 * to the operation; the only flag defined by now is "pkcs1" which
1504 * does PKCS#1 block type 1 style padding.
1506 * Returns: 0 or an errorcode.
1507 * In case of 0 the function returns a new SEXP with the
1508 * signature value; the structure of this signature depends on the
1509 * other arguments but is always suitable to be passed to
1512 * s_hash = See comment for sexp_data_to_mpi
1514 * s_skey = <key-as-defined-in-sexp_to_key>
1517 * (<param_name1> <mpi>)
1519 * (<param_namen> <mpi>)
1522 gcry_pk_sign (gcry_sexp_t
*r_sig
, gcry_sexp_t s_hash
, gcry_sexp_t s_skey
)
1524 gcry_mpi_t
*skey
= NULL
, hash
= NULL
, *result
= NULL
;
1525 gcry_pk_spec_t
*pubkey
= NULL
;
1526 gcry_module_t module
= NULL
;
1527 const char *key_algo_name
, *algo_name
, *algo_elems
;
1532 REGISTER_DEFAULT_PUBKEYS
;
1535 rc
= sexp_to_key (s_skey
, 1, &skey
, &module
);
1540 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
1541 algo_name
= key_algo_name
= pubkey
->name
;
1543 algo_elems
= pubkey
->elements_sig
;
1545 /* get the stuff we want to sign */
1546 /* Note that pk_get_nbits does also work on a private key */
1547 rc
= sexp_data_to_mpi (s_hash
, gcry_pk_get_nbits (s_skey
), &hash
, 0, NULL
);
1552 result
= gcry_xcalloc (strlen (algo_elems
) + 1, sizeof (*result
));
1553 rc
= pubkey_sign (module
->mod_id
, result
, hash
, skey
);
1559 size_t nelem
, needed
= strlen (algo_name
) + 20;
1561 nelem
= strlen (algo_elems
);
1563 /* count elements, so that we can allocate enough space */
1564 needed
+= 10 * nelem
;
1566 /* build the string */
1567 string
= p
= gcry_xmalloc (needed
);
1568 p
= stpcpy (p
, "(sig-val(");
1569 p
= stpcpy (p
, algo_name
);
1570 for(i
= 0; algo_elems
[i
]; i
++)
1573 *p
++ = algo_elems
[i
];
1574 p
= stpcpy (p
, "%m)");
1580 void **arg_list
= malloc (sizeof (void *) * nelem
);
1583 for (i
= 0; i
< nelem
; i
++)
1584 arg_list
[i
] = &result
[i
];
1586 rc
= gcry_sexp_build_array (r_sig
, NULL
, string
, arg_list
);
1591 rc
= gpg_err_code_from_errno (errno
);
1601 release_mpi_array (skey
);
1611 return gpg_error (rc
);
1616 * Verify a signature. Caller has to supply the public key pkey, the
1617 * signature sig and his hashvalue data. Public key has to be a
1618 * standard public key given as an S-Exp, sig is a S-Exp as returned
1619 * from gcry_pk_sign and data must be an S-Exp like the one in sign
1623 gcry_pk_verify (gcry_sexp_t s_sig
, gcry_sexp_t s_hash
, gcry_sexp_t s_pkey
)
1625 gcry_module_t module_key
= NULL
, module_sig
= NULL
;
1626 gcry_mpi_t
*pkey
= NULL
, hash
= NULL
, *sig
= NULL
;
1629 REGISTER_DEFAULT_PUBKEYS
;
1631 rc
= sexp_to_key (s_pkey
, 0, &pkey
, &module_key
);
1633 rc
= sexp_to_sig (s_sig
, &sig
, &module_sig
);
1636 && (module_key
->mod_id
!= module_sig
->mod_id
))
1637 rc
= GPG_ERR_CONFLICT
;
1640 rc
= sexp_data_to_mpi (s_hash
, gcry_pk_get_nbits (s_pkey
), &hash
, 0, 0);
1643 rc
= pubkey_verify (module_key
->mod_id
, hash
, sig
, pkey
, NULL
, NULL
);
1647 release_mpi_array (pkey
);
1652 release_mpi_array (sig
);
1658 if (module_key
|| module_sig
)
1660 ath_mutex_lock (&pubkeys_registered_lock
);
1662 _gcry_module_release (module_key
);
1664 _gcry_module_release (module_sig
);
1665 ath_mutex_unlock (&pubkeys_registered_lock
);
1668 return gpg_error (rc
);
1673 * Test a key. This may be used either for a public or a secret key
1674 * to see whether internal structre is valid.
1676 * Returns: 0 or an errorcode.
1678 * s_key = <key-as-defined-in-sexp_to_key>
1681 gcry_pk_testkey (gcry_sexp_t s_key
)
1683 gcry_module_t module
= NULL
;
1684 gcry_mpi_t
*key
= NULL
;
1687 REGISTER_DEFAULT_PUBKEYS
;
1689 /* Note we currently support only secret key checking */
1690 rc
= sexp_to_key (s_key
, 1, &key
, &module
);
1693 rc
= pubkey_check_secret_key (module
->mod_id
, key
);
1694 release_mpi_array (key
);
1697 return gpg_error (rc
);
1702 * Create a public key pair and return it in r_key.
1703 * How the key is created depends on s_parms:
1706 * (parameter_name_1 ....)
1708 * (parameter_name_n ....)
1710 * The key is returned in a format depending on the
1711 * algorithm. Both, private and secret keys are returned
1712 * and optionally some additional informatin.
1713 * For elgamal we return this structure:
1731 * (pm1-factors n1 n2 ... nn)
1736 gcry_pk_genkey (gcry_sexp_t
*r_key
, gcry_sexp_t s_parms
)
1738 gcry_pk_spec_t
*pubkey
= NULL
;
1739 gcry_module_t module
= NULL
;
1740 gcry_sexp_t list
= NULL
, l2
= NULL
;
1743 gpg_err_code_t rc
= GPG_ERR_NO_ERROR
;
1745 const char *algo_name
= NULL
;
1747 const char *sec_elems
= NULL
, *pub_elems
= NULL
;
1748 gcry_mpi_t skey
[10] = { NULL
}, *factors
= NULL
;
1749 unsigned int nbits
= 0;
1750 unsigned long use_e
= 0;
1752 REGISTER_DEFAULT_PUBKEYS
;
1755 list
= gcry_sexp_find_token (s_parms
, "genkey", 0);
1757 rc
= GPG_ERR_INV_OBJ
; /* Does not contain genkey data */
1761 l2
= gcry_sexp_cadr (list
);
1762 gcry_sexp_release (list
);
1766 rc
= GPG_ERR_NO_OBJ
; /* no cdr for the genkey */
1771 name
= gcry_sexp_nth_data (list
, 0, &n
);
1773 rc
= GPG_ERR_INV_OBJ
; /* algo string missing */
1778 char *name_terminated
= gcry_xmalloc (n
+ 1);
1779 strncpy (name_terminated
, name
, n
);
1780 name_terminated
[n
] = 0;
1782 ath_mutex_lock (&pubkeys_registered_lock
);
1783 module
= gcry_pk_lookup_name (name_terminated
);
1784 ath_mutex_unlock (&pubkeys_registered_lock
);
1786 free (name_terminated
);
1789 rc
= GPG_ERR_PUBKEY_ALGO
; /* unknown algorithm */
1792 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
1793 algo
= module
->mod_id
;
1794 algo_name
= pubkey
->name
;
1795 pub_elems
= pubkey
->elements_pkey
;
1796 sec_elems
= pubkey
->elements_skey
;
1802 l2
= gcry_sexp_find_token (list
, "rsa-use-e", 0);
1807 name
= gcry_sexp_nth_data (l2
, 1, &n
);
1808 if ((! name
) || (n
>= DIM (buf
) - 1))
1809 rc
= GPG_ERR_INV_OBJ
; /* no value or value too large */
1812 memcpy (buf
, name
, n
);
1814 use_e
= strtoul (buf
, NULL
, 0);
1816 gcry_sexp_release (l2
);
1820 use_e
= 65537; /* not given, use the value generated by old versions. */
1825 l2
= gcry_sexp_find_token (list
, "nbits", 0);
1826 gcry_sexp_release (list
);
1830 rc
= GPG_ERR_NO_OBJ
; /* no nbits parameter */
1833 name
= gcry_sexp_nth_data (list
, 1, &n
);
1835 rc
= GPG_ERR_INV_OBJ
; /* nbits without a cdr */
1838 char *p
= gcry_xmalloc (n
+ 1);
1839 memcpy (p
, name
, n
);
1841 nbits
= (unsigned int) strtol (p
, NULL
, 0);
1848 rc
= pubkey_generate (module
->mod_id
, nbits
, use_e
, skey
, &factors
);
1853 size_t nelem
=0, nelem_cp
= 0, needed
=0;
1854 gcry_mpi_t mpis
[30];
1856 nelem
= strlen (pub_elems
) + strlen (sec_elems
);
1857 for (i
= 0; factors
[i
]; i
++)
1861 needed
+= nelem
* 10;
1862 needed
+= 2 * strlen (algo_name
) + 300;
1863 if (nelem
> DIM (mpis
))
1866 /* build the string */
1868 string
= p
= gcry_xmalloc (needed
);
1869 p
= stpcpy (p
, "(key-data");
1870 p
= stpcpy (p
, "(public-key(");
1871 p
= stpcpy (p
, algo_name
);
1872 for(i
= 0; pub_elems
[i
]; i
++)
1875 *p
++ = pub_elems
[i
];
1876 p
= stpcpy (p
, "%m)");
1877 mpis
[nelem
++] = skey
[i
];
1879 p
= stpcpy (p
, "))");
1880 p
= stpcpy (p
, "(private-key(");
1881 p
= stpcpy (p
, algo_name
);
1882 for (i
= 0; sec_elems
[i
]; i
++)
1885 *p
++ = sec_elems
[i
];
1886 p
= stpcpy (p
, "%m)");
1887 mpis
[nelem
++] = skey
[i
];
1889 p
= stpcpy (p
, "))");
1891 /* Very ugly hack to make release_mpi_array() work FIXME */
1894 p
= stpcpy (p
, "(misc-key-info(pm1-factors");
1895 for(i
= 0; factors
[i
]; i
++)
1897 p
= stpcpy (p
, "%m");
1898 mpis
[nelem
++] = factors
[i
];
1902 while (nelem
< DIM (mpis
))
1903 mpis
[nelem
++] = NULL
;
1906 int elem_n
= strlen (pub_elems
) + strlen (sec_elems
), i
;
1907 void **arg_list
= malloc (sizeof (void *) * nelem_cp
);
1910 for (i
= 0; i
< elem_n
; i
++)
1911 arg_list
[i
] = &mpis
[i
];
1912 for (; i
< nelem_cp
; i
++)
1913 arg_list
[i
] = &factors
[i
- elem_n
];
1915 rc
= gcry_sexp_build_array (r_key
, NULL
, string
, arg_list
);
1920 rc
= gpg_err_code_from_errno (errno
);
1925 assert (DIM (mpis
) == 30); /* ? */
1929 release_mpi_array (skey
);
1930 /* no free: skey is a static array */
1934 release_mpi_array ( factors
);
1935 gcry_free (factors
);
1939 gcry_sexp_release (l2
);
1941 gcry_sexp_release (list
);
1945 ath_mutex_lock (&pubkeys_registered_lock
);
1946 _gcry_module_release (module
);
1947 ath_mutex_unlock (&pubkeys_registered_lock
);
1950 return gpg_error (rc
);
1954 * Get the number of nbits from the public key
1955 * Hmmm: Should we have really this function or is it
1956 * better to have a more general function to retrieve
1957 * different propoerties of the key?
1960 gcry_pk_get_nbits (gcry_sexp_t key
)
1962 gcry_module_t module
= NULL
;
1963 gcry_pk_spec_t
*pubkey
;
1964 gcry_mpi_t
*keyarr
= NULL
;
1965 unsigned int nbits
= 0;
1968 REGISTER_DEFAULT_PUBKEYS
;
1970 rc
= sexp_to_key (key
, 0, &keyarr
, &module
);
1971 if (rc
== GPG_ERR_INV_OBJ
)
1972 rc
= sexp_to_key (key
, 1, &keyarr
, &module
);
1977 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
1978 nbits
= (*pubkey
->get_nbits
) (module
->mod_id
, keyarr
);
1980 ath_mutex_lock (&pubkeys_registered_lock
);
1981 _gcry_module_release (module
);
1982 ath_mutex_unlock (&pubkeys_registered_lock
);
1985 release_mpi_array (keyarr
);
1992 /* Return the so called KEYGRIP which is the SHA-1 hash of the public
1993 key parameters expressed in a way depended on the algorithm.
1995 ARRAY must either be 20 bytes long or NULL; in the latter case a
1996 newly allocated array of that size is returned, otherwise ARRAY or
1997 NULL is returned to indicate an error which is most likely an
1998 unknown algorithm. The function accepts public or secret keys. */
2000 gcry_pk_get_keygrip (gcry_sexp_t key
, unsigned char *array
)
2002 gcry_sexp_t list
= NULL
, l2
= NULL
;
2003 gcry_pk_spec_t
*pubkey
= NULL
;
2004 gcry_module_t module
= NULL
;
2005 const char *s
, *name
;
2010 gcry_md_hd_t md
= NULL
;
2012 REGISTER_DEFAULT_PUBKEYS
;
2014 /* check that the first element is valid */
2015 list
= gcry_sexp_find_token (key
, "public-key", 0);
2017 list
= gcry_sexp_find_token (key
, "private-key", 0);
2019 list
= gcry_sexp_find_token (key
, "protected-private-key", 0);
2021 return NULL
; /* no public- or private-key object */
2023 l2
= gcry_sexp_cadr (list
);
2024 gcry_sexp_release (list
);
2028 name
= gcry_sexp_nth_data (list
, 0, &n
);
2030 goto fail
; /* invalid structure of object */
2034 char *name_terminated
= gcry_xmalloc (n
+ 1);
2035 strncpy (name_terminated
, name
, n
);
2036 name_terminated
[n
] = 0;
2038 ath_mutex_lock (&pubkeys_registered_lock
);
2039 module
= gcry_pk_lookup_name (name_terminated
);
2040 ath_mutex_unlock (&pubkeys_registered_lock
);
2042 free (name_terminated
);
2046 goto fail
; /* unknown algorithm */
2048 pubkey
= (gcry_pk_spec_t
*) module
->spec
;
2050 /* FIXME, special handling should be implemented by the algorithms,
2051 not by the libgcrypt core. */
2052 is_rsa
= module
->mod_id
== GCRY_PK_RSA
;
2053 elems
= pubkey
->elements_grip
;
2055 goto fail
; /* no grip parameter */
2057 if (gcry_md_open (&md
, GCRY_MD_SHA1
, 0))
2060 for (idx
= 0, s
= elems
; *s
; s
++, idx
++)
2065 l2
= gcry_sexp_find_token (list
, s
, 1);
2068 data
= gcry_sexp_nth_data (l2
, 1, &datalen
);
2075 sprintf (buf
, "(1:%c%u:", *s
, (unsigned int)datalen
);
2076 gcry_md_write (md
, buf
, strlen (buf
));
2078 /* pkcs-15 says that for RSA only the modulus should be hashed -
2079 however, it is not clear wether this is meant to has the raw
2080 bytes assuming this is an unsigned integer or whether the DER
2081 required 0 should be prefixed. We hash th raw bytes. For
2082 non-RSA we hash S-expressions. */
2083 gcry_md_write (md
, data
, datalen
);
2084 gcry_sexp_release (l2
);
2086 gcry_md_write (md
, ")", 1);
2091 array
= gcry_malloc (20);
2095 memcpy (array
, gcry_md_read (md
, GCRY_MD_SHA1
), 20);
2097 gcry_sexp_release (list
);
2102 gcry_sexp_release (l2
);
2105 gcry_sexp_release (list
);
2111 gcry_pk_ctl (int cmd
, void *buffer
, size_t buflen
)
2113 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
2115 REGISTER_DEFAULT_PUBKEYS
;
2119 case GCRYCTL_DISABLE_ALGO
:
2120 /* this one expects a buffer pointing to an integer with the
2123 if ((! buffer
) || (buflen
!= sizeof (int)))
2124 err
= GPG_ERR_CIPHER_ALGO
; /* FIXME? */
2126 disable_pubkey_algo (*((int *) buffer
));
2130 err
= GPG_ERR_INV_OP
;
2133 return gpg_error (err
);
2138 * Return information about the given algorithm
2139 * WHAT select the kind of information returned:
2140 * GCRYCTL_TEST_ALGO:
2141 * Returns 0 when the specified algorithm is available for use.
2142 * Buffer must be NULL, nbytes may have the address of a variable
2143 * with the required usage of the algorithm. It may be 0 for don't
2144 * care or a combination of the GCRY_PK_USAGE_xxx flags;
2145 * GCRYCTL_GET_ALGO_USAGE:
2146 * Return the usage glafs for the give algo. An invalid alog
2147 * does return 0. Disabled algos are ignored here becuase we
2148 * only want to know whether the algo is at all capable of
2151 * Note: Because this function is in most cases used to return an
2152 * integer value, we can make it easier for the caller to just look at
2153 * the return value. The caller will in all cases consult the value
2154 * and thereby detecting whether a error occured or not (i.e. while checking
2158 gcry_pk_algo_info (int algorithm
, int what
, void *buffer
, size_t *nbytes
)
2160 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
2164 case GCRYCTL_TEST_ALGO
:
2166 int use
= nbytes
? *nbytes
: 0;
2168 err
= GPG_ERR_INV_ARG
;
2169 else if (check_pubkey_algo (algorithm
, use
))
2170 err
= GPG_ERR_PUBKEY_ALGO
;
2174 case GCRYCTL_GET_ALGO_USAGE
:
2176 gcry_module_t pubkey
;
2179 REGISTER_DEFAULT_PUBKEYS
;
2181 ath_mutex_lock (&pubkeys_registered_lock
);
2182 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
2185 use
= ((gcry_pk_spec_t
*) pubkey
->spec
)->use
;
2186 _gcry_module_release (pubkey
);
2188 ath_mutex_unlock (&pubkeys_registered_lock
);
2194 case GCRYCTL_GET_ALGO_NPKEY
:
2197 int npkey
= pubkey_get_npkey (algorithm
);
2201 case GCRYCTL_GET_ALGO_NSKEY
:
2204 int nskey
= pubkey_get_nskey (algorithm
);
2208 case GCRYCTL_GET_ALGO_NSIGN
:
2211 int nsign
= pubkey_get_nsig (algorithm
);
2215 case GCRYCTL_GET_ALGO_NENCR
:
2218 int nencr
= pubkey_get_nenc (algorithm
);
2224 err
= GPG_ERR_INV_OP
;
2227 return gpg_error (err
);
2231 _gcry_pk_init (void)
2233 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
2235 REGISTER_DEFAULT_PUBKEYS
;
2241 _gcry_pk_module_lookup (int algorithm
, gcry_module_t
*module
)
2243 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
2244 gcry_module_t pubkey
;
2246 REGISTER_DEFAULT_PUBKEYS
;
2248 ath_mutex_lock (&pubkeys_registered_lock
);
2249 pubkey
= _gcry_module_lookup_id (pubkeys_registered
, algorithm
);
2253 err
= GPG_ERR_PUBKEY_ALGO
;
2254 ath_mutex_unlock (&pubkeys_registered_lock
);
2260 _gcry_pk_module_release (gcry_module_t module
)
2262 ath_mutex_lock (&pubkeys_registered_lock
);
2263 _gcry_module_release (module
);
2264 ath_mutex_unlock (&pubkeys_registered_lock
);
2267 /* Get a list consisting of the IDs of the loaded pubkey modules. If
2268 LIST is zero, write the number of loaded pubkey modules to
2269 LIST_LENGTH and return. If LIST is non-zero, the first
2270 *LIST_LENGTH algorithm IDs are stored in LIST, which must be of
2271 according size. In case there are less pubkey modules than
2272 *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */
2274 gcry_pk_list (int *list
, int *list_length
)
2276 gpg_err_code_t err
= GPG_ERR_NO_ERROR
;
2278 ath_mutex_lock (&pubkeys_registered_lock
);
2279 err
= _gcry_module_list (pubkeys_registered
, list
, list_length
);
2280 ath_mutex_unlock (&pubkeys_registered_lock
);