Skip to content

Commit 7eab0da

Browse files
davidbendkostic
authored and
dkostic
committed
X509_sign, etc., should return the length of the signature on success
Prior to https://boringssl-review.googlesource.com/c/boringssl/+/58548, ASN1_item_sign_ctx returned the length of the signature on success. It's unclear why anyone would ever want this, but some test was sensitive to it. (I think it was a typo.) Restore the old behavior. Change-Id: Ibf3e45331a339226744d51df703634d02b08a7c4 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/59307 Reviewed-by: Bob Beck <[email protected]> Auto-Submit: David Benjamin <[email protected]> Commit-Queue: Bob Beck <[email protected]> (cherry picked from commit 0c7527bb3a34a95387856827702bbb46c408457d)
1 parent c5ef107 commit 7eab0da

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

crypto/x509/a_sign.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1,
126126
out = NULL;
127127
signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
128128
signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
129-
ret = 1;
129+
ret = (int)out_len;
130130

131131
err:
132132
EVP_MD_CTX_cleanup(ctx);

crypto/x509/x509_test.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -2436,7 +2436,12 @@ TEST(X509Test, SignCertificate) {
24362436
ASSERT_TRUE(
24372437
X509_set1_signature_value(cert.get(), sig.data(), sig.size()));
24382438
} else {
2439-
ASSERT_TRUE(X509_sign(cert.get(), pkey.get(), EVP_sha384()));
2439+
int ret = X509_sign(cert.get(), pkey.get(), EVP_sha384());
2440+
ASSERT_GT(ret, 0);
2441+
// |X509_sign| returns the length of the signature on success.
2442+
const ASN1_BIT_STRING *sig;
2443+
X509_get0_signature(&sig, /*out_alg=*/nullptr, cert.get());
2444+
EXPECT_EQ(ret, ASN1_STRING_length(sig));
24402445
}
24412446

24422447
// Check the signature.

include/openssl/x509.h

+27-26
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,17 @@ OPENSSL_EXPORT X509_EXTENSION *X509_delete_ext(X509 *x, int loc);
355355
OPENSSL_EXPORT int X509_add_ext(X509 *x, const X509_EXTENSION *ex, int loc);
356356

357357
// X509_sign signs |x509| with |pkey| and replaces the signature algorithm and
358-
// signature fields. It returns one on success and zero on error. This function
359-
// uses digest algorithm |md|, or |pkey|'s default if NULL. Other signing
360-
// parameters use |pkey|'s defaults. To customize them, use |X509_sign_ctx|.
358+
// signature fields. It returns the length of the signature on success and zero
359+
// on error. This function uses digest algorithm |md|, or |pkey|'s default if
360+
// NULL. Other signing parameters use |pkey|'s defaults. To customize them, use
361+
// |X509_sign_ctx|.
361362
OPENSSL_EXPORT int X509_sign(X509 *x509, EVP_PKEY *pkey, const EVP_MD *md);
362363

363364
// X509_sign_ctx signs |x509| with |ctx| and replaces the signature algorithm
364-
// and signature fields. It returns one on success and zero on error. The
365-
// signature algorithm and parameters come from |ctx|, which must have been
366-
// initialized with |EVP_DigestSignInit|. The caller should configure the
367-
// corresponding |EVP_PKEY_CTX| before calling this function.
365+
// and signature fields. It returns the length of the signature on success and
366+
// zero on error. The signature algorithm and parameters come from |ctx|, which
367+
// must have been initialized with |EVP_DigestSignInit|. The caller should
368+
// configure the corresponding |EVP_PKEY_CTX| before calling this function.
368369
OPENSSL_EXPORT int X509_sign_ctx(X509 *x509, EVP_MD_CTX *ctx);
369370

370371
// i2d_re_X509_tbs serializes the TBSCertificate portion of |x509|, as described
@@ -642,18 +643,18 @@ OPENSSL_EXPORT int X509_CRL_add_ext(X509_CRL *x, const X509_EXTENSION *ex,
642643
int loc);
643644

644645
// X509_CRL_sign signs |crl| with |pkey| and replaces the signature algorithm
645-
// and signature fields. It returns one on success and zero on error. This
646-
// function uses digest algorithm |md|, or |pkey|'s default if NULL. Other
647-
// signing parameters use |pkey|'s defaults. To customize them, use
648-
// |X509_CRL_sign_ctx|.
646+
// and signature fields. It returns the length of the signature on success and
647+
// zero on error. This function uses digest algorithm |md|, or |pkey|'s default
648+
// if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
649+
// use |X509_CRL_sign_ctx|.
649650
OPENSSL_EXPORT int X509_CRL_sign(X509_CRL *crl, EVP_PKEY *pkey,
650651
const EVP_MD *md);
651652

652653
// X509_CRL_sign_ctx signs |crl| with |ctx| and replaces the signature algorithm
653-
// and signature fields. It returns one on success and zero on error. The
654-
// signature algorithm and parameters come from |ctx|, which must have been
655-
// initialized with |EVP_DigestSignInit|. The caller should configure the
656-
// corresponding |EVP_PKEY_CTX| before calling this function.
654+
// and signature fields. It returns the length of the signature on success and
655+
// zero on error. The signature algorithm and parameters come from |ctx|, which
656+
// must have been initialized with |EVP_DigestSignInit|. The caller should
657+
// configure the corresponding |EVP_PKEY_CTX| before calling this function.
657658
OPENSSL_EXPORT int X509_CRL_sign_ctx(X509_CRL *crl, EVP_MD_CTX *ctx);
658659

659660
// i2d_re_X509_CRL_tbs serializes the TBSCertList portion of |crl|, as described
@@ -881,18 +882,18 @@ OPENSSL_EXPORT int X509_REQ_add_extensions(
881882
X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts);
882883

883884
// X509_REQ_sign signs |req| with |pkey| and replaces the signature algorithm
884-
// and signature fields. It returns one on success and zero on error. This
885-
// function uses digest algorithm |md|, or |pkey|'s default if NULL. Other
886-
// signing parameters use |pkey|'s defaults. To customize them, use
887-
// |X509_REQ_sign_ctx|.
885+
// and signature fields. It returns the length of the signature on success and
886+
// zero on error. This function uses digest algorithm |md|, or |pkey|'s default
887+
// if NULL. Other signing parameters use |pkey|'s defaults. To customize them,
888+
// use |X509_REQ_sign_ctx|.
888889
OPENSSL_EXPORT int X509_REQ_sign(X509_REQ *req, EVP_PKEY *pkey,
889890
const EVP_MD *md);
890891

891892
// X509_REQ_sign_ctx signs |req| with |ctx| and replaces the signature algorithm
892-
// and signature fields. It returns one on success and zero on error. The
893-
// signature algorithm and parameters come from |ctx|, which must have been
894-
// initialized with |EVP_DigestSignInit|. The caller should configure the
895-
// corresponding |EVP_PKEY_CTX| before calling this function.
893+
// and signature fields. It returns the length of the signature on success and
894+
// zero on error. The signature algorithm and parameters come from |ctx|, which
895+
// must have been initialized with |EVP_DigestSignInit|. The caller should
896+
// configure the corresponding |EVP_PKEY_CTX| before calling this function.
896897
OPENSSL_EXPORT int X509_REQ_sign_ctx(X509_REQ *req, EVP_MD_CTX *ctx);
897898

898899
// i2d_re_X509_REQ_tbs serializes the CertificationRequestInfo (see RFC 2986)
@@ -2201,9 +2202,9 @@ OPENSSL_EXPORT int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *spki,
22012202
EVP_PKEY *pkey);
22022203

22032204
// NETSCAPE_SPKI_sign signs |spki| with |pkey| and replaces the signature
2204-
// algorithm and signature fields. It returns one on success and zero on error.
2205-
// This function uses digest algorithm |md|, or |pkey|'s default if NULL. Other
2206-
// signing parameters use |pkey|'s defaults.
2205+
// algorithm and signature fields. It returns the length of the signature on
2206+
// success and zero on error. This function uses digest algorithm |md|, or
2207+
// |pkey|'s default if NULL. Other signing parameters use |pkey|'s defaults.
22072208
OPENSSL_EXPORT int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *spki, EVP_PKEY *pkey,
22082209
const EVP_MD *md);
22092210

0 commit comments

Comments
 (0)