| 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/pem.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/dsa.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/evp.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/obj.h> |
| 26 | #include <openssl/rsa.h> |
| 27 | #include <openssl/x509.h> |
| 28 |
| 29 | #include "internal.h" |
| 30 |
| 31 |
| 32 | static X509_PKEY *X509_PKEY_new(void) { |
| 33 | return reinterpret_cast<X509_PKEY *>(OPENSSL_zalloc(sizeof(X509_PKEY))); |
| 34 | } |
| 35 |
| 36 | static void X509_PKEY_free(X509_PKEY *x) { |
| 37 | if (x == NULL) { |
| 38 | return; |
| 39 | } |
| 40 |
| 41 | EVP_PKEY_free(x->dec_pkey); |
| 42 | OPENSSL_free(x); |
| 43 | } |
| 44 |
| 45 | static X509_INFO *X509_INFO_new(void) { |
| 46 | return reinterpret_cast<X509_INFO *>(OPENSSL_zalloc(sizeof(X509_INFO))); |
| 47 | } |
| 48 |
| 49 | void X509_INFO_free(X509_INFO *x) { |
| 50 | if (x == NULL) { |
| 51 | return; |
| 52 | } |
| 53 |
| 54 | X509_free(x->x509); |
| 55 | X509_CRL_free(x->crl); |
| 56 | X509_PKEY_free(x->x_pkey); |
| 57 | OPENSSL_free(x->enc_data); |
| 58 | OPENSSL_free(x); |
| 59 | } |
| 60 |
| 61 |
| 62 | STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, |
| 63 | pem_password_cb *cb, void *u) { |
| 64 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
| 65 | if (b == NULL) { |
| 66 | OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); |
| 67 | return 0; |
| 68 | } |
| 69 | STACK_OF(X509_INFO) *ret = PEM_X509_INFO_read_bio(b, sk, cb, u); |
| 70 | BIO_free(b); |
| 71 | return ret; |
| 72 | } |
| 73 |
| 74 | enum parse_result_t { |
| 75 | parse_ok, |
| 76 | parse_error, |
| 77 | parse_new_entry, |
| 78 | }; |
| 79 |
| 80 | static enum parse_result_t parse_x509(X509_INFO *info, const uint8_t *data, |
| 81 | size_t len, int key_type) { |
| 82 | if (info->x509 != NULL) { |
| 83 | return parse_new_entry; |
| 84 | } |
| 85 | info->x509 = d2i_X509(NULL, &data, len); |
| 86 | return info->x509 != NULL ? parse_ok : parse_error; |
| 87 | } |
| 88 |
| 89 | static enum parse_result_t parse_x509_aux(X509_INFO *info, const uint8_t *data, |
| 90 | size_t len, int key_type) { |
| 91 | if (info->x509 != NULL) { |
| 92 | return parse_new_entry; |
| 93 | } |
| 94 | info->x509 = d2i_X509_AUX(NULL, &data, len); |
| 95 | return info->x509 != NULL ? parse_ok : parse_error; |
| 96 | } |
| 97 |
| 98 | static enum parse_result_t parse_crl(X509_INFO *info, const uint8_t *data, |
| 99 | size_t len, int key_type) { |
| 100 | if (info->crl != NULL) { |
| 101 | return parse_new_entry; |
| 102 | } |
| 103 | info->crl = d2i_X509_CRL(NULL, &data, len); |
| 104 | return info->crl != NULL ? parse_ok : parse_error; |
| 105 | } |
| 106 |
| 107 | static enum parse_result_t parse_key(X509_INFO *info, const uint8_t *data, |
| 108 | size_t len, int key_type) { |
| 109 | if (info->x_pkey != NULL) { |
| 110 | return parse_new_entry; |
| 111 | } |
| 112 | info->x_pkey = X509_PKEY_new(); |
| 113 | if (info->x_pkey == NULL) { |
| 114 | return parse_error; |
| 115 | } |
| 116 | info->x_pkey->dec_pkey = d2i_PrivateKey(key_type, NULL, &data, len); |
| 117 | return info->x_pkey->dec_pkey != NULL ? parse_ok : parse_error; |
| 118 | } |
| 119 |
| 120 | STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, |
| 121 | pem_password_cb *cb, void *u) { |
| 122 | X509_INFO *info = NULL; |
| 123 | char *name = NULL, *header = NULL; |
| 124 | unsigned char *data = NULL; |
| 125 | long len; |
| 126 | int ok = 0; |
| 127 | STACK_OF(X509_INFO) *ret = NULL; |
| 128 |
| 129 | if (sk == NULL) { |
| 130 | ret = sk_X509_INFO_new_null(); |
| 131 | if (ret == NULL) { |
| 132 | return NULL; |
| 133 | } |
| 134 | } else { |
| 135 | ret = sk; |
| 136 | } |
| 137 | size_t orig_num = sk_X509_INFO_num(ret); |
| 138 |
| 139 | info = X509_INFO_new(); |
| 140 | if (info == NULL) { |
| 141 | goto err; |
| 142 | } |
| 143 |
| 144 | for (;;) { |
| 145 | if (!PEM_read_bio(bp, &name, &header, &data, &len)) { |
| 146 | uint32_t error = ERR_peek_last_error(); |
| 147 | if (ERR_GET_LIB(error) == ERR_LIB_PEM && |
| 148 | ERR_GET_REASON(error) == PEM_R_NO_START_LINE) { |
| 149 | ERR_clear_error(); |
| 150 | break; |
| 151 | } |
| 152 | goto err; |
| 153 | } |
| 154 |
| 155 | enum parse_result_t (*parse_function)(X509_INFO *, const uint8_t *, size_t, |
| 156 | int) = NULL; |
| 157 | int key_type = EVP_PKEY_NONE; |
| 158 | if (strcmp(name, PEM_STRING_X509) == 0 || |
| 159 | strcmp(name, PEM_STRING_X509_OLD) == 0) { |
| 160 | parse_function = parse_x509; |
| 161 | } else if (strcmp(name, PEM_STRING_X509_TRUSTED) == 0) { |
| 162 | parse_function = parse_x509_aux; |
| 163 | } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) { |
| 164 | parse_function = parse_crl; |
| 165 | } else if (strcmp(name, PEM_STRING_RSA) == 0) { |
| 166 | parse_function = parse_key; |
| 167 | key_type = EVP_PKEY_RSA; |
| 168 | } else if (strcmp(name, PEM_STRING_DSA) == 0) { |
| 169 | parse_function = parse_key; |
| 170 | key_type = EVP_PKEY_DSA; |
| 171 | } else if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) { |
| 172 | parse_function = parse_key; |
| 173 | key_type = EVP_PKEY_EC; |
| 174 | } |
| 175 |
| 176 | // If a private key has a header, assume it is encrypted. This function does |
| 177 | // not decrypt private keys. |
| 178 | if (key_type != EVP_PKEY_NONE && strlen(header) > 10) { |
| 179 | if (info->x_pkey != NULL) { |
| 180 | if (!sk_X509_INFO_push(ret, info)) { |
| 181 | goto err; |
| 182 | } |
| 183 | info = X509_INFO_new(); |
| 184 | if (info == NULL) { |
| 185 | goto err; |
| 186 | } |
| 187 | } |
| 188 | // Use an empty key as a placeholder. |
| 189 | info->x_pkey = X509_PKEY_new(); |
| 190 | if (info->x_pkey == NULL || |
| 191 | !PEM_get_EVP_CIPHER_INFO(header, &info->enc_cipher)) { |
| 192 | goto err; |
| 193 | } |
| 194 | info->enc_data = (char *)data; |
| 195 | info->enc_len = (int)len; |
| 196 | data = NULL; |
| 197 | } else if (parse_function != NULL) { |
| 198 | EVP_CIPHER_INFO cipher; |
| 199 | if (!PEM_get_EVP_CIPHER_INFO(header, &cipher) || |
| 200 | !PEM_do_header(&cipher, data, &len, cb, u)) { |
| 201 | goto err; |
| 202 | } |
| 203 | enum parse_result_t result = parse_function(info, data, len, key_type); |
| 204 | if (result == parse_new_entry) { |
| 205 | if (!sk_X509_INFO_push(ret, info)) { |
| 206 | goto err; |
| 207 | } |
| 208 | info = X509_INFO_new(); |
| 209 | if (info == NULL) { |
| 210 | goto err; |
| 211 | } |
| 212 | result = parse_function(info, data, len, key_type); |
| 213 | } |
| 214 | if (result != parse_ok) { |
| 215 | OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB); |
| 216 | goto err; |
| 217 | } |
| 218 | } |
| 219 | OPENSSL_free(name); |
| 220 | OPENSSL_free(header); |
| 221 | OPENSSL_free(data); |
| 222 | name = NULL; |
| 223 | header = NULL; |
| 224 | data = NULL; |
| 225 | } |
| 226 |
| 227 | // Push the last entry on the stack if not empty. |
| 228 | if (info->x509 != NULL || info->crl != NULL || info->x_pkey != NULL || |
| 229 | info->enc_data != NULL) { |
| 230 | if (!sk_X509_INFO_push(ret, info)) { |
| 231 | goto err; |
| 232 | } |
| 233 | info = NULL; |
| 234 | } |
| 235 |
| 236 | ok = 1; |
| 237 |
| 238 | err: |
| 239 | X509_INFO_free(info); |
| 240 | if (!ok) { |
| 241 | while (sk_X509_INFO_num(ret) > orig_num) { |
| 242 | X509_INFO_free(sk_X509_INFO_pop(ret)); |
| 243 | } |
| 244 | if (ret != sk) { |
| 245 | sk_X509_INFO_free(ret); |
| 246 | } |
| 247 | ret = NULL; |
| 248 | } |
| 249 |
| 250 | OPENSSL_free(name); |
| 251 | OPENSSL_free(header); |
| 252 | OPENSSL_free(data); |
| 253 | return ret; |
| 254 | } |
| 255 |