2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Alistair Crooks (agc@NetBSD.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
31 * All rights reserved.
32 * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
33 * their moral rights under the UK Copyright Design and Patents Act 1988 to
34 * be recorded as the authors of this copyright work.
36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
37 * use this file except in compliance with the License.
39 * You may obtain a copy of the License at
40 * http://www.apache.org/licenses/LICENSE-2.0
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS,
44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46 * See the License for the specific language governing permissions and
47 * limitations under the License.
54 #ifdef HAVE_SYS_CDEFS_H
55 #include <sys/cdefs.h>
58 #if defined(__NetBSD__)
59 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
60 __RCSID("$NetBSD: openssl_crypto.c,v 1.17 2009/10/07 16:19:51 agc Exp $");
63 #ifdef HAVE_OPENSSL_DSA_H
64 #include <openssl/dsa.h>
67 #ifdef HAVE_OPENSSL_RSA_H
68 #include <openssl/rsa.h>
71 #ifdef HAVE_OPENSSL_ERR_H
72 #include <openssl/err.h>
75 #include <openssl/pem.h>
76 #include <openssl/evp.h>
80 /* Hash size for secret key check */
84 #include "readerwriter.h"
85 #include "netpgpdefs.h"
86 #include "netpgpdigest.h"
91 test_seckey(const __ops_seckey_t
*seckey
)
93 RSA
*test
= RSA_new();
95 test
->n
= BN_dup(seckey
->pubkey
.key
.rsa
.n
);
96 test
->e
= BN_dup(seckey
->pubkey
.key
.rsa
.e
);
98 test
->d
= BN_dup(seckey
->key
.rsa
.d
);
99 test
->p
= BN_dup(seckey
->key
.rsa
.p
);
100 test
->q
= BN_dup(seckey
->key
.rsa
.q
);
102 if (RSA_check_key(test
) != 1) {
103 (void) fprintf(stderr
,
104 "test_seckey: RSA_check_key failed\n");
110 md5_init(__ops_hash_t
*hash
)
113 (void) fprintf(stderr
, "md5_init: hash data non-null\n");
115 if ((hash
->data
= calloc(1, sizeof(MD5_CTX
))) == NULL
) {
116 (void) fprintf(stderr
, "md5_init: bad alloc\n");
119 MD5_Init(hash
->data
);
124 md5_add(__ops_hash_t
*hash
, const unsigned char *data
, unsigned length
)
126 MD5_Update(hash
->data
, data
, length
);
130 md5_finish(__ops_hash_t
*hash
, unsigned char *out
)
132 MD5_Final(out
, hash
->data
);
138 static const __ops_hash_t md5
= {
150 \brief Initialise to MD5
151 \param hash Hash to initialise
154 __ops_hash_md5(__ops_hash_t
*hash
)
160 sha1_init(__ops_hash_t
*hash
)
162 if (__ops_get_debug_level(__FILE__
)) {
163 fprintf(stderr
, "***\n***\nsha1_init\n***\n");
166 (void) fprintf(stderr
, "sha1_init: hash data non-null\n");
168 if ((hash
->data
= calloc(1, sizeof(SHA_CTX
))) == NULL
) {
169 (void) fprintf(stderr
, "sha1_init: bad alloc\n");
172 SHA1_Init(hash
->data
);
177 sha1_add(__ops_hash_t
*hash
, const unsigned char *data
, unsigned length
)
179 if (__ops_get_debug_level(__FILE__
)) {
182 (void) fprintf(stderr
, "adding %u to hash:\n ", length
);
183 for (i
= 0; i
< length
; i
++) {
184 (void) fprintf(stderr
, "0x%02x ", data
[i
]);
185 if (!((i
+ 1) % 16)) {
186 (void) fprintf(stderr
, "\n");
187 } else if (!((i
+ 1) % 8)) {
188 (void) fprintf(stderr
, " ");
191 (void) fprintf(stderr
, "\n");
193 SHA1_Update(hash
->data
, data
, length
);
197 sha1_finish(__ops_hash_t
*hash
, unsigned char *out
)
199 SHA1_Final(out
, hash
->data
);
200 if (__ops_get_debug_level(__FILE__
)) {
203 (void) fprintf(stderr
, "***\n***\nsha1_finish\n***\n");
204 for (i
= 0; i
< OPS_SHA1_HASH_SIZE
; i
++)
205 (void) fprintf(stderr
, "0x%02x ", out
[i
]);
206 (void) fprintf(stderr
, "\n");
210 return OPS_SHA1_HASH_SIZE
;
213 static const __ops_hash_t sha1
= {
225 \brief Initialise to SHA1
226 \param hash Hash to initialise
229 __ops_hash_sha1(__ops_hash_t
*hash
)
235 sha256_init(__ops_hash_t
*hash
)
237 if (__ops_get_debug_level(__FILE__
)) {
238 fprintf(stderr
, "***\n***\nsha256_init\n***\n");
241 (void) fprintf(stderr
, "sha256_init: hash data non-null\n");
243 if ((hash
->data
= calloc(1, sizeof(SHA256_CTX
))) == NULL
) {
244 (void) fprintf(stderr
, "sha256_init: bad alloc\n");
247 SHA256_Init(hash
->data
);
252 sha256_add(__ops_hash_t
*hash
, const unsigned char *data
, unsigned length
)
254 if (__ops_get_debug_level(__FILE__
)) {
257 (void) fprintf(stderr
, "adding %d to hash:\n ", length
);
258 for (i
= 0; i
< length
; i
++) {
259 (void) fprintf(stderr
, "0x%02x ", data
[i
]);
261 (void) fprintf(stderr
, "\n");
262 else if (!((i
+ 1) % 8))
263 (void) fprintf(stderr
, " ");
265 (void) fprintf(stderr
, "\n");
267 SHA256_Update(hash
->data
, data
, length
);
271 sha256_finish(__ops_hash_t
*hash
, unsigned char *out
)
273 SHA256_Final(out
, hash
->data
);
274 if (__ops_get_debug_level(__FILE__
)) {
277 (void) fprintf(stderr
, "***\n***\nsha1_finish\n***\n");
278 for (i
= 0; i
< SHA256_DIGEST_LENGTH
; i
++)
279 (void) fprintf(stderr
, "0x%02x ", out
[i
]);
280 (void) fprintf(stderr
, "\n");
284 return SHA256_DIGEST_LENGTH
;
287 static const __ops_hash_t sha256
= {
289 SHA256_DIGEST_LENGTH
,
298 __ops_hash_sha256(__ops_hash_t
*hash
)
307 sha384_init(__ops_hash_t
*hash
)
309 if (__ops_get_debug_level(__FILE__
)) {
310 (void) fprintf(stderr
, "***\n***\nsha384_init\n***\n");
313 (void) fprintf(stderr
, "sha384_init: hash data non-null\n");
315 if ((hash
->data
= calloc(1, sizeof(SHA512_CTX
))) == NULL
) {
316 (void) fprintf(stderr
, "sha512_init: bad alloc\n");
319 SHA384_Init(hash
->data
);
324 sha384_add(__ops_hash_t
*hash
, const unsigned char *data
, unsigned length
)
326 if (__ops_get_debug_level(__FILE__
)) {
329 (void) fprintf(stderr
, "adding %u to hash:\n ", length
);
330 for (i
= 0; i
< length
; i
++) {
331 (void) fprintf(stderr
, "0x%02x ", data
[i
]);
333 (void) fprintf(stderr
, "\n");
334 else if (!((i
+ 1) % 8))
335 (void) fprintf(stderr
, " ");
337 (void) fprintf(stderr
, "\n");
339 SHA384_Update(hash
->data
, data
, length
);
343 sha384_finish(__ops_hash_t
*hash
, unsigned char *out
)
345 SHA384_Final(out
, hash
->data
);
346 if (__ops_get_debug_level(__FILE__
)) {
349 (void) fprintf(stderr
, "***\n***\nsha1_finish\n***\n");
350 for (i
= 0; i
< SHA384_DIGEST_LENGTH
; i
++)
351 (void) fprintf(stderr
, "0x%02x ", out
[i
]);
352 (void) fprintf(stderr
, "\n");
356 return SHA384_DIGEST_LENGTH
;
359 static const __ops_hash_t sha384
= {
361 SHA384_DIGEST_LENGTH
,
370 __ops_hash_sha384(__ops_hash_t
*hash
)
379 sha512_init(__ops_hash_t
*hash
)
381 if (__ops_get_debug_level(__FILE__
)) {
382 (void) fprintf(stderr
, "***\n***\nsha512_init\n***\n");
385 (void) fprintf(stderr
, "sha512_init: hash data non-null\n");
387 if ((hash
->data
= calloc(1, sizeof(SHA512_CTX
))) == NULL
) {
388 (void) fprintf(stderr
, "sha512_init: bad alloc\n");
391 SHA512_Init(hash
->data
);
396 sha512_add(__ops_hash_t
*hash
, const unsigned char *data
, unsigned length
)
398 if (__ops_get_debug_level(__FILE__
)) {
401 (void) fprintf(stderr
, "adding %u to hash:\n ", length
);
402 for (i
= 0; i
< length
; i
++) {
403 (void) fprintf(stderr
, "0x%02x ", data
[i
]);
405 (void) fprintf(stderr
, "\n");
406 else if (!((i
+ 1) % 8))
407 (void) fprintf(stderr
, " ");
409 (void) fprintf(stderr
, "\n");
411 SHA512_Update(hash
->data
, data
, length
);
415 sha512_finish(__ops_hash_t
*hash
, unsigned char *out
)
417 SHA512_Final(out
, hash
->data
);
418 if (__ops_get_debug_level(__FILE__
)) {
421 (void) fprintf(stderr
, "***\n***\nsha1_finish\n***\n");
422 for (i
= 0; i
< SHA512_DIGEST_LENGTH
; i
++)
423 (void) fprintf(stderr
, "0x%02x ", out
[i
]);
424 (void) fprintf(stderr
, "\n");
428 return SHA512_DIGEST_LENGTH
;
431 static const __ops_hash_t sha512
= {
433 SHA512_DIGEST_LENGTH
,
442 __ops_hash_sha512(__ops_hash_t
*hash
)
452 sha224_init(__ops_hash_t
*hash
)
454 if (__ops_get_debug_level(__FILE__
)) {
455 (void) fprintf(stderr
, "***\n***\nsha1_init\n***\n");
458 (void) fprintf(stderr
, "sha224_init: hash data non-null\n");
460 if ((hash
->data
= calloc(1, sizeof(SHA256_CTX
))) == NULL
) {
461 (void) fprintf(stderr
, "sha256_init: bad alloc\n");
464 SHA224_Init(hash
->data
);
469 sha224_add(__ops_hash_t
*hash
, const unsigned char *data
, unsigned length
)
471 if (__ops_get_debug_level(__FILE__
)) {
474 (void) fprintf(stderr
, "adding %u to hash:\n ", length
);
475 for (i
= 0; i
< length
; i
++) {
476 (void) fprintf(stderr
, "0x%02x ", data
[i
]);
478 (void) fprintf(stderr
, "\n");
479 else if (!((i
+ 1) % 8))
480 (void) fprintf(stderr
, " ");
482 (void) fprintf(stderr
, "\n");
484 SHA224_Update(hash
->data
, data
, length
);
488 sha224_finish(__ops_hash_t
*hash
, unsigned char *out
)
490 SHA224_Final(out
, hash
->data
);
491 if (__ops_get_debug_level(__FILE__
)) {
494 (void) fprintf(stderr
, "***\n***\nsha1_finish\n***\n");
495 for (i
= 0; i
< SHA224_DIGEST_LENGTH
; i
++)
496 (void) fprintf(stderr
, "0x%02x ", out
[i
]);
497 (void) fprintf(stderr
, "\n");
501 return SHA224_DIGEST_LENGTH
;
504 static const __ops_hash_t sha224
= {
506 SHA224_DIGEST_LENGTH
,
515 __ops_hash_sha224(__ops_hash_t
*hash
)
521 __ops_dsa_verify(const unsigned char *hash
, size_t hash_length
,
522 const __ops_dsa_sig_t
*sig
,
523 const __ops_dsa_pubkey_t
*dsa
)
530 osig
= DSA_SIG_new();
538 odsa
->pub_key
= dsa
->y
;
540 if (__ops_get_debug_level(__FILE__
)) {
543 (void) fprintf(stderr
, "hash passed in:\n");
544 for (i
= 0; i
< hash_length
; i
++) {
545 (void) fprintf(stderr
, "%02x ", hash
[i
]);
547 (void) fprintf(stderr
, "\n");
548 printf("hash_length=%" PRIsize
"d\n", hash_length
);
549 printf("Q=%d\n", BN_num_bytes(odsa
->q
));
551 /* XXX - Flexelint - Info 732: Loss of sign (assignment) (int to unsigned int) */
552 if ((qlen
= BN_num_bytes(odsa
->q
)) < hash_length
) {
555 ret
= DSA_do_verify(hash
, (int)hash_length
, osig
, odsa
);
556 if (__ops_get_debug_level(__FILE__
)) {
557 (void) fprintf(stderr
, "ret=%d\n", ret
);
560 (void) fprintf(stderr
, "__ops_dsa_verify: DSA verification\n");
564 odsa
->p
= odsa
->q
= odsa
->g
= odsa
->pub_key
= NULL
;
567 osig
->r
= osig
->s
= NULL
;
570 return (unsigned)ret
;
575 \brief Recovers message digest from the signature
576 \param out Where to write decrypted data to
577 \param in Encrypted data
578 \param length Length of encrypted data
579 \param pubkey RSA public key
580 \return size of recovered message digest
583 __ops_rsa_public_decrypt(unsigned char *out
,
584 const unsigned char *in
,
586 const __ops_rsa_pubkey_t
*pubkey
)
595 n
= RSA_public_decrypt((int)length
, in
, out
, orsa
, RSA_NO_PADDING
);
597 orsa
->n
= orsa
->e
= NULL
;
605 \brief Signs data with RSA
606 \param out Where to write signature
607 \param in Data to sign
608 \param length Length of data
609 \param seckey RSA secret key
610 \param pubkey RSA public key
611 \return number of bytes decrypted
614 __ops_rsa_private_encrypt(unsigned char *out
,
615 const unsigned char *in
,
617 const __ops_rsa_seckey_t
*seckey
,
618 const __ops_rsa_pubkey_t
*pubkey
)
624 orsa
->n
= pubkey
->n
; /* XXX: do we need n? */
631 /* If this isn't set, it's very likely that the programmer hasn't */
632 /* decrypted the secret key. RSA_check_key segfaults in that case. */
633 /* Use __ops_decrypt_seckey() to do that. */
634 if (orsa
->d
== NULL
) {
635 (void) fprintf(stderr
, "orsa is not set\n");
638 if (RSA_check_key(orsa
) != 1) {
639 (void) fprintf(stderr
, "RSA_check_key is not set\n");
644 n
= RSA_private_encrypt((int)length
, in
, out
, orsa
, RSA_NO_PADDING
);
646 orsa
->n
= orsa
->d
= orsa
->p
= orsa
->q
= NULL
;
654 \brief Decrypts RSA-encrypted data
655 \param out Where to write the plaintext
656 \param in Encrypted data
657 \param length Length of encrypted data
658 \param seckey RSA secret key
659 \param pubkey RSA public key
660 \return size of recovered plaintext
663 __ops_rsa_private_decrypt(unsigned char *out
,
664 const unsigned char *in
,
666 const __ops_rsa_seckey_t
*seckey
,
667 const __ops_rsa_pubkey_t
*pubkey
)
674 keypair
->n
= pubkey
->n
; /* XXX: do we need n? */
675 keypair
->d
= seckey
->d
;
676 keypair
->p
= seckey
->q
;
677 keypair
->q
= seckey
->p
;
680 keypair
->e
= pubkey
->e
;
681 if (RSA_check_key(keypair
) != 1) {
682 (void) fprintf(stderr
, "RSA_check_key is not set\n");
687 n
= RSA_private_decrypt((int)length
, in
, out
, keypair
, RSA_NO_PADDING
);
689 if (__ops_get_debug_level(__FILE__
)) {
690 printf("__ops_rsa_private_decrypt: n=%d\n",n
);
695 unsigned long err
= ERR_get_error();
697 ERR_error_string(err
, &errbuf
[0]);
698 (void) fprintf(stderr
, "openssl error : %s\n", errbuf
);
700 keypair
->n
= keypair
->d
= keypair
->p
= keypair
->q
= NULL
;
708 \brief RSA-encrypts data
709 \param out Where to write the encrypted data
711 \param length Size of plaintext
712 \param pubkey RSA Public Key
715 __ops_rsa_public_encrypt(unsigned char *out
,
716 const unsigned char *in
,
718 const __ops_rsa_pubkey_t
*pubkey
)
723 /* printf("__ops_rsa_public_encrypt: length=%ld\n", length); */
729 /* printf("len: %ld\n", length); */
730 /* __ops_print_bn("n: ", orsa->n); */
731 /* __ops_print_bn("e: ", orsa->e); */
732 n
= RSA_public_encrypt((int)length
, in
, out
, orsa
, RSA_NO_PADDING
);
737 fd_out
= BIO_new_fd(fileno(stderr
), BIO_NOCLOSE
);
738 ERR_print_errors(fd_out
);
740 orsa
->n
= orsa
->e
= NULL
;
748 \brief initialises openssl
749 \note Would usually call __ops_init() instead
753 __ops_crypto_init(void)
756 CRYPTO_malloc_debug_init();
757 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL
);
758 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
764 \brief Finalise openssl
765 \note Would usually call __ops_finish() instead
769 __ops_crypto_finish(void)
771 CRYPTO_cleanup_all_ex_data();
772 ERR_remove_state((unsigned long)0);
774 CRYPTO_mem_leaks_fp(stderr
);
781 \param hash Hash struct
785 __ops_text_from_hash(__ops_hash_t
*hash
)
791 \ingroup HighLevel_KeyGenerate
792 \brief Generates an RSA keypair
793 \param numbits Modulus size
794 \param e Public Exponent
795 \param keydata Pointer to keydata struct to hold new key
796 \return 1 if key generated successfully; otherwise 0
797 \note It is the caller's responsibility to call __ops_keydata_free(keydata)
800 rsa_generate_keypair(__ops_key_t
*keydata
,
802 const unsigned long e
)
804 __ops_seckey_t
*seckey
;
807 __ops_output_t
*output
;
811 __ops_keydata_init(keydata
, OPS_PTAG_CT_SECRET_KEY
);
812 seckey
= __ops_get_writable_seckey(keydata
);
814 /* generate the key pair */
816 rsa
= RSA_generate_key(numbits
, e
, NULL
, NULL
);
818 /* populate __ops key from ssl key */
820 seckey
->pubkey
.version
= OPS_V4
;
821 seckey
->pubkey
.birthtime
= time(NULL
);
822 seckey
->pubkey
.days_valid
= 0;
823 seckey
->pubkey
.alg
= OPS_PKA_RSA
;
825 seckey
->pubkey
.key
.rsa
.n
= BN_dup(rsa
->n
);
826 seckey
->pubkey
.key
.rsa
.e
= BN_dup(rsa
->e
);
828 seckey
->s2k_usage
= OPS_S2KU_ENCRYPTED_AND_HASHED
;
829 seckey
->s2k_specifier
= OPS_S2KS_SALTED
;
830 /* seckey->s2k_specifier=OPS_S2KS_SIMPLE; */
831 seckey
->alg
= OPS_SA_CAST5
; /* \todo make param */
832 seckey
->hash_alg
= OPS_HASH_SHA1
; /* \todo make param */
834 seckey
->checksum
= 0;
836 seckey
->key
.rsa
.d
= BN_dup(rsa
->d
);
837 seckey
->key
.rsa
.p
= BN_dup(rsa
->p
);
838 seckey
->key
.rsa
.q
= BN_dup(rsa
->q
);
839 seckey
->key
.rsa
.u
= BN_mod_inverse(NULL
, rsa
->p
, rsa
->q
, ctx
);
840 if (seckey
->key
.rsa
.u
== NULL
) {
841 (void) fprintf(stderr
, "seckey->key.rsa.u is NULL\n");
848 __ops_keyid(keydata
->key_id
, OPS_KEY_ID_SIZE
,
849 &keydata
->key
.seckey
.pubkey
);
850 __ops_fingerprint(&keydata
->fingerprint
, &keydata
->key
.seckey
.pubkey
);
852 /* Generate checksum */
857 __ops_setup_memory_write(&output
, &mem
, 128);
859 __ops_push_checksum_writer(output
, seckey
);
861 switch (seckey
->pubkey
.alg
) {
862 /* case OPS_PKA_DSA: */
863 /* return __ops_write_mpi(output, key->key.dsa.x); */
866 case OPS_PKA_RSA_ENCRYPT_ONLY
:
867 case OPS_PKA_RSA_SIGN_ONLY
:
868 if (!__ops_write_mpi(output
, seckey
->key
.rsa
.d
) ||
869 !__ops_write_mpi(output
, seckey
->key
.rsa
.p
) ||
870 !__ops_write_mpi(output
, seckey
->key
.rsa
.q
) ||
871 !__ops_write_mpi(output
, seckey
->key
.rsa
.u
)) {
876 /* case OPS_PKA_ELGAMAL: */
877 /* return __ops_write_mpi(output, key->key.elgamal.x); */
880 (void) fprintf(stderr
, "Bad seckey->pubkey.alg\n");
884 /* close rather than pop, since its the only one on the stack */
885 __ops_writer_close(output
);
886 __ops_teardown_memory_write(output
, mem
);
888 /* should now have checksum in seckey struct */
891 if (__ops_get_debug_level(__FILE__
)) {
899 \ingroup HighLevel_KeyGenerate
900 \brief Creates a self-signed RSA keypair
901 \param numbits Modulus size
902 \param e Public Exponent
903 \param userid User ID
904 \return The new keypair or NULL
906 \note It is the caller's responsibility to call __ops_keydata_free(keydata)
907 \sa rsa_generate_keypair()
908 \sa __ops_keydata_free()
911 __ops_rsa_new_selfsign_key(const int numbits
,
912 const unsigned long e
,
913 __ops_userid_t
*userid
)
915 __ops_key_t
*keydata
;
917 keydata
= __ops_keydata_new();
918 if (!rsa_generate_keypair(keydata
, numbits
, e
) ||
919 !__ops_add_selfsigned_userid(keydata
, userid
)) {
920 __ops_keydata_free(keydata
);
927 __ops_dsa_sign(unsigned char *hashbuf
,
929 const __ops_dsa_seckey_t
*secdsa
,
930 const __ops_dsa_pubkey_t
*pubdsa
)
939 odsa
->pub_key
= pubdsa
->y
;
940 odsa
->priv_key
= secdsa
->x
;
942 dsasig
= DSA_do_sign(hashbuf
, (int)hashsize
, odsa
);
944 odsa
->p
= odsa
->q
= odsa
->g
= odsa
->pub_key
= odsa
->priv_key
= NULL
;
951 openssl_read_pem_seckey(const char *f
, __ops_key_t
*key
, const char *type
)
958 if ((fp
= fopen(f
, "r")) == NULL
) {
959 (void) fprintf(stderr
, "can't open '%s'\n", f
);
963 if (strcmp(type
, "ssh-rsa") == 0) {
964 if ((rsa
= PEM_read_RSAPrivateKey(fp
, NULL
, NULL
, NULL
)) == NULL
) {
967 key
->key
.seckey
.key
.rsa
.d
= rsa
->d
;
968 key
->key
.seckey
.key
.rsa
.p
= rsa
->p
;
969 key
->key
.seckey
.key
.rsa
.q
= rsa
->q
;
970 key
->key
.seckey
.key
.rsa
.d
= rsa
->d
;
972 } else if (strcmp(type
, "ssh-dss") == 0) {
973 if ((dsa
= PEM_read_DSAPrivateKey(fp
, NULL
, NULL
, NULL
)) == NULL
) {
976 key
->key
.seckey
.key
.dsa
.x
= dsa
->priv_key
;