| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/pkcs12/p12_key.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* | - | ||||||||||||||||||||||||
| 2 | * Copyright 1999-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/pkcs12.h> | - | ||||||||||||||||||||||||
| 13 | #include <openssl/bn.h> | - | ||||||||||||||||||||||||
| 14 | - | |||||||||||||||||||||||||
| 15 | /* Uncomment out this line to get debugging info about key generation */ | - | ||||||||||||||||||||||||
| 16 | /* | - | ||||||||||||||||||||||||
| 17 | * #define OPENSSL_DEBUG_KEYGEN | - | ||||||||||||||||||||||||
| 18 | */ | - | ||||||||||||||||||||||||
| 19 | #ifdef OPENSSL_DEBUG_KEYGEN | - | ||||||||||||||||||||||||
| 20 | # include <openssl/bio.h> | - | ||||||||||||||||||||||||
| 21 | extern BIO *bio_err; | - | ||||||||||||||||||||||||
| 22 | void h__dump(unsigned char *p, int len); | - | ||||||||||||||||||||||||
| 23 | #endif | - | ||||||||||||||||||||||||
| 24 | - | |||||||||||||||||||||||||
| 25 | /* PKCS12 compatible key/IV generation */ | - | ||||||||||||||||||||||||
| 26 | #ifndef min | - | ||||||||||||||||||||||||
| 27 | # define min(a,b) ((a) < (b) ? (a) : (b)) | - | ||||||||||||||||||||||||
| 28 | #endif | - | ||||||||||||||||||||||||
| 29 | - | |||||||||||||||||||||||||
| 30 | int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt, | - | ||||||||||||||||||||||||
| 31 | int saltlen, int id, int iter, int n, | - | ||||||||||||||||||||||||
| 32 | unsigned char *out, const EVP_MD *md_type) | - | ||||||||||||||||||||||||
| 33 | { | - | ||||||||||||||||||||||||
| 34 | int ret; | - | ||||||||||||||||||||||||
| 35 | unsigned char *unipass; | - | ||||||||||||||||||||||||
| 36 | int uniplen; | - | ||||||||||||||||||||||||
| 37 | - | |||||||||||||||||||||||||
| 38 | if (!pass) {
| 0 | ||||||||||||||||||||||||
| 39 | unipass = NULL; | - | ||||||||||||||||||||||||
| 40 | uniplen = 0; | - | ||||||||||||||||||||||||
| 41 | } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) { never executed: end of block
| 0 | ||||||||||||||||||||||||
| 42 | PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE); | - | ||||||||||||||||||||||||
| 43 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
| 44 | } | - | ||||||||||||||||||||||||
| 45 | ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen, | - | ||||||||||||||||||||||||
| 46 | id, iter, n, out, md_type); | - | ||||||||||||||||||||||||
| 47 | if (ret <= 0)
| 0 | ||||||||||||||||||||||||
| 48 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
| 49 | OPENSSL_clear_free(unipass, uniplen); | - | ||||||||||||||||||||||||
| 50 | return ret; never executed: return ret; | 0 | ||||||||||||||||||||||||
| 51 | } | - | ||||||||||||||||||||||||
| 52 | - | |||||||||||||||||||||||||
| 53 | int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt, | - | ||||||||||||||||||||||||
| 54 | int saltlen, int id, int iter, int n, | - | ||||||||||||||||||||||||
| 55 | unsigned char *out, const EVP_MD *md_type) | - | ||||||||||||||||||||||||
| 56 | { | - | ||||||||||||||||||||||||
| 57 | int ret; | - | ||||||||||||||||||||||||
| 58 | unsigned char *unipass; | - | ||||||||||||||||||||||||
| 59 | int uniplen; | - | ||||||||||||||||||||||||
| 60 | - | |||||||||||||||||||||||||
| 61 | if (!pass) {
| 0-1 | ||||||||||||||||||||||||
| 62 | unipass = NULL; | - | ||||||||||||||||||||||||
| 63 | uniplen = 0; | - | ||||||||||||||||||||||||
| 64 | } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) { never executed: end of block
| 0-1 | ||||||||||||||||||||||||
| 65 | PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UTF8, ERR_R_MALLOC_FAILURE); | - | ||||||||||||||||||||||||
| 66 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
| 67 | } | - | ||||||||||||||||||||||||
| 68 | ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen, | - | ||||||||||||||||||||||||
| 69 | id, iter, n, out, md_type); | - | ||||||||||||||||||||||||
| 70 | if (ret <= 0)
| 0-1 | ||||||||||||||||||||||||
| 71 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
| 72 | OPENSSL_clear_free(unipass, uniplen); | - | ||||||||||||||||||||||||
| 73 | return ret; executed 1 time by 1 test: return ret;Executed by:
| 1 | ||||||||||||||||||||||||
| 74 | } | - | ||||||||||||||||||||||||
| 75 | - | |||||||||||||||||||||||||
| 76 | int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt, | - | ||||||||||||||||||||||||
| 77 | int saltlen, int id, int iter, int n, | - | ||||||||||||||||||||||||
| 78 | unsigned char *out, const EVP_MD *md_type) | - | ||||||||||||||||||||||||
| 79 | { | - | ||||||||||||||||||||||||
| 80 | unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL; | - | ||||||||||||||||||||||||
| 81 | int Slen, Plen, Ilen; | - | ||||||||||||||||||||||||
| 82 | int i, j, u, v; | - | ||||||||||||||||||||||||
| 83 | int ret = 0; | - | ||||||||||||||||||||||||
| 84 | EVP_MD_CTX *ctx = NULL; | - | ||||||||||||||||||||||||
| 85 | #ifdef OPENSSL_DEBUG_KEYGEN | - | ||||||||||||||||||||||||
| 86 | unsigned char *tmpout = out; | - | ||||||||||||||||||||||||
| 87 | int tmpn = n; | - | ||||||||||||||||||||||||
| 88 | #endif | - | ||||||||||||||||||||||||
| 89 | - | |||||||||||||||||||||||||
| 90 | ctx = EVP_MD_CTX_new(); | - | ||||||||||||||||||||||||
| 91 | if (ctx == NULL)
| 0-7 | ||||||||||||||||||||||||
| 92 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 93 | - | |||||||||||||||||||||||||
| 94 | #ifdef OPENSSL_DEBUG_KEYGEN | - | ||||||||||||||||||||||||
| 95 | fprintf(stderr, "KEYGEN DEBUG\n"); | - | ||||||||||||||||||||||||
| 96 | fprintf(stderr, "ID %d, ITER %d\n", id, iter); | - | ||||||||||||||||||||||||
| 97 | fprintf(stderr, "Password (length %d):\n", passlen); | - | ||||||||||||||||||||||||
| 98 | h__dump(pass, passlen); | - | ||||||||||||||||||||||||
| 99 | fprintf(stderr, "Salt (length %d):\n", saltlen); | - | ||||||||||||||||||||||||
| 100 | h__dump(salt, saltlen); | - | ||||||||||||||||||||||||
| 101 | #endif | - | ||||||||||||||||||||||||
| 102 | v = EVP_MD_block_size(md_type); | - | ||||||||||||||||||||||||
| 103 | u = EVP_MD_size(md_type); | - | ||||||||||||||||||||||||
| 104 | if (u < 0 || v <= 0)
| 0-7 | ||||||||||||||||||||||||
| 105 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 106 | D = OPENSSL_malloc(v); | - | ||||||||||||||||||||||||
| 107 | Ai = OPENSSL_malloc(u); | - | ||||||||||||||||||||||||
| 108 | B = OPENSSL_malloc(v + 1); | - | ||||||||||||||||||||||||
| 109 | Slen = v * ((saltlen + v - 1) / v); | - | ||||||||||||||||||||||||
| 110 | if (passlen)
| 0-7 | ||||||||||||||||||||||||
| 111 | Plen = v * ((passlen + v - 1) / v); executed 7 times by 1 test: Plen = v * ((passlen + v - 1) / v);Executed by:
| 7 | ||||||||||||||||||||||||
| 112 | else | - | ||||||||||||||||||||||||
| 113 | Plen = 0; never executed: Plen = 0; | 0 | ||||||||||||||||||||||||
| 114 | Ilen = Slen + Plen; | - | ||||||||||||||||||||||||
| 115 | I = OPENSSL_malloc(Ilen); | - | ||||||||||||||||||||||||
| 116 | if (D == NULL || Ai == NULL || B == NULL || I == NULL)
| 0-7 | ||||||||||||||||||||||||
| 117 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 118 | for (i = 0; i < v; i++)
| 7-448 | ||||||||||||||||||||||||
| 119 | D[i] = id; executed 448 times by 1 test: D[i] = id;Executed by:
| 448 | ||||||||||||||||||||||||
| 120 | p = I; | - | ||||||||||||||||||||||||
| 121 | for (i = 0; i < Slen; i++)
| 7-448 | ||||||||||||||||||||||||
| 122 | *p++ = salt[i % saltlen]; executed 448 times by 1 test: *p++ = salt[i % saltlen];Executed by:
| 448 | ||||||||||||||||||||||||
| 123 | for (i = 0; i < Plen; i++)
| 7-448 | ||||||||||||||||||||||||
| 124 | *p++ = pass[i % passlen]; executed 448 times by 1 test: *p++ = pass[i % passlen];Executed by:
| 448 | ||||||||||||||||||||||||
| 125 | for (;;) { | - | ||||||||||||||||||||||||
| 126 | if (!EVP_DigestInit_ex(ctx, md_type, NULL)
| 0-9 | ||||||||||||||||||||||||
| 127 | || !EVP_DigestUpdate(ctx, D, v)
| 0-9 | ||||||||||||||||||||||||
| 128 | || !EVP_DigestUpdate(ctx, I, Ilen)
| 0-9 | ||||||||||||||||||||||||
| 129 | || !EVP_DigestFinal_ex(ctx, Ai, NULL))
| 0-9 | ||||||||||||||||||||||||
| 130 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 131 | for (j = 1; j < iter; j++) {
| 9-5995 | ||||||||||||||||||||||||
| 132 | if (!EVP_DigestInit_ex(ctx, md_type, NULL)
| 0-5995 | ||||||||||||||||||||||||
| 133 | || !EVP_DigestUpdate(ctx, Ai, u)
| 0-5995 | ||||||||||||||||||||||||
| 134 | || !EVP_DigestFinal_ex(ctx, Ai, NULL))
| 0-5995 | ||||||||||||||||||||||||
| 135 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||
| 136 | } executed 5995 times by 1 test: end of blockExecuted by:
| 5995 | ||||||||||||||||||||||||
| 137 | memcpy(out, Ai, min(n, u)); | - | ||||||||||||||||||||||||
| 138 | if (u >= n) {
| 2-7 | ||||||||||||||||||||||||
| 139 | #ifdef OPENSSL_DEBUG_KEYGEN | - | ||||||||||||||||||||||||
| 140 | fprintf(stderr, "Output KEY (length %d)\n", tmpn); | - | ||||||||||||||||||||||||
| 141 | h__dump(tmpout, tmpn); | - | ||||||||||||||||||||||||
| 142 | #endif | - | ||||||||||||||||||||||||
| 143 | ret = 1; | - | ||||||||||||||||||||||||
| 144 | goto end; executed 7 times by 1 test: goto end;Executed by:
| 7 | ||||||||||||||||||||||||
| 145 | } | - | ||||||||||||||||||||||||
| 146 | n -= u; | - | ||||||||||||||||||||||||
| 147 | out += u; | - | ||||||||||||||||||||||||
| 148 | for (j = 0; j < v; j++)
| 2-128 | ||||||||||||||||||||||||
| 149 | B[j] = Ai[j % u]; executed 128 times by 1 test: B[j] = Ai[j % u];Executed by:
| 128 | ||||||||||||||||||||||||
| 150 | for (j = 0; j < Ilen; j += v) {
| 2-4 | ||||||||||||||||||||||||
| 151 | int k; | - | ||||||||||||||||||||||||
| 152 | unsigned char *Ij = I + j; | - | ||||||||||||||||||||||||
| 153 | uint16_t c = 1; | - | ||||||||||||||||||||||||
| 154 | - | |||||||||||||||||||||||||
| 155 | /* Work out Ij = Ij + B + 1 */ | - | ||||||||||||||||||||||||
| 156 | for (k = v - 1; k >= 0; k--) {
| 4-256 | ||||||||||||||||||||||||
| 157 | c += Ij[k] + B[k]; | - | ||||||||||||||||||||||||
| 158 | Ij[k] = (unsigned char)c; | - | ||||||||||||||||||||||||
| 159 | c >>= 8; | - | ||||||||||||||||||||||||
| 160 | } executed 256 times by 1 test: end of blockExecuted by:
| 256 | ||||||||||||||||||||||||
| 161 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||||||||||||||
| 162 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 163 | - | |||||||||||||||||||||||||
| 164 | err: code before this statement never executed: err: | 0 | ||||||||||||||||||||||||
| 165 | PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE); | - | ||||||||||||||||||||||||
| 166 | - | |||||||||||||||||||||||||
| 167 | end: code before this statement never executed: end: | 0 | ||||||||||||||||||||||||
| 168 | OPENSSL_free(Ai); | - | ||||||||||||||||||||||||
| 169 | OPENSSL_free(B); | - | ||||||||||||||||||||||||
| 170 | OPENSSL_free(D); | - | ||||||||||||||||||||||||
| 171 | OPENSSL_free(I); | - | ||||||||||||||||||||||||
| 172 | EVP_MD_CTX_free(ctx); | - | ||||||||||||||||||||||||
| 173 | return ret; executed 7 times by 1 test: return ret;Executed by:
| 7 | ||||||||||||||||||||||||
| 174 | } | - | ||||||||||||||||||||||||
| 175 | - | |||||||||||||||||||||||||
| 176 | #ifdef OPENSSL_DEBUG_KEYGEN | - | ||||||||||||||||||||||||
| 177 | void h__dump(unsigned char *p, int len) | - | ||||||||||||||||||||||||
| 178 | { | - | ||||||||||||||||||||||||
| 179 | for (; len--; p++) | - | ||||||||||||||||||||||||
| 180 | fprintf(stderr, "%02X", *p); | - | ||||||||||||||||||||||||
| 181 | fprintf(stderr, "\n"); | - | ||||||||||||||||||||||||
| 182 | } | - | ||||||||||||||||||||||||
| 183 | #endif | - | ||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |