| 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/dh.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 |
| 20 | #include <openssl/bn.h> |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/err.h> |
| 23 |
| 24 | #include "../bytestring/internal.h" |
| 25 | #include "../fipsmodule/dh/internal.h" |
| 26 |
| 27 |
| 28 | static int parse_integer(CBS *cbs, BIGNUM **out) { |
| 29 | assert(*out == NULL); |
| 30 | *out = BN_new(); |
| 31 | if (*out == NULL) { |
| 32 | return 0; |
| 33 | } |
| 34 | return BN_parse_asn1_unsigned(cbs, *out); |
| 35 | } |
| 36 |
| 37 | static int marshal_integer(CBB *cbb, BIGNUM *bn) { |
| 38 | if (bn == NULL) { |
| 39 | // A DH object may be missing some components. |
| 40 | OPENSSL_PUT_ERROR(DH, ERR_R_PASSED_NULL_PARAMETER); |
| 41 | return 0; |
| 42 | } |
| 43 | return BN_marshal_asn1(cbb, bn); |
| 44 | } |
| 45 |
| 46 | DH *DH_parse_parameters(CBS *cbs) { |
| 47 | DH *ret = DH_new(); |
| 48 | if (ret == NULL) { |
| 49 | return NULL; |
| 50 | } |
| 51 |
| 52 | CBS child; |
| 53 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 54 | !parse_integer(&child, &ret->p) || |
| 55 | !parse_integer(&child, &ret->g)) { |
| 56 | goto err; |
| 57 | } |
| 58 |
| 59 | uint64_t priv_length; |
| 60 | if (CBS_len(&child) != 0) { |
| 61 | if (!CBS_get_asn1_uint64(&child, &priv_length) || |
| 62 | priv_length > UINT_MAX) { |
| 63 | goto err; |
| 64 | } |
| 65 | ret->priv_length = (unsigned)priv_length; |
| 66 | } |
| 67 |
| 68 | if (CBS_len(&child) != 0) { |
| 69 | goto err; |
| 70 | } |
| 71 |
| 72 | if (!dh_check_params_fast(ret)) { |
| 73 | goto err; |
| 74 | } |
| 75 |
| 76 | return ret; |
| 77 |
| 78 | err: |
| 79 | OPENSSL_PUT_ERROR(DH, DH_R_DECODE_ERROR); |
| 80 | DH_free(ret); |
| 81 | return NULL; |
| 82 | } |
| 83 |
| 84 | int DH_marshal_parameters(CBB *cbb, const DH *dh) { |
| 85 | CBB child; |
| 86 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 87 | !marshal_integer(&child, dh->p) || |
| 88 | !marshal_integer(&child, dh->g) || |
| 89 | (dh->priv_length != 0 && |
| 90 | !CBB_add_asn1_uint64(&child, dh->priv_length)) || |
| 91 | !CBB_flush(cbb)) { |
| 92 | OPENSSL_PUT_ERROR(DH, DH_R_ENCODE_ERROR); |
| 93 | return 0; |
| 94 | } |
| 95 | return 1; |
| 96 | } |
| 97 |
| 98 | DH *d2i_DHparams(DH **out, const uint8_t **inp, long len) { |
| 99 | if (len < 0) { |
| 100 | return NULL; |
| 101 | } |
| 102 | CBS cbs; |
| 103 | CBS_init(&cbs, *inp, (size_t)len); |
| 104 | DH *ret = DH_parse_parameters(&cbs); |
| 105 | if (ret == NULL) { |
| 106 | return NULL; |
| 107 | } |
| 108 | if (out != NULL) { |
| 109 | DH_free(*out); |
| 110 | *out = ret; |
| 111 | } |
| 112 | *inp = CBS_data(&cbs); |
| 113 | return ret; |
| 114 | } |
| 115 |
| 116 | int i2d_DHparams(const DH *in, uint8_t **outp) { |
| 117 | CBB cbb; |
| 118 | if (!CBB_init(&cbb, 0) || |
| 119 | !DH_marshal_parameters(&cbb, in)) { |
| 120 | CBB_cleanup(&cbb); |
| 121 | return -1; |
| 122 | } |
| 123 | return CBB_finish_i2d(&cbb, outp); |
| 124 | } |
| 125 |