| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/dsa/dsa_asn1.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 "dsa_locl.h" | - | ||||||||||||
| 13 | #include <openssl/asn1.h> | - | ||||||||||||
| 14 | #include <openssl/asn1t.h> | - | ||||||||||||
| 15 | #include <openssl/rand.h> | - | ||||||||||||
| 16 | - | |||||||||||||
| 17 | ASN1_SEQUENCE(DSA_SIG) = { | - | ||||||||||||
| 18 | ASN1_SIMPLE(DSA_SIG, r, CBIGNUM), | - | ||||||||||||
| 19 | ASN1_SIMPLE(DSA_SIG, s, CBIGNUM) | - | ||||||||||||
| 20 | } static_ASN1_SEQUENCE_END(DSA_SIG) | - | ||||||||||||
| 21 | - | |||||||||||||
| 22 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA_SIG, DSA_SIG, DSA_SIG) executed 3694 times by 1 test: return (DSA_SIG *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(DSA_SIG_it)));Executed by:
executed 379 times by 1 test: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(DSA_SIG_it)));Executed by:
| 379-3694 | ||||||||||||
| 23 | - | |||||||||||||
| 24 | DSA_SIG *DSA_SIG_new(void) | - | ||||||||||||
| 25 | { | - | ||||||||||||
| 26 | DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); | - | ||||||||||||
| 27 | if (sig == NULL)
| 0-371 | ||||||||||||
| 28 | DSAerr(DSA_F_DSA_SIG_NEW, ERR_R_MALLOC_FAILURE); never executed: ERR_put_error(10,(102),((1|64)),__FILE__,28); | 0 | ||||||||||||
| 29 | return sig; executed 371 times by 1 test: return sig;Executed by:
| 371 | ||||||||||||
| 30 | } | - | ||||||||||||
| 31 | - | |||||||||||||
| 32 | void DSA_SIG_free(DSA_SIG *sig) | - | ||||||||||||
| 33 | { | - | ||||||||||||
| 34 | if (sig == NULL)
| 2-400 | ||||||||||||
| 35 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||
| 36 | BN_clear_free(sig->r); | - | ||||||||||||
| 37 | BN_clear_free(sig->s); | - | ||||||||||||
| 38 | OPENSSL_free(sig); | - | ||||||||||||
| 39 | } executed 400 times by 1 test: end of blockExecuted by:
| 400 | ||||||||||||
| 40 | - | |||||||||||||
| 41 | void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) | - | ||||||||||||
| 42 | { | - | ||||||||||||
| 43 | if (pr != NULL)
| 0-321 | ||||||||||||
| 44 | *pr = sig->r; executed 321 times by 1 test: *pr = sig->r;Executed by:
| 321 | ||||||||||||
| 45 | if (ps != NULL)
| 0-321 | ||||||||||||
| 46 | *ps = sig->s; executed 321 times by 1 test: *ps = sig->s;Executed by:
| 321 | ||||||||||||
| 47 | } executed 321 times by 1 test: end of blockExecuted by:
| 321 | ||||||||||||
| 48 | - | |||||||||||||
| 49 | int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) | - | ||||||||||||
| 50 | { | - | ||||||||||||
| 51 | if (r == NULL || s == NULL)
| 0 | ||||||||||||
| 52 | return 0; never executed: return 0; | 0 | ||||||||||||
| 53 | BN_clear_free(sig->r); | - | ||||||||||||
| 54 | BN_clear_free(sig->s); | - | ||||||||||||
| 55 | sig->r = r; | - | ||||||||||||
| 56 | sig->s = s; | - | ||||||||||||
| 57 | return 1; never executed: return 1; | 0 | ||||||||||||
| 58 | } | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | /* Override the default free and new methods */ | - | ||||||||||||
| 61 | static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | - | ||||||||||||
| 62 | void *exarg) | - | ||||||||||||
| 63 | { | - | ||||||||||||
| 64 | if (operation == ASN1_OP_NEW_PRE) {
| 15092-30349 | ||||||||||||
| 65 | *pval = (ASN1_VALUE *)DSA_new(); | - | ||||||||||||
| 66 | if (*pval != NULL)
| 0-15092 | ||||||||||||
| 67 | return 2; executed 15092 times by 1 test: return 2;Executed by:
| 15092 | ||||||||||||
| 68 | return 0; never executed: return 0; | 0 | ||||||||||||
| 69 | } else if (operation == ASN1_OP_FREE_PRE) {
| 10559-19790 | ||||||||||||
| 70 | DSA_free((DSA *)*pval); | - | ||||||||||||
| 71 | *pval = NULL; | - | ||||||||||||
| 72 | return 2; executed 10559 times by 1 test: return 2;Executed by:
| 10559 | ||||||||||||
| 73 | } | - | ||||||||||||
| 74 | return 1; executed 19790 times by 1 test: return 1;Executed by:
| 19790 | ||||||||||||
| 75 | } | - | ||||||||||||
| 76 | - | |||||||||||||
| 77 | ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = { | - | ||||||||||||
| 78 | ASN1_EMBED(DSA, version, INT32), | - | ||||||||||||
| 79 | ASN1_SIMPLE(DSA, p, BIGNUM), | - | ||||||||||||
| 80 | ASN1_SIMPLE(DSA, q, BIGNUM), | - | ||||||||||||
| 81 | ASN1_SIMPLE(DSA, g, BIGNUM), | - | ||||||||||||
| 82 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | - | ||||||||||||
| 83 | ASN1_SIMPLE(DSA, priv_key, CBIGNUM) | - | ||||||||||||
| 84 | } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey) | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey) executed 5360 times by 1 test: return (DSA *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(DSAPrivateKey_it)));Executed by:
executed 27 times by 1 test: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(DSAPrivateKey_it)));Executed by:
| 27-5360 | ||||||||||||
| 87 | - | |||||||||||||
| 88 | ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = { | - | ||||||||||||
| 89 | ASN1_SIMPLE(DSA, p, BIGNUM), | - | ||||||||||||
| 90 | ASN1_SIMPLE(DSA, q, BIGNUM), | - | ||||||||||||
| 91 | ASN1_SIMPLE(DSA, g, BIGNUM), | - | ||||||||||||
| 92 | } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams) | - | ||||||||||||
| 93 | - | |||||||||||||
| 94 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams) executed 7493 times by 1 test: return (DSA *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(DSAparams_it)));Executed by:
executed 35 times by 1 test: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(DSAparams_it)));Executed by:
| 35-7493 | ||||||||||||
| 95 | - | |||||||||||||
| 96 | ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = { | - | ||||||||||||
| 97 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | - | ||||||||||||
| 98 | ASN1_SIMPLE(DSA, p, BIGNUM), | - | ||||||||||||
| 99 | ASN1_SIMPLE(DSA, q, BIGNUM), | - | ||||||||||||
| 100 | ASN1_SIMPLE(DSA, g, BIGNUM) | - | ||||||||||||
| 101 | } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey) | - | ||||||||||||
| 102 | - | |||||||||||||
| 103 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey) executed 3323 times by 1 test: return (DSA *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (&(DSAPublicKey_it)));Executed by:
executed 3 times by 1 test: return ASN1_item_i2d((ASN1_VALUE *)a, out, (&(DSAPublicKey_it)));Executed by:
| 3-3323 | ||||||||||||
| 104 | - | |||||||||||||
| 105 | DSA *DSAparams_dup(DSA *dsa) | - | ||||||||||||
| 106 | { | - | ||||||||||||
| 107 | return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa); never executed: return ASN1_item_dup((&(DSAparams_it)), dsa); | 0 | ||||||||||||
| 108 | } | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | int DSA_sign(int type, const unsigned char *dgst, int dlen, | - | ||||||||||||
| 111 | unsigned char *sig, unsigned int *siglen, DSA *dsa) | - | ||||||||||||
| 112 | { | - | ||||||||||||
| 113 | DSA_SIG *s; | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | s = DSA_do_sign(dgst, dlen, dsa); | - | ||||||||||||
| 116 | if (s == NULL) {
| 0-64 | ||||||||||||
| 117 | *siglen = 0; | - | ||||||||||||
| 118 | return 0; never executed: return 0; | 0 | ||||||||||||
| 119 | } | - | ||||||||||||
| 120 | *siglen = i2d_DSA_SIG(s, &sig); | - | ||||||||||||
| 121 | DSA_SIG_free(s); | - | ||||||||||||
| 122 | return 1; executed 64 times by 1 test: return 1;Executed by:
| 64 | ||||||||||||
| 123 | } | - | ||||||||||||
| 124 | - | |||||||||||||
| 125 | /* data has already been hashed (probably with SHA or SHA-1). */ | - | ||||||||||||
| 126 | /*- | - | ||||||||||||
| 127 | * returns | - | ||||||||||||
| 128 | * 1: correct signature | - | ||||||||||||
| 129 | * 0: incorrect signature | - | ||||||||||||
| 130 | * -1: error | - | ||||||||||||
| 131 | */ | - | ||||||||||||
| 132 | int DSA_verify(int type, const unsigned char *dgst, int dgst_len, | - | ||||||||||||
| 133 | const unsigned char *sigbuf, int siglen, DSA *dsa) | - | ||||||||||||
| 134 | { | - | ||||||||||||
| 135 | DSA_SIG *s; | - | ||||||||||||
| 136 | const unsigned char *p = sigbuf; | - | ||||||||||||
| 137 | unsigned char *der = NULL; | - | ||||||||||||
| 138 | int derlen = -1; | - | ||||||||||||
| 139 | int ret = -1; | - | ||||||||||||
| 140 | - | |||||||||||||
| 141 | s = DSA_SIG_new(); | - | ||||||||||||
| 142 | if (s == NULL)
| 0-307 | ||||||||||||
| 143 | return ret; never executed: return ret; | 0 | ||||||||||||
| 144 | if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
| 2-305 | ||||||||||||
| 145 | goto err; executed 2 times by 1 test: goto err;Executed by:
| 2 | ||||||||||||
| 146 | /* Ensure signature uses DER and doesn't have trailing garbage */ | - | ||||||||||||
| 147 | derlen = i2d_DSA_SIG(s, &der); | - | ||||||||||||
| 148 | if (derlen != siglen || memcmp(sigbuf, der, derlen))
| 1-302 | ||||||||||||
| 149 | goto err; executed 4 times by 1 test: goto err;Executed by:
| 4 | ||||||||||||
| 150 | ret = DSA_do_verify(dgst, dgst_len, s, dsa); | - | ||||||||||||
| 151 | err: code before this statement executed 301 times by 1 test: err:Executed by:
| 301 | ||||||||||||
| 152 | OPENSSL_clear_free(der, derlen); | - | ||||||||||||
| 153 | DSA_SIG_free(s); | - | ||||||||||||
| 154 | return ret; executed 307 times by 1 test: return ret;Executed by:
| 307 | ||||||||||||
| 155 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |