xref: /openbsd-src/lib/libcrypto/dsa/dsa_prn.c (revision 81dbdade91b3e62ec2fd29e5611b1a0ab5fde727)
1*81dbdadeSbeck /* $OpenBSD: dsa_prn.c,v 1.10 2023/07/08 14:28:15 beck Exp $ */
2f1535dc8Sdjm /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3f1535dc8Sdjm  * project 2006.
4f1535dc8Sdjm  */
5f1535dc8Sdjm /* ====================================================================
6f1535dc8Sdjm  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7f1535dc8Sdjm  *
8f1535dc8Sdjm  * Redistribution and use in source and binary forms, with or without
9f1535dc8Sdjm  * modification, are permitted provided that the following conditions
10f1535dc8Sdjm  * are met:
11f1535dc8Sdjm  *
12f1535dc8Sdjm  * 1. Redistributions of source code must retain the above copyright
13f1535dc8Sdjm  *    notice, this list of conditions and the following disclaimer.
14f1535dc8Sdjm  *
15f1535dc8Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
16f1535dc8Sdjm  *    notice, this list of conditions and the following disclaimer in
17f1535dc8Sdjm  *    the documentation and/or other materials provided with the
18f1535dc8Sdjm  *    distribution.
19f1535dc8Sdjm  *
20f1535dc8Sdjm  * 3. All advertising materials mentioning features or use of this
21f1535dc8Sdjm  *    software must display the following acknowledgment:
22f1535dc8Sdjm  *    "This product includes software developed by the OpenSSL Project
23f1535dc8Sdjm  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24f1535dc8Sdjm  *
25f1535dc8Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26f1535dc8Sdjm  *    endorse or promote products derived from this software without
27f1535dc8Sdjm  *    prior written permission. For written permission, please contact
28f1535dc8Sdjm  *    licensing@OpenSSL.org.
29f1535dc8Sdjm  *
30f1535dc8Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
31f1535dc8Sdjm  *    nor may "OpenSSL" appear in their names without prior written
32f1535dc8Sdjm  *    permission of the OpenSSL Project.
33f1535dc8Sdjm  *
34f1535dc8Sdjm  * 6. Redistributions of any form whatsoever must retain the following
35f1535dc8Sdjm  *    acknowledgment:
36f1535dc8Sdjm  *    "This product includes software developed by the OpenSSL Project
37f1535dc8Sdjm  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38f1535dc8Sdjm  *
39f1535dc8Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40f1535dc8Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f1535dc8Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42f1535dc8Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43f1535dc8Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44f1535dc8Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45f1535dc8Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46f1535dc8Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f1535dc8Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48f1535dc8Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49f1535dc8Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50f1535dc8Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
51f1535dc8Sdjm  * ====================================================================
52f1535dc8Sdjm  *
53f1535dc8Sdjm  * This product includes cryptographic software written by Eric Young
54f1535dc8Sdjm  * (eay@cryptsoft.com).  This product includes software written by Tim
55f1535dc8Sdjm  * Hudson (tjh@cryptsoft.com).
56f1535dc8Sdjm  *
57f1535dc8Sdjm  */
58f1535dc8Sdjm 
59f1535dc8Sdjm #include <stdio.h>
60b6ab114eSjsing 
61f1535dc8Sdjm #include <openssl/dsa.h>
62b6ab114eSjsing #include <openssl/err.h>
63b6ab114eSjsing #include <openssl/evp.h>
64f1535dc8Sdjm 
65e85b1b81Smiod int
DSA_print_fp(FILE * fp,const DSA * x,int off)66e85b1b81Smiod DSA_print_fp(FILE *fp, const DSA *x, int off)
67f1535dc8Sdjm {
68f1535dc8Sdjm 	BIO *b;
69f1535dc8Sdjm 	int ret;
70f1535dc8Sdjm 
71e85b1b81Smiod 	if ((b = BIO_new(BIO_s_file())) == NULL) {
725067ae9fSbeck 		DSAerror(ERR_R_BUF_LIB);
73e85b1b81Smiod 		return 0;
74f1535dc8Sdjm 	}
75f1535dc8Sdjm 	BIO_set_fp(b, fp, BIO_NOCLOSE);
76f1535dc8Sdjm 	ret = DSA_print(b, x, off);
77f1535dc8Sdjm 	BIO_free(b);
78e85b1b81Smiod 	return ret;
79f1535dc8Sdjm }
80*81dbdadeSbeck LCRYPTO_ALIAS(DSA_print_fp);
81f1535dc8Sdjm 
82e85b1b81Smiod int
DSAparams_print_fp(FILE * fp,const DSA * x)83e85b1b81Smiod DSAparams_print_fp(FILE *fp, const DSA *x)
84f1535dc8Sdjm {
85f1535dc8Sdjm 	BIO *b;
86f1535dc8Sdjm 	int ret;
87f1535dc8Sdjm 
88e85b1b81Smiod 	if ((b = BIO_new(BIO_s_file())) == NULL) {
895067ae9fSbeck 		DSAerror(ERR_R_BUF_LIB);
90e85b1b81Smiod 		return 0;
91f1535dc8Sdjm 	}
92f1535dc8Sdjm 	BIO_set_fp(b, fp, BIO_NOCLOSE);
93f1535dc8Sdjm 	ret = DSAparams_print(b, x);
94f1535dc8Sdjm 	BIO_free(b);
95e85b1b81Smiod 	return ret;
96f1535dc8Sdjm }
97*81dbdadeSbeck LCRYPTO_ALIAS(DSAparams_print_fp);
98f1535dc8Sdjm 
99e85b1b81Smiod int
DSA_print(BIO * bp,const DSA * x,int off)100e85b1b81Smiod DSA_print(BIO *bp, const DSA *x, int off)
101f1535dc8Sdjm {
102f1535dc8Sdjm 	EVP_PKEY *pk;
103f69f3e59Stobhe 	int ret = 0;
104e85b1b81Smiod 
105f69f3e59Stobhe 	if ((pk = EVP_PKEY_new()) == NULL)
106f69f3e59Stobhe 		goto err;
107f69f3e59Stobhe 
108f69f3e59Stobhe 	if (!EVP_PKEY_set1_DSA(pk, (DSA *)x))
109f69f3e59Stobhe 		goto err;
110f69f3e59Stobhe 
111f1535dc8Sdjm 	ret = EVP_PKEY_print_private(bp, pk, off, NULL);
112f69f3e59Stobhe  err:
113f1535dc8Sdjm 	EVP_PKEY_free(pk);
114f1535dc8Sdjm 	return ret;
115f1535dc8Sdjm }
116*81dbdadeSbeck LCRYPTO_ALIAS(DSA_print);
117f1535dc8Sdjm 
118e85b1b81Smiod int
DSAparams_print(BIO * bp,const DSA * x)119e85b1b81Smiod DSAparams_print(BIO *bp, const DSA *x)
120f1535dc8Sdjm {
121f1535dc8Sdjm 	EVP_PKEY *pk;
122f3eb1438Stobhe 	int ret = 0;
123e85b1b81Smiod 
124f3eb1438Stobhe 	if ((pk = EVP_PKEY_new()) == NULL)
125f3eb1438Stobhe 		goto err;
126f3eb1438Stobhe 
127f3eb1438Stobhe 	if (!EVP_PKEY_set1_DSA(pk, (DSA *)x))
128f3eb1438Stobhe 		goto err;
129f3eb1438Stobhe 
130f1535dc8Sdjm 	ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
131f3eb1438Stobhe  err:
132f1535dc8Sdjm 	EVP_PKEY_free(pk);
133f1535dc8Sdjm 	return ret;
134f1535dc8Sdjm }
135*81dbdadeSbeck LCRYPTO_ALIAS(DSAparams_print);
136