David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 1 | /* |
Matt Caswell | 3c2bdd7 | 2021-04-08 13:04:41 +0100 | [diff] [blame] | 2 | * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License 2.0 (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 | /* |
| 11 | * A simple ASN.1 DER encoder/decoder for DSA-Sig-Value and ECDSA-Sig-Value. |
| 12 | * |
| 13 | * DSA-Sig-Value ::= SEQUENCE { |
| 14 | * r INTEGER, |
| 15 | * s INTEGER |
| 16 | * } |
| 17 | * |
| 18 | * ECDSA-Sig-Value ::= SEQUENCE { |
| 19 | * r INTEGER, |
| 20 | * s INTEGER |
| 21 | * } |
| 22 | */ |
| 23 | |
| 24 | #include <openssl/crypto.h> |
| 25 | #include <openssl/bn.h> |
Dr. Matthias St. Pierre | 25f2138 | 2019-09-28 00:45:33 +0200 | [diff] [blame] | 26 | #include "crypto/asn1_dsa.h" |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 27 | #include "internal/packet.h" |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 28 | |
| 29 | #define ID_SEQUENCE 0x30 |
| 30 | #define ID_INTEGER 0x02 |
| 31 | |
| 32 | /* |
| 33 | * Outputs the encoding of the length octets for a DER value with a content |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 34 | * length of cont_len bytes to pkt. The maximum supported content length is |
| 35 | * 65535 (0xffff) bytes. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 36 | * |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 37 | * Returns 1 on success or 0 on error. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 38 | */ |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 39 | int ossl_encode_der_length(WPACKET *pkt, size_t cont_len) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 40 | { |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 41 | if (cont_len > 0xffff) |
| 42 | return 0; /* Too large for supported length encodings */ |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 43 | |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 44 | if (cont_len > 0xff) { |
| 45 | if (!WPACKET_put_bytes_u8(pkt, 0x82) |
| 46 | || !WPACKET_put_bytes_u16(pkt, cont_len)) |
| 47 | return 0; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 48 | } else { |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 49 | if (cont_len > 0x7f |
| 50 | && !WPACKET_put_bytes_u8(pkt, 0x81)) |
| 51 | return 0; |
| 52 | if (!WPACKET_put_bytes_u8(pkt, cont_len)) |
| 53 | return 0; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 54 | } |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 55 | |
| 56 | return 1; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 60 | * Outputs the DER encoding of a positive ASN.1 INTEGER to pkt. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 61 | * |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 62 | * Results in an error if n is negative or too large. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 63 | * |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 64 | * Returns 1 on success or 0 on error. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 65 | */ |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 66 | int ossl_encode_der_integer(WPACKET *pkt, const BIGNUM *n) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 67 | { |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 68 | unsigned char *bnbytes; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 69 | size_t cont_len; |
| 70 | |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 71 | if (BN_is_negative(n)) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 72 | return 0; |
| 73 | |
| 74 | /* |
| 75 | * Calculate the ASN.1 INTEGER DER content length for n. |
| 76 | * This is the number of whole bytes required to represent n (i.e. rounded |
| 77 | * down), plus one. |
| 78 | * If n is zero then the content is a single zero byte (length = 1). |
| 79 | * If the number of bits of n is a multiple of 8 then an extra zero padding |
| 80 | * byte is included to ensure that the value is still treated as positive |
| 81 | * in the INTEGER two's complement representation. |
| 82 | */ |
| 83 | cont_len = BN_num_bits(n) / 8 + 1; |
| 84 | |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 85 | if (!WPACKET_start_sub_packet(pkt) |
| 86 | || !WPACKET_put_bytes_u8(pkt, ID_INTEGER) |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 87 | || !ossl_encode_der_length(pkt, cont_len) |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 88 | || !WPACKET_allocate_bytes(pkt, cont_len, &bnbytes) |
| 89 | || !WPACKET_close(pkt)) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 90 | return 0; |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 91 | |
| 92 | if (bnbytes != NULL |
| 93 | && BN_bn2binpad(n, bnbytes, (int)cont_len) != (int)cont_len) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 94 | return 0; |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 95 | |
| 96 | return 1; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 100 | * Outputs the DER encoding of a DSA-Sig-Value or ECDSA-Sig-Value to pkt. pkt |
| 101 | * may be initialised with a NULL buffer which enables pkt to be used to |
Veres Lajos | 79c44b4 | 2019-11-30 23:18:47 +0000 | [diff] [blame] | 102 | * calculate how many bytes would be needed. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 103 | * |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 104 | * Returns 1 on success or 0 on error. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 105 | */ |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 106 | int ossl_encode_der_dsa_sig(WPACKET *pkt, const BIGNUM *r, const BIGNUM *s) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 107 | { |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 108 | WPACKET tmppkt, *dummypkt; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 109 | size_t cont_len; |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 110 | int isnull = WPACKET_is_null_buf(pkt); |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 111 | |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 112 | if (!WPACKET_start_sub_packet(pkt)) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 113 | return 0; |
| 114 | |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 115 | if (!isnull) { |
| 116 | if (!WPACKET_init_null(&tmppkt, 0)) |
| 117 | return 0; |
| 118 | dummypkt = &tmppkt; |
| 119 | } else { |
| 120 | /* If the input packet has a NULL buffer, we don't need a dummy packet */ |
| 121 | dummypkt = pkt; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 122 | } |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 123 | |
| 124 | /* Calculate the content length */ |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 125 | if (!ossl_encode_der_integer(dummypkt, r) |
| 126 | || !ossl_encode_der_integer(dummypkt, s) |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 127 | || !WPACKET_get_length(dummypkt, &cont_len) |
| 128 | || (!isnull && !WPACKET_finish(dummypkt))) { |
| 129 | if (!isnull) |
| 130 | WPACKET_cleanup(dummypkt); |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 131 | return 0; |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Add the tag and length bytes */ |
| 135 | if (!WPACKET_put_bytes_u8(pkt, ID_SEQUENCE) |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 136 | || !ossl_encode_der_length(pkt, cont_len) |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 137 | /* |
| 138 | * Really encode the integers. We already wrote to the main pkt |
| 139 | * if it had a NULL buffer, so don't do it again |
| 140 | */ |
Shane Lontis | 2858156 | 2021-03-09 09:59:13 +1000 | [diff] [blame] | 141 | || (!isnull && !ossl_encode_der_integer(pkt, r)) |
| 142 | || (!isnull && !ossl_encode_der_integer(pkt, s)) |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 143 | || !WPACKET_close(pkt)) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 144 | return 0; |
Matt Caswell | b880583 | 2019-06-10 17:52:15 +0100 | [diff] [blame] | 145 | |
| 146 | return 1; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 150 | * Decodes the DER length octets in pkt and initialises subpkt with the |
| 151 | * following bytes of that length. |
Pauli | 0d03ace | 2019-07-12 06:27:19 +1000 | [diff] [blame] | 152 | * |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 153 | * Returns 1 on success or 0 on failure. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 154 | */ |
Pauli | a55b00b | 2020-09-30 12:15:12 +1000 | [diff] [blame] | 155 | int ossl_decode_der_length(PACKET *pkt, PACKET *subpkt) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 156 | { |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 157 | unsigned int byte; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 158 | |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 159 | if (!PACKET_get_1(pkt, &byte)) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 160 | return 0; |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 161 | |
| 162 | if (byte < 0x80) |
| 163 | return PACKET_get_sub_packet(pkt, subpkt, (size_t)byte); |
| 164 | if (byte == 0x81) |
| 165 | return PACKET_get_length_prefixed_1(pkt, subpkt); |
| 166 | if (byte == 0x82) |
| 167 | return PACKET_get_length_prefixed_2(pkt, subpkt); |
| 168 | |
| 169 | /* Too large, invalid, or not DER. */ |
| 170 | return 0; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 174 | * Decodes a single ASN.1 INTEGER value from pkt, which must be DER encoded, |
| 175 | * and updates n with the decoded value. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 176 | * |
| 177 | * The BIGNUM, n, must have already been allocated by calling BN_new(). |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 178 | * pkt must not be NULL. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 179 | * |
| 180 | * An attempt to consume more than len bytes results in an error. |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 181 | * Returns 1 on success or 0 on error. |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 182 | * |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 183 | * If the PACKET is supposed to only contain a single INTEGER value with no |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 184 | * trailing garbage then it is up to the caller to verify that all bytes |
| 185 | * were consumed. |
| 186 | */ |
Pauli | a55b00b | 2020-09-30 12:15:12 +1000 | [diff] [blame] | 187 | int ossl_decode_der_integer(PACKET *pkt, BIGNUM *n) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 188 | { |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 189 | PACKET contpkt, tmppkt; |
| 190 | unsigned int tag, tmp; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 191 | |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 192 | /* Check we have an integer and get the content bytes */ |
| 193 | if (!PACKET_get_1(pkt, &tag) |
| 194 | || tag != ID_INTEGER |
Pauli | a55b00b | 2020-09-30 12:15:12 +1000 | [diff] [blame] | 195 | || !ossl_decode_der_length(pkt, &contpkt)) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 196 | return 0; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 197 | |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 198 | /* Peek ahead at the first bytes to check for proper encoding */ |
| 199 | tmppkt = contpkt; |
| 200 | /* The INTEGER must be positive */ |
| 201 | if (!PACKET_get_1(&tmppkt, &tmp) |
| 202 | || (tmp & 0x80) != 0) |
| 203 | return 0; |
| 204 | /* If there a zero padding byte the next byte must have the msb set */ |
| 205 | if (PACKET_remaining(&tmppkt) > 0 && tmp == 0) { |
| 206 | if (!PACKET_get_1(&tmppkt, &tmp) |
| 207 | || (tmp & 0x80) == 0) |
| 208 | return 0; |
| 209 | } |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 210 | |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 211 | if (BN_bin2bn(PACKET_data(&contpkt), |
| 212 | (int)PACKET_remaining(&contpkt), n) == NULL) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 213 | return 0; |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 214 | |
| 215 | return 1; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Decodes a single DSA-Sig-Value or ECDSA-Sig-Value from *ppin, which must be |
| 220 | * DER encoded, updates r and s with the decoded values, and increments *ppin |
| 221 | * past the data that was consumed. |
| 222 | * |
| 223 | * The BIGNUMs, r and s, must have already been allocated by calls to BN_new(). |
| 224 | * ppin and *ppin must not be NULL. |
| 225 | * |
| 226 | * An attempt to consume more than len bytes results in an error. |
| 227 | * Returns the number of bytes of input consumed or 0 if an error occurs. |
| 228 | * |
| 229 | * If the buffer is supposed to only contain a single [EC]DSA-Sig-Value with no |
| 230 | * trailing garbage then it is up to the caller to verify that all bytes |
| 231 | * were consumed. |
| 232 | */ |
Pauli | a55b00b | 2020-09-30 12:15:12 +1000 | [diff] [blame] | 233 | size_t ossl_decode_der_dsa_sig(BIGNUM *r, BIGNUM *s, |
| 234 | const unsigned char **ppin, size_t len) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 235 | { |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 236 | size_t consumed; |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 237 | PACKET pkt, contpkt; |
| 238 | unsigned int tag; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 239 | |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 240 | if (!PACKET_buf_init(&pkt, *ppin, len) |
| 241 | || !PACKET_get_1(&pkt, &tag) |
| 242 | || tag != ID_SEQUENCE |
Pauli | a55b00b | 2020-09-30 12:15:12 +1000 | [diff] [blame] | 243 | || !ossl_decode_der_length(&pkt, &contpkt) |
| 244 | || !ossl_decode_der_integer(&contpkt, r) |
| 245 | || !ossl_decode_der_integer(&contpkt, s) |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 246 | || PACKET_remaining(&contpkt) != 0) |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 247 | return 0; |
Matt Caswell | 8ae173b | 2019-06-07 17:40:21 +0100 | [diff] [blame] | 248 | |
| 249 | consumed = PACKET_data(&pkt) - *ppin; |
| 250 | *ppin += consumed; |
David Makepeace | 54846b7 | 2019-06-03 14:58:54 +1000 | [diff] [blame] | 251 | return consumed; |
| 252 | } |