[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-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 <errno.h>
|
---|
| 12 | #include "internal/cryptlib.h"
|
---|
| 13 | #include <openssl/buffer.h>
|
---|
| 14 | #include <openssl/evp.h>
|
---|
| 15 | #include "internal/evp_int.h"
|
---|
| 16 | #include "evp_locl.h"
|
---|
| 17 | #include "internal/bio.h"
|
---|
| 18 |
|
---|
| 19 | /*
|
---|
| 20 | * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | static int md_write(BIO *h, char const *buf, int num);
|
---|
| 24 | static int md_read(BIO *h, char *buf, int size);
|
---|
| 25 | /*
|
---|
| 26 | * static int md_puts(BIO *h, const char *str);
|
---|
| 27 | */
|
---|
| 28 | static int md_gets(BIO *h, char *str, int size);
|
---|
| 29 | static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
---|
| 30 | static int md_new(BIO *h);
|
---|
| 31 | static int md_free(BIO *data);
|
---|
| 32 | static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
|
---|
| 33 |
|
---|
| 34 | static const BIO_METHOD methods_md = {
|
---|
| 35 | BIO_TYPE_MD, "message digest",
|
---|
| 36 | md_write,
|
---|
| 37 | md_read,
|
---|
| 38 | NULL, /* md_puts, */
|
---|
| 39 | md_gets,
|
---|
| 40 | md_ctrl,
|
---|
| 41 | md_new,
|
---|
| 42 | md_free,
|
---|
| 43 | md_callback_ctrl,
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | const BIO_METHOD *BIO_f_md(void)
|
---|
| 47 | {
|
---|
| 48 | return (&methods_md);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | static int md_new(BIO *bi)
|
---|
| 52 | {
|
---|
| 53 | EVP_MD_CTX *ctx;
|
---|
| 54 |
|
---|
| 55 | ctx = EVP_MD_CTX_new();
|
---|
| 56 | if (ctx == NULL)
|
---|
| 57 | return (0);
|
---|
| 58 |
|
---|
| 59 | BIO_set_init(bi, 1);
|
---|
| 60 | BIO_set_data(bi, ctx);
|
---|
| 61 |
|
---|
| 62 | return 1;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | static int md_free(BIO *a)
|
---|
| 66 | {
|
---|
| 67 | if (a == NULL)
|
---|
| 68 | return (0);
|
---|
| 69 | EVP_MD_CTX_free(BIO_get_data(a));
|
---|
| 70 | BIO_set_data(a, NULL);
|
---|
| 71 | BIO_set_init(a, 0);
|
---|
| 72 |
|
---|
| 73 | return 1;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | static int md_read(BIO *b, char *out, int outl)
|
---|
| 77 | {
|
---|
| 78 | int ret = 0;
|
---|
| 79 | EVP_MD_CTX *ctx;
|
---|
| 80 | BIO *next;
|
---|
| 81 |
|
---|
| 82 | if (out == NULL)
|
---|
| 83 | return (0);
|
---|
| 84 |
|
---|
| 85 | ctx = BIO_get_data(b);
|
---|
| 86 | next = BIO_next(b);
|
---|
| 87 |
|
---|
| 88 | if ((ctx == NULL) || (next == NULL))
|
---|
| 89 | return (0);
|
---|
| 90 |
|
---|
| 91 | ret = BIO_read(next, out, outl);
|
---|
| 92 | if (BIO_get_init(b)) {
|
---|
| 93 | if (ret > 0) {
|
---|
| 94 | if (EVP_DigestUpdate(ctx, (unsigned char *)out,
|
---|
| 95 | (unsigned int)ret) <= 0)
|
---|
| 96 | return (-1);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | BIO_clear_retry_flags(b);
|
---|
| 100 | BIO_copy_next_retry(b);
|
---|
| 101 | return (ret);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static int md_write(BIO *b, const char *in, int inl)
|
---|
| 105 | {
|
---|
| 106 | int ret = 0;
|
---|
| 107 | EVP_MD_CTX *ctx;
|
---|
| 108 | BIO *next;
|
---|
| 109 |
|
---|
| 110 | if ((in == NULL) || (inl <= 0))
|
---|
| 111 | return 0;
|
---|
| 112 |
|
---|
| 113 | ctx = BIO_get_data(b);
|
---|
| 114 | next = BIO_next(b);
|
---|
| 115 | if ((ctx != NULL) && (next != NULL))
|
---|
| 116 | ret = BIO_write(next, in, inl);
|
---|
| 117 |
|
---|
| 118 | if (BIO_get_init(b)) {
|
---|
| 119 | if (ret > 0) {
|
---|
| 120 | if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
|
---|
| 121 | (unsigned int)ret)) {
|
---|
| 122 | BIO_clear_retry_flags(b);
|
---|
| 123 | return 0;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | if (next != NULL) {
|
---|
| 128 | BIO_clear_retry_flags(b);
|
---|
| 129 | BIO_copy_next_retry(b);
|
---|
| 130 | }
|
---|
| 131 | return ret;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
|
---|
| 135 | {
|
---|
| 136 | EVP_MD_CTX *ctx, *dctx, **pctx;
|
---|
| 137 | const EVP_MD **ppmd;
|
---|
| 138 | EVP_MD *md;
|
---|
| 139 | long ret = 1;
|
---|
| 140 | BIO *dbio, *next;
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | ctx = BIO_get_data(b);
|
---|
| 144 | next = BIO_next(b);
|
---|
| 145 |
|
---|
| 146 | switch (cmd) {
|
---|
| 147 | case BIO_CTRL_RESET:
|
---|
| 148 | if (BIO_get_init(b))
|
---|
| 149 | ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
|
---|
| 150 | else
|
---|
| 151 | ret = 0;
|
---|
| 152 | if (ret > 0)
|
---|
| 153 | ret = BIO_ctrl(next, cmd, num, ptr);
|
---|
| 154 | break;
|
---|
| 155 | case BIO_C_GET_MD:
|
---|
| 156 | if (BIO_get_init(b)) {
|
---|
| 157 | ppmd = ptr;
|
---|
| 158 | *ppmd = ctx->digest;
|
---|
| 159 | } else
|
---|
| 160 | ret = 0;
|
---|
| 161 | break;
|
---|
| 162 | case BIO_C_GET_MD_CTX:
|
---|
| 163 | pctx = ptr;
|
---|
| 164 | *pctx = ctx;
|
---|
| 165 | BIO_set_init(b, 1);
|
---|
| 166 | break;
|
---|
| 167 | case BIO_C_SET_MD_CTX:
|
---|
| 168 | if (BIO_get_init(b))
|
---|
| 169 | BIO_set_data(b, ptr);
|
---|
| 170 | else
|
---|
| 171 | ret = 0;
|
---|
| 172 | break;
|
---|
| 173 | case BIO_C_DO_STATE_MACHINE:
|
---|
| 174 | BIO_clear_retry_flags(b);
|
---|
| 175 | ret = BIO_ctrl(next, cmd, num, ptr);
|
---|
| 176 | BIO_copy_next_retry(b);
|
---|
| 177 | break;
|
---|
| 178 |
|
---|
| 179 | case BIO_C_SET_MD:
|
---|
| 180 | md = ptr;
|
---|
| 181 | ret = EVP_DigestInit_ex(ctx, md, NULL);
|
---|
| 182 | if (ret > 0)
|
---|
| 183 | BIO_set_init(b, 1);
|
---|
| 184 | break;
|
---|
| 185 | case BIO_CTRL_DUP:
|
---|
| 186 | dbio = ptr;
|
---|
| 187 | dctx = BIO_get_data(dbio);
|
---|
| 188 | if (!EVP_MD_CTX_copy_ex(dctx, ctx))
|
---|
| 189 | return 0;
|
---|
| 190 | BIO_set_init(b, 1);
|
---|
| 191 | break;
|
---|
| 192 | default:
|
---|
| 193 | ret = BIO_ctrl(next, cmd, num, ptr);
|
---|
| 194 | break;
|
---|
| 195 | }
|
---|
| 196 | return (ret);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
|
---|
| 200 | {
|
---|
| 201 | long ret = 1;
|
---|
| 202 | BIO *next;
|
---|
| 203 |
|
---|
| 204 | next = BIO_next(b);
|
---|
| 205 |
|
---|
| 206 | if (next == NULL)
|
---|
| 207 | return 0;
|
---|
| 208 |
|
---|
| 209 | switch (cmd) {
|
---|
| 210 | default:
|
---|
| 211 | ret = BIO_callback_ctrl(next, cmd, fp);
|
---|
| 212 | break;
|
---|
| 213 | }
|
---|
| 214 | return (ret);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | static int md_gets(BIO *bp, char *buf, int size)
|
---|
| 218 | {
|
---|
| 219 | EVP_MD_CTX *ctx;
|
---|
| 220 | unsigned int ret;
|
---|
| 221 |
|
---|
| 222 | ctx = BIO_get_data(bp);
|
---|
| 223 |
|
---|
| 224 | if (size < ctx->digest->md_size)
|
---|
| 225 | return 0;
|
---|
| 226 |
|
---|
| 227 | if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
|
---|
| 228 | return -1;
|
---|
| 229 |
|
---|
| 230 | return ((int)ret);
|
---|
| 231 | }
|
---|