diff options
Diffstat (limited to 'src/lib/libcrypto/dh/dh_lib.c')
-rw-r--r-- | src/lib/libcrypto/dh/dh_lib.c | 92 |
1 files changed, 88 insertions, 4 deletions
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c index 61e0720e8a..6c21463028 100644 --- a/src/lib/libcrypto/dh/dh_lib.c +++ b/src/lib/libcrypto/dh/dh_lib.c | |||
@@ -63,16 +63,49 @@ | |||
63 | 63 | ||
64 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; | 64 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; |
65 | 65 | ||
66 | static DH_METHOD *default_DH_method; | ||
67 | static int dh_meth_num = 0; | ||
68 | static STACK_OF(CRYPTO_EX_DATA_FUNCS) *dh_meth = NULL; | ||
69 | |||
70 | void DH_set_default_method(DH_METHOD *meth) | ||
71 | { | ||
72 | default_DH_method = meth; | ||
73 | } | ||
74 | |||
75 | DH_METHOD *DH_get_default_method(void) | ||
76 | { | ||
77 | if(!default_DH_method) default_DH_method = DH_OpenSSL(); | ||
78 | return default_DH_method; | ||
79 | } | ||
80 | |||
81 | DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth) | ||
82 | { | ||
83 | DH_METHOD *mtmp; | ||
84 | mtmp = dh->meth; | ||
85 | if (mtmp->finish) mtmp->finish(dh); | ||
86 | dh->meth = meth; | ||
87 | if (meth->init) meth->init(dh); | ||
88 | return mtmp; | ||
89 | } | ||
90 | |||
66 | DH *DH_new(void) | 91 | DH *DH_new(void) |
92 | { | ||
93 | return DH_new_method(NULL); | ||
94 | } | ||
95 | |||
96 | DH *DH_new_method(DH_METHOD *meth) | ||
67 | { | 97 | { |
68 | DH *ret; | 98 | DH *ret; |
69 | |||
70 | ret=(DH *)Malloc(sizeof(DH)); | 99 | ret=(DH *)Malloc(sizeof(DH)); |
100 | |||
71 | if (ret == NULL) | 101 | if (ret == NULL) |
72 | { | 102 | { |
73 | DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE); | 103 | DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE); |
74 | return(NULL); | 104 | return(NULL); |
75 | } | 105 | } |
106 | if(!default_DH_method) default_DH_method = DH_OpenSSL(); | ||
107 | if(meth) ret->meth = meth; | ||
108 | else ret->meth = default_DH_method; | ||
76 | ret->pad=0; | 109 | ret->pad=0; |
77 | ret->version=0; | 110 | ret->version=0; |
78 | ret->p=NULL; | 111 | ret->p=NULL; |
@@ -80,23 +113,74 @@ DH *DH_new(void) | |||
80 | ret->length=0; | 113 | ret->length=0; |
81 | ret->pub_key=NULL; | 114 | ret->pub_key=NULL; |
82 | ret->priv_key=NULL; | 115 | ret->priv_key=NULL; |
83 | ret->flags=DH_FLAG_CACHE_MONT_P; | 116 | ret->q=NULL; |
117 | ret->j=NULL; | ||
118 | ret->seed = NULL; | ||
119 | ret->seedlen = 0; | ||
120 | ret->counter = NULL; | ||
84 | ret->method_mont_p=NULL; | 121 | ret->method_mont_p=NULL; |
122 | ret->references = 1; | ||
123 | ret->flags=ret->meth->flags; | ||
124 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | ||
125 | { | ||
126 | Free(ret); | ||
127 | ret=NULL; | ||
128 | } | ||
129 | else | ||
130 | CRYPTO_new_ex_data(dh_meth,ret,&ret->ex_data); | ||
85 | return(ret); | 131 | return(ret); |
86 | } | 132 | } |
87 | 133 | ||
88 | void DH_free(DH *r) | 134 | void DH_free(DH *r) |
89 | { | 135 | { |
136 | int i; | ||
90 | if(r == NULL) return; | 137 | if(r == NULL) return; |
138 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); | ||
139 | #ifdef REF_PRINT | ||
140 | REF_PRINT("DH",r); | ||
141 | #endif | ||
142 | if (i > 0) return; | ||
143 | #ifdef REF_CHECK | ||
144 | if (i < 0) | ||
145 | { | ||
146 | fprintf(stderr,"DH_free, bad reference count\n"); | ||
147 | abort(); | ||
148 | } | ||
149 | #endif | ||
150 | |||
151 | CRYPTO_free_ex_data(dh_meth, r, &r->ex_data); | ||
152 | |||
153 | if(r->meth->finish) r->meth->finish(r); | ||
154 | |||
91 | if (r->p != NULL) BN_clear_free(r->p); | 155 | if (r->p != NULL) BN_clear_free(r->p); |
92 | if (r->g != NULL) BN_clear_free(r->g); | 156 | if (r->g != NULL) BN_clear_free(r->g); |
157 | if (r->q != NULL) BN_clear_free(r->q); | ||
158 | if (r->j != NULL) BN_clear_free(r->j); | ||
159 | if (r->seed) Free(r->seed); | ||
160 | if (r->counter != NULL) BN_clear_free(r->counter); | ||
93 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | 161 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); |
94 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | 162 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); |
95 | if (r->method_mont_p != NULL) | ||
96 | BN_MONT_CTX_free((BN_MONT_CTX *)r->method_mont_p); | ||
97 | Free(r); | 163 | Free(r); |
98 | } | 164 | } |
99 | 165 | ||
166 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
167 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
168 | { | ||
169 | dh_meth_num++; | ||
170 | return(CRYPTO_get_ex_new_index(dh_meth_num-1, | ||
171 | &dh_meth,argl,argp,new_func,dup_func,free_func)); | ||
172 | } | ||
173 | |||
174 | int DH_set_ex_data(DH *d, int idx, void *arg) | ||
175 | { | ||
176 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | ||
177 | } | ||
178 | |||
179 | void *DH_get_ex_data(DH *d, int idx) | ||
180 | { | ||
181 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | ||
182 | } | ||
183 | |||
100 | int DH_size(DH *dh) | 184 | int DH_size(DH *dh) |
101 | { | 185 | { |
102 | return(BN_num_bytes(dh->p)); | 186 | return(BN_num_bytes(dh->p)); |