OSSL_DECODER_from_bio.pod 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. =pod
  2. =head1 NAME
  3. OSSL_DECODER_from_data,
  4. OSSL_DECODER_from_bio,
  5. OSSL_DECODER_from_fp
  6. - Routines to perform a decoding
  7. =head1 SYNOPSIS
  8. #include <openssl/decoder.h>
  9. int OSSL_DECODER_from_bio(OSSL_DECODER_CTX *ctx, BIO *in);
  10. int OSSL_DECODER_from_fp(OSSL_DECODER_CTX *ctx, FILE *fp);
  11. int OSSL_DECODER_from_data(OSSL_DECODER_CTX *ctx, const unsigned char **pdata,
  12. size_t *pdata_len);
  13. Feature availability macros:
  14. =over 4
  15. =item OSSL_DECODER_from_fp() is only available when B<OPENSSL_NO_STDIO>
  16. is undefined.
  17. =back
  18. =head1 DESCRIPTION
  19. OSSL_DECODER_from_data() runs the decoding process for the context I<ctx>,
  20. with input coming from I<*pdata>, I<*pdata_len> bytes long. Both I<*pdata>
  21. and I<*pdata_len> must be non-NULL. When OSSL_DECODER_from_data() returns,
  22. I<*pdata> is updated to point at the location after what has been decoded,
  23. and I<*pdata_len> to have the number of remaining bytes.
  24. OSSL_DECODER_from_bio() runs the decoding process for the context I<ctx>,
  25. with the input coming from the B<BIO> I<in>. Should it make a difference,
  26. it's recommended to have the BIO set in binary mode rather than text mode.
  27. OSSL_DECODER_from_fp() does the same thing as OSSL_DECODER_from_bio(),
  28. except that the input is coming from the B<FILE> I<fp>.
  29. =head1 RETURN VALUES
  30. OSSL_DECODER_from_bio(), OSSL_DECODER_from_data() and OSSL_DECODER_from_fp()
  31. return 1 on success, or 0 on failure.
  32. =head1 EXAMPLES
  33. To decode an RSA key encoded with PEM from a bio:
  34. OSSL_DECODER_CTX *dctx;
  35. EVP_PKEY *pkey = NULL;
  36. const char *format = "PEM"; /* NULL for any format */
  37. const char *structure = NULL; /* any structure */
  38. const char *keytype = "RSA"; /* NULL for any key */
  39. const unsigned char *pass = "my password";
  40. dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, format, structure,
  41. keytype,
  42. OSSL_KEYMGMT_SELECT_KEYPAIR,
  43. NULL, NULL);
  44. if (dctx == NULL) {
  45. /* error: no suitable potential decoders found */
  46. }
  47. if (pass != NULL)
  48. OSSL_DECODER_CTX_set_passphrase(dctx, pass, strlen(pass));
  49. if (OSSL_DECODER_from_bio(dctx, bio)) {
  50. /* pkey is created with the decoded data from the bio */
  51. } else {
  52. /* decoding failure */
  53. }
  54. OSSL_DECODER_CTX_free(dctx);
  55. To decode an EC key encoded with DER from a buffer:
  56. OSSL_DECODER_CTX *dctx;
  57. EVP_PKEY *pkey = NULL;
  58. const char *format = "DER"; /* NULL for any format */
  59. const char *structure = NULL; /* any structure */
  60. const char *keytype = "EC"; /* NULL for any key */
  61. const unsigned char *pass = NULL
  62. const unsigned char *data = buffer;
  63. size_t datalen = sizeof(buffer);
  64. dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, format, structure,
  65. keytype,
  66. OSSL_KEYMGMT_SELECT_KEYPAIR
  67. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  68. NULL, NULL);
  69. if (dctx == NULL) {
  70. /* error: no suitable potential decoders found */
  71. }
  72. if (pass != NULL)
  73. OSSL_DECODER_CTX_set_passphrase(dctx, pass, strlen(pass));
  74. if (OSSL_DECODER_from_data(dctx, &data, &datalen)) {
  75. /* pkey is created with the decoded data from the buffer */
  76. } else {
  77. /* decoding failure */
  78. }
  79. OSSL_DECODER_CTX_free(dctx);
  80. =head1 SEE ALSO
  81. L<provider(7)>, L<OSSL_DECODER_CTX(3)>
  82. =head1 HISTORY
  83. The functions described here were added in OpenSSL 3.0.
  84. =head1 COPYRIGHT
  85. Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  86. Licensed under the Apache License 2.0 (the "License"). You may not use
  87. this file except in compliance with the License. You can obtain a copy
  88. in the file LICENSE in the source distribution or at
  89. L<https://www.openssl.org/source/license.html>.
  90. =cut