pem.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <openssl/pem.h>
  11. #include <openssl/err.h>
  12. #include "fuzzer.h"
  13. int FuzzerInitialize(int *argc, char ***argv)
  14. {
  15. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  16. ERR_clear_error();
  17. CRYPTO_free_ex_index(0, -1);
  18. return 1;
  19. }
  20. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  21. {
  22. BIO *in;
  23. char *name = NULL, *header = NULL;
  24. unsigned char *data = NULL;
  25. long outlen;
  26. if (len <= 1)
  27. return 0;
  28. in = BIO_new(BIO_s_mem());
  29. OPENSSL_assert((size_t)BIO_write(in, buf + 1, len - 1) == len - 1);
  30. if (PEM_read_bio_ex(in, &name, &header, &data, &outlen, buf[0]) == 1) {
  31. /* Try to read all the data we get to see if allocated properly. */
  32. BIO_write(in, name, strlen(name));
  33. BIO_write(in, header, strlen(header));
  34. BIO_write(in, data, outlen);
  35. }
  36. if (buf[0] & PEM_FLAG_SECURE) {
  37. OPENSSL_secure_free(name);
  38. OPENSSL_secure_free(header);
  39. OPENSSL_secure_free(data);
  40. } else {
  41. OPENSSL_free(name);
  42. OPENSSL_free(header);
  43. OPENSSL_free(data);
  44. }
  45. BIO_free(in);
  46. ERR_clear_error();
  47. return 0;
  48. }
  49. void FuzzerCleanup(void)
  50. {
  51. }