1 /** 2 * Port of `openssl.rsa.h` 3 * 4 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. 5 * 6 * Licensed under the Apache License 2.0 (the "License"). You may not use 7 * this file except in compliance with the License. You can obtain a copy 8 * in the file LICENSE in the source distribution or at 9 * https://www.openssl.org/source/license.html 10 */ 11 module deimos.openssl.rsa; 12 13 import deimos.openssl._d_util; 14 import deimos.openssl.opensslv; 15 16 import deimos.openssl.evp; // Needed for EVP_PKEY_ALG_CTRL. 17 18 public import deimos.openssl.asn1; 19 20 version(OPENSSL_NO_BIO) {} else { 21 public import deimos.openssl.bio; 22 } 23 public import deimos.openssl.crypto; 24 public import deimos.openssl.types; 25 version(OPENSSL_NO_DEPRECATED) {} else { 26 public import deimos.openssl.bn; 27 } 28 29 version (OPENSSL_NO_RSA) { 30 static assert(false, "RSA is disabled."); 31 } 32 33 extern (C): 34 nothrow: 35 36 // The following aliases are derived from the `RSA_meth_*` functions' signatures 37 // They are not present in the code, hence are `private`. 38 private alias RSA_enc_dec_fn = extern(C) int function(int flen, 39 const(ubyte)* from, ubyte* to, RSA* rsa, int padding); 40 private alias RSA_modexp_fn = extern(C) int function(BIGNUM* r0, 41 const(BIGNUM)* I, RSA* rsa, BN_CTX* ctx); 42 private alias RSA_bn_modexp_fn = extern(C) int function(BIGNUM* r, 43 const(BIGNUM)* a, const(BIGNUM)* p, const(BIGNUM)* m, BN_CTX* ctx, 44 BN_MONT_CTX* m_ctx); 45 private alias RSA_lifetime_fn = extern(C) int function(RSA* rsa); 46 private alias RSA_sign_fn = extern(C) int function(int type, 47 const(ubyte)* m, uint m_length, ubyte* sigret, uint* siglen, const(RSA)* rsa); 48 private alias RSA_verify_fn = extern(C) int function(int dtype, 49 const(ubyte)* m, uint m_length, const(ubyte)* sigret, uint* siglen, 50 const(RSA)* rsa); 51 private alias RSA_keygen_fn = extern(C) int function(RSA* rsa, 52 int bits, BIGNUM* e, BN_GENCB* cb); 53 54 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 55 { 56 // https://github.com/openssl/openssl/commit/b72c9121379a5de0c8be0d4e1a4a6b9495042621 57 58 RSA_METHOD* RSA_meth_new(const(char)* name, int flags); 59 void RSA_meth_free(RSA_METHOD* meth); 60 RSA_METHOD* RSA_meth_dup(const(RSA_METHOD)* meth); 61 62 const(char)* RSA_meth_get0_name(const(RSA_METHOD)* meth); 63 int RSA_meth_set1_name(RSA_METHOD* meth, const(char)* name); 64 65 int RSA_meth_get_flags(RSA_METHOD* meth); 66 int RSA_meth_set_flags(RSA_METHOD* meth, int flags); 67 void* RSA_meth_get0_app_data(const(RSA_METHOD)* meth); 68 int RSA_meth_set0_app_data(RSA_METHOD* meth, void *app_data); 69 70 RSA_enc_dec_fn RSA_meth_get_pub_enc(const(RSA_METHOD)* meth); 71 int RSA_meth_set_pub_enc(RSA_METHOD* rsa, RSA_enc_dec_fn pub_enc); 72 RSA_enc_dec_fn RSA_meth_get_pub_dec(const(RSA_METHOD)* meth); 73 int RSA_meth_set_pub_dec(RSA_METHOD* rsa, RSA_enc_dec_fn pub_dec); 74 75 RSA_enc_dec_fn RSA_meth_get_priv_enc(const(RSA_METHOD)* meth); 76 int RSA_meth_set_priv_enc(RSA_METHOD* rsa, RSA_enc_dec_fn priv_enc); 77 RSA_enc_dec_fn RSA_meth_get_priv_dec(const(RSA_METHOD)* meth); 78 int RSA_meth_set_priv_dec(RSA_METHOD* rsa, RSA_enc_dec_fn priv_dec); 79 80 RSA_modexp_fn RSA_meth_get_mod_exp(const(RSA_METHOD)* meth); 81 int RSA_meth_set_mod_exp(RSA_METHOD* rsa, RSA_modexp_fn mod_exp); 82 83 RSA_bn_modexp_fn RSA_meth_get_bn_mod_exp(const(RSA_METHOD)* meth); 84 int RSA_meth_set_bn_mod_exp(RSA_METHOD* rsa, RSA_bn_modexp_fn bn_mod_exp); 85 86 RSA_lifetime_fn RSA_meth_get_init(const(RSA_METHOD)* meth); 87 int RSA_meth_set_init(RSA_METHOD* rsa, RSA_lifetime_fn init); 88 RSA_lifetime_fn RSA_meth_get_finish(const(RSA_METHOD)* meth); 89 int RSA_meth_set_finish(RSA_METHOD* rsa, RSA_lifetime_fn finish); 90 91 RSA_sign_fn RSA_meth_get_sign(const(RSA_METHOD)* meth); 92 int RSA_meth_set_sign(RSA_METHOD* rsa, RSA_sign_fn sign); 93 94 RSA_verify_fn RSA_meth_get_verify(const(RSA_METHOD)* meth); 95 int RSA_meth_set_verify(RSA_METHOD* rsa, RSA_verify_fn verify); 96 97 RSA_keygen_fn RSA_meth_get_keygen(const(RSA_METHOD)* meth); 98 int RSA_meth_set_keygen(RSA_METHOD* rsa, RSA_keygen_fn keygen); 99 } 100 else 101 { 102 struct rsa_meth_st 103 { 104 const(char)* name; 105 RSA_enc_dec_fn rsa_pub_enc; 106 RSA_enc_dec_fn rsa_pub_dec; 107 RSA_enc_dec_fn rsa_priv_enc; 108 RSA_enc_dec_fn rsa_priv_dec; 109 RSA_modexp_fn rsa_mod_exp; /* Can be null */ 110 RSA_bn_modexp_fn bn_mod_exp; /* Can be null */ 111 RSA_lifetime_fn init_; /* called at new */ 112 RSA_lifetime_fn finish; /* called at free */ 113 int flags; /* RSA_METHOD_FLAG_* things */ 114 char* app_data; /* may be needed! */ 115 /* New sign and verify functions: some libraries don't allow arbitrary data 116 * to be signed/verified: this allows them to be used. Note: for this to work 117 * the RSA_public_decrypt() and RSA_private_encrypt() should* NOT* be used 118 * RSA_sign(), RSA_verify() should be used instead. Note: for backwards 119 * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER 120 * option is set in 'flags'. 121 */ 122 RSA_sign_fn rsa_sign; 123 RSA_verify_fn rsa_verify; 124 /* If this callback is NULL, the builtin software RSA key-gen will be used. This 125 * is for behavioural compatibility whilst the code gets rewired, but one day 126 * it would be nice to assume there are no such things as "builtin software" 127 * implementations. */ 128 RSA_keygen_fn rsa_keygen; 129 } 130 } 131 132 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 133 { 134 // https://github.com/openssl/openssl/commit/9862e9aa98ee1e38fbcef8d1dd5db0e750eb5e8d 135 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d); 136 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q); 137 int RSA_set0_crt_params(RSA *r,BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp); 138 void RSA_get0_key(const RSA *r, BIGNUM **n, BIGNUM **e, BIGNUM **d); 139 void RSA_get0_factors(const RSA *r, BIGNUM **p, BIGNUM **q); 140 void RSA_get0_crt_params(const RSA *r, 141 BIGNUM **dmp1, BIGNUM **dmq1, BIGNUM **iqmp); 142 void RSA_clear_flags(RSA *r, int flags); 143 int RSA_test_flags(const RSA *r, int flags); 144 void RSA_set_flags(RSA *r, int flags); 145 ENGINE *RSA_get0_engine(RSA *r); 146 } 147 else 148 { 149 struct rsa_st 150 { 151 /* The first parameter is used to pickup errors where 152 * this is passed instead of aEVP_PKEY, it is set to 0 */ 153 int pad; 154 c_long version_; 155 const(RSA_METHOD)* meth; 156 /* functional reference if 'meth' is ENGINE-provided */ 157 ENGINE* engine; 158 BIGNUM* n; 159 BIGNUM* e; 160 BIGNUM* d; 161 BIGNUM* p; 162 BIGNUM* q; 163 BIGNUM* dmp1; 164 BIGNUM* dmq1; 165 BIGNUM* iqmp; 166 /* be careful using this if the RSA structure is shared */ 167 CRYPTO_EX_DATA ex_data; 168 int references; 169 int flags; 170 171 /* Used to cache montgomery values */ 172 BN_MONT_CTX* _method_mod_n; 173 BN_MONT_CTX* _method_mod_p; 174 BN_MONT_CTX* _method_mod_q; 175 176 /* all BIGNUM values are actually in the following data, if it is not 177 * NULL */ 178 char* bignum_data; 179 BN_BLINDING* blinding; 180 BN_BLINDING* mt_blinding; 181 } 182 } 183 184 // #ifndef OPENSSL_RSA_MAX_MODULUS_BITS 185 enum OPENSSL_RSA_MAX_MODULUS_BITS = 16384; 186 // #endif 187 188 // #ifndef OPENSSL_RSA_SMALL_MODULUS_BITS 189 enum OPENSSL_RSA_SMALL_MODULUS_BITS = 3072; 190 // #endif 191 // #ifndef OPENSSL_RSA_MAX_PUBEXP_BITS 192 enum OPENSSL_RSA_MAX_PUBEXP_BITS = 64; /* exponent limit enforced for "large" modulus only */ 193 // #endif 194 195 enum RSA_3 = 0x3; 196 enum RSA_F4 = 0x10001; 197 198 enum RSA_METHOD_FLAG_NO_CHECK = 0x0001; /* don't check pub/private match */ 199 200 enum RSA_FLAG_CACHE_PUBLIC = 0x0002; 201 enum RSA_FLAG_CACHE_PRIVATE = 0x0004; 202 enum RSA_FLAG_BLINDING = 0x0008; 203 enum RSA_FLAG_THREAD_SAFE = 0x0010; 204 /* This flag means the private key operations will be handled by rsa_mod_exp 205 * and that they do not depend on the private key components being present: 206 * for example a key stored in external hardware. Without this flag bn_mod_exp 207 * gets called when private key components are absent. 208 */ 209 enum RSA_FLAG_EXT_PKEY = 0x0020; 210 211 /* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions. 212 */ 213 enum RSA_FLAG_SIGN_VER = 0x0040; 214 215 enum RSA_FLAG_NO_BLINDING = 0x0080; /* new with 0.9.6j and 0.9.7b; the built-in 216 * RSA implementation now uses blinding by 217 * default (ignoring RSA_FLAG_BLINDING), 218 * but other engines might not need it 219 */ 220 enum RSA_FLAG_NO_CONSTTIME = 0x0100; /* new with 0.9.8f; the built-in RSA 221 * implementation now uses constant time 222 * operations by default in private key operations, 223 * e.g., constant time modular exponentiation, 224 * modular inverse without leaking branches, 225 * division without leaking branches. This 226 * flag disables these constant time 227 * operations and results in faster RSA 228 * private key operations. 229 */ 230 version(OPENSSL_NO_DEPRECATED) {} else { 231 alias RSA_FLAG_NO_CONSTTIME RSA_FLAG_NO_EXP_CONSTTIME; /* deprecated name for the flag*/ 232 /* new with 0.9.7h; the built-in RSA 233 * implementation now uses constant time 234 * modular exponentiation for secret exponents 235 * by default. This flag causes the 236 * faster variable sliding window method to 237 * be used for all exponents. 238 */ 239 } 240 241 242 auto EVP_PKEY_CTX_set_rsa_padding()(EVP_PKEY_CTX* ctx, int pad) { 243 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING, 244 pad, null); 245 } 246 247 auto EVP_PKEY_CTX_get_rsa_padding()(EVP_PKEY_CTX* ctx, int *ppad) { 248 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, 249 EVP_PKEY_CTRL_GET_RSA_PADDING, 0, ppad); 250 } 251 252 auto EVP_PKEY_CTX_set_rsa_pss_saltlen()(EVP_PKEY_CTX* ctx, int len) { 253 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, 254 (EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY), 255 EVP_PKEY_CTRL_RSA_PSS_SALTLEN, 256 len, null); 257 } 258 259 auto EVP_PKEY_CTX_get_rsa_pss_saltlen()(EVP_PKEY_CTX* ctx, int *plen) { 260 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, 261 (EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY), 262 EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 263 0, plen); 264 } 265 266 static if (OPENSSL_VERSION_AT_LEAST(3, 0, 0)) 267 { 268 // v3.0.0 deprecated `EVP_PKEY_CTX_set_rsa_keygen_pubexp` and introduced 269 // a `[...]set1[...]` alternative: 270 // https://github.com/openssl/openssl/commit/3786d74868fe440250f902ce1a78974136ca9304 271 // This is for forward compatibility: Old code still works with new OpenSSL version 272 alias EVP_PKEY_CTX_set_rsa_keygen_pubexp = EVP_PKEY_CTX_set1_rsa_keygen_pubexp; 273 274 // Before v3.0.0, those functions were macros (including above deprecated one): 275 // https://github.com/openssl/openssl/commit/2972af109e10c5ce30e548190e3eee28327d6043 276 int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX* ctx, int bits); 277 int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX* ctx, void* pubexp); 278 int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX* ctx, int primes); 279 } 280 else 281 { 282 // Forward compatibility alias: Code written for v3.0.0 works with v1.1.1 and below 283 alias EVP_PKEY_CTX_set1_rsa_keygen_pubexp = EVP_PKEY_CTX_set_rsa_keygen_pubexp; 284 285 auto EVP_PKEY_CTX_set_rsa_keygen_bits()(EVP_PKEY_CTX* ctx, int bits) { 286 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, 287 EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, null); 288 } 289 290 auto EVP_PKEY_CTX_set_rsa_keygen_pubexp()(EVP_PKEY_CTX* ctx, void* pubexp) { 291 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, 292 EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp); 293 } 294 295 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 1)) 296 { 297 // Multi-prime RSA (RFC 8017), introduced in v1.1.1: 298 // https://github.com/openssl/openssl/commit/665d899fa6d3571da016925067ebcf1789d7d19c 299 auto EVP_PKEY_CTX_set_rsa_keygen_primes()(EVP_PKEY_CTX* ctx, int primes) { 300 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, 301 EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes, null); 302 } 303 } 304 } 305 306 auto EVP_PKEY_CTX_set_rsa_mgf1_md()(EVP_PKEY_CTX* ctx, EVP_MD* md) { 307 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 308 enum ExtraFlags = EVP_PKEY_OP_TYPE_CRYPT; 309 else 310 enum ExtraFlags = 0; 311 312 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_SIG | ExtraFlags, 313 EVP_PKEY_CTRL_RSA_MGF1_MD, 0, md); 314 } 315 316 auto EVP_PKEY_CTX_get_rsa_mgf1_md()(EVP_PKEY_CTX* ctx, EVP_MD** pmd) { 317 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 318 enum ExtraFlags = EVP_PKEY_OP_TYPE_CRYPT; 319 else 320 enum ExtraFlags = 0; 321 322 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_SIG | ExtraFlags, 323 EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, pmd); 324 } 325 326 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 327 { 328 auto EVP_PKEY_CTX_set_rsa_oaep_md()(EVP_PKEY_CTX* ctx, EVP_MD* md) { 329 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, 330 EVP_PKEY_CTRL_RSA_OAEP_MD, 0, md); 331 } 332 333 auto EVP_PKEY_CTX_set0_rsa_oaep_label()(EVP_PKEY_CTX* ctx, ubyte* label, int len) { 334 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, 335 EVP_PKEY_CTRL_RSA_OAEP_LABEL, len, label); 336 } 337 338 auto EVP_PKEY_CTX_get_rsa_oaep_md () (EVP_PKEY_CTX* ctx, EVP_MD** pmd) 339 { 340 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, 341 EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, pmd); 342 } 343 344 auto EVP_PKEY_CTX_get0_rsa_oaep_label () (EVP_PKEY_CTX* ctx, ubyte** label) 345 { 346 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, 347 EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, label); 348 } 349 } 350 351 352 enum EVP_PKEY_CTRL_RSA_PADDING = (EVP_PKEY_ALG_CTRL + 1); 353 enum EVP_PKEY_CTRL_RSA_PSS_SALTLEN = (EVP_PKEY_ALG_CTRL + 2); 354 355 enum EVP_PKEY_CTRL_RSA_KEYGEN_BITS = (EVP_PKEY_ALG_CTRL + 3); 356 enum EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP = (EVP_PKEY_ALG_CTRL + 4); 357 enum EVP_PKEY_CTRL_RSA_MGF1_MD = (EVP_PKEY_ALG_CTRL + 5); 358 359 enum EVP_PKEY_CTRL_GET_RSA_PADDING = (EVP_PKEY_ALG_CTRL + 6); 360 enum EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN = (EVP_PKEY_ALG_CTRL + 7); 361 enum EVP_PKEY_CTRL_GET_RSA_MGF1_MD = (EVP_PKEY_ALG_CTRL + 8); 362 363 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 364 { 365 enum EVP_PKEY_CTRL_RSA_OAEP_MD = (EVP_PKEY_ALG_CTRL + 9); 366 enum EVP_PKEY_CTRL_RSA_OAEP_LABEL = (EVP_PKEY_ALG_CTRL + 10); 367 enum EVP_PKEY_CTRL_GET_RSA_OAEP_MD = (EVP_PKEY_ALG_CTRL + 11); 368 enum EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL = (EVP_PKEY_ALG_CTRL + 12); 369 } 370 371 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 1)) 372 enum EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES = (EVP_PKEY_ALG_CTRL + 13); 373 374 enum RSA_PKCS1_PADDING = 1; 375 enum RSA_SSLV23_PADDING = 2; 376 enum RSA_NO_PADDING = 3; 377 enum RSA_PKCS1_OAEP_PADDING = 4; 378 enum RSA_X931_PADDING = 5; 379 /* EVP_PKEY_ only */ 380 enum RSA_PKCS1_PSS_PADDING = 6; 381 382 enum RSA_PKCS1_PADDING_SIZE = 11; 383 384 int RSA_set_app_data()(RSA* s, void* arg) { return RSA_set_ex_data(s,0,arg); } 385 void* RSA_get_app_data()(const(RSA)* s) { return RSA_get_ex_data(s,0); } 386 387 RSA* RSA_new(); 388 RSA* RSA_new_method(ENGINE* engine); 389 int RSA_size(const(RSA)* rsa); 390 391 /* Deprecated version */ 392 version(OPENSSL_NO_DEPRECATED) {} else { 393 RSA* RSA_generate_key(int bits, c_ulong e,ExternC!(void 394 function(int,int,void*)) callback,void* cb_arg); 395 } /* !defined(OPENSSL_NO_DEPRECATED) */ 396 397 /* New version */ 398 int RSA_generate_key_ex(RSA* rsa, int bits, BIGNUM* e, BN_GENCB* cb); 399 400 int RSA_check_key(const(RSA)*); 401 /* next 4 return -1 on error */ 402 int RSA_public_encrypt(int flen, const(ubyte)* from, 403 ubyte* to, RSA* rsa,int padding); 404 int RSA_private_encrypt(int flen, const(ubyte)* from, 405 ubyte* to, RSA* rsa,int padding); 406 int RSA_public_decrypt(int flen, const(ubyte)* from, 407 ubyte* to, RSA* rsa,int padding); 408 int RSA_private_decrypt(int flen, const(ubyte)* from, 409 ubyte* to, RSA* rsa,int padding); 410 void RSA_free (RSA* r); 411 /* "up" the RSA object's reference count */ 412 int RSA_up_ref(RSA* r); 413 414 int RSA_flags(const(RSA)* r); 415 416 void RSA_set_default_method(const(RSA_METHOD)* meth); 417 const(RSA_METHOD)* RSA_get_default_method(); 418 const(RSA_METHOD)* RSA_get_method(const(RSA)* rsa); 419 int RSA_set_method(RSA* rsa, const(RSA_METHOD)* meth); 420 421 /* This function needs the memory locking malloc callbacks to be installed */ 422 int RSA_memory_lock(RSA* r); 423 424 /* these are the actual SSLeay RSA functions */ 425 const(RSA_METHOD)* RSA_PKCS1_SSLeay(); 426 427 const(RSA_METHOD)* RSA_null_method(); 428 429 mixin(DECLARE_ASN1_ENCODE_FUNCTIONS_const!("RSA", "RSAPublicKey")); 430 mixin(DECLARE_ASN1_ENCODE_FUNCTIONS_const!("RSA", "RSAPrivateKey")); 431 432 struct rsa_pss_params_st 433 { 434 X509_ALGOR *hashAlgorithm; 435 X509_ALGOR *maskGenAlgorithm; 436 ASN1_INTEGER *saltLength; 437 ASN1_INTEGER *trailerField; 438 } 439 alias rsa_pss_params_st RSA_PSS_PARAMS; 440 441 mixin(DECLARE_ASN1_FUNCTIONS!"RSA_PSS_PARAMS"); 442 443 version(OPENSSL_NO_FP_API) {} else { 444 int RSA_print_fp(FILE* fp, const(RSA)* r,int offset); 445 } 446 447 version(OPENSSL_NO_BIO) {} else { 448 int RSA_print(BIO* bp, const(RSA)* r,int offset); 449 } 450 451 version(OPENSSL_NO_RC4) {} else { 452 int i2d_RSA_NET(const(RSA)* a, ubyte** pp, 453 ExternC!(int function(char* buf, int len, const(char)* prompt, int verify)) cb, 454 int sgckey); 455 RSA* d2i_RSA_NET(RSA** a, const(ubyte)** pp, c_long length, 456 ExternC!(int function(char* buf, int len, const(char)* prompt, int verify)) cb, 457 int sgckey); 458 459 int i2d_Netscape_RSA(const(RSA)* a, ubyte** pp, 460 ExternC!(int function(char* buf, int len, const(char)* prompt, 461 int verify)) cb); 462 RSA* d2i_Netscape_RSA(RSA** a, const(ubyte)** pp, c_long length, 463 ExternC!(int function(char* buf, int len, const(char)* prompt, 464 int verify)) cb); 465 } 466 467 /* The following 2 functions sign and verify a X509_SIG ASN1 object 468 * inside PKCS#1 padded RSA encryption */ 469 int RSA_sign(int type, const(ubyte)* m, uint m_length, 470 ubyte* sigret, uint* siglen, RSA* rsa); 471 int RSA_verify(int type, const(ubyte)* m, uint m_length, 472 const(ubyte)* sigbuf, uint siglen, RSA* rsa); 473 474 /* The following 2 function sign and verify a ASN1_OCTET_STRING 475 * object inside PKCS#1 padded RSA encryption */ 476 int RSA_sign_ASN1_OCTET_STRING(int type, 477 const(ubyte)* m, uint m_length, 478 ubyte* sigret, uint* siglen, RSA* rsa); 479 int RSA_verify_ASN1_OCTET_STRING(int type, 480 const(ubyte)* m, uint m_length, 481 ubyte* sigbuf, uint siglen, RSA* rsa); 482 483 int RSA_blinding_on(RSA* rsa, BN_CTX* ctx); 484 void RSA_blinding_off(RSA* rsa); 485 BN_BLINDING* RSA_setup_blinding(RSA* rsa, BN_CTX* ctx); 486 487 int RSA_padding_add_PKCS1_type_1(ubyte* to,int tlen, 488 const(ubyte)* f,int fl); 489 int RSA_padding_check_PKCS1_type_1(ubyte* to,int tlen, 490 const(ubyte)* f,int fl,int rsa_len); 491 int RSA_padding_add_PKCS1_type_2(ubyte* to,int tlen, 492 const(ubyte)* f,int fl); 493 int RSA_padding_check_PKCS1_type_2(ubyte* to,int tlen, 494 const(ubyte)* f,int fl,int rsa_len); 495 int PKCS1_MGF1(ubyte* mask, c_long len, 496 const(ubyte)* seed, c_long seedlen, const(EVP_MD)* dgst); 497 int RSA_padding_add_PKCS1_OAEP(ubyte* to,int tlen, 498 const(ubyte)* f,int fl, 499 const(ubyte)* p,int pl); 500 int RSA_padding_check_PKCS1_OAEP(ubyte* to,int tlen, 501 const(ubyte)* f,int fl,int rsa_len, 502 const(ubyte)* p,int pl); 503 static if (OPENSSL_VERSION_AT_LEAST(1, 1, 0)) 504 { 505 int RSA_padding_add_PKCS1_OAEP_mgf1(ubyte* to, int tlen, 506 const(ubyte)* from, int flen, 507 const(ubyte)* param, int plen, 508 const(EVP_MD)* md, const(EVP_MD)* mgf1md); 509 int RSA_padding_check_PKCS1_OAEP_mgf1(ubyte* to, int tlen, 510 const(ubyte)* from, int flen, int num, 511 const(ubyte)* param, int plen, 512 const(EVP_MD)* md, const(EVP_MD)* mgf1md); 513 } 514 int RSA_padding_add_SSLv23(ubyte* to,int tlen, 515 const(ubyte)* f,int fl); 516 int RSA_padding_check_SSLv23(ubyte* to,int tlen, 517 const(ubyte)* f,int fl,int rsa_len); 518 int RSA_padding_add_none(ubyte* to,int tlen, 519 const(ubyte)* f,int fl); 520 int RSA_padding_check_none(ubyte* to,int tlen, 521 const(ubyte)* f,int fl,int rsa_len); 522 int RSA_padding_add_X931(ubyte* to,int tlen, 523 const(ubyte)* f,int fl); 524 int RSA_padding_check_X931(ubyte* to,int tlen, 525 const(ubyte)* f,int fl,int rsa_len); 526 int RSA_X931_hash_id(int nid); 527 528 int RSA_verify_PKCS1_PSS(RSA* rsa, const(ubyte)* mHash, 529 const(EVP_MD)* Hash, const(ubyte)* EM, int sLen); 530 int RSA_padding_add_PKCS1_PSS(RSA* rsa, ubyte* EM, 531 const(ubyte)* mHash, 532 const(EVP_MD)* Hash, int sLen); 533 534 int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const(ubyte)* mHash, 535 const(EVP_MD)* Hash, const(EVP_MD)* mgf1Hash, 536 const(ubyte)* EM, int sLen); 537 538 int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, ubyte* EM, 539 const(ubyte)* mHash, 540 const(EVP_MD)* Hash, const(EVP_MD)* mgf1Hash, int sLen); 541 542 static if (OPENSSL_VERSION_BEFORE(1, 1, 0)) 543 { 544 int RSA_get_ex_new_index(c_long argl, void* argp, CRYPTO_EX_new* new_func, 545 CRYPTO_EX_dup* dup_func, CRYPTO_EX_free* free_func); 546 } 547 else 548 { 549 auto RSA_get_ex_new_index () (c_long l, void* p, CRYPTO_EX_new* newf, 550 CRYPTO_EX_dup* dupf, CRYPTO_EX_free* freef) 551 { 552 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, l, p, newf, dupf, freef); 553 } 554 } 555 int RSA_set_ex_data(RSA* r,int idx,void* arg); 556 void* RSA_get_ex_data(const(RSA)* r, int idx); 557 558 RSA* RSAPublicKey_dup(RSA* rsa); 559 RSA* RSAPrivateKey_dup(RSA* rsa); 560 561 /* If this flag is set the RSA method is FIPS compliant and can be used 562 * in FIPS mode. This is set in the validated module method. If an 563 * application sets this flag in its own methods it is its responsibility 564 * to ensure the result is compliant. 565 */ 566 567 enum RSA_FLAG_FIPS_METHOD = 0x0400; 568 569 /* If this flag is set the operations normally disabled in FIPS mode are 570 * permitted it is then the applications responsibility to ensure that the 571 * usage is compliant. 572 */ 573 574 enum RSA_FLAG_NON_FIPS_ALLOW = 0x0400; 575 /* Application has decided PRNG is good enough to generate a key: don't 576 * check. 577 */ 578 enum RSA_FLAG_CHECKED = 0x0800; 579 580 /* BEGIN ERROR CODES */ 581 /* The following lines are auto generated by the script mkerr.pl. Any changes 582 * made after this point may be overwritten when the script is next run. 583 */ 584 void ERR_load_RSA_strings(); 585 586 /* Error codes for the RSA functions. */ 587 588 /* Function codes. */ 589 enum RSA_F_CHECK_PADDING_MD = 140; 590 enum RSA_F_DO_RSA_PRINT = 146; 591 enum RSA_F_INT_RSA_VERIFY = 145; 592 enum RSA_F_MEMORY_LOCK = 100; 593 enum RSA_F_OLD_RSA_PRIV_DECODE = 147; 594 enum RSA_F_PKEY_RSA_CTRL = 143; 595 enum RSA_F_PKEY_RSA_CTRL_STR = 144; 596 enum RSA_F_PKEY_RSA_SIGN = 142; 597 enum RSA_F_PKEY_RSA_VERIFY = 154; 598 enum RSA_F_PKEY_RSA_VERIFYRECOVER = 141; 599 enum RSA_F_RSA_BUILTIN_KEYGEN = 129; 600 enum RSA_F_RSA_CHECK_KEY = 123; 601 enum RSA_F_RSA_EAY_PRIVATE_DECRYPT = 101; 602 enum RSA_F_RSA_EAY_PRIVATE_ENCRYPT = 102; 603 enum RSA_F_RSA_EAY_PUBLIC_DECRYPT = 103; 604 enum RSA_F_RSA_EAY_PUBLIC_ENCRYPT = 104; 605 enum RSA_F_RSA_GENERATE_KEY = 105; 606 enum RSA_F_RSA_GENERATE_KEY_EX = 155; 607 enum RSA_F_RSA_ITEM_VERIFY = 156; 608 enum RSA_F_RSA_MEMORY_LOCK = 130; 609 enum RSA_F_RSA_NEW_METHOD = 106; 610 enum RSA_F_RSA_NULL = 124; 611 enum RSA_F_RSA_NULL_MOD_EXP = 131; 612 enum RSA_F_RSA_NULL_PRIVATE_DECRYPT = 132; 613 enum RSA_F_RSA_NULL_PRIVATE_ENCRYPT = 133; 614 enum RSA_F_RSA_NULL_PUBLIC_DECRYPT = 134; 615 enum RSA_F_RSA_NULL_PUBLIC_ENCRYPT = 135; 616 enum RSA_F_RSA_PADDING_ADD_NONE = 107; 617 enum RSA_F_RSA_PADDING_ADD_PKCS1_OAEP = 121; 618 enum RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1 = 154; 619 enum RSA_F_RSA_PADDING_ADD_PKCS1_PSS = 125; 620 enum RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1 = 148; 621 enum RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 = 108; 622 enum RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 = 109; 623 enum RSA_F_RSA_PADDING_ADD_SSLV23 = 110; 624 enum RSA_F_RSA_PADDING_ADD_X931 = 127; 625 enum RSA_F_RSA_PADDING_CHECK_NONE = 111; 626 enum RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP = 122; 627 enum RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1 = 153; 628 enum RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 = 112; 629 enum RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 = 113; 630 enum RSA_F_RSA_PADDING_CHECK_SSLV23 = 114; 631 enum RSA_F_RSA_PADDING_CHECK_X931 = 128; 632 enum RSA_F_RSA_PRINT = 115; 633 enum RSA_F_RSA_PRINT_FP = 116; 634 enum RSA_F_RSA_PRIVATE_DECRYPT = 150; 635 enum RSA_F_RSA_PRIVATE_ENCRYPT = 151; 636 enum RSA_F_RSA_PRIV_DECODE = 137; 637 enum RSA_F_RSA_PRIV_ENCODE = 138; 638 enum RSA_F_RSA_PUBLIC_DECRYPT = 152; 639 enum RSA_F_RSA_PUBLIC_ENCRYPT = 153; 640 enum RSA_F_RSA_PUB_DECODE = 139; 641 enum RSA_F_RSA_SETUP_BLINDING = 136; 642 enum RSA_F_RSA_SIGN = 117; 643 enum RSA_F_RSA_SIGN_ASN1_OCTET_STRING = 118; 644 enum RSA_F_RSA_VERIFY = 119; 645 enum RSA_F_RSA_VERIFY_ASN1_OCTET_STRING = 120; 646 enum RSA_F_RSA_VERIFY_PKCS1_PSS = 126; 647 enum RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1 = 149 648 ; 649 /* Reason codes. */ 650 enum RSA_R_ALGORITHM_MISMATCH = 100; 651 enum RSA_R_BAD_E_VALUE = 101; 652 enum RSA_R_BAD_FIXED_HEADER_DECRYPT = 102; 653 enum RSA_R_BAD_PAD_BYTE_COUNT = 103; 654 enum RSA_R_BAD_SIGNATURE = 104; 655 enum RSA_R_BLOCK_TYPE_IS_NOT_01 = 106; 656 enum RSA_R_BLOCK_TYPE_IS_NOT_02 = 107; 657 enum RSA_R_DATA_GREATER_THAN_MOD_LEN = 108; 658 enum RSA_R_DATA_TOO_LARGE = 109; 659 enum RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE = 110; 660 enum RSA_R_DATA_TOO_LARGE_FOR_MODULUS = 132; 661 enum RSA_R_DATA_TOO_SMALL = 111; 662 enum RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE = 122; 663 enum RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY = 112; 664 enum RSA_R_DMP1_NOT_CONGRUENT_TO_D = 124; 665 enum RSA_R_DMQ1_NOT_CONGRUENT_TO_D = 125; 666 enum RSA_R_D_E_NOT_CONGRUENT_TO_1 = 123; 667 enum RSA_R_FIRST_OCTET_INVALID = 133; 668 enum RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE = 144; 669 enum RSA_R_INVALID_DIGEST = 157; 670 enum RSA_R_INVALID_DIGEST_LENGTH = 143; 671 enum RSA_R_INVALID_HEADER = 137; 672 enum RSA_R_INVALID_KEYBITS = 145; 673 enum RSA_R_INVALID_MESSAGE_LENGTH = 131; 674 enum RSA_R_INVALID_MGF1_MD = 156; 675 enum RSA_R_INVALID_PADDING = 138; 676 enum RSA_R_INVALID_PADDING_MODE = 141; 677 enum RSA_R_INVALID_PSS_PARAMETERS = 149; 678 enum RSA_R_INVALID_PSS_SALTLEN = 146; 679 enum RSA_R_INVALID_SALT_LENGTH = 150; 680 enum RSA_R_INVALID_TRAILER = 139; 681 enum RSA_R_INVALID_X931_DIGEST = 142; 682 enum RSA_R_IQMP_NOT_INVERSE_OF_Q = 126; 683 enum RSA_R_KEY_SIZE_TOO_SMALL = 120; 684 enum RSA_R_LAST_OCTET_INVALID = 134; 685 enum RSA_R_MODULUS_TOO_LARGE = 105; 686 enum RSA_R_NON_FIPS_RSA_METHOD = 157; 687 enum RSA_R_NO_PUBLIC_EXPONENT = 140; 688 enum RSA_R_NULL_BEFORE_BLOCK_MISSING = 113; 689 enum RSA_R_N_DOES_NOT_EQUAL_P_Q = 127; 690 enum RSA_R_OAEP_DECODING_ERROR = 121; 691 enum RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE = 158; 692 enum RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE = 148; 693 enum RSA_R_PADDING_CHECK_FAILED = 114; 694 enum RSA_R_P_NOT_PRIME = 128; 695 enum RSA_R_Q_NOT_PRIME = 129; 696 enum RSA_R_RSA_OPERATIONS_NOT_SUPPORTED = 130; 697 enum RSA_R_SLEN_CHECK_FAILED = 136; 698 enum RSA_R_SLEN_RECOVERY_FAILED = 135; 699 enum RSA_R_SSLV3_ROLLBACK_ATTACK = 115; 700 enum RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD = 116; 701 enum RSA_R_UNKNOWN_ALGORITHM_TYPE = 117; 702 enum RSA_R_UNKNOWN_MASK_DIGEST = 151; 703 enum RSA_R_UNKNOWN_PADDING_TYPE = 118; 704 enum RSA_R_UNKNOWN_PSS_DIGEST = 152; 705 enum RSA_R_UNSUPPORTED_MASK_ALGORITHM = 153; 706 enum RSA_R_UNSUPPORTED_MASK_PARAMETER = 154; 707 enum RSA_R_UNSUPPORTED_SIGNATURE_TYPE = 155; 708 enum RSA_R_VALUE_MISSING = 147; 709 enum RSA_R_WRONG_SIGNATURE_LENGTH = 119;