Act on deprecation of LONG and ZLONG, step 2
[openssl.git] / crypto / rsa / rsa_asn1.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/bn.h>
13 #include <openssl/x509.h>
14 #include <openssl/asn1t.h>
15 #include "rsa_locl.h"
16
17 /* Override the default free and new methods */
18 static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
19                   void *exarg)
20 {
21     if (operation == ASN1_OP_NEW_PRE) {
22         *pval = (ASN1_VALUE *)RSA_new();
23         if (*pval != NULL)
24             return 2;
25         return 0;
26     } else if (operation == ASN1_OP_FREE_PRE) {
27         RSA_free((RSA *)*pval);
28         *pval = NULL;
29         return 2;
30     }
31     return 1;
32 }
33
34 ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
35         ASN1_SIMPLE(RSA, version, INT32),
36         ASN1_SIMPLE(RSA, n, BIGNUM),
37         ASN1_SIMPLE(RSA, e, BIGNUM),
38         ASN1_SIMPLE(RSA, d, CBIGNUM),
39         ASN1_SIMPLE(RSA, p, CBIGNUM),
40         ASN1_SIMPLE(RSA, q, CBIGNUM),
41         ASN1_SIMPLE(RSA, dmp1, CBIGNUM),
42         ASN1_SIMPLE(RSA, dmq1, CBIGNUM),
43         ASN1_SIMPLE(RSA, iqmp, CBIGNUM)
44 } ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
45
46
47 ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
48         ASN1_SIMPLE(RSA, n, BIGNUM),
49         ASN1_SIMPLE(RSA, e, BIGNUM),
50 } ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
51
52 /* Free up maskHash */
53 static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
54                       void *exarg)
55 {
56     if (operation == ASN1_OP_FREE_PRE) {
57         RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval;
58         X509_ALGOR_free(pss->maskHash);
59     }
60     return 1;
61 }
62
63 ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
64         ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR,0),
65         ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR,1),
66         ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER,2),
67         ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER,3)
68 } ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
69
70 IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
71
72 /* Free up maskHash */
73 static int rsa_oaep_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
74                        void *exarg)
75 {
76     if (operation == ASN1_OP_FREE_PRE) {
77         RSA_OAEP_PARAMS *oaep = (RSA_OAEP_PARAMS *)*pval;
78         X509_ALGOR_free(oaep->maskHash);
79     }
80     return 1;
81 }
82
83 ASN1_SEQUENCE_cb(RSA_OAEP_PARAMS, rsa_oaep_cb) = {
84         ASN1_EXP_OPT(RSA_OAEP_PARAMS, hashFunc, X509_ALGOR, 0),
85         ASN1_EXP_OPT(RSA_OAEP_PARAMS, maskGenFunc, X509_ALGOR, 1),
86         ASN1_EXP_OPT(RSA_OAEP_PARAMS, pSourceFunc, X509_ALGOR, 2),
87 } ASN1_SEQUENCE_END_cb(RSA_OAEP_PARAMS, RSA_OAEP_PARAMS)
88
89 IMPLEMENT_ASN1_FUNCTIONS(RSA_OAEP_PARAMS)
90
91 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey)
92
93 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey)
94
95 RSA *RSAPublicKey_dup(RSA *rsa)
96 {
97     return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
98 }
99
100 RSA *RSAPrivateKey_dup(RSA *rsa)
101 {
102     return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
103 }