| 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/rsa.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/bn.h> |
| 22 | #include <openssl/bytestring.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 |
| 26 | #include "../fipsmodule/rsa/internal.h" |
| 27 | #include "../bytestring/internal.h" |
| 28 | #include "../internal.h" |
| 29 |
| 30 |
| 31 | static int parse_integer(CBS *cbs, BIGNUM **out) { |
| 32 | assert(*out == NULL); |
| 33 | *out = BN_new(); |
| 34 | if (*out == NULL) { |
| 35 | return 0; |
| 36 | } |
| 37 | return BN_parse_asn1_unsigned(cbs, *out); |
| 38 | } |
| 39 |
| 40 | static int marshal_integer(CBB *cbb, BIGNUM *bn) { |
| 41 | if (bn == NULL) { |
| 42 | // An RSA object may be missing some components. |
| 43 | OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
| 44 | return 0; |
| 45 | } |
| 46 | return BN_marshal_asn1(cbb, bn); |
| 47 | } |
| 48 |
| 49 | RSA *RSA_parse_public_key(CBS *cbs) { |
| 50 | RSA *ret = RSA_new(); |
| 51 | if (ret == NULL) { |
| 52 | return NULL; |
| 53 | } |
| 54 | CBS child; |
| 55 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 56 | !parse_integer(&child, &ret->n) || |
| 57 | !parse_integer(&child, &ret->e) || |
| 58 | CBS_len(&child) != 0) { |
| 59 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 60 | RSA_free(ret); |
| 61 | return NULL; |
| 62 | } |
| 63 |
| 64 | if (!RSA_check_key(ret)) { |
| 65 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 66 | RSA_free(ret); |
| 67 | return NULL; |
| 68 | } |
| 69 |
| 70 | return ret; |
| 71 | } |
| 72 |
| 73 | RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) { |
| 74 | CBS cbs; |
| 75 | CBS_init(&cbs, in, in_len); |
| 76 | RSA *ret = RSA_parse_public_key(&cbs); |
| 77 | if (ret == NULL || CBS_len(&cbs) != 0) { |
| 78 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 79 | RSA_free(ret); |
| 80 | return NULL; |
| 81 | } |
| 82 | return ret; |
| 83 | } |
| 84 |
| 85 | int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) { |
| 86 | CBB child; |
| 87 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 88 | !marshal_integer(&child, rsa->n) || |
| 89 | !marshal_integer(&child, rsa->e) || |
| 90 | !CBB_flush(cbb)) { |
| 91 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 92 | return 0; |
| 93 | } |
| 94 | return 1; |
| 95 | } |
| 96 |
| 97 | int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, |
| 98 | const RSA *rsa) { |
| 99 | CBB cbb; |
| 100 | CBB_zero(&cbb); |
| 101 | if (!CBB_init(&cbb, 0) || |
| 102 | !RSA_marshal_public_key(&cbb, rsa) || |
| 103 | !CBB_finish(&cbb, out_bytes, out_len)) { |
| 104 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 105 | CBB_cleanup(&cbb); |
| 106 | return 0; |
| 107 | } |
| 108 | return 1; |
| 109 | } |
| 110 |
| 111 | // kVersionTwoPrime is the value of the version field for a two-prime |
| 112 | // RSAPrivateKey structure (RFC 3447). |
| 113 | static const uint64_t kVersionTwoPrime = 0; |
| 114 |
| 115 | RSA *RSA_parse_private_key(CBS *cbs) { |
| 116 | RSA *ret = RSA_new(); |
| 117 | if (ret == NULL) { |
| 118 | return NULL; |
| 119 | } |
| 120 |
| 121 | CBS child; |
| 122 | uint64_t version; |
| 123 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 124 | !CBS_get_asn1_uint64(&child, &version)) { |
| 125 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 126 | goto err; |
| 127 | } |
| 128 |
| 129 | if (version != kVersionTwoPrime) { |
| 130 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION); |
| 131 | goto err; |
| 132 | } |
| 133 |
| 134 | if (!parse_integer(&child, &ret->n) || |
| 135 | !parse_integer(&child, &ret->e) || |
| 136 | !parse_integer(&child, &ret->d) || |
| 137 | !parse_integer(&child, &ret->p) || |
| 138 | !parse_integer(&child, &ret->q) || |
| 139 | !parse_integer(&child, &ret->dmp1) || |
| 140 | !parse_integer(&child, &ret->dmq1) || |
| 141 | !parse_integer(&child, &ret->iqmp)) { |
| 142 | goto err; |
| 143 | } |
| 144 |
| 145 | if (CBS_len(&child) != 0) { |
| 146 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 147 | goto err; |
| 148 | } |
| 149 |
| 150 | if (!RSA_check_key(ret)) { |
| 151 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 152 | goto err; |
| 153 | } |
| 154 |
| 155 | return ret; |
| 156 |
| 157 | err: |
| 158 | RSA_free(ret); |
| 159 | return NULL; |
| 160 | } |
| 161 |
| 162 | RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) { |
| 163 | CBS cbs; |
| 164 | CBS_init(&cbs, in, in_len); |
| 165 | RSA *ret = RSA_parse_private_key(&cbs); |
| 166 | if (ret == NULL || CBS_len(&cbs) != 0) { |
| 167 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 168 | RSA_free(ret); |
| 169 | return NULL; |
| 170 | } |
| 171 | return ret; |
| 172 | } |
| 173 |
| 174 | int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) { |
| 175 | CBB child; |
| 176 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 177 | !CBB_add_asn1_uint64(&child, kVersionTwoPrime) || |
| 178 | !marshal_integer(&child, rsa->n) || |
| 179 | !marshal_integer(&child, rsa->e) || |
| 180 | !marshal_integer(&child, rsa->d) || |
| 181 | !marshal_integer(&child, rsa->p) || |
| 182 | !marshal_integer(&child, rsa->q) || |
| 183 | !marshal_integer(&child, rsa->dmp1) || |
| 184 | !marshal_integer(&child, rsa->dmq1) || |
| 185 | !marshal_integer(&child, rsa->iqmp) || |
| 186 | !CBB_flush(cbb)) { |
| 187 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 188 | return 0; |
| 189 | } |
| 190 | return 1; |
| 191 | } |
| 192 |
| 193 | int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len, |
| 194 | const RSA *rsa) { |
| 195 | CBB cbb; |
| 196 | CBB_zero(&cbb); |
| 197 | if (!CBB_init(&cbb, 0) || |
| 198 | !RSA_marshal_private_key(&cbb, rsa) || |
| 199 | !CBB_finish(&cbb, out_bytes, out_len)) { |
| 200 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 201 | CBB_cleanup(&cbb); |
| 202 | return 0; |
| 203 | } |
| 204 | return 1; |
| 205 | } |
| 206 |
| 207 | RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) { |
| 208 | if (len < 0) { |
| 209 | return NULL; |
| 210 | } |
| 211 | CBS cbs; |
| 212 | CBS_init(&cbs, *inp, (size_t)len); |
| 213 | RSA *ret = RSA_parse_public_key(&cbs); |
| 214 | if (ret == NULL) { |
| 215 | return NULL; |
| 216 | } |
| 217 | if (out != NULL) { |
| 218 | RSA_free(*out); |
| 219 | *out = ret; |
| 220 | } |
| 221 | *inp = CBS_data(&cbs); |
| 222 | return ret; |
| 223 | } |
| 224 |
| 225 | int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) { |
| 226 | CBB cbb; |
| 227 | if (!CBB_init(&cbb, 0) || |
| 228 | !RSA_marshal_public_key(&cbb, in)) { |
| 229 | CBB_cleanup(&cbb); |
| 230 | return -1; |
| 231 | } |
| 232 | return CBB_finish_i2d(&cbb, outp); |
| 233 | } |
| 234 |
| 235 | RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) { |
| 236 | if (len < 0) { |
| 237 | return NULL; |
| 238 | } |
| 239 | CBS cbs; |
| 240 | CBS_init(&cbs, *inp, (size_t)len); |
| 241 | RSA *ret = RSA_parse_private_key(&cbs); |
| 242 | if (ret == NULL) { |
| 243 | return NULL; |
| 244 | } |
| 245 | if (out != NULL) { |
| 246 | RSA_free(*out); |
| 247 | *out = ret; |
| 248 | } |
| 249 | *inp = CBS_data(&cbs); |
| 250 | return ret; |
| 251 | } |
| 252 |
| 253 | int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) { |
| 254 | CBB cbb; |
| 255 | if (!CBB_init(&cbb, 0) || |
| 256 | !RSA_marshal_private_key(&cbb, in)) { |
| 257 | CBB_cleanup(&cbb); |
| 258 | return -1; |
| 259 | } |
| 260 | return CBB_finish_i2d(&cbb, outp); |
| 261 | } |
| 262 |
| 263 | RSA *RSAPublicKey_dup(const RSA *rsa) { |
| 264 | uint8_t *der; |
| 265 | size_t der_len; |
| 266 | if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) { |
| 267 | return NULL; |
| 268 | } |
| 269 | RSA *ret = RSA_public_key_from_bytes(der, der_len); |
| 270 | OPENSSL_free(der); |
| 271 | return ret; |
| 272 | } |
| 273 |
| 274 | RSA *RSAPrivateKey_dup(const RSA *rsa) { |
| 275 | uint8_t *der; |
| 276 | size_t der_len; |
| 277 | if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) { |
| 278 | return NULL; |
| 279 | } |
| 280 | RSA *ret = RSA_private_key_from_bytes(der, der_len); |
| 281 | OPENSSL_free(der); |
| 282 | return ret; |
| 283 | } |
| 284 |