Free buffer on error in a2i_ASN1_INTEGER()
[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, LONG),
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 ASN1_SEQUENCE(RSA_PSS_PARAMS) = {
53         ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR,0),
54         ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR,1),
55         ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER,2),
56         ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER,3)
57 } ASN1_SEQUENCE_END(RSA_PSS_PARAMS)
58
59 IMPLEMENT_ASN1_FUNCTIONS(RSA_PSS_PARAMS)
60
61 ASN1_SEQUENCE(RSA_OAEP_PARAMS) = {
62         ASN1_EXP_OPT(RSA_OAEP_PARAMS, hashFunc, X509_ALGOR, 0),
63         ASN1_EXP_OPT(RSA_OAEP_PARAMS, maskGenFunc, X509_ALGOR, 1),
64         ASN1_EXP_OPT(RSA_OAEP_PARAMS, pSourceFunc, X509_ALGOR, 2),
65 } ASN1_SEQUENCE_END(RSA_OAEP_PARAMS)
66
67 IMPLEMENT_ASN1_FUNCTIONS(RSA_OAEP_PARAMS)
68
69 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey)
70
71 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey)
72
73 RSA *RSAPublicKey_dup(RSA *rsa)
74 {
75     return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
76 }
77
78 RSA *RSAPrivateKey_dup(RSA *rsa)
79 {
80     return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
81 }