asn1.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2015-2020 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. #include <openssl/asn1.h>
  10. /* Internal ASN1 structures and functions: not for application use */
  11. /* ASN1 public key method structure */
  12. #include <openssl/core.h>
  13. struct evp_pkey_asn1_method_st {
  14. int pkey_id;
  15. int pkey_base_id;
  16. unsigned long pkey_flags;
  17. char *pem_str;
  18. char *info;
  19. int (*pub_decode) (EVP_PKEY *pk, const X509_PUBKEY *pub);
  20. int (*pub_encode) (X509_PUBKEY *pub, const EVP_PKEY *pk);
  21. int (*pub_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  22. int (*pub_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  23. ASN1_PCTX *pctx);
  24. int (*priv_decode) (EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf);
  25. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
  26. int (*priv_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  27. ASN1_PCTX *pctx);
  28. int (*pkey_size) (const EVP_PKEY *pk);
  29. int (*pkey_bits) (const EVP_PKEY *pk);
  30. int (*pkey_security_bits) (const EVP_PKEY *pk);
  31. int (*param_decode) (EVP_PKEY *pkey,
  32. const unsigned char **pder, int derlen);
  33. int (*param_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  34. int (*param_missing) (const EVP_PKEY *pk);
  35. int (*param_copy) (EVP_PKEY *to, const EVP_PKEY *from);
  36. int (*param_cmp) (const EVP_PKEY *a, const EVP_PKEY *b);
  37. int (*param_print) (BIO *out, const EVP_PKEY *pkey, int indent,
  38. ASN1_PCTX *pctx);
  39. int (*sig_print) (BIO *out,
  40. const X509_ALGOR *sigalg, const ASN1_STRING *sig,
  41. int indent, ASN1_PCTX *pctx);
  42. void (*pkey_free) (EVP_PKEY *pkey);
  43. int (*pkey_ctrl) (EVP_PKEY *pkey, int op, long arg1, void *arg2);
  44. /* Legacy functions for old PEM */
  45. int (*old_priv_decode) (EVP_PKEY *pkey,
  46. const unsigned char **pder, int derlen);
  47. int (*old_priv_encode) (const EVP_PKEY *pkey, unsigned char **pder);
  48. /* Custom ASN1 signature verification */
  49. int (*item_verify) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
  50. const X509_ALGOR *a, const ASN1_BIT_STRING *sig,
  51. EVP_PKEY *pkey);
  52. int (*item_sign) (EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *data,
  53. X509_ALGOR *alg1, X509_ALGOR *alg2,
  54. ASN1_BIT_STRING *sig);
  55. int (*siginf_set) (X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  56. const ASN1_STRING *sig);
  57. /* Check */
  58. int (*pkey_check) (const EVP_PKEY *pk);
  59. int (*pkey_public_check) (const EVP_PKEY *pk);
  60. int (*pkey_param_check) (const EVP_PKEY *pk);
  61. /* Get/set raw private/public key data */
  62. int (*set_priv_key) (EVP_PKEY *pk, const unsigned char *priv, size_t len);
  63. int (*set_pub_key) (EVP_PKEY *pk, const unsigned char *pub, size_t len);
  64. int (*get_priv_key) (const EVP_PKEY *pk, unsigned char *priv, size_t *len);
  65. int (*get_pub_key) (const EVP_PKEY *pk, unsigned char *pub, size_t *len);
  66. /*
  67. * TODO: Make sure these functions are defined for key types that are
  68. * implemented in providers.
  69. */
  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. EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
  74. const char *propq);
  75. OSSL_CALLBACK *import_from;
  76. int (*priv_decode_ex) (EVP_PKEY *pk,
  77. const PKCS8_PRIV_KEY_INFO *p8inf,
  78. OSSL_LIB_CTX *libctx,
  79. const char *propq);
  80. } /* EVP_PKEY_ASN1_METHOD */ ;
  81. DEFINE_STACK_OF_CONST(EVP_PKEY_ASN1_METHOD)
  82. extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
  83. extern const EVP_PKEY_ASN1_METHOD dhx_asn1_meth;
  84. extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5];
  85. extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
  86. extern const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth;
  87. extern const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth;
  88. extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
  89. extern const EVP_PKEY_ASN1_METHOD ed448_asn1_meth;
  90. extern const EVP_PKEY_ASN1_METHOD sm2_asn1_meth;
  91. extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2];
  92. extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
  93. /*
  94. * These are used internally in the ASN1_OBJECT to keep track of whether the
  95. * names and data need to be free()ed
  96. */
  97. # define ASN1_OBJECT_FLAG_DYNAMIC 0x01/* internal use */
  98. # define ASN1_OBJECT_FLAG_CRITICAL 0x02/* critical x509v3 object id */
  99. # define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04/* internal use */
  100. # define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08/* internal use */
  101. struct asn1_object_st {
  102. const char *sn, *ln;
  103. int nid;
  104. int length;
  105. const unsigned char *data; /* data remains const after init */
  106. int flags; /* Should we free this one */
  107. };
  108. /* ASN1 print context structure */
  109. struct asn1_pctx_st {
  110. unsigned long flags;
  111. unsigned long nm_flags;
  112. unsigned long cert_flags;
  113. unsigned long oid_flags;
  114. unsigned long str_flags;
  115. } /* ASN1_PCTX */ ;
  116. /* ASN1 type functions */
  117. int asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
  118. unsigned char *data, int len);
  119. int asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
  120. unsigned char *data, int max_len);
  121. int x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md);
  122. const EVP_MD *x509_algor_get_md(X509_ALGOR *alg);
  123. X509_ALGOR *x509_algor_mgf1_decode(X509_ALGOR *alg);
  124. int x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md);