1 /* $OpenBSD: rsa_asn1.c,v 1.13 2016/12/30 15:47:07 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/asn1t.h>
62 #include <openssl/bn.h>
63 #include <openssl/rsa.h>
64 #include <openssl/x509.h>
66 /* Override the default free and new methods */
68 rsa_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
, void *exarg
)
70 if (operation
== ASN1_OP_NEW_PRE
) {
71 *pval
= (ASN1_VALUE
*)RSA_new();
75 } else if (operation
== ASN1_OP_FREE_PRE
) {
76 RSA_free((RSA
*)*pval
);
83 static const ASN1_AUX RSAPrivateKey_aux
= {
91 static const ASN1_TEMPLATE RSAPrivateKey_seq_tt
[] = {
95 .offset
= offsetof(RSA
, version
),
96 .field_name
= "version",
102 .offset
= offsetof(RSA
, n
),
109 .offset
= offsetof(RSA
, e
),
116 .offset
= offsetof(RSA
, d
),
123 .offset
= offsetof(RSA
, p
),
130 .offset
= offsetof(RSA
, q
),
137 .offset
= offsetof(RSA
, dmp1
),
138 .field_name
= "dmp1",
144 .offset
= offsetof(RSA
, dmq1
),
145 .field_name
= "dmq1",
151 .offset
= offsetof(RSA
, iqmp
),
152 .field_name
= "iqmp",
157 const ASN1_ITEM RSAPrivateKey_it
= {
158 .itype
= ASN1_ITYPE_SEQUENCE
,
159 .utype
= V_ASN1_SEQUENCE
,
160 .templates
= RSAPrivateKey_seq_tt
,
161 .tcount
= sizeof(RSAPrivateKey_seq_tt
) / sizeof(ASN1_TEMPLATE
),
162 .funcs
= &RSAPrivateKey_aux
,
168 static const ASN1_AUX RSAPublicKey_aux
= {
176 static const ASN1_TEMPLATE RSAPublicKey_seq_tt
[] = {
180 .offset
= offsetof(RSA
, n
),
187 .offset
= offsetof(RSA
, e
),
193 const ASN1_ITEM RSAPublicKey_it
= {
194 .itype
= ASN1_ITYPE_SEQUENCE
,
195 .utype
= V_ASN1_SEQUENCE
,
196 .templates
= RSAPublicKey_seq_tt
,
197 .tcount
= sizeof(RSAPublicKey_seq_tt
) / sizeof(ASN1_TEMPLATE
),
198 .funcs
= &RSAPublicKey_aux
,
203 static const ASN1_TEMPLATE RSA_PSS_PARAMS_seq_tt
[] = {
205 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
207 .offset
= offsetof(RSA_PSS_PARAMS
, hashAlgorithm
),
208 .field_name
= "hashAlgorithm",
209 .item
= &X509_ALGOR_it
,
212 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
214 .offset
= offsetof(RSA_PSS_PARAMS
, maskGenAlgorithm
),
215 .field_name
= "maskGenAlgorithm",
216 .item
= &X509_ALGOR_it
,
219 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
221 .offset
= offsetof(RSA_PSS_PARAMS
, saltLength
),
222 .field_name
= "saltLength",
223 .item
= &ASN1_INTEGER_it
,
226 .flags
= ASN1_TFLG_EXPLICIT
| ASN1_TFLG_OPTIONAL
,
228 .offset
= offsetof(RSA_PSS_PARAMS
, trailerField
),
229 .field_name
= "trailerField",
230 .item
= &ASN1_INTEGER_it
,
234 const ASN1_ITEM RSA_PSS_PARAMS_it
= {
235 .itype
= ASN1_ITYPE_SEQUENCE
,
236 .utype
= V_ASN1_SEQUENCE
,
237 .templates
= RSA_PSS_PARAMS_seq_tt
,
238 .tcount
= sizeof(RSA_PSS_PARAMS_seq_tt
) / sizeof(ASN1_TEMPLATE
),
240 .size
= sizeof(RSA_PSS_PARAMS
),
241 .sname
= "RSA_PSS_PARAMS",
246 d2i_RSA_PSS_PARAMS(RSA_PSS_PARAMS
**a
, const unsigned char **in
, long len
)
248 return (RSA_PSS_PARAMS
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
253 i2d_RSA_PSS_PARAMS(RSA_PSS_PARAMS
*a
, unsigned char **out
)
255 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &RSA_PSS_PARAMS_it
);
259 RSA_PSS_PARAMS_new(void)
261 return (RSA_PSS_PARAMS
*)ASN1_item_new(&RSA_PSS_PARAMS_it
);
265 RSA_PSS_PARAMS_free(RSA_PSS_PARAMS
*a
)
267 ASN1_item_free((ASN1_VALUE
*)a
, &RSA_PSS_PARAMS_it
);
272 d2i_RSAPrivateKey(RSA
**a
, const unsigned char **in
, long len
)
274 return (RSA
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
279 i2d_RSAPrivateKey(const RSA
*a
, unsigned char **out
)
281 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &RSAPrivateKey_it
);
286 d2i_RSAPublicKey(RSA
**a
, const unsigned char **in
, long len
)
288 return (RSA
*)ASN1_item_d2i((ASN1_VALUE
**)a
, in
, len
,
293 i2d_RSAPublicKey(const RSA
*a
, unsigned char **out
)
295 return ASN1_item_i2d((ASN1_VALUE
*)a
, out
, &RSAPublicKey_it
);
299 RSAPublicKey_dup(RSA
*rsa
)
301 return ASN1_item_dup(&RSAPublicKey_it
, rsa
);
305 RSAPrivateKey_dup(RSA
*rsa
)
307 return ASN1_item_dup(&RSAPrivateKey_it
, rsa
);