1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* We need to use some engine deprecated APIs */
11e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include <stdio.h>
14e1051a39Sopenharmony_ci#include "crypto/ctype.h"
15e1051a39Sopenharmony_ci#include <string.h>
16e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
17e1051a39Sopenharmony_ci#include <openssl/buffer.h>
18e1051a39Sopenharmony_ci#include <openssl/objects.h>
19e1051a39Sopenharmony_ci#include <openssl/evp.h>
20e1051a39Sopenharmony_ci#include <openssl/rand.h>
21e1051a39Sopenharmony_ci#include <openssl/x509.h>
22e1051a39Sopenharmony_ci#include <openssl/pem.h>
23e1051a39Sopenharmony_ci#include <openssl/pkcs12.h>
24e1051a39Sopenharmony_ci#include "crypto/asn1.h"
25e1051a39Sopenharmony_ci#include <openssl/des.h>
26e1051a39Sopenharmony_ci#include <openssl/engine.h>
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci#define MIN_LENGTH      4
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_cistatic int load_iv(char **fromp, unsigned char *to, int num);
31e1051a39Sopenharmony_cistatic int check_pem(const char *nm, const char *name);
32e1051a39Sopenharmony_ciint ossl_pem_check_suffix(const char *pem_str, const char *suffix);
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ciint PEM_def_callback(char *buf, int num, int rwflag, void *userdata)
35e1051a39Sopenharmony_ci{
36e1051a39Sopenharmony_ci    int i, min_len;
37e1051a39Sopenharmony_ci    const char *prompt;
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci    /* We assume that the user passes a default password as userdata */
40e1051a39Sopenharmony_ci    if (userdata) {
41e1051a39Sopenharmony_ci        i = strlen(userdata);
42e1051a39Sopenharmony_ci        i = (i > num) ? num : i;
43e1051a39Sopenharmony_ci        memcpy(buf, userdata, i);
44e1051a39Sopenharmony_ci        return i;
45e1051a39Sopenharmony_ci    }
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci    prompt = EVP_get_pw_prompt();
48e1051a39Sopenharmony_ci    if (prompt == NULL)
49e1051a39Sopenharmony_ci        prompt = "Enter PEM pass phrase:";
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci    /*
52e1051a39Sopenharmony_ci     * rwflag == 0 means decryption
53e1051a39Sopenharmony_ci     * rwflag == 1 means encryption
54e1051a39Sopenharmony_ci     *
55e1051a39Sopenharmony_ci     * We assume that for encryption, we want a minimum length, while for
56e1051a39Sopenharmony_ci     * decryption, we cannot know any minimum length, so we assume zero.
57e1051a39Sopenharmony_ci     */
58e1051a39Sopenharmony_ci    min_len = rwflag ? MIN_LENGTH : 0;
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    i = EVP_read_pw_string_min(buf, min_len, num, prompt, rwflag);
61e1051a39Sopenharmony_ci    if (i != 0) {
62e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_PROBLEMS_GETTING_PASSWORD);
63e1051a39Sopenharmony_ci        memset(buf, 0, (unsigned int)num);
64e1051a39Sopenharmony_ci        return -1;
65e1051a39Sopenharmony_ci    }
66e1051a39Sopenharmony_ci    return strlen(buf);
67e1051a39Sopenharmony_ci}
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_civoid PEM_proc_type(char *buf, int type)
70e1051a39Sopenharmony_ci{
71e1051a39Sopenharmony_ci    const char *str;
72e1051a39Sopenharmony_ci    char *p = buf + strlen(buf);
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    if (type == PEM_TYPE_ENCRYPTED)
75e1051a39Sopenharmony_ci        str = "ENCRYPTED";
76e1051a39Sopenharmony_ci    else if (type == PEM_TYPE_MIC_CLEAR)
77e1051a39Sopenharmony_ci        str = "MIC-CLEAR";
78e1051a39Sopenharmony_ci    else if (type == PEM_TYPE_MIC_ONLY)
79e1051a39Sopenharmony_ci        str = "MIC-ONLY";
80e1051a39Sopenharmony_ci    else
81e1051a39Sopenharmony_ci        str = "BAD-TYPE";
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci    BIO_snprintf(p, PEM_BUFSIZE - (size_t)(p - buf), "Proc-Type: 4,%s\n", str);
84e1051a39Sopenharmony_ci}
85e1051a39Sopenharmony_ci
86e1051a39Sopenharmony_civoid PEM_dek_info(char *buf, const char *type, int len, const char *str)
87e1051a39Sopenharmony_ci{
88e1051a39Sopenharmony_ci    long i;
89e1051a39Sopenharmony_ci    char *p = buf + strlen(buf);
90e1051a39Sopenharmony_ci    int j = PEM_BUFSIZE - (size_t)(p - buf), n;
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    n = BIO_snprintf(p, j, "DEK-Info: %s,", type);
93e1051a39Sopenharmony_ci    if (n > 0) {
94e1051a39Sopenharmony_ci        j -= n;
95e1051a39Sopenharmony_ci        p += n;
96e1051a39Sopenharmony_ci        for (i = 0; i < len; i++) {
97e1051a39Sopenharmony_ci            n = BIO_snprintf(p, j, "%02X", 0xff & str[i]);
98e1051a39Sopenharmony_ci            if (n <= 0)
99e1051a39Sopenharmony_ci                return;
100e1051a39Sopenharmony_ci            j -= n;
101e1051a39Sopenharmony_ci            p += n;
102e1051a39Sopenharmony_ci        }
103e1051a39Sopenharmony_ci        if (j > 1)
104e1051a39Sopenharmony_ci            strcpy(p, "\n");
105e1051a39Sopenharmony_ci    }
106e1051a39Sopenharmony_ci}
107e1051a39Sopenharmony_ci
108e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_STDIO
109e1051a39Sopenharmony_civoid *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
110e1051a39Sopenharmony_ci                    pem_password_cb *cb, void *u)
111e1051a39Sopenharmony_ci{
112e1051a39Sopenharmony_ci    BIO *b;
113e1051a39Sopenharmony_ci    void *ret;
114e1051a39Sopenharmony_ci
115e1051a39Sopenharmony_ci    if ((b = BIO_new(BIO_s_file())) == NULL) {
116e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
117e1051a39Sopenharmony_ci        return 0;
118e1051a39Sopenharmony_ci    }
119e1051a39Sopenharmony_ci    BIO_set_fp(b, fp, BIO_NOCLOSE);
120e1051a39Sopenharmony_ci    ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u);
121e1051a39Sopenharmony_ci    BIO_free(b);
122e1051a39Sopenharmony_ci    return ret;
123e1051a39Sopenharmony_ci}
124e1051a39Sopenharmony_ci#endif
125e1051a39Sopenharmony_ci
126e1051a39Sopenharmony_cistatic int check_pem(const char *nm, const char *name)
127e1051a39Sopenharmony_ci{
128e1051a39Sopenharmony_ci    /* Normal matching nm and name */
129e1051a39Sopenharmony_ci    if (strcmp(nm, name) == 0)
130e1051a39Sopenharmony_ci        return 1;
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ci    /* Make PEM_STRING_EVP_PKEY match any private key */
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_ci    if (strcmp(name, PEM_STRING_EVP_PKEY) == 0) {
135e1051a39Sopenharmony_ci        int slen;
136e1051a39Sopenharmony_ci        const EVP_PKEY_ASN1_METHOD *ameth;
137e1051a39Sopenharmony_ci        if (strcmp(nm, PEM_STRING_PKCS8) == 0)
138e1051a39Sopenharmony_ci            return 1;
139e1051a39Sopenharmony_ci        if (strcmp(nm, PEM_STRING_PKCS8INF) == 0)
140e1051a39Sopenharmony_ci            return 1;
141e1051a39Sopenharmony_ci        slen = ossl_pem_check_suffix(nm, "PRIVATE KEY");
142e1051a39Sopenharmony_ci        if (slen > 0) {
143e1051a39Sopenharmony_ci            /*
144e1051a39Sopenharmony_ci             * NB: ENGINE implementations won't contain a deprecated old
145e1051a39Sopenharmony_ci             * private key decode function so don't look for them.
146e1051a39Sopenharmony_ci             */
147e1051a39Sopenharmony_ci            ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
148e1051a39Sopenharmony_ci            if (ameth && ameth->old_priv_decode)
149e1051a39Sopenharmony_ci                return 1;
150e1051a39Sopenharmony_ci        }
151e1051a39Sopenharmony_ci        return 0;
152e1051a39Sopenharmony_ci    }
153e1051a39Sopenharmony_ci
154e1051a39Sopenharmony_ci    if (strcmp(name, PEM_STRING_PARAMETERS) == 0) {
155e1051a39Sopenharmony_ci        int slen;
156e1051a39Sopenharmony_ci        const EVP_PKEY_ASN1_METHOD *ameth;
157e1051a39Sopenharmony_ci        slen = ossl_pem_check_suffix(nm, "PARAMETERS");
158e1051a39Sopenharmony_ci        if (slen > 0) {
159e1051a39Sopenharmony_ci            ENGINE *e;
160e1051a39Sopenharmony_ci            ameth = EVP_PKEY_asn1_find_str(&e, nm, slen);
161e1051a39Sopenharmony_ci            if (ameth) {
162e1051a39Sopenharmony_ci                int r;
163e1051a39Sopenharmony_ci                if (ameth->param_decode)
164e1051a39Sopenharmony_ci                    r = 1;
165e1051a39Sopenharmony_ci                else
166e1051a39Sopenharmony_ci                    r = 0;
167e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
168e1051a39Sopenharmony_ci                ENGINE_finish(e);
169e1051a39Sopenharmony_ci#endif
170e1051a39Sopenharmony_ci                return r;
171e1051a39Sopenharmony_ci            }
172e1051a39Sopenharmony_ci        }
173e1051a39Sopenharmony_ci        return 0;
174e1051a39Sopenharmony_ci    }
175e1051a39Sopenharmony_ci    /* If reading DH parameters handle X9.42 DH format too */
176e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0
177e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_DHPARAMS) == 0)
178e1051a39Sopenharmony_ci        return 1;
179e1051a39Sopenharmony_ci
180e1051a39Sopenharmony_ci    /* Permit older strings */
181e1051a39Sopenharmony_ci
182e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_X509_OLD) == 0
183e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_X509) == 0)
184e1051a39Sopenharmony_ci        return 1;
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_X509_REQ_OLD) == 0
187e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_X509_REQ) == 0)
188e1051a39Sopenharmony_ci        return 1;
189e1051a39Sopenharmony_ci
190e1051a39Sopenharmony_ci    /* Allow normal certs to be read as trusted certs */
191e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_X509) == 0
192e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_X509_TRUSTED) == 0)
193e1051a39Sopenharmony_ci        return 1;
194e1051a39Sopenharmony_ci
195e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_X509_OLD) == 0
196e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_X509_TRUSTED) == 0)
197e1051a39Sopenharmony_ci        return 1;
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_ci    /* Some CAs use PKCS#7 with CERTIFICATE headers */
200e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_X509) == 0
201e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_PKCS7) == 0)
202e1051a39Sopenharmony_ci        return 1;
203e1051a39Sopenharmony_ci
204e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_PKCS7_SIGNED) == 0
205e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_PKCS7) == 0)
206e1051a39Sopenharmony_ci        return 1;
207e1051a39Sopenharmony_ci
208e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_CMS
209e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_X509) == 0
210e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_CMS) == 0)
211e1051a39Sopenharmony_ci        return 1;
212e1051a39Sopenharmony_ci    /* Allow CMS to be read from PKCS#7 headers */
213e1051a39Sopenharmony_ci    if (strcmp(nm, PEM_STRING_PKCS7) == 0
214e1051a39Sopenharmony_ci        && strcmp(name, PEM_STRING_CMS) == 0)
215e1051a39Sopenharmony_ci        return 1;
216e1051a39Sopenharmony_ci#endif
217e1051a39Sopenharmony_ci
218e1051a39Sopenharmony_ci    return 0;
219e1051a39Sopenharmony_ci}
220e1051a39Sopenharmony_ci
221e1051a39Sopenharmony_cistatic void pem_free(void *p, unsigned int flags, size_t num)
222e1051a39Sopenharmony_ci{
223e1051a39Sopenharmony_ci    if (flags & PEM_FLAG_SECURE)
224e1051a39Sopenharmony_ci        OPENSSL_secure_clear_free(p, num);
225e1051a39Sopenharmony_ci    else
226e1051a39Sopenharmony_ci        OPENSSL_free(p);
227e1051a39Sopenharmony_ci}
228e1051a39Sopenharmony_ci
229e1051a39Sopenharmony_cistatic void *pem_malloc(int num, unsigned int flags)
230e1051a39Sopenharmony_ci{
231e1051a39Sopenharmony_ci    return (flags & PEM_FLAG_SECURE) ? OPENSSL_secure_malloc(num)
232e1051a39Sopenharmony_ci                                     : OPENSSL_malloc(num);
233e1051a39Sopenharmony_ci}
234e1051a39Sopenharmony_ci
235e1051a39Sopenharmony_cistatic int pem_bytes_read_bio_flags(unsigned char **pdata, long *plen,
236e1051a39Sopenharmony_ci                                    char **pnm, const char *name, BIO *bp,
237e1051a39Sopenharmony_ci                                    pem_password_cb *cb, void *u,
238e1051a39Sopenharmony_ci                                    unsigned int flags)
239e1051a39Sopenharmony_ci{
240e1051a39Sopenharmony_ci    EVP_CIPHER_INFO cipher;
241e1051a39Sopenharmony_ci    char *nm = NULL, *header = NULL;
242e1051a39Sopenharmony_ci    unsigned char *data = NULL;
243e1051a39Sopenharmony_ci    long len = 0;
244e1051a39Sopenharmony_ci    int ret = 0;
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ci    do {
247e1051a39Sopenharmony_ci        pem_free(nm, flags, 0);
248e1051a39Sopenharmony_ci        pem_free(header, flags, 0);
249e1051a39Sopenharmony_ci        pem_free(data, flags, len);
250e1051a39Sopenharmony_ci        if (!PEM_read_bio_ex(bp, &nm, &header, &data, &len, flags)) {
251e1051a39Sopenharmony_ci            if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
252e1051a39Sopenharmony_ci                ERR_add_error_data(2, "Expecting: ", name);
253e1051a39Sopenharmony_ci            return 0;
254e1051a39Sopenharmony_ci        }
255e1051a39Sopenharmony_ci    } while (!check_pem(nm, name));
256e1051a39Sopenharmony_ci    if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
257e1051a39Sopenharmony_ci        goto err;
258e1051a39Sopenharmony_ci    if (!PEM_do_header(&cipher, data, &len, cb, u))
259e1051a39Sopenharmony_ci        goto err;
260e1051a39Sopenharmony_ci
261e1051a39Sopenharmony_ci    *pdata = data;
262e1051a39Sopenharmony_ci    *plen = len;
263e1051a39Sopenharmony_ci
264e1051a39Sopenharmony_ci    if (pnm != NULL)
265e1051a39Sopenharmony_ci        *pnm = nm;
266e1051a39Sopenharmony_ci
267e1051a39Sopenharmony_ci    ret = 1;
268e1051a39Sopenharmony_ci
269e1051a39Sopenharmony_ci err:
270e1051a39Sopenharmony_ci    if (!ret || pnm == NULL)
271e1051a39Sopenharmony_ci        pem_free(nm, flags, 0);
272e1051a39Sopenharmony_ci    pem_free(header, flags, 0);
273e1051a39Sopenharmony_ci    if (!ret)
274e1051a39Sopenharmony_ci        pem_free(data, flags, len);
275e1051a39Sopenharmony_ci    return ret;
276e1051a39Sopenharmony_ci}
277e1051a39Sopenharmony_ci
278e1051a39Sopenharmony_ciint PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
279e1051a39Sopenharmony_ci                       const char *name, BIO *bp, pem_password_cb *cb,
280e1051a39Sopenharmony_ci                       void *u) {
281e1051a39Sopenharmony_ci    return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u,
282e1051a39Sopenharmony_ci                                    PEM_FLAG_EAY_COMPATIBLE);
283e1051a39Sopenharmony_ci}
284e1051a39Sopenharmony_ci
285e1051a39Sopenharmony_ciint PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
286e1051a39Sopenharmony_ci                              const char *name, BIO *bp, pem_password_cb *cb,
287e1051a39Sopenharmony_ci                              void *u) {
288e1051a39Sopenharmony_ci    return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u,
289e1051a39Sopenharmony_ci                                    PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE);
290e1051a39Sopenharmony_ci}
291e1051a39Sopenharmony_ci
292e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_STDIO
293e1051a39Sopenharmony_ciint PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
294e1051a39Sopenharmony_ci                   const void *x, const EVP_CIPHER *enc,
295e1051a39Sopenharmony_ci                   const unsigned char *kstr, int klen,
296e1051a39Sopenharmony_ci                   pem_password_cb *callback, void *u)
297e1051a39Sopenharmony_ci{
298e1051a39Sopenharmony_ci    BIO *b;
299e1051a39Sopenharmony_ci    int ret;
300e1051a39Sopenharmony_ci
301e1051a39Sopenharmony_ci    if ((b = BIO_new(BIO_s_file())) == NULL) {
302e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
303e1051a39Sopenharmony_ci        return 0;
304e1051a39Sopenharmony_ci    }
305e1051a39Sopenharmony_ci    BIO_set_fp(b, fp, BIO_NOCLOSE);
306e1051a39Sopenharmony_ci    ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u);
307e1051a39Sopenharmony_ci    BIO_free(b);
308e1051a39Sopenharmony_ci    return ret;
309e1051a39Sopenharmony_ci}
310e1051a39Sopenharmony_ci#endif
311e1051a39Sopenharmony_ci
312e1051a39Sopenharmony_ciint PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
313e1051a39Sopenharmony_ci                       const void *x, const EVP_CIPHER *enc,
314e1051a39Sopenharmony_ci                       const unsigned char *kstr, int klen,
315e1051a39Sopenharmony_ci                       pem_password_cb *callback, void *u)
316e1051a39Sopenharmony_ci{
317e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
318e1051a39Sopenharmony_ci    int dsize = 0, i = 0, j = 0, ret = 0;
319e1051a39Sopenharmony_ci    unsigned char *p, *data = NULL;
320e1051a39Sopenharmony_ci    const char *objstr = NULL;
321e1051a39Sopenharmony_ci    char buf[PEM_BUFSIZE];
322e1051a39Sopenharmony_ci    unsigned char key[EVP_MAX_KEY_LENGTH];
323e1051a39Sopenharmony_ci    unsigned char iv[EVP_MAX_IV_LENGTH];
324e1051a39Sopenharmony_ci
325e1051a39Sopenharmony_ci    if (enc != NULL) {
326e1051a39Sopenharmony_ci        objstr = EVP_CIPHER_get0_name(enc);
327e1051a39Sopenharmony_ci        if (objstr == NULL || EVP_CIPHER_get_iv_length(enc) == 0
328e1051a39Sopenharmony_ci                || EVP_CIPHER_get_iv_length(enc) > (int)sizeof(iv)
329e1051a39Sopenharmony_ci                   /*
330e1051a39Sopenharmony_ci                    * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n"
331e1051a39Sopenharmony_ci                    * fits into buf
332e1051a39Sopenharmony_ci                    */
333e1051a39Sopenharmony_ci                || strlen(objstr) + 23 + 2 * EVP_CIPHER_get_iv_length(enc) + 13
334e1051a39Sopenharmony_ci                   > sizeof(buf)) {
335e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
336e1051a39Sopenharmony_ci            goto err;
337e1051a39Sopenharmony_ci        }
338e1051a39Sopenharmony_ci    }
339e1051a39Sopenharmony_ci
340e1051a39Sopenharmony_ci    if ((dsize = i2d(x, NULL)) <= 0) {
341e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
342e1051a39Sopenharmony_ci        dsize = 0;
343e1051a39Sopenharmony_ci        goto err;
344e1051a39Sopenharmony_ci    }
345e1051a39Sopenharmony_ci    /* dsize + 8 bytes are needed */
346e1051a39Sopenharmony_ci    /* actually it needs the cipher block size extra... */
347e1051a39Sopenharmony_ci    data = OPENSSL_malloc((unsigned int)dsize + 20);
348e1051a39Sopenharmony_ci    if (data == NULL) {
349e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
350e1051a39Sopenharmony_ci        goto err;
351e1051a39Sopenharmony_ci    }
352e1051a39Sopenharmony_ci    p = data;
353e1051a39Sopenharmony_ci    i = i2d(x, &p);
354e1051a39Sopenharmony_ci
355e1051a39Sopenharmony_ci    if (enc != NULL) {
356e1051a39Sopenharmony_ci        if (kstr == NULL) {
357e1051a39Sopenharmony_ci            if (callback == NULL)
358e1051a39Sopenharmony_ci                klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
359e1051a39Sopenharmony_ci            else
360e1051a39Sopenharmony_ci                klen = (*callback) (buf, PEM_BUFSIZE, 1, u);
361e1051a39Sopenharmony_ci            if (klen <= 0) {
362e1051a39Sopenharmony_ci                ERR_raise(ERR_LIB_PEM, PEM_R_READ_KEY);
363e1051a39Sopenharmony_ci                goto err;
364e1051a39Sopenharmony_ci            }
365e1051a39Sopenharmony_ci#ifdef CHARSET_EBCDIC
366e1051a39Sopenharmony_ci            /* Convert the pass phrase from EBCDIC */
367e1051a39Sopenharmony_ci            ebcdic2ascii(buf, buf, klen);
368e1051a39Sopenharmony_ci#endif
369e1051a39Sopenharmony_ci            kstr = (unsigned char *)buf;
370e1051a39Sopenharmony_ci        }
371e1051a39Sopenharmony_ci        /* Generate a salt */
372e1051a39Sopenharmony_ci        if (RAND_bytes(iv, EVP_CIPHER_get_iv_length(enc)) <= 0)
373e1051a39Sopenharmony_ci            goto err;
374e1051a39Sopenharmony_ci        /*
375e1051a39Sopenharmony_ci         * The 'iv' is used as the iv and as a salt.  It is NOT taken from
376e1051a39Sopenharmony_ci         * the BytesToKey function
377e1051a39Sopenharmony_ci         */
378e1051a39Sopenharmony_ci        if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, key, NULL))
379e1051a39Sopenharmony_ci            goto err;
380e1051a39Sopenharmony_ci
381e1051a39Sopenharmony_ci        if (kstr == (unsigned char *)buf)
382e1051a39Sopenharmony_ci            OPENSSL_cleanse(buf, PEM_BUFSIZE);
383e1051a39Sopenharmony_ci
384e1051a39Sopenharmony_ci        buf[0] = '\0';
385e1051a39Sopenharmony_ci        PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
386e1051a39Sopenharmony_ci        PEM_dek_info(buf, objstr, EVP_CIPHER_get_iv_length(enc), (char *)iv);
387e1051a39Sopenharmony_ci        /* k=strlen(buf); */
388e1051a39Sopenharmony_ci
389e1051a39Sopenharmony_ci        ret = 1;
390e1051a39Sopenharmony_ci        if ((ctx = EVP_CIPHER_CTX_new()) == NULL
391e1051a39Sopenharmony_ci            || !EVP_EncryptInit_ex(ctx, enc, NULL, key, iv)
392e1051a39Sopenharmony_ci            || !EVP_EncryptUpdate(ctx, data, &j, data, i)
393e1051a39Sopenharmony_ci            || !EVP_EncryptFinal_ex(ctx, &(data[j]), &i))
394e1051a39Sopenharmony_ci            ret = 0;
395e1051a39Sopenharmony_ci        if (ret == 0)
396e1051a39Sopenharmony_ci            goto err;
397e1051a39Sopenharmony_ci        i += j;
398e1051a39Sopenharmony_ci    } else {
399e1051a39Sopenharmony_ci        ret = 1;
400e1051a39Sopenharmony_ci        buf[0] = '\0';
401e1051a39Sopenharmony_ci    }
402e1051a39Sopenharmony_ci    i = PEM_write_bio(bp, name, buf, data, i);
403e1051a39Sopenharmony_ci    if (i <= 0)
404e1051a39Sopenharmony_ci        ret = 0;
405e1051a39Sopenharmony_ci err:
406e1051a39Sopenharmony_ci    OPENSSL_cleanse(key, sizeof(key));
407e1051a39Sopenharmony_ci    OPENSSL_cleanse(iv, sizeof(iv));
408e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
409e1051a39Sopenharmony_ci    OPENSSL_cleanse(buf, PEM_BUFSIZE);
410e1051a39Sopenharmony_ci    OPENSSL_clear_free(data, (unsigned int)dsize);
411e1051a39Sopenharmony_ci    return ret;
412e1051a39Sopenharmony_ci}
413e1051a39Sopenharmony_ci
414e1051a39Sopenharmony_ciint PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen,
415e1051a39Sopenharmony_ci                  pem_password_cb *callback, void *u)
416e1051a39Sopenharmony_ci{
417e1051a39Sopenharmony_ci    int ok;
418e1051a39Sopenharmony_ci    int keylen;
419e1051a39Sopenharmony_ci    long len = *plen;
420e1051a39Sopenharmony_ci    int ilen = (int) len;       /* EVP_DecryptUpdate etc. take int lengths */
421e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx;
422e1051a39Sopenharmony_ci    unsigned char key[EVP_MAX_KEY_LENGTH];
423e1051a39Sopenharmony_ci    char buf[PEM_BUFSIZE];
424e1051a39Sopenharmony_ci
425e1051a39Sopenharmony_ci#if LONG_MAX > INT_MAX
426e1051a39Sopenharmony_ci    /* Check that we did not truncate the length */
427e1051a39Sopenharmony_ci    if (len > INT_MAX) {
428e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
429e1051a39Sopenharmony_ci        return 0;
430e1051a39Sopenharmony_ci    }
431e1051a39Sopenharmony_ci#endif
432e1051a39Sopenharmony_ci
433e1051a39Sopenharmony_ci    if (cipher->cipher == NULL)
434e1051a39Sopenharmony_ci        return 1;
435e1051a39Sopenharmony_ci    if (callback == NULL)
436e1051a39Sopenharmony_ci        keylen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u);
437e1051a39Sopenharmony_ci    else
438e1051a39Sopenharmony_ci        keylen = callback(buf, PEM_BUFSIZE, 0, u);
439e1051a39Sopenharmony_ci    if (keylen < 0) {
440e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
441e1051a39Sopenharmony_ci        return 0;
442e1051a39Sopenharmony_ci    }
443e1051a39Sopenharmony_ci#ifdef CHARSET_EBCDIC
444e1051a39Sopenharmony_ci    /* Convert the pass phrase from EBCDIC */
445e1051a39Sopenharmony_ci    ebcdic2ascii(buf, buf, keylen);
446e1051a39Sopenharmony_ci#endif
447e1051a39Sopenharmony_ci
448e1051a39Sopenharmony_ci    if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]),
449e1051a39Sopenharmony_ci                        (unsigned char *)buf, keylen, 1, key, NULL))
450e1051a39Sopenharmony_ci        return 0;
451e1051a39Sopenharmony_ci
452e1051a39Sopenharmony_ci    ctx = EVP_CIPHER_CTX_new();
453e1051a39Sopenharmony_ci    if (ctx == NULL)
454e1051a39Sopenharmony_ci        return 0;
455e1051a39Sopenharmony_ci
456e1051a39Sopenharmony_ci    ok = EVP_DecryptInit_ex(ctx, cipher->cipher, NULL, key, &(cipher->iv[0]));
457e1051a39Sopenharmony_ci    if (ok)
458e1051a39Sopenharmony_ci        ok = EVP_DecryptUpdate(ctx, data, &ilen, data, ilen);
459e1051a39Sopenharmony_ci    if (ok) {
460e1051a39Sopenharmony_ci        /* Squirrel away the length of data decrypted so far. */
461e1051a39Sopenharmony_ci        *plen = ilen;
462e1051a39Sopenharmony_ci        ok = EVP_DecryptFinal_ex(ctx, &(data[ilen]), &ilen);
463e1051a39Sopenharmony_ci    }
464e1051a39Sopenharmony_ci    if (ok)
465e1051a39Sopenharmony_ci        *plen += ilen;
466e1051a39Sopenharmony_ci    else
467e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
468e1051a39Sopenharmony_ci
469e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
470e1051a39Sopenharmony_ci    OPENSSL_cleanse((char *)buf, sizeof(buf));
471e1051a39Sopenharmony_ci    OPENSSL_cleanse((char *)key, sizeof(key));
472e1051a39Sopenharmony_ci    return ok;
473e1051a39Sopenharmony_ci}
474e1051a39Sopenharmony_ci
475e1051a39Sopenharmony_ci/*
476e1051a39Sopenharmony_ci * This implements a very limited PEM header parser that does not support the
477e1051a39Sopenharmony_ci * full grammar of rfc1421.  In particular, folded headers are not supported,
478e1051a39Sopenharmony_ci * nor is additional whitespace.
479e1051a39Sopenharmony_ci *
480e1051a39Sopenharmony_ci * A robust implementation would make use of a library that turns the headers
481e1051a39Sopenharmony_ci * into a BIO from which one folded line is read at a time, and is then split
482e1051a39Sopenharmony_ci * into a header label and content.  We would then parse the content of the
483e1051a39Sopenharmony_ci * headers we care about.  This is overkill for just this limited use-case, but
484e1051a39Sopenharmony_ci * presumably we also parse rfc822-style headers for S/MIME, so a common
485e1051a39Sopenharmony_ci * abstraction might well be more generally useful.
486e1051a39Sopenharmony_ci */
487e1051a39Sopenharmony_ciint PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher)
488e1051a39Sopenharmony_ci{
489e1051a39Sopenharmony_ci    static const char ProcType[] = "Proc-Type:";
490e1051a39Sopenharmony_ci    static const char ENCRYPTED[] = "ENCRYPTED";
491e1051a39Sopenharmony_ci    static const char DEKInfo[] = "DEK-Info:";
492e1051a39Sopenharmony_ci    const EVP_CIPHER *enc = NULL;
493e1051a39Sopenharmony_ci    int ivlen;
494e1051a39Sopenharmony_ci    char *dekinfostart, c;
495e1051a39Sopenharmony_ci
496e1051a39Sopenharmony_ci    cipher->cipher = NULL;
497e1051a39Sopenharmony_ci    memset(cipher->iv, 0, sizeof(cipher->iv));
498e1051a39Sopenharmony_ci    if ((header == NULL) || (*header == '\0') || (*header == '\n'))
499e1051a39Sopenharmony_ci        return 1;
500e1051a39Sopenharmony_ci
501e1051a39Sopenharmony_ci    if (strncmp(header, ProcType, sizeof(ProcType)-1) != 0) {
502e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_NOT_PROC_TYPE);
503e1051a39Sopenharmony_ci        return 0;
504e1051a39Sopenharmony_ci    }
505e1051a39Sopenharmony_ci    header += sizeof(ProcType)-1;
506e1051a39Sopenharmony_ci    header += strspn(header, " \t");
507e1051a39Sopenharmony_ci
508e1051a39Sopenharmony_ci    if (*header++ != '4' || *header++ != ',')
509e1051a39Sopenharmony_ci        return 0;
510e1051a39Sopenharmony_ci    header += strspn(header, " \t");
511e1051a39Sopenharmony_ci
512e1051a39Sopenharmony_ci    /* We expect "ENCRYPTED" followed by optional white-space + line break */
513e1051a39Sopenharmony_ci    if (strncmp(header, ENCRYPTED, sizeof(ENCRYPTED)-1) != 0 ||
514e1051a39Sopenharmony_ci        strspn(header+sizeof(ENCRYPTED)-1, " \t\r\n") == 0) {
515e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_NOT_ENCRYPTED);
516e1051a39Sopenharmony_ci        return 0;
517e1051a39Sopenharmony_ci    }
518e1051a39Sopenharmony_ci    header += sizeof(ENCRYPTED)-1;
519e1051a39Sopenharmony_ci    header += strspn(header, " \t\r");
520e1051a39Sopenharmony_ci    if (*header++ != '\n') {
521e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_SHORT_HEADER);
522e1051a39Sopenharmony_ci        return 0;
523e1051a39Sopenharmony_ci    }
524e1051a39Sopenharmony_ci
525e1051a39Sopenharmony_ci    /*-
526e1051a39Sopenharmony_ci     * https://tools.ietf.org/html/rfc1421#section-4.6.1.3
527e1051a39Sopenharmony_ci     * We expect "DEK-Info: algo[,hex-parameters]"
528e1051a39Sopenharmony_ci     */
529e1051a39Sopenharmony_ci    if (strncmp(header, DEKInfo, sizeof(DEKInfo)-1) != 0) {
530e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_NOT_DEK_INFO);
531e1051a39Sopenharmony_ci        return 0;
532e1051a39Sopenharmony_ci    }
533e1051a39Sopenharmony_ci    header += sizeof(DEKInfo)-1;
534e1051a39Sopenharmony_ci    header += strspn(header, " \t");
535e1051a39Sopenharmony_ci
536e1051a39Sopenharmony_ci    /*
537e1051a39Sopenharmony_ci     * DEK-INFO is a comma-separated combination of algorithm name and optional
538e1051a39Sopenharmony_ci     * parameters.
539e1051a39Sopenharmony_ci     */
540e1051a39Sopenharmony_ci    dekinfostart = header;
541e1051a39Sopenharmony_ci    header += strcspn(header, " \t,");
542e1051a39Sopenharmony_ci    c = *header;
543e1051a39Sopenharmony_ci    *header = '\0';
544e1051a39Sopenharmony_ci    cipher->cipher = enc = EVP_get_cipherbyname(dekinfostart);
545e1051a39Sopenharmony_ci    *header = c;
546e1051a39Sopenharmony_ci    header += strspn(header, " \t");
547e1051a39Sopenharmony_ci
548e1051a39Sopenharmony_ci    if (enc == NULL) {
549e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_ENCRYPTION);
550e1051a39Sopenharmony_ci        return 0;
551e1051a39Sopenharmony_ci    }
552e1051a39Sopenharmony_ci    ivlen = EVP_CIPHER_get_iv_length(enc);
553e1051a39Sopenharmony_ci    if (ivlen > 0 && *header++ != ',') {
554e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_MISSING_DEK_IV);
555e1051a39Sopenharmony_ci        return 0;
556e1051a39Sopenharmony_ci    } else if (ivlen == 0 && *header == ',') {
557e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_UNEXPECTED_DEK_IV);
558e1051a39Sopenharmony_ci        return 0;
559e1051a39Sopenharmony_ci    }
560e1051a39Sopenharmony_ci
561e1051a39Sopenharmony_ci    if (!load_iv(&header, cipher->iv, EVP_CIPHER_get_iv_length(enc)))
562e1051a39Sopenharmony_ci        return 0;
563e1051a39Sopenharmony_ci
564e1051a39Sopenharmony_ci    return 1;
565e1051a39Sopenharmony_ci}
566e1051a39Sopenharmony_ci
567e1051a39Sopenharmony_cistatic int load_iv(char **fromp, unsigned char *to, int num)
568e1051a39Sopenharmony_ci{
569e1051a39Sopenharmony_ci    int v, i;
570e1051a39Sopenharmony_ci    char *from;
571e1051a39Sopenharmony_ci
572e1051a39Sopenharmony_ci    from = *fromp;
573e1051a39Sopenharmony_ci    for (i = 0; i < num; i++)
574e1051a39Sopenharmony_ci        to[i] = 0;
575e1051a39Sopenharmony_ci    num *= 2;
576e1051a39Sopenharmony_ci    for (i = 0; i < num; i++) {
577e1051a39Sopenharmony_ci        v = OPENSSL_hexchar2int(*from);
578e1051a39Sopenharmony_ci        if (v < 0) {
579e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_IV_CHARS);
580e1051a39Sopenharmony_ci            return 0;
581e1051a39Sopenharmony_ci        }
582e1051a39Sopenharmony_ci        from++;
583e1051a39Sopenharmony_ci        to[i / 2] |= v << (long)((!(i & 1)) * 4);
584e1051a39Sopenharmony_ci    }
585e1051a39Sopenharmony_ci
586e1051a39Sopenharmony_ci    *fromp = from;
587e1051a39Sopenharmony_ci    return 1;
588e1051a39Sopenharmony_ci}
589e1051a39Sopenharmony_ci
590e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_STDIO
591e1051a39Sopenharmony_ciint PEM_write(FILE *fp, const char *name, const char *header,
592e1051a39Sopenharmony_ci              const unsigned char *data, long len)
593e1051a39Sopenharmony_ci{
594e1051a39Sopenharmony_ci    BIO *b;
595e1051a39Sopenharmony_ci    int ret;
596e1051a39Sopenharmony_ci
597e1051a39Sopenharmony_ci    if ((b = BIO_new(BIO_s_file())) == NULL) {
598e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
599e1051a39Sopenharmony_ci        return 0;
600e1051a39Sopenharmony_ci    }
601e1051a39Sopenharmony_ci    BIO_set_fp(b, fp, BIO_NOCLOSE);
602e1051a39Sopenharmony_ci    ret = PEM_write_bio(b, name, header, data, len);
603e1051a39Sopenharmony_ci    BIO_free(b);
604e1051a39Sopenharmony_ci    return ret;
605e1051a39Sopenharmony_ci}
606e1051a39Sopenharmony_ci#endif
607e1051a39Sopenharmony_ci
608e1051a39Sopenharmony_ciint PEM_write_bio(BIO *bp, const char *name, const char *header,
609e1051a39Sopenharmony_ci                  const unsigned char *data, long len)
610e1051a39Sopenharmony_ci{
611e1051a39Sopenharmony_ci    int nlen, n, i, j, outl;
612e1051a39Sopenharmony_ci    unsigned char *buf = NULL;
613e1051a39Sopenharmony_ci    EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
614e1051a39Sopenharmony_ci    int reason = ERR_R_BUF_LIB;
615e1051a39Sopenharmony_ci    int retval = 0;
616e1051a39Sopenharmony_ci
617e1051a39Sopenharmony_ci    if (ctx == NULL) {
618e1051a39Sopenharmony_ci        reason = ERR_R_MALLOC_FAILURE;
619e1051a39Sopenharmony_ci        goto err;
620e1051a39Sopenharmony_ci    }
621e1051a39Sopenharmony_ci
622e1051a39Sopenharmony_ci    EVP_EncodeInit(ctx);
623e1051a39Sopenharmony_ci    nlen = strlen(name);
624e1051a39Sopenharmony_ci
625e1051a39Sopenharmony_ci    if ((BIO_write(bp, "-----BEGIN ", 11) != 11) ||
626e1051a39Sopenharmony_ci        (BIO_write(bp, name, nlen) != nlen) ||
627e1051a39Sopenharmony_ci        (BIO_write(bp, "-----\n", 6) != 6))
628e1051a39Sopenharmony_ci        goto err;
629e1051a39Sopenharmony_ci
630e1051a39Sopenharmony_ci    i = header != NULL ? strlen(header) : 0;
631e1051a39Sopenharmony_ci    if (i > 0) {
632e1051a39Sopenharmony_ci        if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1))
633e1051a39Sopenharmony_ci            goto err;
634e1051a39Sopenharmony_ci    }
635e1051a39Sopenharmony_ci
636e1051a39Sopenharmony_ci    buf = OPENSSL_malloc(PEM_BUFSIZE * 8);
637e1051a39Sopenharmony_ci    if (buf == NULL) {
638e1051a39Sopenharmony_ci        reason = ERR_R_MALLOC_FAILURE;
639e1051a39Sopenharmony_ci        goto err;
640e1051a39Sopenharmony_ci    }
641e1051a39Sopenharmony_ci
642e1051a39Sopenharmony_ci    i = j = 0;
643e1051a39Sopenharmony_ci    while (len > 0) {
644e1051a39Sopenharmony_ci        n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len);
645e1051a39Sopenharmony_ci        if (!EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n))
646e1051a39Sopenharmony_ci            goto err;
647e1051a39Sopenharmony_ci        if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl))
648e1051a39Sopenharmony_ci            goto err;
649e1051a39Sopenharmony_ci        i += outl;
650e1051a39Sopenharmony_ci        len -= n;
651e1051a39Sopenharmony_ci        j += n;
652e1051a39Sopenharmony_ci    }
653e1051a39Sopenharmony_ci    EVP_EncodeFinal(ctx, buf, &outl);
654e1051a39Sopenharmony_ci    if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
655e1051a39Sopenharmony_ci        goto err;
656e1051a39Sopenharmony_ci    if ((BIO_write(bp, "-----END ", 9) != 9) ||
657e1051a39Sopenharmony_ci        (BIO_write(bp, name, nlen) != nlen) ||
658e1051a39Sopenharmony_ci        (BIO_write(bp, "-----\n", 6) != 6))
659e1051a39Sopenharmony_ci        goto err;
660e1051a39Sopenharmony_ci    retval = i + outl;
661e1051a39Sopenharmony_ci
662e1051a39Sopenharmony_ci err:
663e1051a39Sopenharmony_ci    if (retval == 0)
664e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, reason);
665e1051a39Sopenharmony_ci    EVP_ENCODE_CTX_free(ctx);
666e1051a39Sopenharmony_ci    OPENSSL_clear_free(buf, PEM_BUFSIZE * 8);
667e1051a39Sopenharmony_ci    return retval;
668e1051a39Sopenharmony_ci}
669e1051a39Sopenharmony_ci
670e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_STDIO
671e1051a39Sopenharmony_ciint PEM_read(FILE *fp, char **name, char **header, unsigned char **data,
672e1051a39Sopenharmony_ci             long *len)
673e1051a39Sopenharmony_ci{
674e1051a39Sopenharmony_ci    BIO *b;
675e1051a39Sopenharmony_ci    int ret;
676e1051a39Sopenharmony_ci
677e1051a39Sopenharmony_ci    if ((b = BIO_new(BIO_s_file())) == NULL) {
678e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
679e1051a39Sopenharmony_ci        return 0;
680e1051a39Sopenharmony_ci    }
681e1051a39Sopenharmony_ci    BIO_set_fp(b, fp, BIO_NOCLOSE);
682e1051a39Sopenharmony_ci    ret = PEM_read_bio(b, name, header, data, len);
683e1051a39Sopenharmony_ci    BIO_free(b);
684e1051a39Sopenharmony_ci    return ret;
685e1051a39Sopenharmony_ci}
686e1051a39Sopenharmony_ci#endif
687e1051a39Sopenharmony_ci
688e1051a39Sopenharmony_ci/* Some helpers for PEM_read_bio_ex(). */
689e1051a39Sopenharmony_cistatic int sanitize_line(char *linebuf, int len, unsigned int flags, int first_call)
690e1051a39Sopenharmony_ci{
691e1051a39Sopenharmony_ci    int i;
692e1051a39Sopenharmony_ci    if (first_call) {
693e1051a39Sopenharmony_ci        /* Other BOMs imply unsupported multibyte encoding,
694e1051a39Sopenharmony_ci         * so don't strip them and let the error raise */
695e1051a39Sopenharmony_ci        const unsigned char utf8_bom[3] = {0xEF, 0xBB, 0xBF};
696e1051a39Sopenharmony_ci
697e1051a39Sopenharmony_ci        if (len > 3 && memcmp(linebuf, utf8_bom, 3) == 0) {
698e1051a39Sopenharmony_ci            memmove(linebuf, linebuf + 3, len - 3);
699e1051a39Sopenharmony_ci            linebuf[len - 3] = 0;
700e1051a39Sopenharmony_ci            len -= 3;
701e1051a39Sopenharmony_ci        }
702e1051a39Sopenharmony_ci    }
703e1051a39Sopenharmony_ci
704e1051a39Sopenharmony_ci    if (flags & PEM_FLAG_EAY_COMPATIBLE) {
705e1051a39Sopenharmony_ci        /* Strip trailing whitespace */
706e1051a39Sopenharmony_ci        while ((len >= 0) && (linebuf[len] <= ' '))
707e1051a39Sopenharmony_ci            len--;
708e1051a39Sopenharmony_ci        /* Go back to whitespace before applying uniform line ending. */
709e1051a39Sopenharmony_ci        len++;
710e1051a39Sopenharmony_ci    } else if (flags & PEM_FLAG_ONLY_B64) {
711e1051a39Sopenharmony_ci        for (i = 0; i < len; ++i) {
712e1051a39Sopenharmony_ci            if (!ossl_isbase64(linebuf[i]) || linebuf[i] == '\n'
713e1051a39Sopenharmony_ci                || linebuf[i] == '\r')
714e1051a39Sopenharmony_ci                break;
715e1051a39Sopenharmony_ci        }
716e1051a39Sopenharmony_ci        len = i;
717e1051a39Sopenharmony_ci    } else {
718e1051a39Sopenharmony_ci        /* EVP_DecodeBlock strips leading and trailing whitespace, so just strip
719e1051a39Sopenharmony_ci         * control characters in-place and let everything through. */
720e1051a39Sopenharmony_ci        for (i = 0; i < len; ++i) {
721e1051a39Sopenharmony_ci            if (linebuf[i] == '\n' || linebuf[i] == '\r')
722e1051a39Sopenharmony_ci                break;
723e1051a39Sopenharmony_ci            if (ossl_iscntrl(linebuf[i]))
724e1051a39Sopenharmony_ci                linebuf[i] = ' ';
725e1051a39Sopenharmony_ci        }
726e1051a39Sopenharmony_ci        len = i;
727e1051a39Sopenharmony_ci    }
728e1051a39Sopenharmony_ci    /* The caller allocated LINESIZE+1, so this is safe. */
729e1051a39Sopenharmony_ci    linebuf[len++] = '\n';
730e1051a39Sopenharmony_ci    linebuf[len] = '\0';
731e1051a39Sopenharmony_ci    return len;
732e1051a39Sopenharmony_ci}
733e1051a39Sopenharmony_ci
734e1051a39Sopenharmony_ci#define LINESIZE 255
735e1051a39Sopenharmony_ci/* Note trailing spaces for begin and end. */
736e1051a39Sopenharmony_cistatic const char beginstr[] = "-----BEGIN ";
737e1051a39Sopenharmony_cistatic const char endstr[] = "-----END ";
738e1051a39Sopenharmony_cistatic const char tailstr[] = "-----\n";
739e1051a39Sopenharmony_ci#define BEGINLEN ((int)(sizeof(beginstr) - 1))
740e1051a39Sopenharmony_ci#define ENDLEN ((int)(sizeof(endstr) - 1))
741e1051a39Sopenharmony_ci#define TAILLEN ((int)(sizeof(tailstr) - 1))
742e1051a39Sopenharmony_cistatic int get_name(BIO *bp, char **name, unsigned int flags)
743e1051a39Sopenharmony_ci{
744e1051a39Sopenharmony_ci    char *linebuf;
745e1051a39Sopenharmony_ci    int ret = 0;
746e1051a39Sopenharmony_ci    int len;
747e1051a39Sopenharmony_ci    int first_call = 1;
748e1051a39Sopenharmony_ci
749e1051a39Sopenharmony_ci    /*
750e1051a39Sopenharmony_ci     * Need to hold trailing NUL (accounted for by BIO_gets() and the newline
751e1051a39Sopenharmony_ci     * that will be added by sanitize_line() (the extra '1').
752e1051a39Sopenharmony_ci     */
753e1051a39Sopenharmony_ci    linebuf = pem_malloc(LINESIZE + 1, flags);
754e1051a39Sopenharmony_ci    if (linebuf == NULL) {
755e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
756e1051a39Sopenharmony_ci        return 0;
757e1051a39Sopenharmony_ci    }
758e1051a39Sopenharmony_ci
759e1051a39Sopenharmony_ci    do {
760e1051a39Sopenharmony_ci        len = BIO_gets(bp, linebuf, LINESIZE);
761e1051a39Sopenharmony_ci
762e1051a39Sopenharmony_ci        if (len <= 0) {
763e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PEM, PEM_R_NO_START_LINE);
764e1051a39Sopenharmony_ci            goto err;
765e1051a39Sopenharmony_ci        }
766e1051a39Sopenharmony_ci
767e1051a39Sopenharmony_ci        /* Strip trailing garbage and standardize ending. */
768e1051a39Sopenharmony_ci        len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64, first_call);
769e1051a39Sopenharmony_ci        first_call = 0;
770e1051a39Sopenharmony_ci
771e1051a39Sopenharmony_ci        /* Allow leading empty or non-matching lines. */
772e1051a39Sopenharmony_ci    } while (strncmp(linebuf, beginstr, BEGINLEN) != 0
773e1051a39Sopenharmony_ci             || len < TAILLEN
774e1051a39Sopenharmony_ci             || strncmp(linebuf + len - TAILLEN, tailstr, TAILLEN) != 0);
775e1051a39Sopenharmony_ci    linebuf[len - TAILLEN] = '\0';
776e1051a39Sopenharmony_ci    len = len - BEGINLEN - TAILLEN + 1;
777e1051a39Sopenharmony_ci    *name = pem_malloc(len, flags);
778e1051a39Sopenharmony_ci    if (*name == NULL) {
779e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
780e1051a39Sopenharmony_ci        goto err;
781e1051a39Sopenharmony_ci    }
782e1051a39Sopenharmony_ci    memcpy(*name, linebuf + BEGINLEN, len);
783e1051a39Sopenharmony_ci    ret = 1;
784e1051a39Sopenharmony_ci
785e1051a39Sopenharmony_cierr:
786e1051a39Sopenharmony_ci    pem_free(linebuf, flags, LINESIZE + 1);
787e1051a39Sopenharmony_ci    return ret;
788e1051a39Sopenharmony_ci}
789e1051a39Sopenharmony_ci
790e1051a39Sopenharmony_ci/* Keep track of how much of a header we've seen. */
791e1051a39Sopenharmony_cienum header_status {
792e1051a39Sopenharmony_ci    MAYBE_HEADER,
793e1051a39Sopenharmony_ci    IN_HEADER,
794e1051a39Sopenharmony_ci    POST_HEADER
795e1051a39Sopenharmony_ci};
796e1051a39Sopenharmony_ci
797e1051a39Sopenharmony_ci/**
798e1051a39Sopenharmony_ci * Extract the optional PEM header, with details on the type of content and
799e1051a39Sopenharmony_ci * any encryption used on the contents, and the bulk of the data from the bio.
800e1051a39Sopenharmony_ci * The end of the header is marked by a blank line; if the end-of-input marker
801e1051a39Sopenharmony_ci * is reached prior to a blank line, there is no header.
802e1051a39Sopenharmony_ci *
803e1051a39Sopenharmony_ci * The header and data arguments are BIO** since we may have to swap them
804e1051a39Sopenharmony_ci * if there is no header, for efficiency.
805e1051a39Sopenharmony_ci *
806e1051a39Sopenharmony_ci * We need the name of the PEM-encoded type to verify the end string.
807e1051a39Sopenharmony_ci */
808e1051a39Sopenharmony_cistatic int get_header_and_data(BIO *bp, BIO **header, BIO **data, char *name,
809e1051a39Sopenharmony_ci                               unsigned int flags)
810e1051a39Sopenharmony_ci{
811e1051a39Sopenharmony_ci    BIO *tmp = *header;
812e1051a39Sopenharmony_ci    char *linebuf, *p;
813e1051a39Sopenharmony_ci    int len, ret = 0, end = 0, prev_partial_line_read = 0, partial_line_read = 0;
814e1051a39Sopenharmony_ci    /* 0 if not seen (yet), 1 if reading header, 2 if finished header */
815e1051a39Sopenharmony_ci    enum header_status got_header = MAYBE_HEADER;
816e1051a39Sopenharmony_ci    unsigned int flags_mask;
817e1051a39Sopenharmony_ci    size_t namelen;
818e1051a39Sopenharmony_ci
819e1051a39Sopenharmony_ci    /* Need to hold trailing NUL (accounted for by BIO_gets() and the newline
820e1051a39Sopenharmony_ci     * that will be added by sanitize_line() (the extra '1'). */
821e1051a39Sopenharmony_ci    linebuf = pem_malloc(LINESIZE + 1, flags);
822e1051a39Sopenharmony_ci    if (linebuf == NULL) {
823e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
824e1051a39Sopenharmony_ci        return 0;
825e1051a39Sopenharmony_ci    }
826e1051a39Sopenharmony_ci
827e1051a39Sopenharmony_ci    while(1) {
828e1051a39Sopenharmony_ci        flags_mask = ~0u;
829e1051a39Sopenharmony_ci        len = BIO_gets(bp, linebuf, LINESIZE);
830e1051a39Sopenharmony_ci        if (len <= 0) {
831e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_END_LINE);
832e1051a39Sopenharmony_ci            goto err;
833e1051a39Sopenharmony_ci        }
834e1051a39Sopenharmony_ci
835e1051a39Sopenharmony_ci        /*
836e1051a39Sopenharmony_ci         * Check if line has been read completely or if only part of the line
837e1051a39Sopenharmony_ci         * has been read. Keep the previous value to ignore newlines that
838e1051a39Sopenharmony_ci         * appear due to reading a line up until the char before the newline.
839e1051a39Sopenharmony_ci         */
840e1051a39Sopenharmony_ci        prev_partial_line_read = partial_line_read;
841e1051a39Sopenharmony_ci        partial_line_read = len == LINESIZE-1 && linebuf[LINESIZE-2] != '\n';
842e1051a39Sopenharmony_ci
843e1051a39Sopenharmony_ci        if (got_header == MAYBE_HEADER) {
844e1051a39Sopenharmony_ci            if (memchr(linebuf, ':', len) != NULL)
845e1051a39Sopenharmony_ci                got_header = IN_HEADER;
846e1051a39Sopenharmony_ci        }
847e1051a39Sopenharmony_ci        if (!strncmp(linebuf, endstr, ENDLEN) || got_header == IN_HEADER)
848e1051a39Sopenharmony_ci            flags_mask &= ~PEM_FLAG_ONLY_B64;
849e1051a39Sopenharmony_ci        len = sanitize_line(linebuf, len, flags & flags_mask, 0);
850e1051a39Sopenharmony_ci
851e1051a39Sopenharmony_ci        /* Check for end of header. */
852e1051a39Sopenharmony_ci        if (linebuf[0] == '\n') {
853e1051a39Sopenharmony_ci            /*
854e1051a39Sopenharmony_ci             * If previous line has been read only partially this newline is a
855e1051a39Sopenharmony_ci             * regular newline at the end of a line and not an empty line.
856e1051a39Sopenharmony_ci             */
857e1051a39Sopenharmony_ci            if (!prev_partial_line_read) {
858e1051a39Sopenharmony_ci                if (got_header == POST_HEADER) {
859e1051a39Sopenharmony_ci                    /* Another blank line is an error. */
860e1051a39Sopenharmony_ci                    ERR_raise(ERR_LIB_PEM, PEM_R_BAD_END_LINE);
861e1051a39Sopenharmony_ci                    goto err;
862e1051a39Sopenharmony_ci                }
863e1051a39Sopenharmony_ci                got_header = POST_HEADER;
864e1051a39Sopenharmony_ci                tmp = *data;
865e1051a39Sopenharmony_ci            }
866e1051a39Sopenharmony_ci            continue;
867e1051a39Sopenharmony_ci        }
868e1051a39Sopenharmony_ci
869e1051a39Sopenharmony_ci        /* Check for end of stream (which means there is no header). */
870e1051a39Sopenharmony_ci        if (strncmp(linebuf, endstr, ENDLEN) == 0) {
871e1051a39Sopenharmony_ci            p = linebuf + ENDLEN;
872e1051a39Sopenharmony_ci            namelen = strlen(name);
873e1051a39Sopenharmony_ci            if (strncmp(p, name, namelen) != 0 ||
874e1051a39Sopenharmony_ci                strncmp(p + namelen, tailstr, TAILLEN) != 0) {
875e1051a39Sopenharmony_ci                ERR_raise(ERR_LIB_PEM, PEM_R_BAD_END_LINE);
876e1051a39Sopenharmony_ci                goto err;
877e1051a39Sopenharmony_ci            }
878e1051a39Sopenharmony_ci            if (got_header == MAYBE_HEADER) {
879e1051a39Sopenharmony_ci                *header = *data;
880e1051a39Sopenharmony_ci                *data = tmp;
881e1051a39Sopenharmony_ci            }
882e1051a39Sopenharmony_ci            break;
883e1051a39Sopenharmony_ci        } else if (end) {
884e1051a39Sopenharmony_ci            /* Malformed input; short line not at end of data. */
885e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_END_LINE);
886e1051a39Sopenharmony_ci            goto err;
887e1051a39Sopenharmony_ci        }
888e1051a39Sopenharmony_ci        /*
889e1051a39Sopenharmony_ci         * Else, a line of text -- could be header or data; we don't
890e1051a39Sopenharmony_ci         * know yet.  Just pass it through.
891e1051a39Sopenharmony_ci         */
892e1051a39Sopenharmony_ci        if (BIO_puts(tmp, linebuf) < 0)
893e1051a39Sopenharmony_ci            goto err;
894e1051a39Sopenharmony_ci        /*
895e1051a39Sopenharmony_ci         * Only encrypted files need the line length check applied.
896e1051a39Sopenharmony_ci         */
897e1051a39Sopenharmony_ci        if (got_header == POST_HEADER) {
898e1051a39Sopenharmony_ci            /* 65 includes the trailing newline */
899e1051a39Sopenharmony_ci            if (len > 65)
900e1051a39Sopenharmony_ci                goto err;
901e1051a39Sopenharmony_ci            if (len < 65)
902e1051a39Sopenharmony_ci                end = 1;
903e1051a39Sopenharmony_ci        }
904e1051a39Sopenharmony_ci    }
905e1051a39Sopenharmony_ci
906e1051a39Sopenharmony_ci    ret = 1;
907e1051a39Sopenharmony_cierr:
908e1051a39Sopenharmony_ci    pem_free(linebuf, flags, LINESIZE + 1);
909e1051a39Sopenharmony_ci    return ret;
910e1051a39Sopenharmony_ci}
911e1051a39Sopenharmony_ci
912e1051a39Sopenharmony_ci/**
913e1051a39Sopenharmony_ci * Read in PEM-formatted data from the given BIO.
914e1051a39Sopenharmony_ci *
915e1051a39Sopenharmony_ci * By nature of the PEM format, all content must be printable ASCII (except
916e1051a39Sopenharmony_ci * for line endings).  Other characters are malformed input and will be rejected.
917e1051a39Sopenharmony_ci */
918e1051a39Sopenharmony_ciint PEM_read_bio_ex(BIO *bp, char **name_out, char **header,
919e1051a39Sopenharmony_ci                    unsigned char **data, long *len_out, unsigned int flags)
920e1051a39Sopenharmony_ci{
921e1051a39Sopenharmony_ci    EVP_ENCODE_CTX *ctx = NULL;
922e1051a39Sopenharmony_ci    const BIO_METHOD *bmeth;
923e1051a39Sopenharmony_ci    BIO *headerB = NULL, *dataB = NULL;
924e1051a39Sopenharmony_ci    char *name = NULL;
925e1051a39Sopenharmony_ci    int len, taillen, headerlen, ret = 0;
926e1051a39Sopenharmony_ci    BUF_MEM * buf_mem;
927e1051a39Sopenharmony_ci
928e1051a39Sopenharmony_ci    *len_out = 0;
929e1051a39Sopenharmony_ci    *name_out = *header = NULL;
930e1051a39Sopenharmony_ci    *data = NULL;
931e1051a39Sopenharmony_ci    if ((flags & PEM_FLAG_EAY_COMPATIBLE) && (flags & PEM_FLAG_ONLY_B64)) {
932e1051a39Sopenharmony_ci        /* These two are mutually incompatible; bail out. */
933e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_PASSED_INVALID_ARGUMENT);
934e1051a39Sopenharmony_ci        goto end;
935e1051a39Sopenharmony_ci    }
936e1051a39Sopenharmony_ci    bmeth = (flags & PEM_FLAG_SECURE) ? BIO_s_secmem() : BIO_s_mem();
937e1051a39Sopenharmony_ci
938e1051a39Sopenharmony_ci    headerB = BIO_new(bmeth);
939e1051a39Sopenharmony_ci    dataB = BIO_new(bmeth);
940e1051a39Sopenharmony_ci    if (headerB == NULL || dataB == NULL) {
941e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
942e1051a39Sopenharmony_ci        goto end;
943e1051a39Sopenharmony_ci    }
944e1051a39Sopenharmony_ci
945e1051a39Sopenharmony_ci    if (!get_name(bp, &name, flags))
946e1051a39Sopenharmony_ci        goto end;
947e1051a39Sopenharmony_ci    if (!get_header_and_data(bp, &headerB, &dataB, name, flags))
948e1051a39Sopenharmony_ci        goto end;
949e1051a39Sopenharmony_ci
950e1051a39Sopenharmony_ci    BIO_get_mem_ptr(dataB, &buf_mem);
951e1051a39Sopenharmony_ci    len = buf_mem->length;
952e1051a39Sopenharmony_ci
953e1051a39Sopenharmony_ci    /* There was no data in the PEM file */
954e1051a39Sopenharmony_ci    if (len == 0)
955e1051a39Sopenharmony_ci        goto end;
956e1051a39Sopenharmony_ci
957e1051a39Sopenharmony_ci    ctx = EVP_ENCODE_CTX_new();
958e1051a39Sopenharmony_ci    if (ctx == NULL) {
959e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
960e1051a39Sopenharmony_ci        goto end;
961e1051a39Sopenharmony_ci    }
962e1051a39Sopenharmony_ci
963e1051a39Sopenharmony_ci    EVP_DecodeInit(ctx);
964e1051a39Sopenharmony_ci    if (EVP_DecodeUpdate(ctx, (unsigned char*)buf_mem->data, &len,
965e1051a39Sopenharmony_ci                         (unsigned char*)buf_mem->data, len) < 0
966e1051a39Sopenharmony_ci            || EVP_DecodeFinal(ctx, (unsigned char*)&(buf_mem->data[len]),
967e1051a39Sopenharmony_ci                               &taillen) < 0) {
968e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_BAD_BASE64_DECODE);
969e1051a39Sopenharmony_ci        goto end;
970e1051a39Sopenharmony_ci    }
971e1051a39Sopenharmony_ci    len += taillen;
972e1051a39Sopenharmony_ci    buf_mem->length = len;
973e1051a39Sopenharmony_ci
974e1051a39Sopenharmony_ci    headerlen = BIO_get_mem_data(headerB, NULL);
975e1051a39Sopenharmony_ci    *header = pem_malloc(headerlen + 1, flags);
976e1051a39Sopenharmony_ci    *data = pem_malloc(len, flags);
977e1051a39Sopenharmony_ci    if (*header == NULL || *data == NULL)
978e1051a39Sopenharmony_ci        goto out_free;
979e1051a39Sopenharmony_ci    if (headerlen != 0 && BIO_read(headerB, *header, headerlen) != headerlen)
980e1051a39Sopenharmony_ci        goto out_free;
981e1051a39Sopenharmony_ci    (*header)[headerlen] = '\0';
982e1051a39Sopenharmony_ci    if (BIO_read(dataB, *data, len) != len)
983e1051a39Sopenharmony_ci        goto out_free;
984e1051a39Sopenharmony_ci    *len_out = len;
985e1051a39Sopenharmony_ci    *name_out = name;
986e1051a39Sopenharmony_ci    name = NULL;
987e1051a39Sopenharmony_ci    ret = 1;
988e1051a39Sopenharmony_ci    goto end;
989e1051a39Sopenharmony_ci
990e1051a39Sopenharmony_ciout_free:
991e1051a39Sopenharmony_ci    pem_free(*header, flags, 0);
992e1051a39Sopenharmony_ci    *header = NULL;
993e1051a39Sopenharmony_ci    pem_free(*data, flags, 0);
994e1051a39Sopenharmony_ci    *data = NULL;
995e1051a39Sopenharmony_ciend:
996e1051a39Sopenharmony_ci    EVP_ENCODE_CTX_free(ctx);
997e1051a39Sopenharmony_ci    pem_free(name, flags, 0);
998e1051a39Sopenharmony_ci    BIO_free(headerB);
999e1051a39Sopenharmony_ci    BIO_free(dataB);
1000e1051a39Sopenharmony_ci    return ret;
1001e1051a39Sopenharmony_ci}
1002e1051a39Sopenharmony_ci
1003e1051a39Sopenharmony_ciint PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,
1004e1051a39Sopenharmony_ci                 long *len)
1005e1051a39Sopenharmony_ci{
1006e1051a39Sopenharmony_ci    return PEM_read_bio_ex(bp, name, header, data, len, PEM_FLAG_EAY_COMPATIBLE);
1007e1051a39Sopenharmony_ci}
1008e1051a39Sopenharmony_ci
1009e1051a39Sopenharmony_ci/*
1010e1051a39Sopenharmony_ci * Check pem string and return prefix length. If for example the pem_str ==
1011e1051a39Sopenharmony_ci * "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" the return value is 3 for the
1012e1051a39Sopenharmony_ci * string "RSA".
1013e1051a39Sopenharmony_ci */
1014e1051a39Sopenharmony_ci
1015e1051a39Sopenharmony_ciint ossl_pem_check_suffix(const char *pem_str, const char *suffix)
1016e1051a39Sopenharmony_ci{
1017e1051a39Sopenharmony_ci    int pem_len = strlen(pem_str);
1018e1051a39Sopenharmony_ci    int suffix_len = strlen(suffix);
1019e1051a39Sopenharmony_ci    const char *p;
1020e1051a39Sopenharmony_ci    if (suffix_len + 1 >= pem_len)
1021e1051a39Sopenharmony_ci        return 0;
1022e1051a39Sopenharmony_ci    p = pem_str + pem_len - suffix_len;
1023e1051a39Sopenharmony_ci    if (strcmp(p, suffix))
1024e1051a39Sopenharmony_ci        return 0;
1025e1051a39Sopenharmony_ci    p--;
1026e1051a39Sopenharmony_ci    if (*p != ' ')
1027e1051a39Sopenharmony_ci        return 0;
1028e1051a39Sopenharmony_ci    return p - pem_str;
1029e1051a39Sopenharmony_ci}
1030