xref: /openbsd-src/lib/libcrypto/rsa/rsa_prn.c (revision 1da360153a2aac4b87b158742c54c8dbb9d58f24)
1*1da36015Sbeck /* $OpenBSD: rsa_prn.c,v 1.10 2023/07/08 12:26:45 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 
61b6ab114eSjsing #include <openssl/err.h>
62f1535dc8Sdjm #include <openssl/evp.h>
63b6ab114eSjsing #include <openssl/rsa.h>
64f1535dc8Sdjm 
6587203b09Smiod int
RSA_print_fp(FILE * fp,const RSA * x,int off)6687203b09Smiod RSA_print_fp(FILE *fp, const RSA *x, int off)
67f1535dc8Sdjm {
68f1535dc8Sdjm 	BIO *b;
69f1535dc8Sdjm 	int ret;
70f1535dc8Sdjm 
7187203b09Smiod 	if ((b = BIO_new(BIO_s_file())) == NULL) {
725067ae9fSbeck 		RSAerror(ERR_R_BUF_LIB);
7387203b09Smiod 		return 0;
74f1535dc8Sdjm 	}
75f1535dc8Sdjm 	BIO_set_fp(b, fp, BIO_NOCLOSE);
76f1535dc8Sdjm 	ret = RSA_print(b, x, off);
77f1535dc8Sdjm 	BIO_free(b);
7887203b09Smiod 	return ret;
79f1535dc8Sdjm }
80*1da36015Sbeck LCRYPTO_ALIAS(RSA_print_fp);
81f1535dc8Sdjm 
8287203b09Smiod int
RSA_print(BIO * bp,const RSA * x,int off)8387203b09Smiod RSA_print(BIO *bp, const RSA *x, int off)
84f1535dc8Sdjm {
85f1535dc8Sdjm 	EVP_PKEY *pk;
8634054017Stobhe 	int ret = 0;
8787203b09Smiod 
8834054017Stobhe 	if ((pk = EVP_PKEY_new()) == NULL)
899cfb59b2Stobhe 		goto err;
9034054017Stobhe 
9134054017Stobhe 	if (!EVP_PKEY_set1_RSA(pk, (RSA *)x))
929cfb59b2Stobhe 		goto err;
9334054017Stobhe 
94f1535dc8Sdjm 	ret = EVP_PKEY_print_private(bp, pk, off, NULL);
959cfb59b2Stobhe  err:
96f1535dc8Sdjm 	EVP_PKEY_free(pk);
97f1535dc8Sdjm 	return ret;
98f1535dc8Sdjm }
99*1da36015Sbeck LCRYPTO_ALIAS(RSA_print);
100