| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/evp/bio_md.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 | static int md_gets(BIO *h, char *str, int size); | - | ||||||||||||
| 26 | static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2); | - | ||||||||||||
| 27 | static int md_new(BIO *h); | - | ||||||||||||
| 28 | static int md_free(BIO *data); | - | ||||||||||||
| 29 | static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); | - | ||||||||||||
| 30 | - | |||||||||||||
| 31 | static const BIO_METHOD methods_md = { | - | ||||||||||||
| 32 | BIO_TYPE_MD, | - | ||||||||||||
| 33 | "message digest", | - | ||||||||||||
| 34 | /* TODO: Convert to new style write function */ | - | ||||||||||||
| 35 | bwrite_conv, | - | ||||||||||||
| 36 | md_write, | - | ||||||||||||
| 37 | /* TODO: Convert to new style read function */ | - | ||||||||||||
| 38 | bread_conv, | - | ||||||||||||
| 39 | md_read, | - | ||||||||||||
| 40 | NULL, /* md_puts, */ | - | ||||||||||||
| 41 | md_gets, | - | ||||||||||||
| 42 | md_ctrl, | - | ||||||||||||
| 43 | md_new, | - | ||||||||||||
| 44 | md_free, | - | ||||||||||||
| 45 | md_callback_ctrl, | - | ||||||||||||
| 46 | }; | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | const BIO_METHOD *BIO_f_md(void) | - | ||||||||||||
| 49 | { | - | ||||||||||||
| 50 | return &methods_md; executed 88 times by 1 test: return &methods_md;Executed by:
| 88 | ||||||||||||
| 51 | } | - | ||||||||||||
| 52 | - | |||||||||||||
| 53 | static int md_new(BIO *bi) | - | ||||||||||||
| 54 | { | - | ||||||||||||
| 55 | EVP_MD_CTX *ctx; | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | ctx = EVP_MD_CTX_new(); | - | ||||||||||||
| 58 | if (ctx == NULL)
| 0-88 | ||||||||||||
| 59 | return 0; never executed: return 0; | 0 | ||||||||||||
| 60 | - | |||||||||||||
| 61 | BIO_set_init(bi, 1); | - | ||||||||||||
| 62 | BIO_set_data(bi, ctx); | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | return 1; executed 88 times by 1 test: return 1;Executed by:
| 88 | ||||||||||||
| 65 | } | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | static int md_free(BIO *a) | - | ||||||||||||
| 68 | { | - | ||||||||||||
| 69 | if (a == NULL)
| 0-88 | ||||||||||||
| 70 | return 0; never executed: return 0; | 0 | ||||||||||||
| 71 | EVP_MD_CTX_free(BIO_get_data(a)); | - | ||||||||||||
| 72 | BIO_set_data(a, NULL); | - | ||||||||||||
| 73 | BIO_set_init(a, 0); | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | return 1; executed 88 times by 1 test: return 1;Executed by:
| 88 | ||||||||||||
| 76 | } | - | ||||||||||||
| 77 | - | |||||||||||||
| 78 | static int md_read(BIO *b, char *out, int outl) | - | ||||||||||||
| 79 | { | - | ||||||||||||
| 80 | int ret = 0; | - | ||||||||||||
| 81 | EVP_MD_CTX *ctx; | - | ||||||||||||
| 82 | BIO *next; | - | ||||||||||||
| 83 | - | |||||||||||||
| 84 | if (out == NULL)
| 0-76 | ||||||||||||
| 85 | return 0; never executed: return 0; | 0 | ||||||||||||
| 86 | - | |||||||||||||
| 87 | ctx = BIO_get_data(b); | - | ||||||||||||
| 88 | next = BIO_next(b); | - | ||||||||||||
| 89 | - | |||||||||||||
| 90 | if ((ctx == NULL) || (next == NULL))
| 0-76 | ||||||||||||
| 91 | return 0; never executed: return 0; | 0 | ||||||||||||
| 92 | - | |||||||||||||
| 93 | ret = BIO_read(next, out, outl); | - | ||||||||||||
| 94 | if (BIO_get_init(b)) {
| 0-76 | ||||||||||||
| 95 | if (ret > 0) {
| 38 | ||||||||||||
| 96 | if (EVP_DigestUpdate(ctx, (unsigned char *)out,
| 0-38 | ||||||||||||
| 97 | (unsigned int)ret) <= 0)
| 0-38 | ||||||||||||
| 98 | return -1; never executed: return -1; | 0 | ||||||||||||
| 99 | } executed 38 times by 1 test: end of blockExecuted by:
| 38 | ||||||||||||
| 100 | } executed 76 times by 1 test: end of blockExecuted by:
| 76 | ||||||||||||
| 101 | BIO_clear_retry_flags(b); | - | ||||||||||||
| 102 | BIO_copy_next_retry(b); | - | ||||||||||||
| 103 | return ret; executed 76 times by 1 test: return ret;Executed by:
| 76 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | static int md_write(BIO *b, const char *in, int inl) | - | ||||||||||||
| 107 | { | - | ||||||||||||
| 108 | int ret = 0; | - | ||||||||||||
| 109 | EVP_MD_CTX *ctx; | - | ||||||||||||
| 110 | BIO *next; | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | if ((in == NULL) || (inl <= 0))
| 0-50 | ||||||||||||
| 113 | return 0; never executed: return 0; | 0 | ||||||||||||
| 114 | - | |||||||||||||
| 115 | ctx = BIO_get_data(b); | - | ||||||||||||
| 116 | next = BIO_next(b); | - | ||||||||||||
| 117 | if ((ctx != NULL) && (next != NULL))
| 0-50 | ||||||||||||
| 118 | ret = BIO_write(next, in, inl); executed 50 times by 1 test: ret = BIO_write(next, in, inl);Executed by:
| 50 | ||||||||||||
| 119 | - | |||||||||||||
| 120 | if (BIO_get_init(b)) {
| 0-50 | ||||||||||||
| 121 | if (ret > 0) {
| 0-50 | ||||||||||||
| 122 | if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
| 0-50 | ||||||||||||
| 123 | (unsigned int)ret)) {
| 0-50 | ||||||||||||
| 124 | BIO_clear_retry_flags(b); | - | ||||||||||||
| 125 | return 0; never executed: return 0; | 0 | ||||||||||||
| 126 | } | - | ||||||||||||
| 127 | } executed 50 times by 1 test: end of blockExecuted by:
| 50 | ||||||||||||
| 128 | } executed 50 times by 1 test: end of blockExecuted by:
| 50 | ||||||||||||
| 129 | if (next != NULL) {
| 0-50 | ||||||||||||
| 130 | BIO_clear_retry_flags(b); | - | ||||||||||||
| 131 | BIO_copy_next_retry(b); | - | ||||||||||||
| 132 | } executed 50 times by 1 test: end of blockExecuted by:
| 50 | ||||||||||||
| 133 | return ret; executed 50 times by 1 test: return ret;Executed by:
| 50 | ||||||||||||
| 134 | } | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | static long md_ctrl(BIO *b, int cmd, long num, void *ptr) | - | ||||||||||||
| 137 | { | - | ||||||||||||
| 138 | EVP_MD_CTX *ctx, *dctx, **pctx; | - | ||||||||||||
| 139 | const EVP_MD **ppmd; | - | ||||||||||||
| 140 | EVP_MD *md; | - | ||||||||||||
| 141 | long ret = 1; | - | ||||||||||||
| 142 | BIO *dbio, *next; | - | ||||||||||||
| 143 | - | |||||||||||||
| 144 | - | |||||||||||||
| 145 | ctx = BIO_get_data(b); | - | ||||||||||||
| 146 | next = BIO_next(b); | - | ||||||||||||
| 147 | - | |||||||||||||
| 148 | switch (cmd) { | - | ||||||||||||
| 149 | case BIO_CTRL_RESET: executed 5 times by 1 test: case 1:Executed by:
| 5 | ||||||||||||
| 150 | if (BIO_get_init(b))
| 0-5 | ||||||||||||
| 151 | ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL); executed 5 times by 1 test: ret = EVP_DigestInit_ex(ctx, ctx->digest, ((void *)0) );Executed by:
| 5 | ||||||||||||
| 152 | else | - | ||||||||||||
| 153 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
| 154 | if (ret > 0)
| 0-5 | ||||||||||||
| 155 | ret = BIO_ctrl(next, cmd, num, ptr); executed 5 times by 1 test: ret = BIO_ctrl(next, cmd, num, ptr);Executed by:
| 5 | ||||||||||||
| 156 | break; executed 5 times by 1 test: break;Executed by:
| 5 | ||||||||||||
| 157 | case BIO_C_GET_MD: never executed: case 112: | 0 | ||||||||||||
| 158 | if (BIO_get_init(b)) {
| 0 | ||||||||||||
| 159 | ppmd = ptr; | - | ||||||||||||
| 160 | *ppmd = ctx->digest; | - | ||||||||||||
| 161 | } else never executed: end of block | 0 | ||||||||||||
| 162 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
| 163 | break; never executed: break; | 0 | ||||||||||||
| 164 | case BIO_C_GET_MD_CTX: executed 180 times by 1 test: case 120:Executed by:
| 180 | ||||||||||||
| 165 | pctx = ptr; | - | ||||||||||||
| 166 | *pctx = ctx; | - | ||||||||||||
| 167 | BIO_set_init(b, 1); | - | ||||||||||||
| 168 | break; executed 180 times by 1 test: break;Executed by:
| 180 | ||||||||||||
| 169 | case BIO_C_SET_MD_CTX: never executed: case 148: | 0 | ||||||||||||
| 170 | if (BIO_get_init(b))
| 0 | ||||||||||||
| 171 | BIO_set_data(b, ptr); never executed: BIO_set_data(b, ptr); | 0 | ||||||||||||
| 172 | else | - | ||||||||||||
| 173 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
| 174 | break; never executed: break; | 0 | ||||||||||||
| 175 | case BIO_C_DO_STATE_MACHINE: never executed: case 101: | 0 | ||||||||||||
| 176 | BIO_clear_retry_flags(b); | - | ||||||||||||
| 177 | ret = BIO_ctrl(next, cmd, num, ptr); | - | ||||||||||||
| 178 | BIO_copy_next_retry(b); | - | ||||||||||||
| 179 | break; never executed: break; | 0 | ||||||||||||
| 180 | - | |||||||||||||
| 181 | case BIO_C_SET_MD: executed 83 times by 1 test: case 111:Executed by:
| 83 | ||||||||||||
| 182 | md = ptr; | - | ||||||||||||
| 183 | ret = EVP_DigestInit_ex(ctx, md, NULL); | - | ||||||||||||
| 184 | if (ret > 0)
| 0-83 | ||||||||||||
| 185 | BIO_set_init(b, 1); executed 83 times by 1 test: BIO_set_init(b, 1);Executed by:
| 83 | ||||||||||||
| 186 | break; executed 83 times by 1 test: break;Executed by:
| 83 | ||||||||||||
| 187 | case BIO_CTRL_DUP: never executed: case 12: | 0 | ||||||||||||
| 188 | dbio = ptr; | - | ||||||||||||
| 189 | dctx = BIO_get_data(dbio); | - | ||||||||||||
| 190 | if (!EVP_MD_CTX_copy_ex(dctx, ctx))
| 0 | ||||||||||||
| 191 | return 0; never executed: return 0; | 0 | ||||||||||||
| 192 | BIO_set_init(b, 1); | - | ||||||||||||
| 193 | break; never executed: break; | 0 | ||||||||||||
| 194 | default: executed 308 times by 1 test: default:Executed by:
| 308 | ||||||||||||
| 195 | ret = BIO_ctrl(next, cmd, num, ptr); | - | ||||||||||||
| 196 | break; executed 308 times by 1 test: break;Executed by:
| 308 | ||||||||||||
| 197 | } | - | ||||||||||||
| 198 | return ret; executed 576 times by 1 test: return ret;Executed by:
| 576 | ||||||||||||
| 199 | } | - | ||||||||||||
| 200 | - | |||||||||||||
| 201 | static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) | - | ||||||||||||
| 202 | { | - | ||||||||||||
| 203 | long ret = 1; | - | ||||||||||||
| 204 | BIO *next; | - | ||||||||||||
| 205 | - | |||||||||||||
| 206 | next = BIO_next(b); | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | if (next == NULL)
| 0 | ||||||||||||
| 209 | return 0; never executed: return 0; | 0 | ||||||||||||
| 210 | - | |||||||||||||
| 211 | switch (cmd) { | - | ||||||||||||
| 212 | default: never executed: default: | 0 | ||||||||||||
| 213 | ret = BIO_callback_ctrl(next, cmd, fp); | - | ||||||||||||
| 214 | break; never executed: break; | 0 | ||||||||||||
| 215 | } | - | ||||||||||||
| 216 | return ret; never executed: return ret; | 0 | ||||||||||||
| 217 | } | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | static int md_gets(BIO *bp, char *buf, int size) | - | ||||||||||||
| 220 | { | - | ||||||||||||
| 221 | EVP_MD_CTX *ctx; | - | ||||||||||||
| 222 | unsigned int ret; | - | ||||||||||||
| 223 | - | |||||||||||||
| 224 | ctx = BIO_get_data(bp); | - | ||||||||||||
| 225 | - | |||||||||||||
| 226 | if (size < ctx->digest->md_size)
| 0 | ||||||||||||
| 227 | return 0; never executed: return 0; | 0 | ||||||||||||
| 228 | - | |||||||||||||
| 229 | if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
| 0 | ||||||||||||
| 230 | return -1; never executed: return -1; | 0 | ||||||||||||
| 231 | - | |||||||||||||
| 232 | return (int)ret; never executed: return (int)ret; | 0 | ||||||||||||
| 233 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |