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