2
0

asn1.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_ASN1_H
  10. # define OSSL_CRYPTO_ASN1_H
  11. # pragma once
  12. # include <openssl/asn1.h>
  13. # include <openssl/core_dispatch.h> /* OSSL_FUNC_keymgmt_import() */
  14. /* Internal ASN1 structures and functions: not for application use */
  15. /* ASN1 public key method structure */
  16. #include <openssl/core.h>
  17. struct evp_pkey_asn1_method_st {
  18. int pkey_id;
  19. int pkey_base_id;
  20. unsigned long pkey_flags;
  21. char *pem_str;
  22. char *info;
  23. int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub);
  24. int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
  25. int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  26. int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  27. ASN1_PCTX *pctx);
  28. int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
  29. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
  30. int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  31. ASN1_PCTX *pctx);
  32. int (*pkey_size) (const EVP_PKEY *pk);
  33. int (*pkey_bits) (const EVP_PKEY *pk);
  34. int (*pkey_security_bits) (const EVP_PKEY *pk);
  35. int (*param_decode) (EVP_PKEY *pkey,
  36. const unsigned char **pder, int derlen);
  37. int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  38. int (*param_missing) (const EVP_PKEY *pk);
  39. int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
  40. int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  41. int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  42. ASN1_PCTX *pctx);
  43. int (*sig_print) (BIO *out,
  44. const X509_ALGOR *sigalg, const ASN1_STRING *sig,
  45. int indent, ASN1_PCTX *pctx);
  46. void (*pkey_free) (EVP_PKEY *pkey);
  47. int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
  48. /* Legacy functions for old PEM */
  49. int (*old_priv_decode) (EVP_PKEY *pkey,
  50. const unsigned char **pder, int derlen);
  51. int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  52. /* Custom ASN1 signature verification */
  53. int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
  54. const X509_ALGOR *a, const ASN1_BIT_STRING *sig,
  55. EVP_PKEY *pkey);
  56. int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
  57. X509_ALGOR *alg1, X509_ALGOR *alg2,
  58. ASN1_BIT_STRING *sig);
  59. int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  60. const ASN1_STRING *sig);
  61. /* Check */
  62. int (*pkey_check) (const EVP_PKEY *pk);
  63. int (*pkey_public_check) (const EVP_PKEY *pk);
  64. int (*pkey_param_check) (const EVP_PKEY *pk);
  65. /* Get/set raw private/public key data */
  66. int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
  67. int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
  68. int (*get_priv_key) (const EVP_PKEY *pk, unsigned char *priv, size_t *len);
  69. int (*get_pub_key) (const EVP_PKEY *pk, unsigned char *pub, size_t *len);
  70. /* Exports and imports to / from providers */
  71. size_t (*dirty_cnt) (const EVP_PKEY *pk);
  72. int (*export_to) (const EVP_PKEY *pk, void *to_keydata,
  73. OSSL_FUNC_keymgmt_import_fn *importer,
  74. OSSL_LIB_CTX *libctx, const char *propq);
  75. OSSL_CALLBACK *import_from;
  76. int (*copy) (EVP_PKEY *to, EVP_PKEY *from);
  77. int (*priv_decode_ex) (EVP_PKEY *pk,
  78. const PKCS8_PRIV_KEY_INFO *p8inf,
  79. OSSL_LIB_CTX *libctx,
  80. const char *propq);
  81. } /* EVP_PKEY_ASN1_METHOD */ ;
  82. DEFINE_STACK_OF_CONST(EVP_PKEY_ASN1_METHOD)
  83. extern const EVP_PKEY_ASN1_METHOD ossl_dh_asn1_meth;
  84. extern const EVP_PKEY_ASN1_METHOD ossl_dhx_asn1_meth;
  85. extern const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5];
  86. extern const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth;
  87. extern const EVP_PKEY_ASN1_METHOD ossl_ecx25519_asn1_meth;
  88. extern const EVP_PKEY_ASN1_METHOD ossl_ecx448_asn1_meth;
  89. extern const EVP_PKEY_ASN1_METHOD ossl_ed25519_asn1_meth;
  90. extern const EVP_PKEY_ASN1_METHOD ossl_ed448_asn1_meth;
  91. extern const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth;
  92. extern const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2];
  93. extern const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth;
  94. /*
  95. * These are used internally in the ASN1_OBJECT to keep track of whether the
  96. * names and data need to be free()ed
  97. */
  98. # define ASN1_OBJECT_FLAG_DYNAMIC 0x01/* internal use */
  99. # define ASN1_OBJECT_FLAG_CRITICAL 0x02/* critical x509v3 object id */
  100. # define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04/* internal use */
  101. # define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08/* internal use */
  102. struct asn1_object_st {
  103. const char *sn, *ln;
  104. int nid;
  105. int length;
  106. const unsigned char *data; /* data remains const after init */
  107. int flags; /* Should we free this one */
  108. };
  109. /* ASN1 print context structure */
  110. struct asn1_pctx_st {
  111. unsigned long flags;
  112. unsigned long nm_flags;
  113. unsigned long cert_flags;
  114. unsigned long oid_flags;
  115. unsigned long str_flags;
  116. } /* ASN1_PCTX */ ;
  117. /* ASN1 type functions */
  118. int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
  119. unsigned char *data, int len);
  120. int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
  121. unsigned char *data, int max_len);
  122. int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md);
  123. const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg);
  124. X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg);
  125. int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md);
  126. int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags);
  127. EVP_PKEY *ossl_d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a,
  128. const unsigned char **pp, long length,
  129. OSSL_LIB_CTX *libctx, const char *propq);
  130. X509_ALGOR *ossl_X509_ALGOR_from_nid(int nid, int ptype, void *pval);
  131. time_t ossl_asn1_string_to_time_t(const char *asn1_string);
  132. void ossl_asn1_string_set_bits_left(ASN1_STRING *str, unsigned int num);
  133. #endif /* ndef OSSL_CRYPTO_ASN1_H */