You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

514 lines
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/ssl.h>
  57. #include <limits.h>
  58. #include <openssl/ec.h>
  59. #include <openssl/ec_key.h>
  60. #include <openssl/err.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/type_check.h>
  64. #include "internal.h"
  65. #include "../crypto/internal.h"
  66. int ssl_is_key_type_supported(int key_type) {
  67. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
  68. key_type == EVP_PKEY_ED25519;
  69. }
  70. static int ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
  71. if (!ssl_is_key_type_supported(pkey->type)) {
  72. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  73. return 0;
  74. }
  75. if (cert->chain != NULL &&
  76. sk_CRYPTO_BUFFER_value(cert->chain, 0) != NULL &&
  77. /* Sanity-check that the private key and the certificate match. */
  78. !ssl_cert_check_private_key(cert, pkey)) {
  79. return 0;
  80. }
  81. EVP_PKEY_free(cert->privatekey);
  82. EVP_PKEY_up_ref(pkey);
  83. cert->privatekey = pkey;
  84. return 1;
  85. }
  86. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  87. EVP_PKEY *pkey;
  88. int ret;
  89. if (rsa == NULL) {
  90. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  91. return 0;
  92. }
  93. pkey = EVP_PKEY_new();
  94. if (pkey == NULL) {
  95. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  96. return 0;
  97. }
  98. RSA_up_ref(rsa);
  99. EVP_PKEY_assign_RSA(pkey, rsa);
  100. ret = ssl_set_pkey(ssl->cert, pkey);
  101. EVP_PKEY_free(pkey);
  102. return ret;
  103. }
  104. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  105. if (pkey == NULL) {
  106. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  107. return 0;
  108. }
  109. return ssl_set_pkey(ssl->cert, pkey);
  110. }
  111. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
  112. size_t der_len) {
  113. if (der_len > LONG_MAX) {
  114. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  115. return 0;
  116. }
  117. const uint8_t *p = der;
  118. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  119. if (pkey == NULL || p != der + der_len) {
  120. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  121. EVP_PKEY_free(pkey);
  122. return 0;
  123. }
  124. int ret = SSL_use_PrivateKey(ssl, pkey);
  125. EVP_PKEY_free(pkey);
  126. return ret;
  127. }
  128. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  129. int ret;
  130. EVP_PKEY *pkey;
  131. if (rsa == NULL) {
  132. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  133. return 0;
  134. }
  135. pkey = EVP_PKEY_new();
  136. if (pkey == NULL) {
  137. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  138. return 0;
  139. }
  140. RSA_up_ref(rsa);
  141. EVP_PKEY_assign_RSA(pkey, rsa);
  142. ret = ssl_set_pkey(ctx->cert, pkey);
  143. EVP_PKEY_free(pkey);
  144. return ret;
  145. }
  146. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  147. size_t der_len) {
  148. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  149. if (rsa == NULL) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  151. return 0;
  152. }
  153. int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  154. RSA_free(rsa);
  155. return ret;
  156. }
  157. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  158. if (pkey == NULL) {
  159. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  160. return 0;
  161. }
  162. return ssl_set_pkey(ctx->cert, pkey);
  163. }
  164. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
  165. size_t der_len) {
  166. if (der_len > LONG_MAX) {
  167. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  168. return 0;
  169. }
  170. const uint8_t *p = der;
  171. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  172. if (pkey == NULL || p != der + der_len) {
  173. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  174. EVP_PKEY_free(pkey);
  175. return 0;
  176. }
  177. int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  178. EVP_PKEY_free(pkey);
  179. return ret;
  180. }
  181. void SSL_set_private_key_method(SSL *ssl,
  182. const SSL_PRIVATE_KEY_METHOD *key_method) {
  183. ssl->cert->key_method = key_method;
  184. }
  185. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  186. const SSL_PRIVATE_KEY_METHOD *key_method) {
  187. ctx->cert->key_method = key_method;
  188. }
  189. static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs,
  190. const uint16_t *prefs, size_t num_prefs) {
  191. OPENSSL_free(*out_prefs);
  192. *out_num_prefs = 0;
  193. *out_prefs = BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
  194. if (*out_prefs == NULL) {
  195. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  196. return 0;
  197. }
  198. *out_num_prefs = num_prefs;
  199. return 1;
  200. }
  201. int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  202. size_t num_prefs) {
  203. return set_algorithm_prefs(&ctx->cert->sigalgs, &ctx->cert->num_sigalgs,
  204. prefs, num_prefs);
  205. }
  206. int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
  207. size_t num_prefs) {
  208. return set_algorithm_prefs(&ssl->cert->sigalgs, &ssl->cert->num_sigalgs,
  209. prefs, num_prefs);
  210. }
  211. int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  212. size_t num_prefs) {
  213. return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs,
  214. prefs, num_prefs);
  215. }
  216. int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
  217. size_t num_digests) {
  218. OPENSSL_free(ssl->cert->sigalgs);
  219. OPENSSL_COMPILE_ASSERT(sizeof(int) >= 2 * sizeof(uint16_t),
  220. digest_list_conversion_cannot_overflow);
  221. ssl->cert->num_sigalgs = 0;
  222. ssl->cert->sigalgs = OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests);
  223. if (ssl->cert->sigalgs == NULL) {
  224. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  225. return 0;
  226. }
  227. /* Convert the digest list to a signature algorithms list.
  228. *
  229. * TODO(davidben): Replace this API with one that can express RSA-PSS, etc. */
  230. for (size_t i = 0; i < num_digests; i++) {
  231. switch (digest_nids[i]) {
  232. case NID_sha1:
  233. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1;
  234. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1;
  235. ssl->cert->num_sigalgs += 2;
  236. break;
  237. case NID_sha256:
  238. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256;
  239. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  240. SSL_SIGN_ECDSA_SECP256R1_SHA256;
  241. ssl->cert->num_sigalgs += 2;
  242. break;
  243. case NID_sha384:
  244. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384;
  245. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  246. SSL_SIGN_ECDSA_SECP384R1_SHA384;
  247. ssl->cert->num_sigalgs += 2;
  248. break;
  249. case NID_sha512:
  250. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512;
  251. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  252. SSL_SIGN_ECDSA_SECP521R1_SHA512;
  253. ssl->cert->num_sigalgs += 2;
  254. break;
  255. }
  256. }
  257. return 1;
  258. }
  259. typedef struct {
  260. uint16_t sigalg;
  261. int pkey_type;
  262. int curve;
  263. const EVP_MD *(*digest_func)(void);
  264. char is_rsa_pss;
  265. } SSL_SIGNATURE_ALGORITHM;
  266. static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
  267. {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, 0},
  268. {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, 0},
  269. {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 0},
  270. {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 0},
  271. {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 0},
  272. {SSL_SIGN_RSA_PSS_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 1},
  273. {SSL_SIGN_RSA_PSS_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 1},
  274. {SSL_SIGN_RSA_PSS_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 1},
  275. {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, 0},
  276. {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
  277. &EVP_sha256, 0},
  278. {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
  279. 0},
  280. {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
  281. 0},
  282. {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, NULL, 0},
  283. };
  284. static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) {
  285. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
  286. if (kSignatureAlgorithms[i].sigalg == sigalg) {
  287. return &kSignatureAlgorithms[i];
  288. }
  289. }
  290. return NULL;
  291. }
  292. int ssl_has_private_key(const SSL *ssl) {
  293. return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
  294. }
  295. static int pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
  296. uint16_t sigalg) {
  297. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  298. if (alg == NULL ||
  299. EVP_PKEY_id(pkey) != alg->pkey_type) {
  300. return 0;
  301. }
  302. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  303. /* RSA keys may only be used with RSA-PSS. */
  304. if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
  305. return 0;
  306. }
  307. /* EC keys have a curve requirement. */
  308. if (alg->pkey_type == EVP_PKEY_EC &&
  309. (alg->curve == NID_undef ||
  310. EC_GROUP_get_curve_name(
  311. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
  312. return 0;
  313. }
  314. }
  315. return 1;
  316. }
  317. static int setup_ctx(SSL *ssl, EVP_PKEY_CTX *ctx, uint16_t sigalg) {
  318. if (!pkey_supports_algorithm(ssl, EVP_PKEY_CTX_get0_pkey(ctx), sigalg)) {
  319. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  320. return 0;
  321. }
  322. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  323. if (alg->digest_func != NULL &&
  324. !EVP_PKEY_CTX_set_signature_md(ctx, alg->digest_func())) {
  325. return 0;
  326. }
  327. if (alg->is_rsa_pss) {
  328. if (!EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) ||
  329. !EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, -1 /* salt len = hash len */)) {
  330. return 0;
  331. }
  332. }
  333. return 1;
  334. }
  335. static int legacy_sign_digest_supported(const SSL_SIGNATURE_ALGORITHM *alg) {
  336. return (alg->pkey_type == EVP_PKEY_EC || alg->pkey_type == EVP_PKEY_RSA) &&
  337. !alg->is_rsa_pss;
  338. }
  339. enum ssl_private_key_result_t ssl_private_key_sign(
  340. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  341. uint16_t sigalg, const uint8_t *in, size_t in_len) {
  342. if (ssl->cert->key_method != NULL) {
  343. if (ssl->cert->key_method->sign != NULL) {
  344. return ssl->cert->key_method->sign(ssl, out, out_len, max_out, sigalg, in,
  345. in_len);
  346. }
  347. /* TODO(davidben): Remove support for |sign_digest|-only
  348. * |SSL_PRIVATE_KEY_METHOD|s. */
  349. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  350. if (alg == NULL ||
  351. !legacy_sign_digest_supported(alg)) {
  352. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  353. return ssl_private_key_failure;
  354. }
  355. const EVP_MD *md = alg->digest_func();
  356. uint8_t hash[EVP_MAX_MD_SIZE];
  357. unsigned hash_len;
  358. if (!EVP_Digest(in, in_len, hash, &hash_len, md, NULL)) {
  359. return ssl_private_key_failure;
  360. }
  361. return ssl->cert->key_method->sign_digest(ssl, out, out_len, max_out, md,
  362. hash, hash_len);
  363. }
  364. *out_len = max_out;
  365. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
  366. int ret = ctx != NULL &&
  367. EVP_PKEY_sign_init(ctx) &&
  368. setup_ctx(ssl, ctx, sigalg) &&
  369. EVP_PKEY_sign_message(ctx, out, out_len, in, in_len);
  370. EVP_PKEY_CTX_free(ctx);
  371. return ret ? ssl_private_key_success : ssl_private_key_failure;
  372. }
  373. int ssl_public_key_verify(SSL *ssl, const uint8_t *signature,
  374. size_t signature_len, uint16_t signature_algorithm,
  375. EVP_PKEY *pkey, const uint8_t *in, size_t in_len) {
  376. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL);
  377. int ret = ctx != NULL &&
  378. EVP_PKEY_verify_init(ctx) &&
  379. setup_ctx(ssl, ctx, signature_algorithm) &&
  380. EVP_PKEY_verify_message(ctx, signature, signature_len, in, in_len);
  381. EVP_PKEY_CTX_free(ctx);
  382. return ret;
  383. }
  384. enum ssl_private_key_result_t ssl_private_key_decrypt(
  385. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  386. const uint8_t *in, size_t in_len) {
  387. if (ssl->cert->key_method != NULL) {
  388. return ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
  389. in_len);
  390. }
  391. RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey);
  392. if (rsa == NULL) {
  393. /* Decrypt operations are only supported for RSA keys. */
  394. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  395. return ssl_private_key_failure;
  396. }
  397. /* Decrypt with no padding. PKCS#1 padding will be removed as part
  398. * of the timing-sensitive code by the caller. */
  399. if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
  400. return ssl_private_key_failure;
  401. }
  402. return ssl_private_key_success;
  403. }
  404. enum ssl_private_key_result_t ssl_private_key_complete(SSL *ssl, uint8_t *out,
  405. size_t *out_len,
  406. size_t max_out) {
  407. /* Only custom keys may be asynchronous. */
  408. return ssl->cert->key_method->complete(ssl, out, out_len, max_out);
  409. }
  410. int ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
  411. uint16_t sigalg) {
  412. SSL *const ssl = hs->ssl;
  413. if (!pkey_supports_algorithm(ssl, hs->local_pubkey, sigalg)) {
  414. return 0;
  415. }
  416. /* Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
  417. * emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
  418. * hash in TLS. Reasonable RSA key sizes are large enough for the largest
  419. * defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
  420. * SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
  421. * size so that we can fall back to another algorithm in that case. */
  422. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  423. if (alg->is_rsa_pss &&
  424. (size_t)EVP_PKEY_size(hs->local_pubkey) <
  425. 2 * EVP_MD_size(alg->digest_func()) + 2) {
  426. return 0;
  427. }
  428. /* Newer algorithms require message-based private keys.
  429. * TODO(davidben): Remove this check when sign_digest is gone. */
  430. if (ssl->cert->key_method != NULL &&
  431. ssl->cert->key_method->sign == NULL &&
  432. !legacy_sign_digest_supported(alg)) {
  433. return 0;
  434. }
  435. return 1;
  436. }