diff options
Diffstat (limited to 'src/lib/libcrypto/evp/bio_md.c')
| -rw-r--r-- | src/lib/libcrypto/evp/bio_md.c | 231 |
1 files changed, 117 insertions, 114 deletions
diff --git a/src/lib/libcrypto/evp/bio_md.c b/src/lib/libcrypto/evp/bio_md.c index 85eead6c95..7ace6a4e89 100644 --- a/src/lib/libcrypto/evp/bio_md.c +++ b/src/lib/libcrypto/evp/bio_md.c | |||
| @@ -5,21 +5,21 @@ | |||
| 5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * | 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * | 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
| 22 | * | 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: | 25 | * are met: |
| @@ -34,10 +34,10 @@ | |||
| 34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * | 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -49,7 +49,7 @@ | |||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
| 52 | * | 52 | * |
| 53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
| @@ -72,7 +72,7 @@ static int md_gets(BIO *h, char *str, int size); | |||
| 72 | static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); | 72 | static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
| 73 | static int md_new(BIO *h); | 73 | static int md_new(BIO *h); |
| 74 | static int md_free(BIO *data); | 74 | static int md_free(BIO *data); |
| 75 | static long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); | 75 | static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); |
| 76 | 76 | ||
| 77 | static BIO_METHOD methods_md = { | 77 | static BIO_METHOD methods_md = { |
| 78 | .type = BIO_TYPE_MD, | 78 | .type = BIO_TYPE_MD, |
| @@ -86,182 +86,186 @@ static BIO_METHOD methods_md = { | |||
| 86 | .callback_ctrl = md_callback_ctrl | 86 | .callback_ctrl = md_callback_ctrl |
| 87 | }; | 87 | }; |
| 88 | 88 | ||
| 89 | BIO_METHOD *BIO_f_md(void) | 89 | BIO_METHOD * |
| 90 | { | 90 | BIO_f_md(void) |
| 91 | return(&methods_md); | 91 | { |
| 92 | } | 92 | return (&methods_md); |
| 93 | } | ||
| 93 | 94 | ||
| 94 | static int md_new(BIO *bi) | 95 | static int |
| 95 | { | 96 | md_new(BIO *bi) |
| 97 | { | ||
| 96 | EVP_MD_CTX *ctx; | 98 | EVP_MD_CTX *ctx; |
| 97 | 99 | ||
| 98 | ctx=EVP_MD_CTX_create(); | 100 | ctx = EVP_MD_CTX_create(); |
| 99 | if (ctx == NULL) return(0); | 101 | if (ctx == NULL) |
| 102 | return (0); | ||
| 100 | 103 | ||
| 101 | bi->init=0; | 104 | bi->init = 0; |
| 102 | bi->ptr=(char *)ctx; | 105 | bi->ptr = (char *)ctx; |
| 103 | bi->flags=0; | 106 | bi->flags = 0; |
| 104 | return(1); | 107 | return (1); |
| 105 | } | 108 | } |
| 106 | 109 | ||
| 107 | static int md_free(BIO *a) | 110 | static int |
| 108 | { | 111 | md_free(BIO *a) |
| 109 | if (a == NULL) return(0); | 112 | { |
| 113 | if (a == NULL) | ||
| 114 | return (0); | ||
| 110 | EVP_MD_CTX_destroy(a->ptr); | 115 | EVP_MD_CTX_destroy(a->ptr); |
| 111 | a->ptr=NULL; | 116 | a->ptr = NULL; |
| 112 | a->init=0; | 117 | a->init = 0; |
| 113 | a->flags=0; | 118 | a->flags = 0; |
| 114 | return(1); | 119 | return (1); |
| 115 | } | 120 | } |
| 116 | 121 | ||
| 117 | static int md_read(BIO *b, char *out, int outl) | 122 | static int |
| 118 | { | 123 | md_read(BIO *b, char *out, int outl) |
| 119 | int ret=0; | 124 | { |
| 125 | int ret = 0; | ||
| 120 | EVP_MD_CTX *ctx; | 126 | EVP_MD_CTX *ctx; |
| 121 | 127 | ||
| 122 | if (out == NULL) return(0); | 128 | if (out == NULL) |
| 123 | ctx=b->ptr; | 129 | return (0); |
| 130 | ctx = b->ptr; | ||
| 124 | 131 | ||
| 125 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | 132 | if ((ctx == NULL) || (b->next_bio == NULL)) |
| 133 | return (0); | ||
| 126 | 134 | ||
| 127 | ret=BIO_read(b->next_bio,out,outl); | 135 | ret = BIO_read(b->next_bio, out, outl); |
| 128 | if (b->init) | 136 | if (b->init) { |
| 129 | { | 137 | if (ret > 0) { |
| 130 | if (ret > 0) | 138 | if (EVP_DigestUpdate(ctx, (unsigned char *)out, |
| 131 | { | 139 | (unsigned int)ret) <= 0) |
| 132 | if (EVP_DigestUpdate(ctx,(unsigned char *)out, | 140 | return (-1); |
| 133 | (unsigned int)ret)<=0) return (-1); | ||
| 134 | } | ||
| 135 | } | 141 | } |
| 142 | } | ||
| 136 | BIO_clear_retry_flags(b); | 143 | BIO_clear_retry_flags(b); |
| 137 | BIO_copy_next_retry(b); | 144 | BIO_copy_next_retry(b); |
| 138 | return(ret); | 145 | return (ret); |
| 139 | } | 146 | } |
| 140 | 147 | ||
| 141 | static int md_write(BIO *b, const char *in, int inl) | 148 | static int |
| 142 | { | 149 | md_write(BIO *b, const char *in, int inl) |
| 143 | int ret=0; | 150 | { |
| 151 | int ret = 0; | ||
| 144 | EVP_MD_CTX *ctx; | 152 | EVP_MD_CTX *ctx; |
| 145 | 153 | ||
| 146 | if ((in == NULL) || (inl <= 0)) return(0); | 154 | if ((in == NULL) || (inl <= 0)) |
| 147 | ctx=b->ptr; | 155 | return (0); |
| 156 | ctx = b->ptr; | ||
| 148 | 157 | ||
| 149 | if ((ctx != NULL) && (b->next_bio != NULL)) | 158 | if ((ctx != NULL) && (b->next_bio != NULL)) |
| 150 | ret=BIO_write(b->next_bio,in,inl); | 159 | ret = BIO_write(b->next_bio, in, inl); |
| 151 | if (b->init) | 160 | if (b->init) { |
| 152 | { | 161 | if (ret > 0) { |
| 153 | if (ret > 0) | 162 | if (!EVP_DigestUpdate(ctx, (const unsigned char *)in, |
| 154 | { | 163 | (unsigned int)ret)) { |
| 155 | if (!EVP_DigestUpdate(ctx,(const unsigned char *)in, | ||
| 156 | (unsigned int)ret)) | ||
| 157 | { | ||
| 158 | BIO_clear_retry_flags(b); | 164 | BIO_clear_retry_flags(b); |
| 159 | return 0; | 165 | return 0; |
| 160 | } | ||
| 161 | } | 166 | } |
| 162 | } | 167 | } |
| 163 | if(b->next_bio != NULL) | 168 | } |
| 164 | { | 169 | if (b->next_bio != NULL) { |
| 165 | BIO_clear_retry_flags(b); | 170 | BIO_clear_retry_flags(b); |
| 166 | BIO_copy_next_retry(b); | 171 | BIO_copy_next_retry(b); |
| 167 | } | ||
| 168 | return(ret); | ||
| 169 | } | 172 | } |
| 173 | return (ret); | ||
| 174 | } | ||
| 170 | 175 | ||
| 171 | static long md_ctrl(BIO *b, int cmd, long num, void *ptr) | 176 | static long |
| 172 | { | 177 | md_ctrl(BIO *b, int cmd, long num, void *ptr) |
| 173 | EVP_MD_CTX *ctx,*dctx,**pctx; | 178 | { |
| 179 | EVP_MD_CTX *ctx, *dctx, **pctx; | ||
| 174 | const EVP_MD **ppmd; | 180 | const EVP_MD **ppmd; |
| 175 | EVP_MD *md; | 181 | EVP_MD *md; |
| 176 | long ret=1; | 182 | long ret = 1; |
| 177 | BIO *dbio; | 183 | BIO *dbio; |
| 178 | 184 | ||
| 179 | ctx=b->ptr; | 185 | ctx = b->ptr; |
| 180 | 186 | ||
| 181 | switch (cmd) | 187 | switch (cmd) { |
| 182 | { | ||
| 183 | case BIO_CTRL_RESET: | 188 | case BIO_CTRL_RESET: |
| 184 | if (b->init) | 189 | if (b->init) |
| 185 | ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL); | 190 | ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL); |
| 186 | else | 191 | else |
| 187 | ret=0; | 192 | ret = 0; |
| 188 | if (ret > 0) | 193 | if (ret > 0) |
| 189 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 194 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 190 | break; | 195 | break; |
| 191 | case BIO_C_GET_MD: | 196 | case BIO_C_GET_MD: |
| 192 | if (b->init) | 197 | if (b->init) { |
| 193 | { | 198 | ppmd = ptr; |
| 194 | ppmd=ptr; | 199 | *ppmd = ctx->digest; |
| 195 | *ppmd=ctx->digest; | 200 | } else |
| 196 | } | 201 | ret = 0; |
| 197 | else | ||
| 198 | ret=0; | ||
| 199 | break; | 202 | break; |
| 200 | case BIO_C_GET_MD_CTX: | 203 | case BIO_C_GET_MD_CTX: |
| 201 | pctx=ptr; | 204 | pctx = ptr; |
| 202 | *pctx=ctx; | 205 | *pctx = ctx; |
| 203 | b->init = 1; | 206 | b->init = 1; |
| 204 | break; | 207 | break; |
| 205 | case BIO_C_SET_MD_CTX: | 208 | case BIO_C_SET_MD_CTX: |
| 206 | if (b->init) | 209 | if (b->init) |
| 207 | b->ptr=ptr; | 210 | b->ptr = ptr; |
| 208 | else | 211 | else |
| 209 | ret=0; | 212 | ret = 0; |
| 210 | break; | 213 | break; |
| 211 | case BIO_C_DO_STATE_MACHINE: | 214 | case BIO_C_DO_STATE_MACHINE: |
| 212 | BIO_clear_retry_flags(b); | 215 | BIO_clear_retry_flags(b); |
| 213 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 216 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 214 | BIO_copy_next_retry(b); | 217 | BIO_copy_next_retry(b); |
| 215 | break; | 218 | break; |
| 216 | 219 | ||
| 217 | case BIO_C_SET_MD: | 220 | case BIO_C_SET_MD: |
| 218 | md=ptr; | 221 | md = ptr; |
| 219 | ret = EVP_DigestInit_ex(ctx,md, NULL); | 222 | ret = EVP_DigestInit_ex(ctx, md, NULL); |
| 220 | if (ret > 0) | 223 | if (ret > 0) |
| 221 | b->init=1; | 224 | b->init = 1; |
| 222 | break; | 225 | break; |
| 223 | case BIO_CTRL_DUP: | 226 | case BIO_CTRL_DUP: |
| 224 | dbio=ptr; | 227 | dbio = ptr; |
| 225 | dctx=dbio->ptr; | 228 | dctx = dbio->ptr; |
| 226 | if (!EVP_MD_CTX_copy_ex(dctx,ctx)) | 229 | if (!EVP_MD_CTX_copy_ex(dctx, ctx)) |
| 227 | return 0; | 230 | return 0; |
| 228 | b->init=1; | 231 | b->init = 1; |
| 229 | break; | 232 | break; |
| 230 | default: | 233 | default: |
| 231 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 234 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
| 232 | break; | 235 | break; |
| 233 | } | ||
| 234 | return(ret); | ||
| 235 | } | 236 | } |
| 237 | return (ret); | ||
| 238 | } | ||
| 236 | 239 | ||
| 237 | static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | 240 | static long |
| 238 | { | 241 | md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
| 239 | long ret=1; | 242 | { |
| 243 | long ret = 1; | ||
| 240 | 244 | ||
| 241 | if (b->next_bio == NULL) return(0); | 245 | if (b->next_bio == NULL) |
| 242 | switch (cmd) | 246 | return (0); |
| 243 | { | 247 | switch (cmd) { |
| 244 | default: | 248 | default: |
| 245 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | 249 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); |
| 246 | break; | 250 | break; |
| 247 | } | ||
| 248 | return(ret); | ||
| 249 | } | 251 | } |
| 252 | return (ret); | ||
| 253 | } | ||
| 250 | 254 | ||
| 251 | static int md_gets(BIO *bp, char *buf, int size) | 255 | static int |
| 252 | { | 256 | md_gets(BIO *bp, char *buf, int size) |
| 257 | { | ||
| 253 | EVP_MD_CTX *ctx; | 258 | EVP_MD_CTX *ctx; |
| 254 | unsigned int ret; | 259 | unsigned int ret; |
| 255 | 260 | ||
| 256 | 261 | ctx = bp->ptr; | |
| 257 | ctx=bp->ptr; | ||
| 258 | if (size < ctx->digest->md_size) | 262 | if (size < ctx->digest->md_size) |
| 259 | return(0); | 263 | return (0); |
| 260 | if (EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret)<=0) | 264 | if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0) |
| 261 | return -1; | 265 | return -1; |
| 262 | 266 | ||
| 263 | return((int)ret); | 267 | return ((int)ret); |
| 264 | } | 268 | } |
| 265 | 269 | ||
| 266 | /* | 270 | /* |
| 267 | static int md_puts(bp,str) | 271 | static int md_puts(bp,str) |
| @@ -271,4 +275,3 @@ char *str; | |||
| 271 | return(-1); | 275 | return(-1); |
| 272 | } | 276 | } |
| 273 | */ | 277 | */ |
| 274 | |||
