| 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/pkcs8.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/bytestring.h> |
| 21 | #include <openssl/cipher.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/nid.h> |
| 25 | #include <openssl/rand.h> |
| 26 |
| 27 | #include "internal.h" |
| 28 | #include "../internal.h" |
| 29 |
| 30 |
| 31 | // 1.2.840.113549.1.5.12 |
| 32 | static const uint8_t kPBKDF2[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 33 | 0x0d, 0x01, 0x05, 0x0c}; |
| 34 |
| 35 | // 1.2.840.113549.1.5.13 |
| 36 | static const uint8_t kPBES2[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 37 | 0x0d, 0x01, 0x05, 0x0d}; |
| 38 |
| 39 | // 1.2.840.113549.2.7 |
| 40 | static const uint8_t kHMACWithSHA1[] = {0x2a, 0x86, 0x48, 0x86, |
| 41 | 0xf7, 0x0d, 0x02, 0x07}; |
| 42 |
| 43 | // 1.2.840.113549.2.9 |
| 44 | static const uint8_t kHMACWithSHA256[] = {0x2a, 0x86, 0x48, 0x86, |
| 45 | 0xf7, 0x0d, 0x02, 0x09}; |
| 46 |
| 47 | static const struct { |
| 48 | uint8_t oid[9]; |
| 49 | uint8_t oid_len; |
| 50 | int nid; |
| 51 | const EVP_CIPHER *(*cipher_func)(void); |
| 52 | } kCipherOIDs[] = { |
| 53 | // 1.2.840.113549.3.2 |
| 54 | {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x02}, |
| 55 | 8, |
| 56 | NID_rc2_cbc, |
| 57 | &EVP_rc2_cbc}, |
| 58 | // 1.2.840.113549.3.7 |
| 59 | {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x03, 0x07}, |
| 60 | 8, |
| 61 | NID_des_ede3_cbc, |
| 62 | &EVP_des_ede3_cbc}, |
| 63 | // 2.16.840.1.101.3.4.1.2 |
| 64 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x02}, |
| 65 | 9, |
| 66 | NID_aes_128_cbc, |
| 67 | &EVP_aes_128_cbc}, |
| 68 | // 2.16.840.1.101.3.4.1.22 |
| 69 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x16}, |
| 70 | 9, |
| 71 | NID_aes_192_cbc, |
| 72 | &EVP_aes_192_cbc}, |
| 73 | // 2.16.840.1.101.3.4.1.42 |
| 74 | {{0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a}, |
| 75 | 9, |
| 76 | NID_aes_256_cbc, |
| 77 | &EVP_aes_256_cbc}, |
| 78 | }; |
| 79 |
| 80 | static const EVP_CIPHER *cbs_to_cipher(const CBS *cbs) { |
| 81 | for (const auto &cipher : kCipherOIDs) { |
| 82 | if (CBS_mem_equal(cbs, cipher.oid, cipher.oid_len)) { |
| 83 | return cipher.cipher_func(); |
| 84 | } |
| 85 | } |
| 86 |
| 87 | return nullptr; |
| 88 | } |
| 89 |
| 90 | static int add_cipher_oid(CBB *out, int nid) { |
| 91 | for (const auto &cipher : kCipherOIDs) { |
| 92 | if (cipher.nid == nid) { |
| 93 | CBB child; |
| 94 | return CBB_add_asn1(out, &child, CBS_ASN1_OBJECT) && |
| 95 | CBB_add_bytes(&child, cipher.oid, cipher.oid_len) && |
| 96 | CBB_flush(out); |
| 97 | } |
| 98 | } |
| 99 |
| 100 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER); |
| 101 | return 0; |
| 102 | } |
| 103 |
| 104 | const EVP_CIPHER *pkcs5_pbe2_nid_to_cipher(int nid) { |
| 105 | for (const auto &cipher : kCipherOIDs) { |
| 106 | if (cipher.nid == nid) { |
| 107 | return cipher.cipher_func(); |
| 108 | } |
| 109 | } |
| 110 | return nullptr; |
| 111 | } |
| 112 |
| 113 | static int pkcs5_pbe2_cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 114 | const EVP_MD *pbkdf2_md, uint32_t iterations, |
| 115 | const char *pass, size_t pass_len, |
| 116 | const uint8_t *salt, size_t salt_len, |
| 117 | const uint8_t *iv, size_t iv_len, int enc) { |
| 118 | if (iv_len != EVP_CIPHER_iv_length(cipher)) { |
| 119 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS); |
| 120 | return 0; |
| 121 | } |
| 122 |
| 123 | uint8_t key[EVP_MAX_KEY_LENGTH]; |
| 124 | int ret = PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iterations, |
| 125 | pbkdf2_md, EVP_CIPHER_key_length(cipher), key) && |
| 126 | EVP_CipherInit_ex(ctx, cipher, NULL /* engine */, key, iv, enc); |
| 127 | OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); |
| 128 | return ret; |
| 129 | } |
| 130 |
| 131 | int PKCS5_pbe2_encrypt_init(CBB *out, EVP_CIPHER_CTX *ctx, |
| 132 | const EVP_CIPHER *cipher, uint32_t iterations, |
| 133 | const char *pass, size_t pass_len, |
| 134 | const uint8_t *salt, size_t salt_len) { |
| 135 | int cipher_nid = EVP_CIPHER_nid(cipher); |
| 136 | if (cipher_nid == NID_undef) { |
| 137 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); |
| 138 | return 0; |
| 139 | } |
| 140 |
| 141 | // Generate a random IV. |
| 142 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 143 | if (!RAND_bytes(iv, EVP_CIPHER_iv_length(cipher))) { |
| 144 | return 0; |
| 145 | } |
| 146 |
| 147 | // See RFC 2898, appendix A. |
| 148 | CBB algorithm, oid, param, kdf, kdf_oid, kdf_param, salt_cbb, cipher_cbb, |
| 149 | iv_cbb; |
| 150 | if (!CBB_add_asn1(out, &algorithm, CBS_ASN1_SEQUENCE) || |
| 151 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
| 152 | !CBB_add_bytes(&oid, kPBES2, sizeof(kPBES2)) || |
| 153 | !CBB_add_asn1(&algorithm, ¶m, CBS_ASN1_SEQUENCE) || |
| 154 | !CBB_add_asn1(¶m, &kdf, CBS_ASN1_SEQUENCE) || |
| 155 | !CBB_add_asn1(&kdf, &kdf_oid, CBS_ASN1_OBJECT) || |
| 156 | !CBB_add_bytes(&kdf_oid, kPBKDF2, sizeof(kPBKDF2)) || |
| 157 | !CBB_add_asn1(&kdf, &kdf_param, CBS_ASN1_SEQUENCE) || |
| 158 | !CBB_add_asn1(&kdf_param, &salt_cbb, CBS_ASN1_OCTETSTRING) || |
| 159 | !CBB_add_bytes(&salt_cbb, salt, salt_len) || |
| 160 | !CBB_add_asn1_uint64(&kdf_param, iterations) || |
| 161 | // Specify a key length for RC2. |
| 162 | (cipher_nid == NID_rc2_cbc && |
| 163 | !CBB_add_asn1_uint64(&kdf_param, EVP_CIPHER_key_length(cipher))) || |
| 164 | // Omit the PRF. We use the default hmacWithSHA1. |
| 165 | // TODO(crbug.com/396434682): Improve this defaults. |
| 166 | !CBB_add_asn1(¶m, &cipher_cbb, CBS_ASN1_SEQUENCE) || |
| 167 | !add_cipher_oid(&cipher_cbb, cipher_nid) || |
| 168 | // RFC 2898 says RC2-CBC and RC5-CBC-Pad use a SEQUENCE with version and |
| 169 | // IV, but OpenSSL always uses an OCTET STRING IV, so we do the same. |
| 170 | !CBB_add_asn1(&cipher_cbb, &iv_cbb, CBS_ASN1_OCTETSTRING) || |
| 171 | !CBB_add_bytes(&iv_cbb, iv, EVP_CIPHER_iv_length(cipher)) || |
| 172 | !CBB_flush(out)) { |
| 173 | return 0; |
| 174 | } |
| 175 |
| 176 | return pkcs5_pbe2_cipher_init(ctx, cipher, EVP_sha1(), iterations, pass, |
| 177 | pass_len, salt, salt_len, iv, |
| 178 | EVP_CIPHER_iv_length(cipher), 1 /* encrypt */); |
| 179 | } |
| 180 |
| 181 | int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx, |
| 182 | const char *pass, size_t pass_len, CBS *param) { |
| 183 | CBS pbe_param, kdf, kdf_obj, enc_scheme, enc_obj; |
| 184 | if (!CBS_get_asn1(param, &pbe_param, CBS_ASN1_SEQUENCE) || |
| 185 | CBS_len(param) != 0 || |
| 186 | !CBS_get_asn1(&pbe_param, &kdf, CBS_ASN1_SEQUENCE) || |
| 187 | !CBS_get_asn1(&pbe_param, &enc_scheme, CBS_ASN1_SEQUENCE) || |
| 188 | CBS_len(&pbe_param) != 0 || |
| 189 | !CBS_get_asn1(&kdf, &kdf_obj, CBS_ASN1_OBJECT) || |
| 190 | !CBS_get_asn1(&enc_scheme, &enc_obj, CBS_ASN1_OBJECT)) { |
| 191 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 192 | return 0; |
| 193 | } |
| 194 |
| 195 | // Only PBKDF2 is supported. |
| 196 | if (!CBS_mem_equal(&kdf_obj, kPBKDF2, sizeof(kPBKDF2))) { |
| 197 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION); |
| 198 | return 0; |
| 199 | } |
| 200 |
| 201 | // See if we recognise the encryption algorithm. |
| 202 | const EVP_CIPHER *cipher = cbs_to_cipher(&enc_obj); |
| 203 | if (cipher == NULL) { |
| 204 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_CIPHER); |
| 205 | return 0; |
| 206 | } |
| 207 |
| 208 | // Parse the KDF parameters. See RFC 8018, appendix A.2. |
| 209 | CBS pbkdf2_params, salt; |
| 210 | uint64_t iterations; |
| 211 | if (!CBS_get_asn1(&kdf, &pbkdf2_params, CBS_ASN1_SEQUENCE) || |
| 212 | CBS_len(&kdf) != 0 || |
| 213 | !CBS_get_asn1(&pbkdf2_params, &salt, CBS_ASN1_OCTETSTRING) || |
| 214 | !CBS_get_asn1_uint64(&pbkdf2_params, &iterations)) { |
| 215 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 216 | return 0; |
| 217 | } |
| 218 |
| 219 | if (!pkcs12_iterations_acceptable(iterations)) { |
| 220 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_ITERATION_COUNT); |
| 221 | return 0; |
| 222 | } |
| 223 |
| 224 | // The optional keyLength parameter, if present, must match the key length of |
| 225 | // the cipher. |
| 226 | if (CBS_peek_asn1_tag(&pbkdf2_params, CBS_ASN1_INTEGER)) { |
| 227 | uint64_t key_len; |
| 228 | if (!CBS_get_asn1_uint64(&pbkdf2_params, &key_len)) { |
| 229 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 230 | return 0; |
| 231 | } |
| 232 |
| 233 | if (key_len != EVP_CIPHER_key_length(cipher)) { |
| 234 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_KEYLENGTH); |
| 235 | return 0; |
| 236 | } |
| 237 | } |
| 238 |
| 239 | const EVP_MD *md = EVP_sha1(); |
| 240 | if (CBS_len(&pbkdf2_params) != 0) { |
| 241 | CBS alg_id, prf; |
| 242 | if (!CBS_get_asn1(&pbkdf2_params, &alg_id, CBS_ASN1_SEQUENCE) || |
| 243 | !CBS_get_asn1(&alg_id, &prf, CBS_ASN1_OBJECT) || |
| 244 | CBS_len(&pbkdf2_params) != 0) { |
| 245 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 246 | return 0; |
| 247 | } |
| 248 |
| 249 | if (CBS_mem_equal(&prf, kHMACWithSHA1, sizeof(kHMACWithSHA1))) { |
| 250 | // hmacWithSHA1 is the DEFAULT, so DER requires it be omitted, but we |
| 251 | // match OpenSSL in tolerating it being present. |
| 252 | md = EVP_sha1(); |
| 253 | } else if (CBS_mem_equal(&prf, kHMACWithSHA256, sizeof(kHMACWithSHA256))) { |
| 254 | md = EVP_sha256(); |
| 255 | } else { |
| 256 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF); |
| 257 | return 0; |
| 258 | } |
| 259 |
| 260 | // All supported PRFs use a NULL parameter. |
| 261 | CBS null; |
| 262 | if (!CBS_get_asn1(&alg_id, &null, CBS_ASN1_NULL) || |
| 263 | CBS_len(&null) != 0 || |
| 264 | CBS_len(&alg_id) != 0) { |
| 265 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR); |
| 266 | return 0; |
| 267 | } |
| 268 | } |
| 269 |
| 270 | // Parse the encryption scheme parameters. Note OpenSSL does not match the |
| 271 | // specification. Per RFC 2898, this should depend on the encryption scheme. |
| 272 | // In particular, RC2-CBC uses a SEQUENCE with version and IV. We align with |
| 273 | // OpenSSL. |
| 274 | CBS iv; |
| 275 | if (!CBS_get_asn1(&enc_scheme, &iv, CBS_ASN1_OCTETSTRING) || |
| 276 | CBS_len(&enc_scheme) != 0) { |
| 277 | OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNSUPPORTED_PRF); |
| 278 | return 0; |
| 279 | } |
| 280 |
| 281 | return pkcs5_pbe2_cipher_init(ctx, cipher, md, (uint32_t)iterations, pass, |
| 282 | pass_len, CBS_data(&salt), CBS_len(&salt), |
| 283 | CBS_data(&iv), CBS_len(&iv), 0 /* decrypt */); |
| 284 | } |
| 285 |