rsa_asn1.c revision 109998
1202188Sed/* rsa_asn1.c */
2202188Sed/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3202188Sed * project 2000.
4202188Sed */
5202188Sed/* ====================================================================
6202188Sed * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7202188Sed *
8202188Sed * Redistribution and use in source and binary forms, with or without
9202188Sed * modification, are permitted provided that the following conditions
10202188Sed * are met:
11202188Sed *
12202188Sed * 1. Redistributions of source code must retain the above copyright
13202188Sed *    notice, this list of conditions and the following disclaimer.
14202188Sed *
15202188Sed * 2. Redistributions in binary form must reproduce the above copyright
16202188Sed *    notice, this list of conditions and the following disclaimer in
17202188Sed *    the documentation and/or other materials provided with the
18202188Sed *    distribution.
19202188Sed *
20202188Sed * 3. All advertising materials mentioning features or use of this
21202188Sed *    software must display the following acknowledgment:
22202188Sed *    "This product includes software developed by the OpenSSL Project
23202188Sed *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24202188Sed *
25202188Sed * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26202188Sed *    endorse or promote products derived from this software without
27202188Sed *    prior written permission. For written permission, please contact
28202188Sed *    licensing@OpenSSL.org.
29202188Sed *
30202188Sed * 5. Products derived from this software may not be called "OpenSSL"
31202188Sed *    nor may "OpenSSL" appear in their names without prior written
32202188Sed *    permission of the OpenSSL Project.
33202188Sed *
34215310Sed * 6. Redistributions of any form whatsoever must retain the following
35202188Sed *    acknowledgment:
36202188Sed *    "This product includes software developed by the OpenSSL Project
37202188Sed *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38202188Sed *
39202188Sed * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40202188Sed * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41202188Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42202188Sed * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43202188Sed * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44202188Sed * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45202188Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46219045Sed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47218846Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48202188Sed * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49202188Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50241046Sjilles * OF THE POSSIBILITY OF SUCH DAMAGE.
51202188Sed * ====================================================================
52202188Sed *
53202188Sed * This product includes cryptographic software written by Eric Young
54202188Sed * (eay@cryptsoft.com).  This product includes software written by Tim
55202188Sed * Hudson (tjh@cryptsoft.com).
56202188Sed *
57218846Sed */
58202188Sed
59202188Sed#include <stdio.h>
60223576Sed#include "cryptlib.h"
61202188Sed#include <openssl/bn.h>
62202188Sed#include <openssl/rsa.h>
63202188Sed#include <openssl/asn1t.h>
64202188Sed
65202188Sedstatic ASN1_METHOD method={
66202188Sed        (int (*)())  i2d_RSAPrivateKey,
67202188Sed        (char *(*)())d2i_RSAPrivateKey,
68202188Sed        (char *(*)())RSA_new,
69214134Sed        (void (*)()) RSA_free};
70202188Sed
71202188SedASN1_METHOD *RSAPrivateKey_asn1_meth(void)
72219045Sed	{
73218846Sed	return(&method);
74219045Sed	}
75218846Sed
76202188Sed/* Override the default free and new methods */
77219045Sedstatic int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
78219045Sed{
79219045Sed	if(operation == ASN1_OP_NEW_PRE) {
80202188Sed		*pval = (ASN1_VALUE *)RSA_new();
81202188Sed		if(*pval) return 2;
82202188Sed		return 0;
83202188Sed	} else if(operation == ASN1_OP_FREE_PRE) {
84202188Sed		RSA_free((RSA *)*pval);
85202188Sed		*pval = NULL;
86218846Sed		return 2;
87218846Sed	}
88202188Sed	return 1;
89226846Sed}
90226846Sed
91226846SedASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
92202188Sed	ASN1_SIMPLE(RSA, version, LONG),
93202188Sed	ASN1_SIMPLE(RSA, n, BIGNUM),
94202188Sed	ASN1_SIMPLE(RSA, e, BIGNUM),
95202188Sed	ASN1_SIMPLE(RSA, d, BIGNUM),
96202188Sed	ASN1_SIMPLE(RSA, p, BIGNUM),
97218846Sed	ASN1_SIMPLE(RSA, q, BIGNUM),
98218846Sed	ASN1_SIMPLE(RSA, dmp1, BIGNUM),
99218846Sed	ASN1_SIMPLE(RSA, dmq1, BIGNUM),
100202188Sed	ASN1_SIMPLE(RSA, iqmp, BIGNUM)
101202188Sed} ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
102202188Sed
103202188Sed
104202188SedASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
105202188Sed	ASN1_SIMPLE(RSA, n, BIGNUM),
106202188Sed	ASN1_SIMPLE(RSA, e, BIGNUM),
107218846Sed} ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
108218846Sed
109223576SedIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey)
110218846Sed
111218846SedIMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey)
112218846Sed
113218846SedRSA *RSAPublicKey_dup(RSA *rsa)
114218846Sed	{
115218846Sed	return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
116202188Sed	}
117202188Sed
118202188SedRSA *RSAPrivateKey_dup(RSA *rsa)
119218846Sed	{
120202188Sed	return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
121202188Sed	}
122202188Sed