ASN1_item_sign.pod 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. =pod
  2. =head1 NAME
  3. ASN1_item_sign, ASN1_item_sign_ex, ASN1_item_sign_ctx,
  4. ASN1_item_verify, ASN1_item_verify_ex, ASN1_item_verify_ctx -
  5. ASN1 sign and verify
  6. =head1 SYNOPSIS
  7. #include <openssl/x509.h>
  8. int ASN1_item_sign_ex(const ASN1_ITEM *it, X509_ALGOR *algor1,
  9. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  10. const void *data, const ASN1_OCTET_STRING *id,
  11. EVP_PKEY *pkey, const EVP_MD *md, OSSL_LIB_CTX *libctx,
  12. const char *propq);
  13. int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
  14. ASN1_BIT_STRING *signature, const void *data,
  15. EVP_PKEY *pkey, const EVP_MD *md);
  16. int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
  17. X509_ALGOR *algor2, ASN1_BIT_STRING *signature,
  18. const void *data, EVP_MD_CTX *ctx);
  19. int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
  20. const ASN1_BIT_STRING *signature, const void *data,
  21. const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
  22. OSSL_LIB_CTX *libctx, const char *propq);
  23. int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
  24. const ASN1_BIT_STRING *signature, const void *data,
  25. EVP_PKEY *pkey);
  26. int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
  27. const ASN1_BIT_STRING *signature, const void *data,
  28. EVP_MD_CTX *ctx);
  29. =head1 DESCRIPTION
  30. ASN1_item_sign_ex() is used to sign arbitrary ASN1 data using a data object
  31. I<data>, the ASN.1 structure I<it>, private key I<pkey> and message digest I<md>.
  32. The data that is signed is formed by taking the data object in I<data> and
  33. converting it to der format using the ASN.1 structure I<it>.
  34. The I<data> that will be signed, and a structure containing the signature may
  35. both have a copy of the B<X509_ALGOR>. The ASN1_item_sign_ex() function will
  36. write the correct B<X509_ALGOR> to the structs based on the algorithms and
  37. parameters that have been set up. If one of I<algor1> or I<algor2> points to the
  38. B<X509_ALGOR> of the I<data> to be signed, then that B<X509_ALGOR> will first be
  39. written before the signature is generated.
  40. Examples of valid values that can be used by the ASN.1 structure I<it> are
  41. ASN1_ITEM_rptr(X509_CINF), ASN1_ITEM_rptr(X509_REQ_INFO) and
  42. ASN1_ITEM_rptr(X509_CRL_INFO).
  43. The B<OSSL_LIB_CTX> specified in I<libctx> and the property query string
  44. specified in I<props> are used when searching for algorithms in providers.
  45. The generated signature is set into I<signature>.
  46. The optional parameter I<id> can be NULL, but can be set for special key types.
  47. See EVP_PKEY_CTX_set1_id() for further info. The output parameters <algor1> and
  48. I<algor2> are ignored if they are NULL.
  49. ASN1_item_sign() is similar to ASN1_item_sign_ex() but uses default values of
  50. NULL for the I<id>, I<libctx> and I<propq>.
  51. ASN1_item_sign_ctx() is similar to ASN1_item_sign() but uses the parameters
  52. contained in digest context I<ctx>.
  53. ASN1_item_verify_ex() is used to verify the signature I<signature> of internal
  54. data I<data> using the public key I<pkey> and algorithm identifier I<alg>.
  55. The data that is verified is formed by taking the data object in I<data> and
  56. converting it to der format using the ASN.1 structure I<it>.
  57. The B<OSSL_LIB_CTX> specified in I<libctx> and the property query string
  58. specified in I<props> are used when searching for algorithms in providers.
  59. The optional parameter I<id> can be NULL, but can be set for special key types.
  60. See EVP_PKEY_CTX_set1_id() for further info.
  61. ASN1_item_verify() is similar to ASN1_item_verify_ex() but uses default values of
  62. NULL for the I<id>, I<libctx> and I<propq>.
  63. ASN1_item_verify_ctx() is similar to ASN1_item_verify() but uses the parameters
  64. contained in digest context I<ctx>.
  65. =head1 RETURN VALUES
  66. All sign functions return the size of the signature in bytes for success and
  67. zero for failure.
  68. All verify functions return 1 if the signature is valid and 0 if the signature
  69. check fails. If the signature could not be checked at all because it was
  70. ill-formed or some other error occurred then -1 is returned.
  71. =head1 EXAMPLES
  72. In the following example a 'MyObject' object is signed using the key contained
  73. in an EVP_MD_CTX. The signature is written to MyObject.signature. The object is
  74. then output in DER format and then loaded back in and verified.
  75. #include <openssl/x509.h>
  76. #include <openssl/asn1t.h>
  77. /* An object used to store the ASN1 data fields that will be signed */
  78. typedef struct MySignInfoObject_st
  79. {
  80. ASN1_INTEGER *version;
  81. X509_ALGOR sig_alg;
  82. } MySignInfoObject;
  83. DECLARE_ASN1_FUNCTIONS(MySignInfoObject)
  84. /*
  85. * A higher level object containing the ASN1 fields, signature alg and
  86. * output signature.
  87. */
  88. typedef struct MyObject_st
  89. {
  90. MySignInfoObject info;
  91. X509_ALGOR sig_alg;
  92. ASN1_BIT_STRING *signature;
  93. } MyObject;
  94. DECLARE_ASN1_FUNCTIONS(MyObject)
  95. /* The ASN1 definition of MySignInfoObject */
  96. ASN1_SEQUENCE_cb(MySignInfoObject, NULL) = {
  97. ASN1_SIMPLE(MySignInfoObject, version, ASN1_INTEGER)
  98. ASN1_EMBED(MySignInfoObject, sig_alg, X509_ALGOR),
  99. } ASN1_SEQUENCE_END_cb(MySignInfoObject, MySignInfoObject)
  100. /* new, free, d2i & i2d functions for MySignInfoObject */
  101. IMPLEMENT_ASN1_FUNCTIONS(MySignInfoObject)
  102. /* The ASN1 definition of MyObject */
  103. ASN1_SEQUENCE_cb(MyObject, NULL) = {
  104. ASN1_EMBED(MyObject, info, MySignInfoObject),
  105. ASN1_EMBED(MyObject, sig_alg, X509_ALGOR),
  106. ASN1_SIMPLE(MyObject, signature, ASN1_BIT_STRING)
  107. } ASN1_SEQUENCE_END_cb(MyObject, MyObject)
  108. /* new, free, d2i & i2d functions for MyObject */
  109. IMPLEMENT_ASN1_FUNCTIONS(MyObject)
  110. int test_asn1_item_sign_verify(const char *mdname, EVP_PKEY *pkey, long version)
  111. {
  112. int ret = 0;
  113. unsigned char *obj_der = NULL;
  114. const unsigned char *p = NULL;
  115. MyObject *obj = NULL, *loaded_obj = NULL;
  116. const ASN1_ITEM *it = ASN1_ITEM_rptr(MySignInfoObject);
  117. EVP_MD_CTX *sctx = NULL, *vctx = NULL;
  118. int len;
  119. /* Create MyObject and set its version */
  120. obj = MyObject_new();
  121. if (obj == NULL)
  122. goto err;
  123. if (!ASN1_INTEGER_set(obj->info.version, version))
  124. goto err;
  125. /* Set the key and digest used for signing */
  126. sctx = EVP_MD_CTX_new();
  127. if (sctx == NULL
  128. || !EVP_DigestSignInit_ex(sctx, NULL, mdname, NULL, NULL, pkey))
  129. goto err;
  130. /*
  131. * it contains the mapping between ASN.1 data and an object MySignInfoObject
  132. * obj->info is the 'MySignInfoObject' object that will be
  133. * converted into DER data and then signed.
  134. * obj->signature will contain the output signature.
  135. * obj->sig_alg is filled with the private key's signing algorithm id.
  136. * obj->info.sig_alg is another copy of the signing algorithm id that sits
  137. * within MyObject.
  138. */
  139. len = ASN1_item_sign_ctx(it, &obj->sig_alg, &obj->info.sig_alg,
  140. obj->signature, &obj->info, sctx);
  141. if (len <= 0
  142. || X509_ALGOR_cmp(&obj->sig_alg, &obj->info.sig_alg) != 0)
  143. goto err;
  144. /* Output MyObject in der form */
  145. len = i2d_MyObject(obj, &obj_der);
  146. if (len <= 0)
  147. goto err;
  148. /* Set the key and digest used for verifying */
  149. vctx = EVP_MD_CTX_new();
  150. if (vctx == NULL
  151. || !EVP_DigestVerifyInit_ex(vctx, NULL, mdname, NULL, NULL, pkey))
  152. goto err;
  153. /* Load the der data back into an object */
  154. p = obj_der;
  155. loaded_obj = d2i_MyObject(NULL, &p, len);
  156. if (loaded_obj == NULL)
  157. goto err;
  158. /* Verify the loaded object */
  159. ret = ASN1_item_verify_ctx(it, &loaded_obj->sig_alg, loaded_obj->signature,
  160. &loaded_obj->info, vctx);
  161. err:
  162. OPENSSL_free(obj_der);
  163. MyObject_free(loaded_obj);
  164. MyObject_free(obj);
  165. EVP_MD_CTX_free(sctx);
  166. EVP_MD_CTX_free(vctx);
  167. return ret;
  168. }
  169. =head1 SEE ALSO
  170. L<X509_sign(3)>,
  171. L<X509_verify(3)>
  172. =head1 HISTORY
  173. ASN1_item_sign_ex() and ASN1_item_verify_ex() were added in OpenSSL 3.0.
  174. =head1 COPYRIGHT
  175. Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
  176. Licensed under the Apache License 2.0 (the "License"). You may not use
  177. this file except in compliance with the License. You can obtain a copy
  178. in the file LICENSE in the source distribution or at
  179. L<https://www.openssl.org/source/license.html>.
  180. =cut