| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/dsa.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 |
| 19 | #include <openssl/bn.h> |
| 20 | #include <openssl/bytestring.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 |
| 24 | #include "internal.h" |
| 25 | #include "../bytestring/internal.h" |
| 26 |
| 27 |
| 28 | // This function is in dsa_asn1.c rather than dsa.c because it is reachable from |
| 29 | // |EVP_PKEY| parsers. This makes it easier for the static linker to drop most |
| 30 | // of the DSA implementation. |
| 31 | int dsa_check_key(const DSA *dsa) { |
| 32 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 33 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 34 | return 0; |
| 35 | } |
| 36 |
| 37 | // Fully checking for invalid DSA groups is expensive, so security and |
| 38 | // correctness of the signature scheme depend on how |dsa| was computed. I.e. |
| 39 | // we leave "assurance of domain parameter validity" from FIPS 186-4 to the |
| 40 | // caller. However, we check bounds on all values to avoid DoS vectors even |
| 41 | // when domain parameters are invalid. In particular, signing will infinite |
| 42 | // loop if |g| is zero. |
| 43 | if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) || |
| 44 | BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) || |
| 45 | // |q| must be a prime divisor of |p - 1|, which implies |q < p|. |
| 46 | BN_cmp(dsa->q, dsa->p) >= 0 || |
| 47 | // |g| is in the multiplicative group of |p|. |
| 48 | BN_is_negative(dsa->g) || BN_is_zero(dsa->g) || |
| 49 | BN_cmp(dsa->g, dsa->p) >= 0) { |
| 50 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
| 51 | return 0; |
| 52 | } |
| 53 |
| 54 | // FIPS 186-4 allows only three different sizes for q. |
| 55 | unsigned q_bits = BN_num_bits(dsa->q); |
| 56 | if (q_bits != 160 && q_bits != 224 && q_bits != 256) { |
| 57 | OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE); |
| 58 | return 0; |
| 59 | } |
| 60 |
| 61 | // Bound |dsa->p| to avoid a DoS vector. Note this limit is much larger than |
| 62 | // the one in FIPS 186-4, which only allows L = 1024, 2048, and 3072. |
| 63 | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) { |
| 64 | OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE); |
| 65 | return 0; |
| 66 | } |
| 67 |
| 68 | if (dsa->pub_key != NULL) { |
| 69 | // The public key is also in the multiplicative group of |p|. |
| 70 | if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) || |
| 71 | BN_cmp(dsa->pub_key, dsa->p) >= 0) { |
| 72 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 |
| 77 | if (dsa->priv_key != NULL) { |
| 78 | // The private key is a non-zero element of the scalar field, determined by |
| 79 | // |q|. |
| 80 | if (BN_is_negative(dsa->priv_key) || |
| 81 | constant_time_declassify_int(BN_is_zero(dsa->priv_key)) || |
| 82 | constant_time_declassify_int(BN_cmp(dsa->priv_key, dsa->q) >= 0)) { |
| 83 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
| 84 | return 0; |
| 85 | } |
| 86 | } |
| 87 |
| 88 | return 1; |
| 89 | } |
| 90 |
| 91 | static int parse_integer(CBS *cbs, BIGNUM **out) { |
| 92 | assert(*out == NULL); |
| 93 | *out = BN_new(); |
| 94 | if (*out == NULL) { |
| 95 | return 0; |
| 96 | } |
| 97 | return BN_parse_asn1_unsigned(cbs, *out); |
| 98 | } |
| 99 |
| 100 | static int marshal_integer(CBB *cbb, BIGNUM *bn) { |
| 101 | if (bn == NULL) { |
| 102 | // A DSA object may be missing some components. |
| 103 | OPENSSL_PUT_ERROR(DSA, ERR_R_PASSED_NULL_PARAMETER); |
| 104 | return 0; |
| 105 | } |
| 106 | return BN_marshal_asn1(cbb, bn); |
| 107 | } |
| 108 |
| 109 | DSA_SIG *DSA_SIG_parse(CBS *cbs) { |
| 110 | DSA_SIG *ret = DSA_SIG_new(); |
| 111 | if (ret == NULL) { |
| 112 | return NULL; |
| 113 | } |
| 114 | CBS child; |
| 115 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 116 | !parse_integer(&child, &ret->r) || |
| 117 | !parse_integer(&child, &ret->s) || |
| 118 | CBS_len(&child) != 0) { |
| 119 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
| 120 | DSA_SIG_free(ret); |
| 121 | return NULL; |
| 122 | } |
| 123 | return ret; |
| 124 | } |
| 125 |
| 126 | int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig) { |
| 127 | CBB child; |
| 128 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 129 | !marshal_integer(&child, sig->r) || |
| 130 | !marshal_integer(&child, sig->s) || |
| 131 | !CBB_flush(cbb)) { |
| 132 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
| 133 | return 0; |
| 134 | } |
| 135 | return 1; |
| 136 | } |
| 137 |
| 138 | DSA *DSA_parse_public_key(CBS *cbs) { |
| 139 | DSA *ret = DSA_new(); |
| 140 | if (ret == NULL) { |
| 141 | return NULL; |
| 142 | } |
| 143 | CBS child; |
| 144 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 145 | !parse_integer(&child, &ret->pub_key) || |
| 146 | !parse_integer(&child, &ret->p) || |
| 147 | !parse_integer(&child, &ret->q) || |
| 148 | !parse_integer(&child, &ret->g) || |
| 149 | CBS_len(&child) != 0) { |
| 150 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
| 151 | goto err; |
| 152 | } |
| 153 | if (!dsa_check_key(ret)) { |
| 154 | goto err; |
| 155 | } |
| 156 | return ret; |
| 157 |
| 158 | err: |
| 159 | DSA_free(ret); |
| 160 | return NULL; |
| 161 | } |
| 162 |
| 163 | int DSA_marshal_public_key(CBB *cbb, const DSA *dsa) { |
| 164 | CBB child; |
| 165 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 166 | !marshal_integer(&child, dsa->pub_key) || |
| 167 | !marshal_integer(&child, dsa->p) || |
| 168 | !marshal_integer(&child, dsa->q) || |
| 169 | !marshal_integer(&child, dsa->g) || |
| 170 | !CBB_flush(cbb)) { |
| 171 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
| 172 | return 0; |
| 173 | } |
| 174 | return 1; |
| 175 | } |
| 176 |
| 177 | DSA *DSA_parse_parameters(CBS *cbs) { |
| 178 | DSA *ret = DSA_new(); |
| 179 | if (ret == NULL) { |
| 180 | return NULL; |
| 181 | } |
| 182 | CBS child; |
| 183 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 184 | !parse_integer(&child, &ret->p) || |
| 185 | !parse_integer(&child, &ret->q) || |
| 186 | !parse_integer(&child, &ret->g) || |
| 187 | CBS_len(&child) != 0) { |
| 188 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
| 189 | goto err; |
| 190 | } |
| 191 | if (!dsa_check_key(ret)) { |
| 192 | goto err; |
| 193 | } |
| 194 | return ret; |
| 195 |
| 196 | err: |
| 197 | DSA_free(ret); |
| 198 | return NULL; |
| 199 | } |
| 200 |
| 201 | int DSA_marshal_parameters(CBB *cbb, const DSA *dsa) { |
| 202 | CBB child; |
| 203 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 204 | !marshal_integer(&child, dsa->p) || |
| 205 | !marshal_integer(&child, dsa->q) || |
| 206 | !marshal_integer(&child, dsa->g) || |
| 207 | !CBB_flush(cbb)) { |
| 208 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
| 209 | return 0; |
| 210 | } |
| 211 | return 1; |
| 212 | } |
| 213 |
| 214 | DSA *DSA_parse_private_key(CBS *cbs) { |
| 215 | DSA *ret = DSA_new(); |
| 216 | if (ret == NULL) { |
| 217 | return NULL; |
| 218 | } |
| 219 |
| 220 | CBS child; |
| 221 | uint64_t version; |
| 222 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 223 | !CBS_get_asn1_uint64(&child, &version)) { |
| 224 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
| 225 | goto err; |
| 226 | } |
| 227 |
| 228 | if (version != 0) { |
| 229 | OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_VERSION); |
| 230 | goto err; |
| 231 | } |
| 232 |
| 233 | if (!parse_integer(&child, &ret->p) || |
| 234 | !parse_integer(&child, &ret->q) || |
| 235 | !parse_integer(&child, &ret->g) || |
| 236 | !parse_integer(&child, &ret->pub_key) || |
| 237 | !parse_integer(&child, &ret->priv_key) || |
| 238 | CBS_len(&child) != 0) { |
| 239 | OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR); |
| 240 | goto err; |
| 241 | } |
| 242 | if (!dsa_check_key(ret)) { |
| 243 | goto err; |
| 244 | } |
| 245 |
| 246 | return ret; |
| 247 |
| 248 | err: |
| 249 | DSA_free(ret); |
| 250 | return NULL; |
| 251 | } |
| 252 |
| 253 | int DSA_marshal_private_key(CBB *cbb, const DSA *dsa) { |
| 254 | CBB child; |
| 255 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 256 | !CBB_add_asn1_uint64(&child, 0 /* version */) || |
| 257 | !marshal_integer(&child, dsa->p) || |
| 258 | !marshal_integer(&child, dsa->q) || |
| 259 | !marshal_integer(&child, dsa->g) || |
| 260 | !marshal_integer(&child, dsa->pub_key) || |
| 261 | !marshal_integer(&child, dsa->priv_key) || |
| 262 | !CBB_flush(cbb)) { |
| 263 | OPENSSL_PUT_ERROR(DSA, DSA_R_ENCODE_ERROR); |
| 264 | return 0; |
| 265 | } |
| 266 | return 1; |
| 267 | } |
| 268 |
| 269 | DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, long len) { |
| 270 | if (len < 0) { |
| 271 | return NULL; |
| 272 | } |
| 273 | CBS cbs; |
| 274 | CBS_init(&cbs, *inp, (size_t)len); |
| 275 | DSA_SIG *ret = DSA_SIG_parse(&cbs); |
| 276 | if (ret == NULL) { |
| 277 | return NULL; |
| 278 | } |
| 279 | if (out_sig != NULL) { |
| 280 | DSA_SIG_free(*out_sig); |
| 281 | *out_sig = ret; |
| 282 | } |
| 283 | *inp = CBS_data(&cbs); |
| 284 | return ret; |
| 285 | } |
| 286 |
| 287 | int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp) { |
| 288 | CBB cbb; |
| 289 | if (!CBB_init(&cbb, 0) || |
| 290 | !DSA_SIG_marshal(&cbb, in)) { |
| 291 | CBB_cleanup(&cbb); |
| 292 | return -1; |
| 293 | } |
| 294 | return CBB_finish_i2d(&cbb, outp); |
| 295 | } |
| 296 |
| 297 | DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len) { |
| 298 | if (len < 0) { |
| 299 | return NULL; |
| 300 | } |
| 301 | CBS cbs; |
| 302 | CBS_init(&cbs, *inp, (size_t)len); |
| 303 | DSA *ret = DSA_parse_public_key(&cbs); |
| 304 | if (ret == NULL) { |
| 305 | return NULL; |
| 306 | } |
| 307 | if (out != NULL) { |
| 308 | DSA_free(*out); |
| 309 | *out = ret; |
| 310 | } |
| 311 | *inp = CBS_data(&cbs); |
| 312 | return ret; |
| 313 | } |
| 314 |
| 315 | int i2d_DSAPublicKey(const DSA *in, uint8_t **outp) { |
| 316 | CBB cbb; |
| 317 | if (!CBB_init(&cbb, 0) || |
| 318 | !DSA_marshal_public_key(&cbb, in)) { |
| 319 | CBB_cleanup(&cbb); |
| 320 | return -1; |
| 321 | } |
| 322 | return CBB_finish_i2d(&cbb, outp); |
| 323 | } |
| 324 |
| 325 | DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len) { |
| 326 | if (len < 0) { |
| 327 | return NULL; |
| 328 | } |
| 329 | CBS cbs; |
| 330 | CBS_init(&cbs, *inp, (size_t)len); |
| 331 | DSA *ret = DSA_parse_private_key(&cbs); |
| 332 | if (ret == NULL) { |
| 333 | return NULL; |
| 334 | } |
| 335 | if (out != NULL) { |
| 336 | DSA_free(*out); |
| 337 | *out = ret; |
| 338 | } |
| 339 | *inp = CBS_data(&cbs); |
| 340 | return ret; |
| 341 | } |
| 342 |
| 343 | int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp) { |
| 344 | CBB cbb; |
| 345 | if (!CBB_init(&cbb, 0) || |
| 346 | !DSA_marshal_private_key(&cbb, in)) { |
| 347 | CBB_cleanup(&cbb); |
| 348 | return -1; |
| 349 | } |
| 350 | return CBB_finish_i2d(&cbb, outp); |
| 351 | } |
| 352 |
| 353 | DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len) { |
| 354 | if (len < 0) { |
| 355 | return NULL; |
| 356 | } |
| 357 | CBS cbs; |
| 358 | CBS_init(&cbs, *inp, (size_t)len); |
| 359 | DSA *ret = DSA_parse_parameters(&cbs); |
| 360 | if (ret == NULL) { |
| 361 | return NULL; |
| 362 | } |
| 363 | if (out != NULL) { |
| 364 | DSA_free(*out); |
| 365 | *out = ret; |
| 366 | } |
| 367 | *inp = CBS_data(&cbs); |
| 368 | return ret; |
| 369 | } |
| 370 |
| 371 | int i2d_DSAparams(const DSA *in, uint8_t **outp) { |
| 372 | CBB cbb; |
| 373 | if (!CBB_init(&cbb, 0) || |
| 374 | !DSA_marshal_parameters(&cbb, in)) { |
| 375 | CBB_cleanup(&cbb); |
| 376 | return -1; |
| 377 | } |
| 378 | return CBB_finish_i2d(&cbb, outp); |
| 379 | } |
| 380 |