Skip to content

Commit d123650

Browse files
davidbentorben-hansen
authored andcommitted
Add int64 ASN1_INTEGER setters too.
https://boringssl-review.googlesource.com/c/boringssl/+/54307 added just the getters because no one was using the setters yet. But our long setter *already* implements the int64 version, so just complete the whole set and deprecate the old long-based APIs. Change-Id: Ieb793f3cf90d4214c6416ba2f10e641c46403188 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/54526 Commit-Queue: Adam Langley <[email protected]> Reviewed-by: Adam Langley <[email protected]> Commit-Queue: David Benjamin <[email protected]> (cherry picked from commit cab31f65f1ad6e6daca62e95b25dd6cd805fce0b)
1 parent 414b8e4 commit d123650

File tree

8 files changed

+55
-30
lines changed

8 files changed

+55
-30
lines changed

crypto/asn1/a_int.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **out, const unsigned char **inp,
246246
return NULL;
247247
}
248248

249-
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) {
249+
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t v) {
250250
if (v >= 0) {
251251
return ASN1_INTEGER_set_uint64(a, (uint64_t)v);
252252
}
@@ -259,7 +259,7 @@ int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) {
259259
return 1;
260260
}
261261

262-
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) {
262+
int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t v) {
263263
if (v >= 0) {
264264
return ASN1_ENUMERATED_set_uint64(a, (uint64_t)v);
265265
}
@@ -272,6 +272,16 @@ int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) {
272272
return 1;
273273
}
274274

275+
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) {
276+
static_assert(sizeof(long) <= sizeof(int64_t), "long fits in int64_t");
277+
return ASN1_INTEGER_set_int64(a, v);
278+
}
279+
280+
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) {
281+
static_assert(sizeof(long) <= sizeof(int64_t), "long fits in int64_t");
282+
return ASN1_ENUMERATED_set_int64(a, v);
283+
}
284+
275285
static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type) {
276286
uint8_t buf[sizeof(uint64_t)];
277287
CRYPTO_store_u64_be(buf, v);

crypto/asn1/asn1_test.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,16 @@ TEST(ASN1Test, Integer) {
346346

347347
fits_in_i64 = BN_cmp(int64_min.get(), bn.get()) <= 0 &&
348348
BN_cmp(bn.get(), int64_max.get()) <= 0;
349-
350349
if (fits_in_i64) {
351350
if (BN_is_negative(bn.get())) {
352351
i64 = static_cast<int64_t>(0u - abs_u64);
353352
} else {
354353
i64 = static_cast<int64_t>(abs_u64);
355354
}
355+
bssl::UniquePtr<ASN1_INTEGER> by_i64(ASN1_INTEGER_new());
356+
ASSERT_TRUE(by_i64);
357+
ASSERT_TRUE(ASN1_INTEGER_set_int64(by_i64.get(), i64));
358+
objs["i64"] = std::move(by_i64);
356359
}
357360

358361
if (sizeof(long) == 8) {

crypto/x509/rsa_pss.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
229229

230230
if (saltlen != 20) {
231231
pss->saltLength = ASN1_INTEGER_new();
232-
if (!pss->saltLength || !ASN1_INTEGER_set(pss->saltLength, saltlen)) {
232+
if (!pss->saltLength || !ASN1_INTEGER_set_int64(pss->saltLength, saltlen)) {
233233
goto err;
234234
}
235235
}

crypto/x509/x509_set.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int X509_set_version(X509 *x, long version) {
9494
return 0;
9595
}
9696
}
97-
return ASN1_INTEGER_set(x->cert_info->version, version);
97+
return ASN1_INTEGER_set_int64(x->cert_info->version, version);
9898
}
9999

100100
int X509_set_serialNumber(X509 *x, const ASN1_INTEGER *serial) {

crypto/x509/x509_test.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ TEST(X509Test, RSASignManual) {
19431943
cert.reset(X509_new());
19441944
// Fill in some fields for the certificate arbitrarily.
19451945
EXPECT_TRUE(X509_set_version(cert.get(), X509_VERSION_3));
1946-
EXPECT_TRUE(ASN1_INTEGER_set(X509_get_serialNumber(cert.get()), 1));
1946+
EXPECT_TRUE(ASN1_INTEGER_set_int64(X509_get_serialNumber(cert.get()), 1));
19471947
EXPECT_TRUE(X509_gmtime_adj(X509_getm_notBefore(cert.get()), 0));
19481948
EXPECT_TRUE(
19491949
X509_gmtime_adj(X509_getm_notAfter(cert.get()), 60 * 60 * 24));
@@ -2088,7 +2088,7 @@ TEST(X509Test, TestFromBufferModified) {
20882088
ASSERT_TRUE(root);
20892089

20902090
bssl::UniquePtr<ASN1_INTEGER> fourty_two(ASN1_INTEGER_new());
2091-
ASN1_INTEGER_set(fourty_two.get(), 42);
2091+
ASN1_INTEGER_set_int64(fourty_two.get(), 42);
20922092
X509_set_serialNumber(root.get(), fourty_two.get());
20932093

20942094
ASSERT_EQ(static_cast<long>(data_len), i2d_X509(root.get(), nullptr));
@@ -4823,7 +4823,7 @@ TEST(X509Test, SetSerialNumberChecksASN1StringType) {
48234823
// them and some callers rely in this for tests.
48244824
bssl::UniquePtr<ASN1_INTEGER> serial(ASN1_INTEGER_new());
48254825
ASSERT_TRUE(serial);
4826-
ASSERT_TRUE(ASN1_INTEGER_set(serial.get(), -1));
4826+
ASSERT_TRUE(ASN1_INTEGER_set_int64(serial.get(), -1));
48274827
ASSERT_TRUE(X509_set_serialNumber(root.get(), serial.get()));
48284828
int64_t val;
48294829
ASSERT_TRUE(ASN1_INTEGER_get_int64(&val, X509_get0_serialNumber(root.get())));

crypto/x509/x509cset.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int X509_CRL_set_version(X509_CRL *x, long version) {
8585
return 0;
8686
}
8787
}
88-
return ASN1_INTEGER_set(x->crl->version, version);
88+
return ASN1_INTEGER_set_int64(x->crl->version, version);
8989
}
9090

9191
int X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name) {

crypto/x509/x509rset.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int X509_REQ_set_version(X509_REQ *x, long version) {
7070
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
7171
return 0;
7272
}
73-
return ASN1_INTEGER_set(x->req_info->version, version);
73+
return ASN1_INTEGER_set_int64(x->req_info->version, version);
7474
}
7575

7676
int X509_REQ_set_subject_name(X509_REQ *x, X509_NAME *name) {

include/openssl/asn1.h

+32-20
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,9 @@ DECLARE_ASN1_ITEM(ASN1_INTEGER)
10831083
// on success and zero on error.
10841084
OPENSSL_EXPORT int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v);
10851085

1086-
// ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on
1087-
// success and zero on error.
1088-
OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
1086+
// ASN1_INTEGER_set_int64 sets |a| to an INTEGER with value |v|. It returns one
1087+
// on success and zero on error.
1088+
OPENSSL_EXPORT int ASN1_INTEGER_set_int64(ASN1_INTEGER *out, int64_t v);
10891089

10901090
// ASN1_INTEGER_get_uint64 converts |a| to a |uint64_t|. On success, it returns
10911091
// one and sets |*out| to the result. If |a| did not fit or has the wrong type,
@@ -1098,13 +1098,6 @@ OPENSSL_EXPORT int ASN1_INTEGER_get_uint64(uint64_t *out,
10981098
// it returns zero.
10991099
OPENSSL_EXPORT int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a);
11001100

1101-
// ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of
1102-
// range or the wrong type.
1103-
//
1104-
// WARNING: This function's return value cannot distinguish errors from -1.
1105-
// Prefer |ASN1_INTEGER_get_uint64|.
1106-
OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a);
1107-
11081101
// BN_to_ASN1_INTEGER sets |ai| to an INTEGER with value |bn| and returns |ai|
11091102
// on success or NULL or error. If |ai| is NULL, it returns a newly-allocated
11101103
// |ASN1_INTEGER| on success instead, which the caller must release with
@@ -1152,9 +1145,9 @@ DECLARE_ASN1_ITEM(ASN1_ENUMERATED)
11521145
// returns one on success and zero on error.
11531146
OPENSSL_EXPORT int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v);
11541147

1155-
// ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one
1156-
// on success and zero on error.
1157-
OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
1148+
// ASN1_ENUMERATED_set_int64 sets |a| to an ENUMERATED with value |v|. It
1149+
// returns one on success and zero on error.
1150+
OPENSSL_EXPORT int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *out, int64_t v);
11581151

11591152
// ASN1_ENUMERATED_get_uint64 converts |a| to a |uint64_t|. On success, it
11601153
// returns one and sets |*out| to the result. If |a| did not fit or has the
@@ -1168,13 +1161,6 @@ OPENSSL_EXPORT int ASN1_ENUMERATED_get_uint64(uint64_t *out,
11681161
OPENSSL_EXPORT int ASN1_ENUMERATED_get_int64(int64_t *out,
11691162
const ASN1_ENUMERATED *a);
11701163

1171-
// ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out
1172-
// of range or the wrong type.
1173-
//
1174-
// WARNING: This function's return value cannot distinguish errors from -1.
1175-
// Prefer |ASN1_ENUMERATED_get_uint64|.
1176-
OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a);
1177-
11781164
// BN_to_ASN1_ENUMERATED sets |ai| to an ENUMERATED with value |bn| and returns
11791165
// |ai| on success or NULL or error. If |ai| is NULL, it returns a
11801166
// newly-allocated |ASN1_ENUMERATED| on success instead, which the caller must
@@ -1977,6 +1963,32 @@ OPENSSL_EXPORT int i2d_ASN1_PRINTABLE(const ASN1_STRING *in, uint8_t **outp);
19771963
// printable characters. See https://crbug.com/boringssl/412.
19781964
DECLARE_ASN1_ITEM(ASN1_PRINTABLE)
19791965

1966+
// ASN1_INTEGER_set sets |a| to an INTEGER with value |v|. It returns one on
1967+
// success and zero on error.
1968+
//
1969+
// Use |ASN1_INTEGER_set_uint64| and |ASN1_INTEGER_set_int64| instead.
1970+
OPENSSL_EXPORT int ASN1_INTEGER_set(ASN1_INTEGER *a, long v);
1971+
1972+
// ASN1_ENUMERATED_set sets |a| to an ENUMERATED with value |v|. It returns one
1973+
// on success and zero on error.
1974+
//
1975+
// Use |ASN1_ENUMERATED_set_uint64| and |ASN1_ENUMERATED_set_int64| instead.
1976+
OPENSSL_EXPORT int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v);
1977+
1978+
// ASN1_INTEGER_get returns the value of |a| as a |long|, or -1 if |a| is out of
1979+
// range or the wrong type.
1980+
//
1981+
// WARNING: This function's return value cannot distinguish errors from -1.
1982+
// Use |ASN1_INTEGER_get_uint64| and |ASN1_INTEGER_get_int64| instead.
1983+
OPENSSL_EXPORT long ASN1_INTEGER_get(const ASN1_INTEGER *a);
1984+
1985+
// ASN1_ENUMERATED_get returns the value of |a| as a |long|, or -1 if |a| is out
1986+
// of range or the wrong type.
1987+
//
1988+
// WARNING: This function's return value cannot distinguish errors from -1.
1989+
// Use |ASN1_ENUMERATED_get_uint64| and |ASN1_ENUMERATED_get_int64| instead.
1990+
OPENSSL_EXPORT long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a);
1991+
19801992

19811993
#if defined(__cplusplus)
19821994
} // extern C

0 commit comments

Comments
 (0)