mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c53b7a758 | ||
|
|
bd5abc2a12 | ||
|
|
9b5ef28601 | ||
|
|
f47c1950ae | ||
|
|
ab6b6fc81b | ||
|
|
127d3745c3 | ||
|
|
d89b2ef9ed | ||
|
|
684952cafa | ||
|
|
8273610176 | ||
|
|
53da7b3706 | ||
|
|
22fd389195 | ||
|
|
01c3fdfb85 | ||
|
|
eefb07cc59 | ||
|
|
158e2b140c | ||
|
|
8b7b68753f | ||
|
|
6c245a7f7f | ||
|
|
3bea268b22 | ||
|
|
0141f53dd5 | ||
|
|
b985ba6482 |
No files matched your search
@@ -8,7 +8,7 @@ about: If something isn't working as expected
|
||||
|
||||
### New Issue Checklist
|
||||
|
||||
- [ ] I'm using the latest version of Swift Crypto (master branch)
|
||||
- [ ] I'm using the latest version of Swift Crypto (`main` branch)
|
||||
- [ ] I read the [Contribution Guidelines](https://github.com/apple/swift-crypto/blob/master/CONTRIBUTING.md)
|
||||
- [ ] I searched for [existing GitHub issues](https://github.com/apple/swift-crypto/issues)
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
# file options
|
||||
|
||||
--swiftversion 5.0
|
||||
--swiftversion 5.2
|
||||
--exclude .build
|
||||
|
||||
# format options
|
||||
|
||||
+5
-2
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:5.1
|
||||
// swift-tools-version:5.2
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
@@ -20,7 +20,7 @@
|
||||
// Sources/CCryptoBoringSSL directory. The source repository is at
|
||||
// https://boringssl.googlesource.com/boringssl.
|
||||
//
|
||||
// BoringSSL Commit: bb43a45d6de7375f3310511d37f040d1055f8a10
|
||||
// BoringSSL Commit: 2042972e8458833714bce23386931b1c79978439
|
||||
|
||||
import PackageDescription
|
||||
|
||||
@@ -40,6 +40,7 @@ let package = Package(
|
||||
],
|
||||
products: [
|
||||
.library(name: "Crypto", targets: ["Crypto"]),
|
||||
.library(name: "_CryptoExtras", targets: ["_CryptoExtras"]),
|
||||
/* This target is used only for symbol mangling. It's added and removed automatically because it emits build warnings. MANGLE_START
|
||||
.library(name: "CCryptoBoringSSL", type: .static, targets: ["CCryptoBoringSSL"]),
|
||||
MANGLE_END */
|
||||
@@ -59,8 +60,10 @@ let package = Package(
|
||||
),
|
||||
.target(name: "CCryptoBoringSSLShims", dependencies: ["CCryptoBoringSSL"]),
|
||||
.target(name: "Crypto", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims"], swiftSettings: swiftSettings),
|
||||
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "Crypto"]),
|
||||
.target(name: "crypto-shasum", dependencies: ["Crypto"]),
|
||||
.testTarget(name: "CryptoTests", dependencies: ["Crypto"], swiftSettings: swiftSettings),
|
||||
.testTarget(name: "_CryptoExtrasTests", dependencies: ["_CryptoExtras"]),
|
||||
],
|
||||
cxxLanguageStandard: .cxx11
|
||||
)
|
||||
@@ -2,6 +2,18 @@
|
||||
|
||||
Swift Crypto is an open-source implementation of a substantial portion of the API of [Apple CryptoKit](https://developer.apple.com/documentation/cryptokit) suitable for use on Linux platforms. It enables cross-platform or server applications with the advantages of CryptoKit.
|
||||
|
||||
## Using Swift Crypto
|
||||
|
||||
Swift Crypto is available as a Swift Package Manager package. To use it, add the following dependency in your `Package.swift`:
|
||||
|
||||
```swift
|
||||
// swift-crypto 1.x and 2.x are almost API compatible, so most clients should
|
||||
// allow either
|
||||
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
|
||||
```
|
||||
|
||||
and to your target, add `Crypto` to your dependencies. You can then `import Crypto` to get access to Swift Crypto's functionality.
|
||||
|
||||
## Functionality
|
||||
|
||||
Swift Crypto exposes the portions of the CryptoKit API that do not rely on specialised hardware to any Swift application. It provides safe APIs that abstract over the complexity of many cryptographic primitives that need to be used in modern applications. These APIs encourage safe usages of the underlying primitives, follow cryptographic best practices, and should be the first choice for building applications that need to use cryptography.
|
||||
@@ -96,16 +108,24 @@ If you believe you have identified a vulnerability in Swift Crypto, please [repo
|
||||
|
||||
### Swift versions
|
||||
|
||||
Swift Crypto supports Swift 5.1 and later.
|
||||
Swift Crypto supports Swift 5.2 and later. Swift Crypto supported Swift 5.1 until version 1.2.
|
||||
|
||||
### Compatibility
|
||||
|
||||
Swift Crypto follows [SemVer 2.0.0](https://semver.org/#semantic-versioning-200). Our public API is the same as that of CryptoKit (except where we lack an implementation entirely), as well as everything in the Server and Staging namespaces. We do not maintain a stable ABI, as Swift Crypto is a source-only distribution.
|
||||
Swift Crypto follows [SemVer 2.0.0](https://semver.org/#semantic-versioning-200). Our public API is the same as that of CryptoKit (except where we lack an implementation entirely), as well as everything in the Server and Staging namespaces. Any symbol beginning with an underscore, and any product beginning with an underscore, is not subject to semantic versioning: these APIs may change without warning. We do not maintain a stable ABI, as Swift Crypto is a source-only distribution.
|
||||
|
||||
What this means for you is that you should depend on Swift Crypto with a version range that covers everything from the minimum Swift Crypto version you require up to the next major version.
|
||||
In SwiftPM that can be easily done specifying for example `from: "1.0.0"` meaning that you support Swift Crypto in every version starting from 1.0.0 up to (excluding) 2.0.0.
|
||||
SemVer and Swift Crypto's Public API guarantees should result in a working program without having to worry about testing every single version for compatibility.
|
||||
|
||||
Swift Crypto 2.0.0 was released in September 2021. The only breaking change between Swift Crypto 2.0.0 and 1.0.0 was the addition of new cases in the `CryptoKitError` enumeration. For most users, then, it's safe to depend on either the 1.0.0 _or_ 2.0.0 series of releases.
|
||||
|
||||
To do so, please use the following dependency in your `Package.swift`:
|
||||
|
||||
```swift
|
||||
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
|
||||
```
|
||||
|
||||
### Developing Swift Crypto on macOS
|
||||
|
||||
Swift Crypto normally defers to the OS implementation of CryptoKit on macOS. Naturally, this makes developing Swift Crypto on macOS tricky. To get Swift Crypto to build the open source implementation on macOS, in `Package.swift`, uncomment the line that reads: `//.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API")`, as this will force Swift Crypto to build its public API.
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
# Security
|
||||
|
||||
This document specifies the security process for Swift Crypto.
|
||||
|
||||
## Disclosures
|
||||
|
||||
To report a known or suspected vulnerability in Swift Crypto, [please report that vulnerability to Apple through the usual channel](https://support.apple.com/en-us/HT201220).
|
||||
|
||||
**Do not file a public issue.**
|
||||
|
||||
Fixes to Swift Crypto will be released simultaneously with any changes that need to be made in CryptoKit, to avoid the risk of having only partially fixed projects.
|
||||
|
||||
+65
-46
@@ -25,6 +25,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/asn1/a_object.c"
|
||||
"crypto/asn1/a_octet.c"
|
||||
"crypto/asn1/a_print.c"
|
||||
"crypto/asn1/a_strex.c"
|
||||
"crypto/asn1/a_strnid.c"
|
||||
"crypto/asn1/a_time.c"
|
||||
"crypto/asn1/a_type.c"
|
||||
@@ -33,7 +34,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/asn1/asn1_lib.c"
|
||||
"crypto/asn1/asn1_par.c"
|
||||
"crypto/asn1/asn_pack.c"
|
||||
"crypto/asn1/f_enum.c"
|
||||
"crypto/asn1/f_int.c"
|
||||
"crypto/asn1/f_string.c"
|
||||
"crypto/asn1/tasn_dec.c"
|
||||
@@ -169,7 +169,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/ecdsa/ecdsa.c"
|
||||
"crypto/fipsmodule/fips_shared_support.c"
|
||||
"crypto/fipsmodule/hmac/hmac.c"
|
||||
"crypto/fipsmodule/is_fips.c"
|
||||
"crypto/fipsmodule/md4/md4.c"
|
||||
"crypto/fipsmodule/md5/md5.c"
|
||||
"crypto/fipsmodule/modes/cbc.c"
|
||||
@@ -187,6 +186,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/rsa/padding.c"
|
||||
"crypto/fipsmodule/rsa/rsa.c"
|
||||
"crypto/fipsmodule/rsa/rsa_impl.c"
|
||||
"crypto/fipsmodule/self_check/fips.c"
|
||||
"crypto/fipsmodule/self_check/self_check.c"
|
||||
"crypto/fipsmodule/sha/sha1-altivec.c"
|
||||
"crypto/fipsmodule/sha/sha1.c"
|
||||
@@ -239,13 +239,13 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/trust_token/voprf.c"
|
||||
"crypto/x509/a_digest.c"
|
||||
"crypto/x509/a_sign.c"
|
||||
"crypto/x509/a_strex.c"
|
||||
"crypto/x509/a_verify.c"
|
||||
"crypto/x509/algorithm.c"
|
||||
"crypto/x509/asn1_gen.c"
|
||||
"crypto/x509/by_dir.c"
|
||||
"crypto/x509/by_file.c"
|
||||
"crypto/x509/i2d_pr.c"
|
||||
"crypto/x509/name_print.c"
|
||||
"crypto/x509/rsa_pss.c"
|
||||
"crypto/x509/t_crl.c"
|
||||
"crypto/x509/t_req.c"
|
||||
@@ -259,7 +259,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509/x509_ext.c"
|
||||
"crypto/x509/x509_lu.c"
|
||||
"crypto/x509/x509_obj.c"
|
||||
"crypto/x509/x509_r2x.c"
|
||||
"crypto/x509/x509_req.c"
|
||||
"crypto/x509/x509_set.c"
|
||||
"crypto/x509/x509_trs.c"
|
||||
@@ -318,48 +317,68 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509v3/v3_skey.c"
|
||||
"crypto/x509v3/v3_utl.c")
|
||||
|
||||
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64")
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.mac.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.mac.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.linux.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.linux.x86_64.S)
|
||||
endif()
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.mac.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.mac.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.linux.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.linux.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont.ios.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8.ios.aarch64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armx64.linux.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont.linux.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8.linux.aarch64.S)
|
||||
endif()
|
||||
|
||||
target_include_directories(CCryptoBoringSSL PUBLIC
|
||||
|
||||
@@ -63,66 +63,72 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len)
|
||||
{
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
}
|
||||
|
||||
int asn1_bit_string_length(const ASN1_BIT_STRING *str,
|
||||
uint8_t *out_padding_bits) {
|
||||
int len = str->length;
|
||||
if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) {
|
||||
// If the string is already empty, it cannot have padding bits.
|
||||
*out_padding_bits = len == 0 ? 0 : str->flags & 0x07;
|
||||
return len;
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/boringssl/447): If we move this logic to
|
||||
// |ASN1_BIT_STRING_set_bit|, can we remove this representation?
|
||||
while (len > 0 && str->data[len - 1] == 0) {
|
||||
len--;
|
||||
}
|
||||
uint8_t padding_bits = 0;
|
||||
if (len > 0) {
|
||||
uint8_t last = str->data[len - 1];
|
||||
assert(last != 0);
|
||||
for (; padding_bits < 7; padding_bits++) {
|
||||
if (last & (1 << padding_bits)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*out_padding_bits = padding_bits;
|
||||
return len;
|
||||
}
|
||||
|
||||
int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out) {
|
||||
uint8_t padding_bits;
|
||||
int len = asn1_bit_string_length(str, &padding_bits);
|
||||
if (padding_bits != 0) {
|
||||
return 0;
|
||||
}
|
||||
*out = len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
|
||||
{
|
||||
int ret, j, bits, len;
|
||||
unsigned char *p, *d;
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
|
||||
len = a->length;
|
||||
uint8_t bits;
|
||||
int len = asn1_bit_string_length(a, &bits);
|
||||
int ret = 1 + len;
|
||||
if (pp == NULL) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t *p = *pp;
|
||||
*(p++) = bits;
|
||||
OPENSSL_memcpy(p, a->data, len);
|
||||
if (len > 0) {
|
||||
if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
|
||||
bits = (int)a->flags & 0x07;
|
||||
} else {
|
||||
for (; len > 0; len--) {
|
||||
if (a->data[len - 1])
|
||||
break;
|
||||
}
|
||||
j = a->data[len - 1];
|
||||
if (j & 0x01)
|
||||
bits = 0;
|
||||
else if (j & 0x02)
|
||||
bits = 1;
|
||||
else if (j & 0x04)
|
||||
bits = 2;
|
||||
else if (j & 0x08)
|
||||
bits = 3;
|
||||
else if (j & 0x10)
|
||||
bits = 4;
|
||||
else if (j & 0x20)
|
||||
bits = 5;
|
||||
else if (j & 0x40)
|
||||
bits = 6;
|
||||
else if (j & 0x80)
|
||||
bits = 7;
|
||||
else
|
||||
bits = 0; /* should not happen */
|
||||
}
|
||||
} else
|
||||
bits = 0;
|
||||
|
||||
ret = 1 + len;
|
||||
if (pp == NULL)
|
||||
return (ret);
|
||||
|
||||
p = *pp;
|
||||
|
||||
*(p++) = (unsigned char)bits;
|
||||
d = a->data;
|
||||
OPENSSL_memcpy(p, d, len);
|
||||
p[len - 1] &= (0xff << bits);
|
||||
}
|
||||
p += len;
|
||||
if (len > 0)
|
||||
p[-1] &= (0xff << bits);
|
||||
*pp = p;
|
||||
return (ret);
|
||||
}
|
||||
@@ -153,11 +159,20 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
|
||||
p = *pp;
|
||||
padding = *(p++);
|
||||
len--;
|
||||
if (padding > 7) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Unused bits in a BIT STRING must be zero. */
|
||||
uint8_t padding_mask = (1 << padding) - 1;
|
||||
if (padding != 0 &&
|
||||
(len < 1 || (p[len - 1] & padding_mask) != 0)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_PADDING);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We do this to preserve the settings. If we modify the settings, via
|
||||
* the _set_bit function, we will recalculate on output
|
||||
@@ -165,21 +180,19 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
|
||||
ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); /* set */
|
||||
|
||||
if (len-- > 1) { /* using one because of the bits left byte */
|
||||
s = (unsigned char *)OPENSSL_malloc((int)len);
|
||||
if (len > 0) {
|
||||
s = OPENSSL_memdup(p, len);
|
||||
if (s == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_memcpy(s, p, (int)len);
|
||||
s[len - 1] &= (0xff << padding);
|
||||
p += len;
|
||||
} else
|
||||
} else {
|
||||
s = NULL;
|
||||
}
|
||||
|
||||
ret->length = (int)len;
|
||||
if (ret->data != NULL)
|
||||
OPENSSL_free(ret->data);
|
||||
OPENSSL_free(ret->data);
|
||||
ret->data = s;
|
||||
ret->type = V_ASN1_BIT_STRING;
|
||||
if (a != NULL)
|
||||
@@ -251,7 +264,7 @@ int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
|
||||
* 'len' is the length of 'flags'.
|
||||
*/
|
||||
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
|
||||
unsigned char *flags, int flags_len)
|
||||
const unsigned char *flags, int flags_len)
|
||||
{
|
||||
int i, ok;
|
||||
/* Check if there is one bit set at all. */
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
int i2d_ASN1_BOOLEAN(int a, unsigned char **pp)
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **pp)
|
||||
{
|
||||
int r;
|
||||
unsigned char *p, *allocated = NULL;
|
||||
@@ -71,14 +71,14 @@ int i2d_ASN1_BOOLEAN(int a, unsigned char **pp)
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(r)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
|
||||
*p = (unsigned char)a;
|
||||
*p = a ? 0xff : 0x00;
|
||||
|
||||
/*
|
||||
* If a new buffer was allocated, just return it back.
|
||||
@@ -88,36 +88,35 @@ int i2d_ASN1_BOOLEAN(int a, unsigned char **pp)
|
||||
return r;
|
||||
}
|
||||
|
||||
int d2i_ASN1_BOOLEAN(int *a, const unsigned char **pp, long length)
|
||||
{
|
||||
int ret = -1;
|
||||
const unsigned char *p;
|
||||
ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *a, const unsigned char **pp,
|
||||
long length) {
|
||||
const unsigned char *p = *pp;
|
||||
long len;
|
||||
int inf, tag, xclass;
|
||||
int i = 0;
|
||||
|
||||
p = *pp;
|
||||
inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
i = ASN1_R_BAD_OBJECT_HEADER;
|
||||
goto err;
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_BOOLEAN) {
|
||||
i = ASN1_R_EXPECTING_A_BOOLEAN;
|
||||
goto err;
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_BOOLEAN || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_A_BOOLEAN);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len != 1) {
|
||||
i = ASN1_R_BOOLEAN_IS_WRONG_LENGTH;
|
||||
goto err;
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
ret = (int)*(p++);
|
||||
if (a != NULL)
|
||||
ASN1_BOOLEAN ret = (ASN1_BOOLEAN)*(p++);
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
return (ret);
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -78,7 +78,6 @@ void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_FP_API
|
||||
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
|
||||
{
|
||||
BIO *b = BIO_new_fp(in, BIO_NOCLOSE);
|
||||
@@ -90,4 +89,3 @@ void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
|
||||
BIO_free(b);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
@@ -62,7 +62,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
|
||||
{
|
||||
@@ -237,6 +237,11 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ts->tm_year < 0 - 1900 || ts->tm_year > 9999 - 1900) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
p = (char *)tmps->data;
|
||||
if ((p == NULL) || ((size_t)tmps->length < len)) {
|
||||
p = OPENSSL_malloc(len);
|
||||
|
||||
@@ -63,11 +63,9 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
#include "../bytestring/internal.h"
|
||||
|
||||
static int is_printable(uint32_t value);
|
||||
|
||||
/*
|
||||
* These functions take a string in UTF8, ASCII or multibyte form and a mask
|
||||
* of permissible ASN1 string types. It then works out the minimal type
|
||||
@@ -153,7 +151,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
}
|
||||
|
||||
/* Update which output formats are still possible. */
|
||||
if ((mask & B_ASN1_PRINTABLESTRING) && !is_printable(c)) {
|
||||
if ((mask & B_ASN1_PRINTABLESTRING) && !asn1_is_printable(c)) {
|
||||
mask &= ~B_ASN1_PRINTABLESTRING;
|
||||
}
|
||||
if ((mask & B_ASN1_IA5STRING) && (c > 127)) {
|
||||
@@ -208,11 +206,14 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
encode_func = cbb_add_utf32_be;
|
||||
size_estimate = 4 * nchar;
|
||||
outform = MBSTRING_UNIV;
|
||||
} else {
|
||||
} else if (mask & B_ASN1_UTF8STRING) {
|
||||
str_type = V_ASN1_UTF8STRING;
|
||||
outform = MBSTRING_UTF8;
|
||||
encode_func = cbb_add_utf8;
|
||||
size_estimate = utf8_len;
|
||||
} else {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out)
|
||||
@@ -282,24 +283,16 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Return 1 if the character is permitted in a PrintableString */
|
||||
static int is_printable(uint32_t value)
|
||||
int asn1_is_printable(uint32_t value)
|
||||
{
|
||||
int ch;
|
||||
if (value > 0x7f)
|
||||
if (value > 0x7f) {
|
||||
return 0;
|
||||
ch = (int)value;
|
||||
/*
|
||||
* Note: we can't use 'isalnum' because certain accented characters may
|
||||
* count as alphanumeric in some environments.
|
||||
*/
|
||||
if ((ch >= 'a') && (ch <= 'z'))
|
||||
return 1;
|
||||
if ((ch >= 'A') && (ch <= 'Z'))
|
||||
return 1;
|
||||
if ((ch >= '0') && (ch <= '9'))
|
||||
return 1;
|
||||
if ((ch == ' ') || strchr("'()+,-./:=?", ch))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
/* Note we cannot use |isalnum| because it is locale-dependent. */
|
||||
return ('a' <= value && value <= 'z') || //
|
||||
('A' <= value && value <= 'Z') || //
|
||||
('0' <= value && value <= '9') || //
|
||||
value == ' ' || value == '\'' || value == '(' || value == ')' ||
|
||||
value == '+' || value == ',' || value == '-' || value == '.' ||
|
||||
value == '/' || value == ':' || value == '=' || value == '?';
|
||||
}
|
||||
@@ -64,24 +64,31 @@
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
|
||||
{
|
||||
unsigned char *p, *allocated = NULL;
|
||||
int objsize;
|
||||
if (a == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((a == NULL) || (a->data == NULL))
|
||||
return (0);
|
||||
if (a->length == 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
|
||||
if (pp == NULL || objsize == -1)
|
||||
int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
|
||||
if (pp == NULL || objsize == -1) {
|
||||
return objsize;
|
||||
}
|
||||
|
||||
unsigned char *p, *allocated = NULL;
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
@@ -103,54 +110,65 @@ int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
|
||||
return OBJ_obj2txt(buf, buf_len, a, 0);
|
||||
}
|
||||
|
||||
static int write_str(BIO *bp, const char *str)
|
||||
{
|
||||
int len = strlen(str);
|
||||
return BIO_write(bp, str, len) == len ? len : -1;
|
||||
}
|
||||
|
||||
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
|
||||
{
|
||||
char buf[80], *p = buf;
|
||||
int i;
|
||||
|
||||
if ((a == NULL) || (a->data == NULL))
|
||||
return (BIO_write(bp, "NULL", 4));
|
||||
i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
|
||||
if (i > (int)(sizeof(buf) - 1)) {
|
||||
p = OPENSSL_malloc(i + 1);
|
||||
if (!p)
|
||||
return -1;
|
||||
i2t_ASN1_OBJECT(p, i + 1, a);
|
||||
if (a == NULL || a->data == NULL) {
|
||||
return write_str(bp, "NULL");
|
||||
}
|
||||
if (i <= 0)
|
||||
return BIO_write(bp, "<INVALID>", 9);
|
||||
BIO_write(bp, p, i);
|
||||
if (p != buf)
|
||||
OPENSSL_free(p);
|
||||
return (i);
|
||||
|
||||
char buf[80], *allocated = NULL;
|
||||
const char *str = buf;
|
||||
int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
|
||||
if (len > (int)sizeof(buf) - 1) {
|
||||
/* The input was truncated. Allocate a buffer that fits. */
|
||||
allocated = OPENSSL_malloc(len + 1);
|
||||
if (allocated == NULL) {
|
||||
return -1;
|
||||
}
|
||||
len = i2t_ASN1_OBJECT(allocated, len + 1, a);
|
||||
str = allocated;
|
||||
}
|
||||
if (len <= 0) {
|
||||
str = "<INVALID>";
|
||||
}
|
||||
|
||||
int ret = write_str(bp, str);
|
||||
OPENSSL_free(allocated);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
long length)
|
||||
{
|
||||
const unsigned char *p;
|
||||
long len;
|
||||
int tag, xclass;
|
||||
int inf, i;
|
||||
ASN1_OBJECT *ret = NULL;
|
||||
p = *pp;
|
||||
inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
const unsigned char *p = *pp;
|
||||
int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
i = ASN1_R_BAD_OBJECT_HEADER;
|
||||
goto err;
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_OBJECT) {
|
||||
i = ASN1_R_EXPECTING_AN_OBJECT;
|
||||
goto err;
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return NULL;
|
||||
}
|
||||
ret = c2i_ASN1_OBJECT(a, &p, len);
|
||||
if (ret)
|
||||
|
||||
if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_AN_OBJECT);
|
||||
return NULL;
|
||||
}
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(a, &p, len);
|
||||
if (ret) {
|
||||
*pp = p;
|
||||
}
|
||||
return ret;
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
@@ -180,16 +198,13 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* only the ASN1_OBJECTs from the 'table' will have values for ->sn or
|
||||
* ->ln
|
||||
*/
|
||||
if ((a == NULL) || ((*a) == NULL) ||
|
||||
!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
|
||||
if ((ret = ASN1_OBJECT_new()) == NULL)
|
||||
return (NULL);
|
||||
} else
|
||||
} else {
|
||||
ret = (*a);
|
||||
}
|
||||
|
||||
p = *pp;
|
||||
/* detach data from object */
|
||||
@@ -208,12 +223,17 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
}
|
||||
OPENSSL_memcpy(data, p, length);
|
||||
/* If there are dynamic strings, free them here, and clear the flag */
|
||||
if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
|
||||
OPENSSL_free((char *)ret->sn);
|
||||
OPENSSL_free((char *)ret->ln);
|
||||
ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
|
||||
}
|
||||
/* reattach data to object, after which it remains const */
|
||||
ret->data = data;
|
||||
ret->length = length;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
/* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
|
||||
p += length;
|
||||
|
||||
if (a != NULL)
|
||||
@@ -263,7 +283,7 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a)
|
||||
OPENSSL_free(a);
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
|
||||
const char *sn, const char *ln)
|
||||
{
|
||||
ASN1_OBJECT o;
|
||||
|
||||
@@ -56,38 +56,28 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_PRINTABLE_type(const unsigned char *s, int len)
|
||||
{
|
||||
int c;
|
||||
int ia5 = 0;
|
||||
int t61 = 0;
|
||||
|
||||
if (len <= 0)
|
||||
len = -1;
|
||||
if (s == NULL)
|
||||
return (V_ASN1_PRINTABLESTRING);
|
||||
|
||||
while ((*s) && (len-- != 0)) {
|
||||
c = *(s++);
|
||||
if (!(((c >= 'a') && (c <= 'z')) ||
|
||||
((c >= 'A') && (c <= 'Z')) ||
|
||||
(c == ' ') ||
|
||||
((c >= '0') && (c <= '9')) ||
|
||||
(c == ' ') || (c == '\'') ||
|
||||
(c == '(') || (c == ')') ||
|
||||
(c == '+') || (c == ',') ||
|
||||
(c == '-') || (c == '.') ||
|
||||
(c == '/') || (c == ':') || (c == '=') || (c == '?')))
|
||||
ia5 = 1;
|
||||
if (c & 0x80)
|
||||
t61 = 1;
|
||||
if (len < 0) {
|
||||
len = strlen((const char *)s);
|
||||
}
|
||||
if (t61)
|
||||
return (V_ASN1_T61STRING);
|
||||
if (ia5)
|
||||
return (V_ASN1_IA5STRING);
|
||||
return (V_ASN1_PRINTABLESTRING);
|
||||
|
||||
int printable = 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
unsigned char c = s[i];
|
||||
if (c & 0x80) {
|
||||
/* No need to continue iterating. */
|
||||
return V_ASN1_T61STRING;
|
||||
}
|
||||
if (!asn1_is_printable(c)) {
|
||||
printable = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return printable ? V_ASN1_PRINTABLESTRING : V_ASN1_IA5STRING;
|
||||
}
|
||||
+269
-272
@@ -54,23 +54,27 @@
|
||||
* copied and put under another distribution licence
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <CCryptoBoringSSL_x509.h>
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "charmap.h"
|
||||
#include "../asn1/asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
|
||||
* printing routines handling multibyte characters, RFC2253 and a host of
|
||||
* other options.
|
||||
*/
|
||||
|
||||
// These flags must be distinct from |ESC_FLAGS| and fit in a byte.
|
||||
|
||||
// Character is a valid PrintableString character
|
||||
#define CHARTYPE_PRINTABLESTRING 0x10
|
||||
// Character needs escaping if it is the first character
|
||||
#define CHARTYPE_FIRST_ESC_2253 0x20
|
||||
// Character needs escaping if it is the last character
|
||||
#define CHARTYPE_LAST_ESC_2253 0x40
|
||||
|
||||
#define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
|
||||
|
||||
@@ -79,26 +83,12 @@
|
||||
ASN1_STRFLGS_ESC_CTRL | \
|
||||
ASN1_STRFLGS_ESC_MSB)
|
||||
|
||||
static int send_bio_chars(void *arg, const void *buf, int len)
|
||||
static int maybe_write(BIO *out, const void *buf, int len)
|
||||
{
|
||||
if (!arg)
|
||||
return 1;
|
||||
if (BIO_write(arg, buf, len) != len)
|
||||
return 0;
|
||||
return 1;
|
||||
/* If |out| is NULL, ignore the output but report the length. */
|
||||
return out == NULL || BIO_write(out, buf, len) == len;
|
||||
}
|
||||
|
||||
static int send_fp_chars(void *arg, const void *buf, int len)
|
||||
{
|
||||
if (!arg)
|
||||
return 1;
|
||||
if (fwrite(buf, 1, len, arg) != (unsigned int)len)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
typedef int char_io (void *arg, const void *buf, int len);
|
||||
|
||||
/*
|
||||
* This function handles display of strings, one character at a time. It is
|
||||
* passed an unsigned long for each character because it could come from 2 or
|
||||
@@ -108,20 +98,20 @@ typedef int char_io (void *arg, const void *buf, int len);
|
||||
#define HEX_SIZE(type) (sizeof(type)*2)
|
||||
|
||||
static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
char_io *io_ch, void *arg)
|
||||
BIO *out)
|
||||
{
|
||||
unsigned char chflgs, chtmp;
|
||||
char tmphex[HEX_SIZE(uint32_t) + 3];
|
||||
|
||||
if (c > 0xffff) {
|
||||
BIO_snprintf(tmphex, sizeof tmphex, "\\W%08" PRIX32, c);
|
||||
if (!io_ch(arg, tmphex, 10))
|
||||
if (!maybe_write(out, tmphex, 10))
|
||||
return -1;
|
||||
return 10;
|
||||
}
|
||||
if (c > 0xff) {
|
||||
BIO_snprintf(tmphex, sizeof tmphex, "\\U%04" PRIX32, c);
|
||||
if (!io_ch(arg, tmphex, 6))
|
||||
if (!maybe_write(out, tmphex, 6))
|
||||
return -1;
|
||||
return 6;
|
||||
}
|
||||
@@ -135,19 +125,19 @@ static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
|
||||
if (do_quotes)
|
||||
*do_quotes = 1;
|
||||
if (!io_ch(arg, &chtmp, 1))
|
||||
if (!maybe_write(out, &chtmp, 1))
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
if (!io_ch(arg, "\\", 1))
|
||||
if (!maybe_write(out, "\\", 1))
|
||||
return -1;
|
||||
if (!io_ch(arg, &chtmp, 1))
|
||||
if (!maybe_write(out, &chtmp, 1))
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
if (chflgs & (ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB)) {
|
||||
BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
|
||||
if (!io_ch(arg, tmphex, 3))
|
||||
if (!maybe_write(out, tmphex, 3))
|
||||
return -1;
|
||||
return 3;
|
||||
}
|
||||
@@ -156,11 +146,11 @@ static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
* character itself: backslash.
|
||||
*/
|
||||
if (chtmp == '\\' && flags & ESC_FLAGS) {
|
||||
if (!io_ch(arg, "\\\\", 2))
|
||||
if (!maybe_write(out, "\\\\", 2))
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
if (!io_ch(arg, &chtmp, 1))
|
||||
if (!maybe_write(out, &chtmp, 1))
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
@@ -175,8 +165,7 @@ static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
*/
|
||||
|
||||
static int do_buf(unsigned char *buf, int buflen,
|
||||
int type, unsigned char flags, char *quotes, char_io *io_ch,
|
||||
void *arg)
|
||||
int type, unsigned char flags, char *quotes, BIO *out)
|
||||
{
|
||||
int i, outlen, len, charwidth;
|
||||
unsigned char orflags, *p, *q;
|
||||
@@ -208,6 +197,8 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
orflags = CHARTYPE_FIRST_ESC_2253;
|
||||
else
|
||||
orflags = 0;
|
||||
/* TODO(davidben): Replace this with |cbs_get_ucs2_be|, etc., to check
|
||||
* for invalid codepoints. */
|
||||
switch (charwidth) {
|
||||
case 4:
|
||||
c = ((uint32_t)*p++) << 24;
|
||||
@@ -248,17 +239,14 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
* otherwise each character will be > 0x7f and so the
|
||||
* character will never be escaped on first and last.
|
||||
*/
|
||||
len =
|
||||
do_esc_char(utfbuf[i], (unsigned char)(flags | orflags),
|
||||
quotes, io_ch, arg);
|
||||
len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags),
|
||||
quotes, out);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
}
|
||||
} else {
|
||||
len =
|
||||
do_esc_char(c, (unsigned char)(flags | orflags), quotes,
|
||||
io_ch, arg);
|
||||
len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, out);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
@@ -269,19 +257,18 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
|
||||
/* This function hex dumps a buffer of characters */
|
||||
|
||||
static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
|
||||
int buflen)
|
||||
static int do_hex_dump(BIO *out, unsigned char *buf, int buflen)
|
||||
{
|
||||
static const char hexdig[] = "0123456789ABCDEF";
|
||||
unsigned char *p, *q;
|
||||
char hextmp[2];
|
||||
if (arg) {
|
||||
if (out) {
|
||||
p = buf;
|
||||
q = buf + buflen;
|
||||
while (p != q) {
|
||||
hextmp[0] = hexdig[*p >> 4];
|
||||
hextmp[1] = hexdig[*p & 0xf];
|
||||
if (!io_ch(arg, hextmp, 2))
|
||||
if (!maybe_write(out, hextmp, 2))
|
||||
return -1;
|
||||
p++;
|
||||
}
|
||||
@@ -292,41 +279,55 @@ static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
|
||||
/*
|
||||
* "dump" a string. This is done when the type is unknown, or the flags
|
||||
* request it. We can either dump the content octets or the entire DER
|
||||
* encoding. This uses the RFC2253 #01234 format.
|
||||
* encoding. This uses the RFC 2253 #01234 format.
|
||||
*/
|
||||
|
||||
static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
|
||||
const ASN1_STRING *str)
|
||||
static int do_dump(unsigned long lflags, BIO *out, const ASN1_STRING *str)
|
||||
{
|
||||
/*
|
||||
* Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
|
||||
* readily obtained
|
||||
*/
|
||||
ASN1_TYPE t;
|
||||
unsigned char *der_buf, *p;
|
||||
int outlen, der_len;
|
||||
|
||||
if (!io_ch(arg, "#", 1))
|
||||
if (!maybe_write(out, "#", 1)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If we don't dump DER encoding just dump content octets */
|
||||
if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
|
||||
outlen = do_hex_dump(io_ch, arg, str->data, str->length);
|
||||
if (outlen < 0)
|
||||
int outlen = do_hex_dump(out, str->data, str->length);
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding
|
||||
* to readily obtained.
|
||||
*/
|
||||
ASN1_TYPE t;
|
||||
t.type = str->type;
|
||||
t.value.ptr = (char *)str;
|
||||
der_len = i2d_ASN1_TYPE(&t, NULL);
|
||||
der_buf = OPENSSL_malloc(der_len);
|
||||
if (!der_buf)
|
||||
/* Negative INTEGER and ENUMERATED values are the only case where
|
||||
* |ASN1_STRING| and |ASN1_TYPE| types do not match.
|
||||
*
|
||||
* TODO(davidben): There are also some type fields which, in |ASN1_TYPE|, do
|
||||
* not correspond to |ASN1_STRING|. It is unclear whether those are allowed
|
||||
* in |ASN1_STRING| at all, or what the space of allowed types is.
|
||||
* |ASN1_item_ex_d2i| will never produce such a value so, for now, we say
|
||||
* this is an invalid input. But this corner of the library in general
|
||||
* should be more robust. */
|
||||
if (t.type == V_ASN1_NEG_INTEGER) {
|
||||
t.type = V_ASN1_INTEGER;
|
||||
} else if (t.type == V_ASN1_NEG_ENUMERATED) {
|
||||
t.type = V_ASN1_ENUMERATED;
|
||||
}
|
||||
t.value.asn1_string = (ASN1_STRING *)str;
|
||||
unsigned char *der_buf = NULL;
|
||||
int der_len = i2d_ASN1_TYPE(&t, &der_buf);
|
||||
if (der_len < 0) {
|
||||
return -1;
|
||||
p = der_buf;
|
||||
i2d_ASN1_TYPE(&t, &p);
|
||||
outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
|
||||
}
|
||||
int outlen = do_hex_dump(out, der_buf, der_len);
|
||||
OPENSSL_free(der_buf);
|
||||
if (outlen < 0)
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
@@ -353,8 +354,7 @@ static const signed char tag2nbyte[] = {
|
||||
* an error occurred.
|
||||
*/
|
||||
|
||||
static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
const ASN1_STRING *str)
|
||||
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long lflags)
|
||||
{
|
||||
int outlen, len;
|
||||
int type;
|
||||
@@ -372,7 +372,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
const char *tagname;
|
||||
tagname = ASN1_tag2str(type);
|
||||
outlen += strlen(tagname);
|
||||
if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
|
||||
if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1))
|
||||
return -1;
|
||||
outlen++;
|
||||
}
|
||||
@@ -396,7 +396,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
}
|
||||
|
||||
if (type == -1) {
|
||||
len = do_dump(lflags, io_ch, arg, str);
|
||||
len = do_dump(lflags, out, str);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
@@ -415,219 +415,41 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
type |= BUF_TYPE_CONVUTF8;
|
||||
}
|
||||
|
||||
len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);
|
||||
len = do_buf(str->data, str->length, type, flags, "es, NULL);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
if (quotes)
|
||||
outlen += 2;
|
||||
if (!arg)
|
||||
if (!out)
|
||||
return outlen;
|
||||
if (quotes && !io_ch(arg, "\"", 1))
|
||||
if (quotes && !maybe_write(out, "\"", 1))
|
||||
return -1;
|
||||
if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
|
||||
if (do_buf(str->data, str->length, type, flags, NULL, out) < 0)
|
||||
return -1;
|
||||
if (quotes && !io_ch(arg, "\"", 1))
|
||||
if (quotes && !maybe_write(out, "\"", 1))
|
||||
return -1;
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/* Used for line indenting: print 'indent' spaces */
|
||||
|
||||
static int do_indent(char_io *io_ch, void *arg, int indent)
|
||||
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str,
|
||||
unsigned long flags)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < indent; i++)
|
||||
if (!io_ch(arg, " ", 1))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define FN_WIDTH_LN 25
|
||||
#define FN_WIDTH_SN 10
|
||||
|
||||
static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,
|
||||
int indent, unsigned long flags)
|
||||
{
|
||||
int i, prev = -1, orflags, cnt;
|
||||
int fn_opt, fn_nid;
|
||||
ASN1_OBJECT *fn;
|
||||
ASN1_STRING *val;
|
||||
X509_NAME_ENTRY *ent;
|
||||
char objtmp[80];
|
||||
const char *objbuf;
|
||||
int outlen, len;
|
||||
const char *sep_dn, *sep_mv, *sep_eq;
|
||||
int sep_dn_len, sep_mv_len, sep_eq_len;
|
||||
if (indent < 0)
|
||||
indent = 0;
|
||||
outlen = indent;
|
||||
if (!do_indent(io_ch, arg, indent))
|
||||
return -1;
|
||||
switch (flags & XN_FLAG_SEP_MASK) {
|
||||
case XN_FLAG_SEP_MULTILINE:
|
||||
sep_dn = "\n";
|
||||
sep_dn_len = 1;
|
||||
sep_mv = " + ";
|
||||
sep_mv_len = 3;
|
||||
break;
|
||||
|
||||
case XN_FLAG_SEP_COMMA_PLUS:
|
||||
sep_dn = ",";
|
||||
sep_dn_len = 1;
|
||||
sep_mv = "+";
|
||||
sep_mv_len = 1;
|
||||
indent = 0;
|
||||
break;
|
||||
|
||||
case XN_FLAG_SEP_CPLUS_SPC:
|
||||
sep_dn = ", ";
|
||||
sep_dn_len = 2;
|
||||
sep_mv = " + ";
|
||||
sep_mv_len = 3;
|
||||
indent = 0;
|
||||
break;
|
||||
|
||||
case XN_FLAG_SEP_SPLUS_SPC:
|
||||
sep_dn = "; ";
|
||||
sep_dn_len = 2;
|
||||
sep_mv = " + ";
|
||||
sep_mv_len = 3;
|
||||
indent = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (flags & XN_FLAG_SPC_EQ) {
|
||||
sep_eq = " = ";
|
||||
sep_eq_len = 3;
|
||||
} else {
|
||||
sep_eq = "=";
|
||||
sep_eq_len = 1;
|
||||
}
|
||||
|
||||
fn_opt = flags & XN_FLAG_FN_MASK;
|
||||
|
||||
cnt = X509_NAME_entry_count(n);
|
||||
for (i = 0; i < cnt; i++) {
|
||||
if (flags & XN_FLAG_DN_REV)
|
||||
ent = X509_NAME_get_entry(n, cnt - i - 1);
|
||||
else
|
||||
ent = X509_NAME_get_entry(n, i);
|
||||
if (prev != -1) {
|
||||
if (prev == ent->set) {
|
||||
if (!io_ch(arg, sep_mv, sep_mv_len))
|
||||
return -1;
|
||||
outlen += sep_mv_len;
|
||||
} else {
|
||||
if (!io_ch(arg, sep_dn, sep_dn_len))
|
||||
return -1;
|
||||
outlen += sep_dn_len;
|
||||
if (!do_indent(io_ch, arg, indent))
|
||||
return -1;
|
||||
outlen += indent;
|
||||
}
|
||||
}
|
||||
prev = ent->set;
|
||||
fn = X509_NAME_ENTRY_get_object(ent);
|
||||
val = X509_NAME_ENTRY_get_data(ent);
|
||||
fn_nid = OBJ_obj2nid(fn);
|
||||
if (fn_opt != XN_FLAG_FN_NONE) {
|
||||
int objlen, fld_len;
|
||||
if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
|
||||
OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
|
||||
fld_len = 0; /* XXX: what should this be? */
|
||||
objbuf = objtmp;
|
||||
} else {
|
||||
if (fn_opt == XN_FLAG_FN_SN) {
|
||||
fld_len = FN_WIDTH_SN;
|
||||
objbuf = OBJ_nid2sn(fn_nid);
|
||||
} else if (fn_opt == XN_FLAG_FN_LN) {
|
||||
fld_len = FN_WIDTH_LN;
|
||||
objbuf = OBJ_nid2ln(fn_nid);
|
||||
} else {
|
||||
fld_len = 0; /* XXX: what should this be? */
|
||||
objbuf = "";
|
||||
}
|
||||
}
|
||||
objlen = strlen(objbuf);
|
||||
if (!io_ch(arg, objbuf, objlen))
|
||||
return -1;
|
||||
if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
|
||||
if (!do_indent(io_ch, arg, fld_len - objlen))
|
||||
return -1;
|
||||
outlen += fld_len - objlen;
|
||||
}
|
||||
if (!io_ch(arg, sep_eq, sep_eq_len))
|
||||
return -1;
|
||||
outlen += objlen + sep_eq_len;
|
||||
}
|
||||
/*
|
||||
* If the field name is unknown then fix up the DER dump flag. We
|
||||
* might want to limit this further so it will DER dump on anything
|
||||
* other than a few 'standard' fields.
|
||||
*/
|
||||
if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
|
||||
orflags = ASN1_STRFLGS_DUMP_ALL;
|
||||
else
|
||||
orflags = 0;
|
||||
|
||||
len = do_print_ex(io_ch, arg, flags | orflags, val);
|
||||
if (len < 0)
|
||||
BIO *bio = NULL;
|
||||
if (fp != NULL) {
|
||||
/* If |fp| is NULL, this function returns the number of bytes without
|
||||
* writing. */
|
||||
bio = BIO_new_fp(fp, BIO_NOCLOSE);
|
||||
if (bio == NULL) {
|
||||
return -1;
|
||||
outlen += len;
|
||||
}
|
||||
}
|
||||
return outlen;
|
||||
int ret = ASN1_STRING_print_ex(bio, str, flags);
|
||||
BIO_free(bio);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Wrappers round the main functions */
|
||||
|
||||
int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
|
||||
unsigned long flags)
|
||||
{
|
||||
if (flags == XN_FLAG_COMPAT)
|
||||
return X509_NAME_print(out, nm, indent);
|
||||
return do_name_ex(send_bio_chars, out, nm, indent, flags);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_FP_API
|
||||
int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
|
||||
unsigned long flags)
|
||||
{
|
||||
if (flags == XN_FLAG_COMPAT) {
|
||||
BIO *btmp;
|
||||
int ret;
|
||||
btmp = BIO_new_fp(fp, BIO_NOCLOSE);
|
||||
if (!btmp)
|
||||
return -1;
|
||||
ret = X509_NAME_print(btmp, nm, indent);
|
||||
BIO_free(btmp);
|
||||
return ret;
|
||||
}
|
||||
return do_name_ex(send_fp_chars, fp, nm, indent, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
|
||||
{
|
||||
return do_print_ex(send_bio_chars, out, flags, str);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_FP_API
|
||||
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)
|
||||
{
|
||||
return do_print_ex(send_fp_chars, fp, flags, str);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Utility function: convert any string type to UTF8, returns number of bytes
|
||||
* in output string or a negative error code
|
||||
*/
|
||||
|
||||
int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
|
||||
int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
|
||||
{
|
||||
ASN1_STRING stmp, *str = &stmp;
|
||||
int mbflag, type, ret;
|
||||
@@ -643,11 +465,186 @@ int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
|
||||
stmp.data = NULL;
|
||||
stmp.length = 0;
|
||||
stmp.flags = 0;
|
||||
ret =
|
||||
ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
|
||||
B_ASN1_UTF8STRING);
|
||||
ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
|
||||
B_ASN1_UTF8STRING);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
*out = stmp.data;
|
||||
return stmp.length;
|
||||
}
|
||||
|
||||
int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
|
||||
{
|
||||
int i, n;
|
||||
char buf[80];
|
||||
const char *p;
|
||||
|
||||
if (v == NULL)
|
||||
return (0);
|
||||
n = 0;
|
||||
p = (const char *)v->data;
|
||||
for (i = 0; i < v->length; i++) {
|
||||
if ((p[i] > '~') || ((p[i] < ' ') &&
|
||||
(p[i] != '\n') && (p[i] != '\r')))
|
||||
buf[n] = '.';
|
||||
else
|
||||
buf[n] = p[i];
|
||||
n++;
|
||||
if (n >= 80) {
|
||||
if (BIO_write(bp, buf, n) <= 0)
|
||||
return (0);
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
if (n > 0)
|
||||
if (BIO_write(bp, buf, n) <= 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
|
||||
{
|
||||
if (tm->type == V_ASN1_UTCTIME)
|
||||
return ASN1_UTCTIME_print(bp, tm);
|
||||
if (tm->type == V_ASN1_GENERALIZEDTIME)
|
||||
return ASN1_GENERALIZEDTIME_print(bp, tm);
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static const char *const mon[12] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
|
||||
int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
|
||||
{
|
||||
char *v;
|
||||
int gmt = 0;
|
||||
int i;
|
||||
int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
|
||||
char *f = NULL;
|
||||
int f_len = 0;
|
||||
|
||||
i = tm->length;
|
||||
v = (char *)tm->data;
|
||||
|
||||
if (i < 12)
|
||||
goto err;
|
||||
if (v[i - 1] == 'Z')
|
||||
gmt = 1;
|
||||
for (i = 0; i < 12; i++)
|
||||
if ((v[i] > '9') || (v[i] < '0'))
|
||||
goto err;
|
||||
y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 + (v[2] - '0') * 10 + (v[3] -
|
||||
'0');
|
||||
M = (v[4] - '0') * 10 + (v[5] - '0');
|
||||
if ((M > 12) || (M < 1))
|
||||
goto err;
|
||||
d = (v[6] - '0') * 10 + (v[7] - '0');
|
||||
h = (v[8] - '0') * 10 + (v[9] - '0');
|
||||
m = (v[10] - '0') * 10 + (v[11] - '0');
|
||||
if (tm->length >= 14 &&
|
||||
(v[12] >= '0') && (v[12] <= '9') &&
|
||||
(v[13] >= '0') && (v[13] <= '9')) {
|
||||
s = (v[12] - '0') * 10 + (v[13] - '0');
|
||||
/* Check for fractions of seconds. */
|
||||
if (tm->length >= 15 && v[14] == '.') {
|
||||
int l = tm->length;
|
||||
f = &v[14]; /* The decimal point. */
|
||||
f_len = 1;
|
||||
while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
|
||||
++f_len;
|
||||
}
|
||||
}
|
||||
|
||||
if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
|
||||
mon[M - 1], d, h, m, s, f_len, f, y,
|
||||
(gmt) ? " GMT" : "") <= 0)
|
||||
return (0);
|
||||
else
|
||||
return (1);
|
||||
err:
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
// consume_two_digits is a helper function for ASN1_UTCTIME_print. If |*v|,
|
||||
// assumed to be |*len| bytes long, has two leading digits, updates |*out| with
|
||||
// their value, updates |v| and |len|, and returns one. Otherwise, returns
|
||||
// zero.
|
||||
static int consume_two_digits(int* out, const char **v, int *len) {
|
||||
if (*len < 2|| !isdigit((*v)[0]) || !isdigit((*v)[1])) {
|
||||
return 0;
|
||||
}
|
||||
*out = ((*v)[0] - '0') * 10 + ((*v)[1] - '0');
|
||||
*len -= 2;
|
||||
*v += 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// consume_zulu_timezone is a helper function for ASN1_UTCTIME_print. If |*v|,
|
||||
// assumed to be |*len| bytes long, starts with "Z" then it updates |*v| and
|
||||
// |*len| and returns one. Otherwise returns zero.
|
||||
static int consume_zulu_timezone(const char **v, int *len) {
|
||||
if (*len == 0 || (*v)[0] != 'Z') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*len -= 1;
|
||||
*v += 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) {
|
||||
const char *v = (const char *)tm->data;
|
||||
int len = tm->length;
|
||||
int Y = 0, M = 0, D = 0, h = 0, m = 0, s = 0;
|
||||
|
||||
// YYMMDDhhmm are required to be present.
|
||||
if (!consume_two_digits(&Y, &v, &len) ||
|
||||
!consume_two_digits(&M, &v, &len) ||
|
||||
!consume_two_digits(&D, &v, &len) ||
|
||||
!consume_two_digits(&h, &v, &len) ||
|
||||
!consume_two_digits(&m, &v, &len)) {
|
||||
goto err;
|
||||
}
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires seconds
|
||||
// to be present, but historically this code has forgiven its absence.
|
||||
consume_two_digits(&s, &v, &len);
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, specifies this
|
||||
// interpretation of the year.
|
||||
if (Y < 50) {
|
||||
Y += 2000;
|
||||
} else {
|
||||
Y += 1900;
|
||||
}
|
||||
if (M > 12 || M == 0) {
|
||||
goto err;
|
||||
}
|
||||
if (D > 31 || D == 0) {
|
||||
goto err;
|
||||
}
|
||||
if (h > 23 || m > 59 || s > 60) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires the "Z"
|
||||
// to be present, but historically this code has forgiven its absence.
|
||||
const int is_gmt = consume_zulu_timezone(&v, &len);
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, does not permit
|
||||
// the specification of timezones using the +hhmm / -hhmm syntax, which is
|
||||
// the only other thing that might legitimately be found at the end.
|
||||
if (len) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", mon[M - 1], D, h, m, s, Y,
|
||||
is_gmt ? " GMT" : "") > 0;
|
||||
|
||||
err:
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return 0;
|
||||
}
|
||||
@@ -56,98 +56,68 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <stdlib.h> /* For bsearch */
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
#include <CCryptoBoringSSL_stack.h>
|
||||
|
||||
DEFINE_STACK_OF(ASN1_STRING_TABLE)
|
||||
#include "../internal.h"
|
||||
#include "../lhash/internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
|
||||
static void st_free(ASN1_STRING_TABLE *tbl);
|
||||
|
||||
/*
|
||||
* This is the global mask for the mbstring functions: this is use to mask
|
||||
* out certain types (such as BMPString and UTF8String) because certain
|
||||
* software (e.g. Netscape) has problems with them.
|
||||
*/
|
||||
DEFINE_LHASH_OF(ASN1_STRING_TABLE)
|
||||
|
||||
static unsigned long global_mask = B_ASN1_UTF8STRING;
|
||||
static LHASH_OF(ASN1_STRING_TABLE) *string_tables = NULL;
|
||||
static struct CRYPTO_STATIC_MUTEX string_tables_lock = CRYPTO_STATIC_MUTEX_INIT;
|
||||
|
||||
void ASN1_STRING_set_default_mask(unsigned long mask)
|
||||
{
|
||||
global_mask = mask;
|
||||
}
|
||||
|
||||
unsigned long ASN1_STRING_get_default_mask(void)
|
||||
{
|
||||
return global_mask;
|
||||
return B_ASN1_UTF8STRING;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function sets the default to various "flavours" of configuration.
|
||||
* based on an ASCII string. Currently this is: MASK:XXXX : a numerical mask
|
||||
* value. nobmp : Don't use BMPStrings (just Printable, T61). pkix : PKIX
|
||||
* recommendation in RFC2459. utf8only : only use UTF8Strings (RFC2459
|
||||
* recommendation for 2004). default: the default value, Printable, T61, BMP.
|
||||
*/
|
||||
|
||||
int ASN1_STRING_set_default_mask_asc(const char *p)
|
||||
{
|
||||
unsigned long mask;
|
||||
char *end;
|
||||
if (!strncmp(p, "MASK:", 5)) {
|
||||
if (!p[5])
|
||||
return 0;
|
||||
mask = strtoul(p + 5, &end, 0);
|
||||
if (*end)
|
||||
return 0;
|
||||
} else if (!strcmp(p, "nombstr"))
|
||||
mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
|
||||
else if (!strcmp(p, "pkix"))
|
||||
mask = ~((unsigned long)B_ASN1_T61STRING);
|
||||
else if (!strcmp(p, "utf8only"))
|
||||
mask = B_ASN1_UTF8STRING;
|
||||
else if (!strcmp(p, "default"))
|
||||
mask = 0xFFFFFFFFL;
|
||||
else
|
||||
return 0;
|
||||
ASN1_STRING_set_default_mask(mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const ASN1_STRING_TABLE *asn1_string_table_get(int nid);
|
||||
|
||||
/*
|
||||
* The following function generates an ASN1_STRING based on limits in a
|
||||
* table. Frequently the types and length of an ASN1_STRING are restricted by
|
||||
* a corresponding OID. For example certificates and certificate requests.
|
||||
*/
|
||||
|
||||
ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
|
||||
const unsigned char *in, int inlen,
|
||||
int inform, int nid)
|
||||
ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
|
||||
int len, int inform, int nid)
|
||||
{
|
||||
ASN1_STRING_TABLE *tbl;
|
||||
ASN1_STRING *str = NULL;
|
||||
unsigned long mask;
|
||||
int ret;
|
||||
if (!out)
|
||||
if (!out) {
|
||||
out = &str;
|
||||
tbl = ASN1_STRING_TABLE_get(nid);
|
||||
if (tbl) {
|
||||
mask = tbl->mask;
|
||||
if (!(tbl->flags & STABLE_NO_MASK))
|
||||
mask &= global_mask;
|
||||
ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
|
||||
tbl->minsize, tbl->maxsize);
|
||||
} else
|
||||
ret =
|
||||
ASN1_mbstring_copy(out, in, inlen, inform,
|
||||
DIRSTRING_TYPE & global_mask);
|
||||
if (ret <= 0)
|
||||
}
|
||||
const ASN1_STRING_TABLE *tbl = asn1_string_table_get(nid);
|
||||
if (tbl != NULL) {
|
||||
unsigned long mask = tbl->mask;
|
||||
if (!(tbl->flags & STABLE_NO_MASK)) {
|
||||
mask &= B_ASN1_UTF8STRING;
|
||||
}
|
||||
ret = ASN1_mbstring_ncopy(out, in, len, inform, mask, tbl->minsize,
|
||||
tbl->maxsize);
|
||||
} else {
|
||||
ret = ASN1_mbstring_copy(out, in, len, inform, B_ASN1_UTF8STRING);
|
||||
}
|
||||
if (ret <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
return *out;
|
||||
}
|
||||
|
||||
@@ -155,15 +125,13 @@ ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
|
||||
* Now the tables and helper functions for the string table:
|
||||
*/
|
||||
|
||||
/* size limits: this stuff is taken straight from RFC3280 */
|
||||
|
||||
/* See RFC 5280. */
|
||||
#define ub_name 32768
|
||||
#define ub_common_name 64
|
||||
#define ub_locality_name 128
|
||||
#define ub_state_name 128
|
||||
#define ub_organization_name 64
|
||||
#define ub_organization_unit_name 64
|
||||
#define ub_title 64
|
||||
#define ub_email_address 128
|
||||
#define ub_serial_number 64
|
||||
|
||||
@@ -194,120 +162,105 @@ static const ASN1_STRING_TABLE tbl_standard[] = {
|
||||
{NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
|
||||
};
|
||||
|
||||
static int sk_table_cmp(const ASN1_STRING_TABLE **a,
|
||||
const ASN1_STRING_TABLE **b)
|
||||
static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
|
||||
{
|
||||
return (*a)->nid - (*b)->nid;
|
||||
if (a->nid < b->nid) {
|
||||
return -1;
|
||||
}
|
||||
if (a->nid > b->nid) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int table_cmp(const void *in_a, const void *in_b)
|
||||
static int table_cmp_void(const void *a, const void *b)
|
||||
{
|
||||
const ASN1_STRING_TABLE *a = in_a;
|
||||
const ASN1_STRING_TABLE *b = in_b;
|
||||
return a->nid - b->nid;
|
||||
return table_cmp(a, b);
|
||||
}
|
||||
|
||||
ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
|
||||
static uint32_t table_hash(const ASN1_STRING_TABLE *tbl)
|
||||
{
|
||||
int found;
|
||||
size_t idx;
|
||||
ASN1_STRING_TABLE *ttmp;
|
||||
ASN1_STRING_TABLE fnd;
|
||||
fnd.nid = nid;
|
||||
|
||||
ttmp =
|
||||
bsearch(&fnd, tbl_standard,
|
||||
sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE),
|
||||
sizeof(ASN1_STRING_TABLE), table_cmp);
|
||||
if (ttmp)
|
||||
return ttmp;
|
||||
if (!stable)
|
||||
return NULL;
|
||||
sk_ASN1_STRING_TABLE_sort(stable);
|
||||
found = sk_ASN1_STRING_TABLE_find(stable, &idx, &fnd);
|
||||
if (!found)
|
||||
return NULL;
|
||||
return sk_ASN1_STRING_TABLE_value(stable, idx);
|
||||
return OPENSSL_hash32(&tbl->nid, sizeof(tbl->nid));
|
||||
}
|
||||
|
||||
int ASN1_STRING_TABLE_add(int nid,
|
||||
long minsize, long maxsize, unsigned long mask,
|
||||
unsigned long flags)
|
||||
static const ASN1_STRING_TABLE *asn1_string_table_get(int nid)
|
||||
{
|
||||
ASN1_STRING_TABLE *tmp;
|
||||
char new_nid = 0;
|
||||
flags &= ~STABLE_FLAGS_MALLOC;
|
||||
if (!stable)
|
||||
stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
|
||||
if (!stable) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_TABLE key;
|
||||
key.nid = nid;
|
||||
const ASN1_STRING_TABLE *tbl =
|
||||
bsearch(&key, tbl_standard, OPENSSL_ARRAY_SIZE(tbl_standard),
|
||||
sizeof(ASN1_STRING_TABLE), table_cmp_void);
|
||||
if (tbl != NULL) {
|
||||
return tbl;
|
||||
}
|
||||
|
||||
CRYPTO_STATIC_MUTEX_lock_read(&string_tables_lock);
|
||||
if (string_tables != NULL) {
|
||||
tbl = lh_ASN1_STRING_TABLE_retrieve(string_tables, &key);
|
||||
}
|
||||
CRYPTO_STATIC_MUTEX_unlock_read(&string_tables_lock);
|
||||
/* Note returning |tbl| without the lock is only safe because
|
||||
* |ASN1_STRING_TABLE_add| cannot modify or delete existing entries. If we
|
||||
* wish to support that, this function must copy the result under a lock. */
|
||||
return tbl;
|
||||
}
|
||||
|
||||
int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
|
||||
unsigned long mask, unsigned long flags)
|
||||
{
|
||||
/* Existing entries cannot be overwritten. */
|
||||
if (asn1_string_table_get(nid) != NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
if (!(tmp = ASN1_STRING_TABLE_get(nid))) {
|
||||
tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
|
||||
if (!tmp) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
|
||||
int ret = 0;
|
||||
CRYPTO_STATIC_MUTEX_lock_write(&string_tables_lock);
|
||||
|
||||
if (string_tables == NULL) {
|
||||
string_tables = lh_ASN1_STRING_TABLE_new(table_hash, table_cmp);
|
||||
if (string_tables == NULL) {
|
||||
goto err;
|
||||
}
|
||||
tmp->flags = flags | STABLE_FLAGS_MALLOC;
|
||||
tmp->nid = nid;
|
||||
tmp->minsize = tmp->maxsize = -1;
|
||||
new_nid = 1;
|
||||
} else
|
||||
tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
|
||||
if (minsize != -1)
|
||||
tmp->minsize = minsize;
|
||||
if (maxsize != -1)
|
||||
tmp->maxsize = maxsize;
|
||||
tmp->mask = mask;
|
||||
if (new_nid)
|
||||
sk_ASN1_STRING_TABLE_push(stable, tmp);
|
||||
return 1;
|
||||
} else {
|
||||
/* Check again for an existing entry. One may have been added while
|
||||
* unlocked. */
|
||||
ASN1_STRING_TABLE key;
|
||||
key.nid = nid;
|
||||
if (lh_ASN1_STRING_TABLE_retrieve(string_tables, &key) != NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_STRING_TABLE *tbl = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
|
||||
if (tbl == NULL) {
|
||||
goto err;
|
||||
}
|
||||
tbl->nid = nid;
|
||||
tbl->flags = flags;
|
||||
tbl->minsize = minsize;
|
||||
tbl->maxsize = maxsize;
|
||||
tbl->mask = mask;
|
||||
ASN1_STRING_TABLE *old_tbl;
|
||||
if (!lh_ASN1_STRING_TABLE_insert(string_tables, &old_tbl, tbl)) {
|
||||
OPENSSL_free(tbl);
|
||||
goto err;
|
||||
}
|
||||
assert(old_tbl == NULL);
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
CRYPTO_STATIC_MUTEX_unlock_write(&string_tables_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ASN1_STRING_TABLE_cleanup(void)
|
||||
{
|
||||
STACK_OF(ASN1_STRING_TABLE) *tmp;
|
||||
tmp = stable;
|
||||
if (!tmp)
|
||||
return;
|
||||
stable = NULL;
|
||||
sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
|
||||
}
|
||||
|
||||
static void st_free(ASN1_STRING_TABLE *tbl)
|
||||
{
|
||||
if (tbl->flags & STABLE_FLAGS_MALLOC)
|
||||
OPENSSL_free(tbl);
|
||||
void asn1_get_string_table_for_testing(const ASN1_STRING_TABLE **out_ptr,
|
||||
size_t *out_len) {
|
||||
*out_ptr = tbl_standard;
|
||||
*out_len = OPENSSL_ARRAY_SIZE(tbl_standard);
|
||||
}
|
||||
|
||||
#ifdef STRING_TABLE_TEST
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ASN1_STRING_TABLE *tmp;
|
||||
int i, last_nid = -1;
|
||||
|
||||
for (tmp = tbl_standard, i = 0;
|
||||
i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) {
|
||||
if (tmp->nid < last_nid) {
|
||||
last_nid = 0;
|
||||
break;
|
||||
}
|
||||
last_nid = tmp->nid;
|
||||
}
|
||||
|
||||
if (last_nid != 0) {
|
||||
printf("Table order OK\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
for (tmp = tbl_standard, i = 0;
|
||||
i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++)
|
||||
printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
|
||||
OBJ_nid2ln(tmp->nid));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -63,7 +63,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* This is an implementation of the ASN1 Time structure which is: Time ::=
|
||||
@@ -73,7 +73,7 @@
|
||||
|
||||
IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_TIME)
|
||||
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
|
||||
{
|
||||
@@ -200,7 +200,7 @@ static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ASN1_TIME_diff(int *pday, int *psec,
|
||||
int ASN1_TIME_diff(int *out_days, int *out_seconds,
|
||||
const ASN1_TIME *from, const ASN1_TIME *to)
|
||||
{
|
||||
struct tm tm_from, tm_to;
|
||||
@@ -208,5 +208,5 @@ int ASN1_TIME_diff(int *pday, int *psec,
|
||||
return 0;
|
||||
if (!asn1_time_to_tm(&tm_to, to))
|
||||
return 0;
|
||||
return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
|
||||
return OPENSSL_gmtime_diff(out_days, out_seconds, &tm_from, &tm_to);
|
||||
}
|
||||
@@ -61,23 +61,33 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_TYPE_get(const ASN1_TYPE *a)
|
||||
{
|
||||
if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
|
||||
return (a->type);
|
||||
else
|
||||
return (0);
|
||||
if (a->type == V_ASN1_BOOLEAN || a->type == V_ASN1_NULL ||
|
||||
a->value.ptr != NULL) {
|
||||
return a->type;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a)
|
||||
{
|
||||
if (a->type == V_ASN1_BOOLEAN) {
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
}
|
||||
if (a->type == V_ASN1_NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return a->value.ptr;
|
||||
}
|
||||
|
||||
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
|
||||
{
|
||||
if (a->value.ptr != NULL) {
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
}
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
a->type = type;
|
||||
if (type == V_ASN1_BOOLEAN)
|
||||
a->value.boolean = value ? 0xff : 0;
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
|
||||
@@ -262,42 +262,3 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s)
|
||||
{
|
||||
struct tm tm;
|
||||
int offset;
|
||||
|
||||
OPENSSL_memset(&tm, '\0', sizeof tm);
|
||||
|
||||
# define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
|
||||
tm.tm_year = g2(s->data);
|
||||
if (tm.tm_year < 50)
|
||||
tm.tm_year += 100;
|
||||
tm.tm_mon = g2(s->data + 2) - 1;
|
||||
tm.tm_mday = g2(s->data + 4);
|
||||
tm.tm_hour = g2(s->data + 6);
|
||||
tm.tm_min = g2(s->data + 8);
|
||||
tm.tm_sec = g2(s->data + 10);
|
||||
if (s->data[12] == 'Z')
|
||||
offset = 0;
|
||||
else {
|
||||
offset = g2(s->data + 13) * 60 + g2(s->data + 15);
|
||||
if (s->data[12] == '-')
|
||||
offset = -offset;
|
||||
}
|
||||
# undef g2
|
||||
|
||||
return mktime(&tm) - offset * 60; /* FIXME: mktime assumes the current
|
||||
* timezone instead of UTC, and unless
|
||||
* we rewrite OpenSSL in Lisp we cannot
|
||||
* locally change the timezone without
|
||||
* possibly interfering with other
|
||||
* parts of the program. timegm, which
|
||||
* uses UTC, is non-standard. Also
|
||||
* time_t is inappropriate for general
|
||||
* UTC times because it may a 32 bit
|
||||
* type. */
|
||||
}
|
||||
#endif
|
||||
@@ -59,7 +59,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* UTF8 utilities */
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
/* Cross-module errors from crypto/x509/i2d_pr.c. */
|
||||
@@ -103,8 +104,7 @@ OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE)
|
||||
|
||||
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
||||
long max);
|
||||
static int asn1_get_length(const unsigned char **pp, long *rl, long max);
|
||||
static void asn1_put_length(unsigned char **pp, int length);
|
||||
|
||||
int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
|
||||
@@ -113,7 +113,7 @@ int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
|
||||
int i, ret;
|
||||
long l;
|
||||
const unsigned char *p = *pp;
|
||||
int tag, xclass, inf;
|
||||
int tag, xclass;
|
||||
long max = omax;
|
||||
|
||||
if (!max)
|
||||
@@ -152,58 +152,43 @@ int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
|
||||
|
||||
*ptag = tag;
|
||||
*pclass = xclass;
|
||||
if (!asn1_get_length(&p, &inf, plength, max))
|
||||
if (!asn1_get_length(&p, plength, max))
|
||||
goto err;
|
||||
|
||||
if (inf && !(ret & V_ASN1_CONSTRUCTED))
|
||||
goto err;
|
||||
|
||||
#if 0
|
||||
fprintf(stderr, "p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\n",
|
||||
(int)p, *plength, omax, (int)*pp, (int)(p + *plength),
|
||||
(int)(omax + *pp));
|
||||
|
||||
#endif
|
||||
if (*plength > (omax - (p - *pp))) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
|
||||
/*
|
||||
* Set this so that even if things are not long enough the values are
|
||||
* set correctly
|
||||
*/
|
||||
ret |= 0x80;
|
||||
return 0x80;
|
||||
}
|
||||
*pp = p;
|
||||
return (ret | inf);
|
||||
return ret;
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return (0x80);
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
||||
long max)
|
||||
static int asn1_get_length(const unsigned char **pp, long *rl, long max)
|
||||
{
|
||||
const unsigned char *p = *pp;
|
||||
unsigned long ret = 0;
|
||||
unsigned long i;
|
||||
|
||||
if (max-- < 1)
|
||||
if (max-- < 1) {
|
||||
return 0;
|
||||
}
|
||||
if (*p == 0x80) {
|
||||
*inf = 1;
|
||||
ret = 0;
|
||||
p++;
|
||||
/* We do not support BER indefinite-length encoding. */
|
||||
return 0;
|
||||
}
|
||||
i = *p & 0x7f;
|
||||
if (*(p++) & 0x80) {
|
||||
if (i > sizeof(ret) || max < (long)i)
|
||||
return 0;
|
||||
while (i-- > 0) {
|
||||
ret <<= 8L;
|
||||
ret |= *(p++);
|
||||
}
|
||||
} else {
|
||||
*inf = 0;
|
||||
i = *p & 0x7f;
|
||||
if (*(p++) & 0x80) {
|
||||
if (i > sizeof(ret) || max < (long)i)
|
||||
return 0;
|
||||
while (i-- > 0) {
|
||||
ret <<= 8L;
|
||||
ret |= *(p++);
|
||||
}
|
||||
} else
|
||||
ret = i;
|
||||
ret = i;
|
||||
}
|
||||
/*
|
||||
* Bound the length to comfortably fit in an int. Lengths in this module
|
||||
@@ -370,8 +355,7 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
|
||||
|
||||
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
|
||||
{
|
||||
if (str->data)
|
||||
OPENSSL_free(str->data);
|
||||
OPENSSL_free(str->data);
|
||||
str->data = data;
|
||||
str->length = len;
|
||||
}
|
||||
@@ -407,17 +391,44 @@ void ASN1_STRING_free(ASN1_STRING *str)
|
||||
|
||||
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
|
||||
{
|
||||
int i;
|
||||
/* Capture padding bits and implicit truncation in BIT STRINGs. */
|
||||
int a_length = a->length, b_length = b->length;
|
||||
uint8_t a_padding = 0, b_padding = 0;
|
||||
if (a->type == V_ASN1_BIT_STRING) {
|
||||
a_length = asn1_bit_string_length(a, &a_padding);
|
||||
}
|
||||
if (b->type == V_ASN1_BIT_STRING) {
|
||||
b_length = asn1_bit_string_length(b, &b_padding);
|
||||
}
|
||||
|
||||
i = (a->length - b->length);
|
||||
if (i == 0) {
|
||||
i = OPENSSL_memcmp(a->data, b->data, a->length);
|
||||
if (i == 0)
|
||||
return (a->type - b->type);
|
||||
else
|
||||
return (i);
|
||||
} else
|
||||
return (i);
|
||||
if (a_length < b_length) {
|
||||
return -1;
|
||||
}
|
||||
if (a_length > b_length) {
|
||||
return 1;
|
||||
}
|
||||
/* In a BIT STRING, the number of bits is 8 * length - padding. Invert this
|
||||
* comparison so we compare by lengths. */
|
||||
if (a_padding > b_padding) {
|
||||
return -1;
|
||||
}
|
||||
if (a_padding < b_padding) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret = OPENSSL_memcmp(a->data, b->data, a_length);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Comparing the type first is more natural, but this matches OpenSSL. */
|
||||
if (a->type < b->type) {
|
||||
return -1;
|
||||
}
|
||||
if (a->type > b->type) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ASN1_STRING_length(const ASN1_STRING *str)
|
||||
|
||||
@@ -72,7 +72,7 @@ const char *ASN1_tag2str(int tag)
|
||||
};
|
||||
|
||||
if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
|
||||
tag &= ~0x100;
|
||||
tag &= ~V_ASN1_NEG;
|
||||
|
||||
if (tag < 0 || tag > 30)
|
||||
return "(unknown)";
|
||||
|
||||
@@ -59,47 +59,43 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
/* ASN1_ITEM versions of the above */
|
||||
|
||||
ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
|
||||
ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out)
|
||||
{
|
||||
ASN1_STRING *octmp;
|
||||
|
||||
if (!oct || !*oct) {
|
||||
if (!(octmp = ASN1_STRING_new())) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
if (oct)
|
||||
*oct = octmp;
|
||||
} else
|
||||
octmp = *oct;
|
||||
|
||||
if (octmp->data) {
|
||||
OPENSSL_free(octmp->data);
|
||||
octmp->data = NULL;
|
||||
}
|
||||
|
||||
if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
|
||||
uint8_t *new_data = NULL;
|
||||
int len = ASN1_item_i2d(obj, &new_data, it);
|
||||
if (len <= 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ENCODE_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
if (!octmp->data) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
return octmp;
|
||||
}
|
||||
|
||||
/* Extract an ASN1 object from an ASN1_STRING */
|
||||
ASN1_STRING *ret = NULL;
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_STRING_new();
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(new_data);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
ASN1_STRING_set0(ret, new_data, len);
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *ASN1_item_unpack(const ASN1_STRING *oct, const ASN1_ITEM *it)
|
||||
{
|
||||
const unsigned char *p;
|
||||
void *ret;
|
||||
|
||||
p = oct->data;
|
||||
if (!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
|
||||
const unsigned char *p = oct->data;
|
||||
void *ret = ASN1_item_d2i(NULL, &p, oct->length, it);
|
||||
if (ret == NULL || p != oct->data + oct->length) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
ASN1_item_free(ret, it);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
File renamed without changes.
@@ -95,3 +95,8 @@ int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
|
||||
err:
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
|
||||
{
|
||||
return i2a_ASN1_INTEGER(bp, a);
|
||||
}
|
||||
+91
-1
@@ -1,4 +1,3 @@
|
||||
/* asn1t.h */
|
||||
/*
|
||||
* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
|
||||
* 2006.
|
||||
@@ -63,6 +62,7 @@
|
||||
#include <time.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
@@ -87,6 +87,45 @@ int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
|
||||
|
||||
/* Internal ASN1 structures and functions: not for application use */
|
||||
|
||||
/* These are used internally in the ASN1_OBJECT to keep track of
|
||||
* whether the names and data need to be free()ed */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
|
||||
|
||||
/* An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
|
||||
* Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
|
||||
* library maintains a table of static |ASN1_OBJECT|s, which may be referenced
|
||||
* by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
|
||||
* pointer externally must assume it is immutable, even if the pointer is not
|
||||
* const. */
|
||||
struct asn1_object_st {
|
||||
const char *sn, *ln;
|
||||
int nid;
|
||||
int length;
|
||||
const unsigned char *data; /* data remains const after init */
|
||||
int flags; /* Should we free this one */
|
||||
};
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void);
|
||||
|
||||
// ASN1_ENCODING structure: this is used to save the received
|
||||
// encoding of an ASN1 type. This is useful to get round
|
||||
// problems with invalid encodings which can break signatures.
|
||||
typedef struct ASN1_ENCODING_st {
|
||||
unsigned char *enc; // DER encoding
|
||||
long len; // Length of encoding
|
||||
int modified; // set to 1 if 'enc' is invalid
|
||||
// alias_only is zero if |enc| owns the buffer that it points to
|
||||
// (although |enc| may still be NULL). If one, |enc| points into a
|
||||
// buffer that is owned elsewhere.
|
||||
unsigned alias_only : 1;
|
||||
// alias_only_on_next_parse is one iff the next parsing operation
|
||||
// should avoid taking a copy of the input and rather set
|
||||
// |alias_only|.
|
||||
unsigned alias_only_on_next_parse : 1;
|
||||
} ASN1_ENCODING;
|
||||
|
||||
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d);
|
||||
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d);
|
||||
|
||||
@@ -104,15 +143,31 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it, int tag, int aclass, char opt,
|
||||
ASN1_TLC *ctx);
|
||||
|
||||
/* ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
|
||||
* i2d output convention. It returns a non-zero length on success and -1 on
|
||||
* error. If |tag| is -1. the tag and class come from |it|. Otherwise, the tag
|
||||
* number is |tag| and the class is |aclass|. This is used for implicit tagging.
|
||||
* This function treats a missing value as an error, not an optional field. */
|
||||
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass);
|
||||
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_get_choice_selector returns the CHOICE selector value for |*pval|, which
|
||||
* must of type |it|. */
|
||||
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_get_field_ptr returns a pointer to the field in |*pval| corresponding to
|
||||
* |tt|. */
|
||||
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
|
||||
/* asn1_do_adb returns the |ASN1_TEMPLATE| for the ANY DEFINED BY field |tt|,
|
||||
* based on the selector INTEGER or OID in |*pval|. If |tt| is not an ADB field,
|
||||
* it returns |tt|. If the selector does not match any value, it returns NULL.
|
||||
* If |nullerr| is non-zero, it will additionally push an error to the error
|
||||
* queue when there is no match. */
|
||||
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
int nullerr);
|
||||
|
||||
@@ -121,11 +176,46 @@ int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_enc_restore, if |*pval| has a saved encoding, writes it to |out| under
|
||||
* the i2d output convention, sets |*len| to the length, and returns one. If it
|
||||
* has no saved encoding, it returns zero. */
|
||||
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it);
|
||||
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
|
||||
* usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
|
||||
* a pointer. */
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
|
||||
|
||||
/* asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
|
||||
* ASN.1 PrintableString, and zero otherwise. */
|
||||
int asn1_is_printable(uint32_t value);
|
||||
|
||||
/* asn1_bit_string_length returns the number of bytes in |str| and sets
|
||||
* |*out_padding_bits| to the number of padding bits.
|
||||
*
|
||||
* This function should be used instead of |ASN1_STRING_length| to correctly
|
||||
* handle the non-|ASN1_STRING_FLAG_BITS_LEFT| case. */
|
||||
int asn1_bit_string_length(const ASN1_BIT_STRING *str,
|
||||
uint8_t *out_padding_bits);
|
||||
|
||||
typedef struct {
|
||||
int nid;
|
||||
long minsize;
|
||||
long maxsize;
|
||||
unsigned long mask;
|
||||
unsigned long flags;
|
||||
} ASN1_STRING_TABLE;
|
||||
|
||||
/* asn1_get_string_table_for_testing sets |*out_ptr| and |*out_len| to the table
|
||||
* of built-in |ASN1_STRING_TABLE| values. It is exported for testing. */
|
||||
OPENSSL_EXPORT void asn1_get_string_table_for_testing(
|
||||
const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} /* extern C */
|
||||
@@ -60,12 +60,11 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
#include <CCryptoBoringSSL_buf.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* Constructed types with a recursive definition (such as can be found in PKCS7)
|
||||
@@ -76,16 +75,9 @@
|
||||
#define ASN1_MAX_CONSTRUCTED_NEST 30
|
||||
|
||||
static int asn1_check_eoc(const unsigned char **in, long len);
|
||||
static int asn1_find_end(const unsigned char **in, long len, char inf);
|
||||
|
||||
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
|
||||
char inf, int tag, int aclass, int depth);
|
||||
|
||||
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
|
||||
|
||||
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
|
||||
char *inf, char *cst,
|
||||
const unsigned char **in, long len,
|
||||
char *cst, const unsigned char **in, long len,
|
||||
int exptag, int expclass, char opt, ASN1_TLC *ctx);
|
||||
|
||||
static int asn1_template_ex_d2i(ASN1_VALUE **pval,
|
||||
@@ -97,7 +89,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
const ASN1_TEMPLATE *tt, char opt,
|
||||
ASN1_TLC *ctx, int depth);
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
int utype, char *free_cont, const ASN1_ITEM *it);
|
||||
int utype, const ASN1_ITEM *it);
|
||||
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it,
|
||||
@@ -170,11 +162,9 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
{
|
||||
const ASN1_TEMPLATE *tt, *errtt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb;
|
||||
const unsigned char *p = NULL, *q;
|
||||
unsigned char oclass;
|
||||
char seq_eoc, seq_nolen, cst, isopt;
|
||||
char cst, isopt;
|
||||
int i;
|
||||
int otag;
|
||||
int ret = 0;
|
||||
@@ -183,10 +173,6 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
aclass &= ~ASN1_TFLG_COMBINE;
|
||||
if (!pval)
|
||||
return 0;
|
||||
if (aux && aux->asn1_cb)
|
||||
asn1_cb = aux->asn1_cb;
|
||||
else
|
||||
asn1_cb = 0;
|
||||
|
||||
/*
|
||||
* Bound |len| to comfortably fit in an int. Lengths in this module often
|
||||
@@ -234,7 +220,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
|
||||
p = *in;
|
||||
/* Just read in tag and class */
|
||||
ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
|
||||
ret = asn1_check_tlen(NULL, &otag, &oclass, NULL,
|
||||
&p, len, -1, 0, 1, ctx);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
@@ -264,7 +250,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
ef = it->funcs;
|
||||
return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
/*
|
||||
* It never makes sense for CHOICE types to have implicit tagging, so if
|
||||
* tag != -1, then this looks like an error in the template.
|
||||
@@ -274,6 +260,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
goto err;
|
||||
}
|
||||
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
|
||||
goto auxerr;
|
||||
|
||||
@@ -327,8 +315,9 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
goto auxerr;
|
||||
*in = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
case ASN1_ITYPE_SEQUENCE: {
|
||||
p = *in;
|
||||
|
||||
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
|
||||
@@ -337,15 +326,13 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
aclass = V_ASN1_UNIVERSAL;
|
||||
}
|
||||
/* Get SEQUENCE length and update len, p */
|
||||
ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
|
||||
ret = asn1_check_tlen(&len, NULL, NULL, &cst,
|
||||
&p, len, tag, aclass, opt, ctx);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
} else if (ret == -1)
|
||||
return -1;
|
||||
/* If indefinite we don't do a length check */
|
||||
seq_nolen = seq_eoc;
|
||||
if (!cst) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
|
||||
goto err;
|
||||
@@ -356,6 +343,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
goto err;
|
||||
}
|
||||
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
|
||||
goto auxerr;
|
||||
|
||||
@@ -384,15 +373,12 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
if (!len)
|
||||
break;
|
||||
q = p;
|
||||
/* TODO(https://crbug.com/boringssl/455): Although we've removed
|
||||
* indefinite-length support, this check is not quite a no-op.
|
||||
* Reject [UNIVERSAL 0] in the tag parsers themselves. */
|
||||
if (asn1_check_eoc(&p, len)) {
|
||||
if (!seq_eoc) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
|
||||
goto err;
|
||||
}
|
||||
len -= p - q;
|
||||
seq_eoc = 0;
|
||||
q = p;
|
||||
break;
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* This determines the OPTIONAL flag value. The field cannot be
|
||||
@@ -424,13 +410,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
len -= p - q;
|
||||
}
|
||||
|
||||
/* Check for EOC if expecting one */
|
||||
if (seq_eoc && !asn1_check_eoc(&p, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
|
||||
goto err;
|
||||
}
|
||||
/* Check all data read */
|
||||
if (!seq_nolen && len) {
|
||||
if (len) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
@@ -462,6 +443,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
goto auxerr;
|
||||
*in = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return 0;
|
||||
@@ -500,7 +482,6 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
|
||||
int ret;
|
||||
long len;
|
||||
const unsigned char *p, *q;
|
||||
char exp_eoc;
|
||||
if (!val)
|
||||
return 0;
|
||||
flags = tt->flags;
|
||||
@@ -515,7 +496,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
|
||||
* Need to work out amount of data available to the inner content and
|
||||
* where it starts: so read in EXPLICIT header to get the info.
|
||||
*/
|
||||
ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
|
||||
ret = asn1_check_tlen(&len, NULL, NULL, &cst,
|
||||
&p, inlen, tt->tag, aclass, opt, ctx);
|
||||
q = p;
|
||||
if (!ret) {
|
||||
@@ -535,20 +516,10 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
|
||||
}
|
||||
/* We read the field in OK so update length */
|
||||
len -= p - q;
|
||||
if (exp_eoc) {
|
||||
/* If NDEF we must have an EOC here */
|
||||
if (!asn1_check_eoc(&p, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Otherwise we must hit the EXPLICIT tag end or its an error
|
||||
*/
|
||||
if (len) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
/* Check for trailing data. */
|
||||
if (len) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
|
||||
goto err;
|
||||
}
|
||||
} else
|
||||
return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
|
||||
@@ -579,7 +550,6 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
if (flags & ASN1_TFLG_SK_MASK) {
|
||||
/* SET OF, SEQUENCE OF */
|
||||
int sktag, skaclass;
|
||||
char sk_eoc;
|
||||
/* First work out expected inner tag value */
|
||||
if (flags & ASN1_TFLG_IMPTAG) {
|
||||
sktag = tt->tag;
|
||||
@@ -592,7 +562,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
sktag = V_ASN1_SEQUENCE;
|
||||
}
|
||||
/* Get the tag */
|
||||
ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
|
||||
ret = asn1_check_tlen(&len, NULL, NULL, NULL,
|
||||
&p, len, sktag, skaclass, opt, ctx);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
@@ -622,15 +592,12 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
while (len > 0) {
|
||||
ASN1_VALUE *skfield;
|
||||
const unsigned char *q = p;
|
||||
/* See if EOC found */
|
||||
/* TODO(https://crbug.com/boringssl/455): Although we've removed
|
||||
* indefinite-length support, this check is not quite a no-op.
|
||||
* Reject [UNIVERSAL 0] in the tag parsers themselves. */
|
||||
if (asn1_check_eoc(&p, len)) {
|
||||
if (!sk_eoc) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
|
||||
goto err;
|
||||
}
|
||||
len -= p - q;
|
||||
sk_eoc = 0;
|
||||
break;
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
|
||||
goto err;
|
||||
}
|
||||
skfield = NULL;
|
||||
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
|
||||
@@ -645,10 +612,6 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
if (sk_eoc) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
|
||||
goto err;
|
||||
}
|
||||
} else if (flags & ASN1_TFLG_IMPTAG) {
|
||||
/* IMPLICIT tagging */
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag,
|
||||
@@ -685,9 +648,8 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
{
|
||||
int ret = 0, utype;
|
||||
long plen;
|
||||
char cst, inf, free_cont = 0;
|
||||
char cst;
|
||||
const unsigned char *p;
|
||||
BUF_MEM buf = {0, NULL, 0 };
|
||||
const unsigned char *cont = NULL;
|
||||
long len;
|
||||
if (!pval) {
|
||||
@@ -713,7 +675,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
return 0;
|
||||
}
|
||||
p = *in;
|
||||
ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
|
||||
ret = asn1_check_tlen(NULL, &utype, &oclass, NULL,
|
||||
&p, inlen, -1, 0, 0, ctx);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
@@ -728,7 +690,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
}
|
||||
p = *in;
|
||||
/* Check header */
|
||||
ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
|
||||
ret = asn1_check_tlen(&plen, NULL, NULL, &cst,
|
||||
&p, inlen, tag, aclass, opt, ctx);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
@@ -753,43 +715,14 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
}
|
||||
|
||||
cont = *in;
|
||||
/* If indefinite length constructed find the real end */
|
||||
if (inf) {
|
||||
if (!asn1_find_end(&p, plen, inf))
|
||||
goto err;
|
||||
len = p - cont;
|
||||
} else {
|
||||
len = p - cont + plen;
|
||||
p += plen;
|
||||
}
|
||||
len = p - cont + plen;
|
||||
p += plen;
|
||||
} else if (cst) {
|
||||
if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
|
||||
|| utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
|
||||
|| utype == V_ASN1_ENUMERATED) {
|
||||
/* These types only have primitive encodings. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Free any returned 'buf' content */
|
||||
free_cont = 1;
|
||||
/*
|
||||
* Should really check the internal tags are correct but some things
|
||||
* may get this wrong. The relevant specs say that constructed string
|
||||
* types should be OCTET STRINGs internally irrespective of the type.
|
||||
* So instead just check for UNIVERSAL class and ignore the tag.
|
||||
*/
|
||||
if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
|
||||
goto err;
|
||||
}
|
||||
len = buf.length;
|
||||
/* Append a final null to string */
|
||||
if (!BUF_MEM_grow_clean(&buf, len + 1)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
buf.data[len] = 0;
|
||||
cont = (const unsigned char *)buf.data;
|
||||
/* This parser historically supported BER constructed strings. We no
|
||||
* longer do and will gradually tighten this parser into a DER
|
||||
* parser. BER types should use |CBS_asn1_ber_to_der|. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return 0;
|
||||
} else {
|
||||
cont = p;
|
||||
len = plen;
|
||||
@@ -797,22 +730,19 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
}
|
||||
|
||||
/* We now have content length and type: translate into a structure */
|
||||
/* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
|
||||
if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
|
||||
if (!asn1_ex_c2i(pval, cont, len, utype, it))
|
||||
goto err;
|
||||
|
||||
*in = p;
|
||||
ret = 1;
|
||||
err:
|
||||
if (free_cont && buf.data)
|
||||
OPENSSL_free(buf.data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Translate ASN1 content octets into a structure */
|
||||
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
int utype, char *free_cont, const ASN1_ITEM *it)
|
||||
int utype, const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_VALUE **opval = NULL;
|
||||
ASN1_STRING *stmp;
|
||||
@@ -916,20 +846,11 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
stmp = (ASN1_STRING *)*pval;
|
||||
stmp->type = utype;
|
||||
}
|
||||
/* If we've already allocated a buffer use it */
|
||||
if (*free_cont) {
|
||||
if (stmp->data)
|
||||
OPENSSL_free(stmp->data);
|
||||
stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
|
||||
stmp->length = len;
|
||||
*free_cont = 0;
|
||||
} else {
|
||||
if (!ASN1_STRING_set(stmp, cont, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_free(stmp);
|
||||
*pval = NULL;
|
||||
goto err;
|
||||
}
|
||||
if (!ASN1_STRING_set(stmp, cont, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_free(stmp);
|
||||
*pval = NULL;
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -947,145 +868,6 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function finds the end of an ASN1 structure when passed its maximum
|
||||
* length, whether it is indefinite length and a pointer to the content. This
|
||||
* is more efficient than calling asn1_collect because it does not recurse on
|
||||
* each indefinite length header.
|
||||
*/
|
||||
|
||||
static int asn1_find_end(const unsigned char **in, long len, char inf)
|
||||
{
|
||||
int expected_eoc;
|
||||
long plen;
|
||||
const unsigned char *p = *in, *q;
|
||||
/* If not indefinite length constructed just add length */
|
||||
if (inf == 0) {
|
||||
*in += len;
|
||||
return 1;
|
||||
}
|
||||
expected_eoc = 1;
|
||||
/*
|
||||
* Indefinite length constructed form. Find the end when enough EOCs are
|
||||
* found. If more indefinite length constructed headers are encountered
|
||||
* increment the expected eoc count otherwise just skip to the end of the
|
||||
* data.
|
||||
*/
|
||||
while (len > 0) {
|
||||
if (asn1_check_eoc(&p, len)) {
|
||||
expected_eoc--;
|
||||
if (expected_eoc == 0)
|
||||
break;
|
||||
len -= 2;
|
||||
continue;
|
||||
}
|
||||
q = p;
|
||||
/* Just read in a header: only care about the length */
|
||||
if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
|
||||
-1, 0, 0, NULL)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
return 0;
|
||||
}
|
||||
if (inf)
|
||||
expected_eoc++;
|
||||
else
|
||||
p += plen;
|
||||
len -= p - q;
|
||||
}
|
||||
if (expected_eoc) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
|
||||
return 0;
|
||||
}
|
||||
*in = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function collects the asn1 data from a constructred string type into
|
||||
* a buffer. The values of 'in' and 'len' should refer to the contents of the
|
||||
* constructed type and 'inf' should be set if it is indefinite length.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This determines how many levels of recursion are permitted in ASN1 string
|
||||
* types. If it is not limited stack overflows can occur. If set to zero no
|
||||
* recursion is allowed at all. Although zero should be adequate examples
|
||||
* exist that require a value of 1. So 5 should be more than enough.
|
||||
*/
|
||||
#define ASN1_MAX_STRING_NEST 5
|
||||
|
||||
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
|
||||
char inf, int tag, int aclass, int depth)
|
||||
{
|
||||
const unsigned char *p, *q;
|
||||
long plen;
|
||||
char cst, ininf;
|
||||
p = *in;
|
||||
inf &= 1;
|
||||
/*
|
||||
* If no buffer and not indefinite length constructed just pass over the
|
||||
* encoded data
|
||||
*/
|
||||
if (!buf && !inf) {
|
||||
*in += len;
|
||||
return 1;
|
||||
}
|
||||
while (len > 0) {
|
||||
q = p;
|
||||
/* Check for EOC */
|
||||
if (asn1_check_eoc(&p, len)) {
|
||||
/*
|
||||
* EOC is illegal outside indefinite length constructed form
|
||||
*/
|
||||
if (!inf) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNEXPECTED_EOC);
|
||||
return 0;
|
||||
}
|
||||
inf = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
|
||||
len, tag, aclass, 0, NULL)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If indefinite length constructed update max length */
|
||||
if (cst) {
|
||||
if (depth >= ASN1_MAX_STRING_NEST) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_STRING);
|
||||
return 0;
|
||||
}
|
||||
if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
|
||||
return 0;
|
||||
} else if (plen && !collect_data(buf, &p, plen))
|
||||
return 0;
|
||||
len -= p - q;
|
||||
}
|
||||
if (inf) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_EOC);
|
||||
return 0;
|
||||
}
|
||||
*in = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
|
||||
{
|
||||
int len;
|
||||
if (buf) {
|
||||
len = buf->length;
|
||||
if (!BUF_MEM_grow_clean(buf, len + plen)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(buf->data + len, *p, plen);
|
||||
}
|
||||
*p += plen;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Check for ASN1 EOC and swallow it if found */
|
||||
|
||||
static int asn1_check_eoc(const unsigned char **in, long len)
|
||||
@@ -1102,15 +884,12 @@ static int asn1_check_eoc(const unsigned char **in, long len)
|
||||
}
|
||||
|
||||
/*
|
||||
* Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
|
||||
* length for indefinite length constructed form, we don't know the exact
|
||||
* length but we can set an upper bound to the amount of data available minus
|
||||
* the header length just read.
|
||||
* Check an ASN1 tag and length: a bit like ASN1_get_object but it handles
|
||||
* the ASN1_TLC cache and checks the expected tag.
|
||||
*/
|
||||
|
||||
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
|
||||
char *inf, char *cst,
|
||||
const unsigned char **in, long len,
|
||||
char *cst, const unsigned char **in, long len,
|
||||
int exptag, int expclass, char opt, ASN1_TLC *ctx)
|
||||
{
|
||||
int i;
|
||||
@@ -1136,10 +915,13 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
|
||||
ctx->hdrlen = p - q;
|
||||
ctx->valid = 1;
|
||||
/*
|
||||
* If definite length, and no error, length + header can't exceed
|
||||
* total amount of data available.
|
||||
* If no error, length + header can't exceed total amount of data
|
||||
* available.
|
||||
*
|
||||
* TODO(davidben): Is this check necessary? |ASN1_get_object|
|
||||
* should already guarantee this.
|
||||
*/
|
||||
if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
|
||||
if (!(i & 0x80) && ((plen + ctx->hdrlen) > len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
|
||||
asn1_tlc_clear(ctx);
|
||||
return 0;
|
||||
@@ -1170,12 +952,6 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
|
||||
asn1_tlc_clear(ctx);
|
||||
}
|
||||
|
||||
if (i & 1)
|
||||
plen = len - (p - q);
|
||||
|
||||
if (inf)
|
||||
*inf = i & 1;
|
||||
|
||||
if (cst)
|
||||
*cst = i & V_ASN1_CONSTRUCTED;
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -63,56 +64,50 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass,
|
||||
int optional);
|
||||
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass);
|
||||
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
|
||||
const ASN1_ITEM *it);
|
||||
const ASN1_ITEM *it, int tag, int aclass,
|
||||
int optional);
|
||||
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *out_omit,
|
||||
int *putype, const ASN1_ITEM *it);
|
||||
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
int skcontlen, const ASN1_ITEM *item,
|
||||
int do_sort, int iclass);
|
||||
int skcontlen, const ASN1_ITEM *item, int do_sort);
|
||||
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_TEMPLATE *tt, int tag, int aclass);
|
||||
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
|
||||
const ASN1_ITEM *it, int flags);
|
||||
|
||||
/*
|
||||
* Top level i2d equivalents
|
||||
*/
|
||||
|
||||
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
|
||||
{
|
||||
return asn1_item_flags_i2d(val, out, it, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
|
||||
* points to a buffer to output the data to. The new i2d has one additional
|
||||
* feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
|
||||
* allocated and populated with the encoding.
|
||||
*/
|
||||
|
||||
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
|
||||
const ASN1_ITEM *it, int flags)
|
||||
{
|
||||
if (out && !*out) {
|
||||
unsigned char *p, *buf;
|
||||
int len;
|
||||
len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
|
||||
if (len <= 0)
|
||||
int len = ASN1_item_ex_i2d(&val, NULL, it, /*tag=*/-1, /*aclass=*/0);
|
||||
if (len <= 0) {
|
||||
return len;
|
||||
}
|
||||
buf = OPENSSL_malloc(len);
|
||||
if (!buf)
|
||||
if (!buf) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
p = buf;
|
||||
ASN1_item_ex_i2d(&val, &p, it, -1, flags);
|
||||
int len2 = ASN1_item_ex_i2d(&val, &p, it, /*tag=*/-1, /*aclass=*/0);
|
||||
if (len2 <= 0) {
|
||||
return len2;
|
||||
}
|
||||
assert(len == len2);
|
||||
*out = buf;
|
||||
return len;
|
||||
}
|
||||
|
||||
return ASN1_item_ex_i2d(&val, out, it, -1, flags);
|
||||
return ASN1_item_ex_i2d(&val, out, it, /*tag=*/-1, /*aclass=*/0);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -122,27 +117,48 @@ static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
|
||||
|
||||
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass)
|
||||
{
|
||||
int ret = asn1_item_ex_i2d_opt(pval, out, it, tag, aclass, /*optional=*/0);
|
||||
assert(ret != 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* asn1_item_ex_i2d_opt behaves like |ASN1_item_ex_i2d| but, if |optional| is
|
||||
* non-zero and |*pval| is omitted, it returns zero and writes no bytes. */
|
||||
int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass,
|
||||
int optional)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
int i, seqcontlen, seqlen;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = 0;
|
||||
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
|
||||
return 0;
|
||||
/* Historically, |aclass| was repurposed to pass additional flags into the
|
||||
* encoding process. */
|
||||
assert((aclass & ASN1_TFLG_TAG_CLASS) == aclass);
|
||||
/* If not overridding the tag, |aclass| is ignored and should be zero. */
|
||||
assert(tag != -1 || aclass == 0);
|
||||
|
||||
if (aux && aux->asn1_cb)
|
||||
asn1_cb = aux->asn1_cb;
|
||||
/* All fields are pointers, except for boolean |ASN1_ITYPE_PRIMITIVE|s.
|
||||
* Optional primitives are handled later. */
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) {
|
||||
if (optional) {
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (it->itype) {
|
||||
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates)
|
||||
return asn1_template_ex_i2d(pval, out, it->templates,
|
||||
tag, aclass);
|
||||
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
|
||||
break;
|
||||
if (it->templates) {
|
||||
if (it->templates->flags & ASN1_TFLG_OPTIONAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass);
|
||||
}
|
||||
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass, optional);
|
||||
|
||||
case ASN1_ITYPE_MSTRING:
|
||||
/*
|
||||
@@ -153,9 +169,9 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
|
||||
return asn1_i2d_ex_primitive(pval, out, it, -1, 0, optional);
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
/*
|
||||
* It never makes sense for CHOICE types to have implicit tagging, so if
|
||||
* tag != -1, then this looks like an error in the template.
|
||||
@@ -164,31 +180,39 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
|
||||
return 0;
|
||||
i = asn1_get_choice_selector(pval, it);
|
||||
if ((i >= 0) && (i < it->tcount)) {
|
||||
ASN1_VALUE **pchval;
|
||||
const ASN1_TEMPLATE *chtt;
|
||||
chtt = it->templates + i;
|
||||
pchval = asn1_get_field_ptr(pval, chtt);
|
||||
return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
|
||||
if (i < 0 || i >= it->tcount) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
|
||||
return -1;
|
||||
}
|
||||
/* Fixme: error condition if selector out of range */
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
|
||||
return 0;
|
||||
break;
|
||||
const ASN1_TEMPLATE *chtt = it->templates + i;
|
||||
if (chtt->flags & ASN1_TFLG_OPTIONAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
ASN1_VALUE **pchval = asn1_get_field_ptr(pval, chtt);
|
||||
return asn1_template_ex_i2d(pchval, out, chtt, -1, 0);
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
case ASN1_ITYPE_EXTERN: {
|
||||
/* If new style i2d it does all the work */
|
||||
ef = it->funcs;
|
||||
return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
|
||||
const ASN1_EXTERN_FUNCS *ef = it->funcs;
|
||||
int ret = ef->asn1_ex_i2d(pval, out, it, tag, aclass);
|
||||
if (ret == 0) {
|
||||
/* |asn1_ex_i2d| should never return zero. We have already checked
|
||||
* for optional values generically, and |ASN1_ITYPE_EXTERN| fields
|
||||
* must be pointers. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
case ASN1_ITYPE_SEQUENCE: {
|
||||
i = asn1_enc_restore(&seqcontlen, out, pval, it);
|
||||
/* An error occurred */
|
||||
if (i < 0)
|
||||
return 0;
|
||||
return -1;
|
||||
/* We have a valid cached encoding... */
|
||||
if (i > 0)
|
||||
return seqcontlen;
|
||||
@@ -197,12 +221,8 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
|
||||
if (tag == -1) {
|
||||
tag = V_ASN1_SEQUENCE;
|
||||
/* Retain any other flags in aclass */
|
||||
aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
|
||||
| V_ASN1_UNIVERSAL;
|
||||
aclass = V_ASN1_UNIVERSAL;
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
|
||||
return 0;
|
||||
/* First work out sequence content length */
|
||||
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
|
||||
const ASN1_TEMPLATE *seqtt;
|
||||
@@ -210,9 +230,9 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
int tmplen;
|
||||
seqtt = asn1_do_adb(pval, tt, 1);
|
||||
if (!seqtt)
|
||||
return 0;
|
||||
return -1;
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
|
||||
tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, 0);
|
||||
if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
|
||||
return -1;
|
||||
seqcontlen += tmplen;
|
||||
@@ -228,40 +248,49 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
ASN1_VALUE **pseqval;
|
||||
seqtt = asn1_do_adb(pval, tt, 1);
|
||||
if (!seqtt)
|
||||
return 0;
|
||||
return -1;
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
/* FIXME: check for errors in enhanced version */
|
||||
asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
|
||||
if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
|
||||
return 0;
|
||||
return seqlen;
|
||||
}
|
||||
|
||||
default:
|
||||
return 0;
|
||||
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* asn1_template_ex_i2d behaves like |asn1_item_ex_i2d_opt| but uses an
|
||||
* |ASN1_TEMPLATE| instead of an |ASN1_ITEM|. An |ASN1_TEMPLATE| wraps an
|
||||
* |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc. Instead of
|
||||
* taking an |optional| parameter, it uses the |ASN1_TFLG_OPTIONAL| flag. */
|
||||
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_TEMPLATE *tt, int tag, int iclass)
|
||||
{
|
||||
int i, ret, flags, ttag, tclass;
|
||||
size_t j;
|
||||
flags = tt->flags;
|
||||
|
||||
/* Historically, |iclass| was repurposed to pass additional flags into the
|
||||
* encoding process. */
|
||||
assert((iclass & ASN1_TFLG_TAG_CLASS) == iclass);
|
||||
/* If not overridding the tag, |iclass| is ignored and should be zero. */
|
||||
assert(tag != -1 || iclass == 0);
|
||||
|
||||
/*
|
||||
* Work out tag and class to use: tagging may come either from the
|
||||
* template or the arguments, not both because this would create
|
||||
* ambiguity. Additionally the iclass argument may contain some
|
||||
* additional flags which should be noted and passed down to other
|
||||
* levels.
|
||||
* ambiguity.
|
||||
*/
|
||||
if (flags & ASN1_TFLG_TAG_MASK) {
|
||||
/* Error if argument and template tagging */
|
||||
if (tag != -1)
|
||||
/* FIXME: error code here */
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
/* Get tagging from template */
|
||||
ttag = tt->tag;
|
||||
tclass = flags & ASN1_TFLG_TAG_CLASS;
|
||||
@@ -273,14 +302,12 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
ttag = -1;
|
||||
tclass = 0;
|
||||
}
|
||||
/*
|
||||
* Remove any class mask from iflag.
|
||||
*/
|
||||
iclass &= ~ASN1_TFLG_TAG_CLASS;
|
||||
|
||||
const int optional = (flags & ASN1_TFLG_OPTIONAL) != 0;
|
||||
|
||||
/*
|
||||
* At this point 'ttag' contains the outer tag to use, 'tclass' is the
|
||||
* class and iclass is any flags passed to this function.
|
||||
* At this point 'ttag' contains the outer tag to use, and 'tclass' is the
|
||||
* class.
|
||||
*/
|
||||
|
||||
if (flags & ASN1_TFLG_SK_MASK) {
|
||||
@@ -290,16 +317,22 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
int skcontlen, sklen;
|
||||
ASN1_VALUE *skitem;
|
||||
|
||||
if (!*pval)
|
||||
return 0;
|
||||
if (!*pval) {
|
||||
if (optional) {
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (flags & ASN1_TFLG_SET_OF) {
|
||||
isset = 1;
|
||||
/* 2 means we reorder */
|
||||
if (flags & ASN1_TFLG_SEQUENCE_OF)
|
||||
isset = 2;
|
||||
} else
|
||||
/* Historically, types with both bits set were mutated when
|
||||
* serialized to apply the sort. We no longer support this. */
|
||||
assert((flags & ASN1_TFLG_SEQUENCE_OF) == 0);
|
||||
} else {
|
||||
isset = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Work out inner tag value: if EXPLICIT or no tagging use underlying
|
||||
@@ -322,7 +355,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
int tmplen;
|
||||
skitem = sk_ASN1_VALUE_value(sk, j);
|
||||
tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
|
||||
-1, iclass);
|
||||
-1, 0);
|
||||
if (tmplen == -1 || (skcontlen > INT_MAX - tmplen))
|
||||
return -1;
|
||||
skcontlen += tmplen;
|
||||
@@ -346,30 +379,36 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
/* SET or SEQUENCE and IMPLICIT tag */
|
||||
ASN1_put_object(out, /*constructed=*/1, skcontlen, sktag, skaclass);
|
||||
/* And the stuff itself */
|
||||
asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
|
||||
isset, iclass);
|
||||
if (!asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
|
||||
isset)) {
|
||||
return -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (flags & ASN1_TFLG_EXPTAG) {
|
||||
/* EXPLICIT tagging */
|
||||
/* Find length of tagged item */
|
||||
i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
|
||||
if (!i)
|
||||
return 0;
|
||||
i = asn1_item_ex_i2d_opt(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, 0,
|
||||
optional);
|
||||
if (i <= 0)
|
||||
return i;
|
||||
/* Find length of EXPLICIT tag */
|
||||
ret = ASN1_object_size(/*constructed=*/1, i, ttag);
|
||||
if (out && ret != -1) {
|
||||
/* Output tag and item */
|
||||
ASN1_put_object(out, /*constructed=*/1, i, ttag, tclass);
|
||||
ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
|
||||
if (ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1,
|
||||
0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Either normal or IMPLICIT tagging: combine class and flags */
|
||||
return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
|
||||
ttag, tclass | iclass);
|
||||
/* Either normal or IMPLICIT tagging */
|
||||
return asn1_item_ex_i2d_opt(pval, out, ASN1_ITEM_ptr(tt->item),
|
||||
ttag, tclass, optional);
|
||||
|
||||
}
|
||||
|
||||
@@ -378,7 +417,6 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
typedef struct {
|
||||
unsigned char *data;
|
||||
int length;
|
||||
ASN1_VALUE *field;
|
||||
} DER_ENC;
|
||||
|
||||
static int der_cmp(const void *a, const void *b)
|
||||
@@ -392,99 +430,96 @@ static int der_cmp(const void *a, const void *b)
|
||||
return d1->length - d2->length;
|
||||
}
|
||||
|
||||
/* Output the content octets of SET OF or SEQUENCE OF */
|
||||
|
||||
/* asn1_set_seq_out writes |sk| to |out| under the i2d output convention,
|
||||
* excluding the tag and length. It returns one on success and zero on error.
|
||||
* |skcontlen| must be the total encoded size. If |do_sort| is non-zero, the
|
||||
* elements are sorted for a SET OF type. Each element of |sk| has type
|
||||
* |item|. */
|
||||
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
int skcontlen, const ASN1_ITEM *item,
|
||||
int do_sort, int iclass)
|
||||
int skcontlen, const ASN1_ITEM *item, int do_sort)
|
||||
{
|
||||
size_t i;
|
||||
ASN1_VALUE *skitem;
|
||||
unsigned char *tmpdat = NULL, *p = NULL;
|
||||
DER_ENC *derlst = NULL, *tder;
|
||||
if (do_sort) {
|
||||
/* Don't need to sort less than 2 items */
|
||||
if (sk_ASN1_VALUE_num(sk) < 2)
|
||||
do_sort = 0;
|
||||
else {
|
||||
derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
|
||||
* sizeof(*derlst));
|
||||
if (!derlst)
|
||||
return 0;
|
||||
tmpdat = OPENSSL_malloc(skcontlen);
|
||||
if (!tmpdat) {
|
||||
OPENSSL_free(derlst);
|
||||
/* No need to sort if there are fewer than two items. */
|
||||
if (!do_sort || sk_ASN1_VALUE_num(sk) < 2) {
|
||||
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *skitem = sk_ASN1_VALUE_value(sk, i);
|
||||
if (ASN1_item_ex_i2d(&skitem, out, item, -1, 0) < 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* If not sorting just output each item */
|
||||
if (!do_sort) {
|
||||
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
skitem = sk_ASN1_VALUE_value(sk, i);
|
||||
ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
p = tmpdat;
|
||||
|
||||
/* Doing sort: build up a list of each member's DER encoding */
|
||||
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
|
||||
skitem = sk_ASN1_VALUE_value(sk, i);
|
||||
tder->data = p;
|
||||
tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
|
||||
tder->field = skitem;
|
||||
if (sk_ASN1_VALUE_num(sk) > ((size_t)-1) / sizeof(DER_ENC)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Now sort them */
|
||||
qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
|
||||
/* Output sorted DER encoding */
|
||||
int ret = 0;
|
||||
unsigned char *const buf = OPENSSL_malloc(skcontlen);
|
||||
DER_ENC *encoded = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) * sizeof(*encoded));
|
||||
if (encoded == NULL || buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Encode all the elements into |buf| and populate |encoded|. */
|
||||
unsigned char *p = buf;
|
||||
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *skitem = sk_ASN1_VALUE_value(sk, i);
|
||||
encoded[i].data = p;
|
||||
encoded[i].length = ASN1_item_ex_i2d(&skitem, &p, item, -1, 0);
|
||||
if (encoded[i].length < 0) {
|
||||
goto err;
|
||||
}
|
||||
assert(p - buf <= skcontlen);
|
||||
}
|
||||
|
||||
qsort(encoded, sk_ASN1_VALUE_num(sk), sizeof(*encoded), der_cmp);
|
||||
|
||||
/* Output the elements in sorted order. */
|
||||
p = *out;
|
||||
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
|
||||
OPENSSL_memcpy(p, tder->data, tder->length);
|
||||
p += tder->length;
|
||||
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
OPENSSL_memcpy(p, encoded[i].data, encoded[i].length);
|
||||
p += encoded[i].length;
|
||||
}
|
||||
*out = p;
|
||||
/* If do_sort is 2 then reorder the STACK */
|
||||
if (do_sort == 2) {
|
||||
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
|
||||
(void)sk_ASN1_VALUE_set(sk, i, tder->field);
|
||||
}
|
||||
OPENSSL_free(derlst);
|
||||
OPENSSL_free(tmpdat);
|
||||
return 1;
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
OPENSSL_free(encoded);
|
||||
OPENSSL_free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* asn1_i2d_ex_primitive behaves like |ASN1_item_ex_i2d| but |item| must be a
|
||||
* a PRIMITIVE or MSTRING type that is not an |ASN1_ITEM_TEMPLATE|. */
|
||||
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass)
|
||||
const ASN1_ITEM *it, int tag, int aclass,
|
||||
int optional)
|
||||
{
|
||||
int len;
|
||||
int utype;
|
||||
int usetag;
|
||||
|
||||
utype = it->utype;
|
||||
|
||||
/*
|
||||
* Get length of content octets and maybe find out the underlying type.
|
||||
*/
|
||||
|
||||
len = asn1_ex_i2c(pval, NULL, &utype, it);
|
||||
/* Get length of content octets and maybe find out the underlying type. */
|
||||
int omit;
|
||||
int utype = it->utype;
|
||||
int len = asn1_ex_i2c(pval, NULL, &omit, &utype, it);
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (omit) {
|
||||
if (optional) {
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* If SEQUENCE, SET or OTHER then header is included in pseudo content
|
||||
* octets so don't include tag+length. We need to check here because the
|
||||
* call to asn1_ex_i2c() could change utype.
|
||||
*/
|
||||
if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
|
||||
(utype == V_ASN1_OTHER))
|
||||
usetag = 0;
|
||||
else
|
||||
usetag = 1;
|
||||
|
||||
/* -1 means omit type */
|
||||
|
||||
if (len == -1)
|
||||
return 0;
|
||||
int usetag = utype != V_ASN1_SEQUENCE && utype != V_ASN1_SET &&
|
||||
utype != V_ASN1_OTHER;
|
||||
|
||||
/* If not implicitly tagged get tag from underlying type */
|
||||
if (tag == -1)
|
||||
@@ -492,21 +527,42 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
|
||||
/* Output tag+length followed by content octets */
|
||||
if (out) {
|
||||
if (usetag)
|
||||
if (usetag) {
|
||||
ASN1_put_object(out, /*constructed=*/0, len, tag, aclass);
|
||||
asn1_ex_i2c(pval, *out, &utype, it);
|
||||
}
|
||||
int len2 = asn1_ex_i2c(pval, *out, &omit, &utype, it);
|
||||
if (len2 < 0) {
|
||||
return -1;
|
||||
}
|
||||
assert(len == len2);
|
||||
assert(!omit);
|
||||
*out += len;
|
||||
}
|
||||
|
||||
if (usetag)
|
||||
if (usetag) {
|
||||
return ASN1_object_size(/*constructed=*/0, len, tag);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Produce content octets from a structure */
|
||||
|
||||
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
const ASN1_ITEM *it)
|
||||
/* asn1_ex_i2c writes the |*pval| to |cout| under the i2d output convention,
|
||||
* excluding the tag and length. It returns the number of bytes written,
|
||||
* possibly zero, on success or -1 on error. If |*pval| should be omitted, it
|
||||
* returns zero and sets |*out_omit| to true.
|
||||
*
|
||||
* If |it| is an MSTRING or ANY type, it gets the underlying type from |*pval|,
|
||||
* which must be an |ASN1_STRING| or |ASN1_TYPE|, respectively. It then updates
|
||||
* |*putype| with the tag number of type used, or |V_ASN1_OTHER| if it was not a
|
||||
* universal type. If |*putype| is set to |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or
|
||||
* |V_ASN1_OTHER|, it additionally outputs the tag and length, so the caller
|
||||
* must not do so.
|
||||
*
|
||||
* Otherwise, |*putype| must contain |it->utype|.
|
||||
*
|
||||
* WARNING: Unlike most functions in this file, |asn1_ex_i2c| can return zero
|
||||
* without omitting the element. ASN.1 values may have empty contents. */
|
||||
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit,
|
||||
int *putype, const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_BOOLEAN *tbool = NULL;
|
||||
ASN1_STRING *strtmp;
|
||||
@@ -520,23 +576,51 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of callbacks. */
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
*out_omit = 0;
|
||||
|
||||
/* Should type be omitted? */
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE)
|
||||
|| (it->utype != V_ASN1_BOOLEAN)) {
|
||||
if (!*pval)
|
||||
return -1;
|
||||
if (!*pval) {
|
||||
*out_omit = 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
/* If MSTRING type set the underlying type */
|
||||
strtmp = (ASN1_STRING *)*pval;
|
||||
utype = strtmp->type;
|
||||
if (utype < 0 && utype != V_ASN1_OTHER) {
|
||||
/* MSTRINGs can have type -1 when default-constructed. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE);
|
||||
return -1;
|
||||
}
|
||||
/* Negative INTEGER and ENUMERATED values use |ASN1_STRING| type values
|
||||
* that do not match their corresponding utype values. INTEGERs cannot
|
||||
* participate in MSTRING types, but ENUMERATEDs can.
|
||||
*
|
||||
* TODO(davidben): Is this a bug? Although arguably one of the MSTRING
|
||||
* types should contain more values, rather than less. See
|
||||
* https://crbug.com/boringssl/412. But it is not possible to fit all
|
||||
* possible ANY values into an |ASN1_STRING|, so matching the spec here
|
||||
* is somewhat hopeless. */
|
||||
if (utype == V_ASN1_NEG_INTEGER) {
|
||||
utype = V_ASN1_INTEGER;
|
||||
} else if (utype == V_ASN1_NEG_ENUMERATED) {
|
||||
utype = V_ASN1_ENUMERATED;
|
||||
}
|
||||
*putype = utype;
|
||||
} else if (it->utype == V_ASN1_ANY) {
|
||||
/* If ANY set type and pointer to value */
|
||||
ASN1_TYPE *typ;
|
||||
typ = (ASN1_TYPE *)*pval;
|
||||
utype = typ->type;
|
||||
if (utype < 0 && utype != V_ASN1_OTHER) {
|
||||
/* |ASN1_TYPE|s can have type -1 when default-constructed. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE);
|
||||
return -1;
|
||||
}
|
||||
*putype = utype;
|
||||
pval = &typ->value.asn1_value;
|
||||
} else
|
||||
@@ -547,8 +631,11 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
otmp = (ASN1_OBJECT *)*pval;
|
||||
cont = otmp->data;
|
||||
len = otmp->length;
|
||||
if (cont == NULL || len == 0)
|
||||
if (len == 0) {
|
||||
/* Some |ASN1_OBJECT|s do not have OIDs and cannot be serialized. */
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
|
||||
case V_ASN1_NULL:
|
||||
@@ -558,34 +645,39 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
tbool = (ASN1_BOOLEAN *)pval;
|
||||
if (*tbool == -1)
|
||||
return -1;
|
||||
if (*tbool == -1) {
|
||||
*out_omit = 1;
|
||||
return 0;
|
||||
}
|
||||
if (it->utype != V_ASN1_ANY) {
|
||||
/*
|
||||
* Default handling if value == size field then omit
|
||||
*/
|
||||
if (*tbool && (it->size > 0))
|
||||
return -1;
|
||||
if (!*tbool && !it->size)
|
||||
return -1;
|
||||
if ((*tbool && (it->size > 0)) ||
|
||||
(!*tbool && !it->size)) {
|
||||
*out_omit = 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
c = (unsigned char)*tbool;
|
||||
c = *tbool ? 0xff : 0x00;
|
||||
cont = &c;
|
||||
len = 1;
|
||||
break;
|
||||
|
||||
case V_ASN1_BIT_STRING:
|
||||
return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
|
||||
cout ? &cout : NULL);
|
||||
break;
|
||||
case V_ASN1_BIT_STRING: {
|
||||
int ret = i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
|
||||
cout ? &cout : NULL);
|
||||
/* |i2c_ASN1_BIT_STRING| returns zero on error instead of -1. */
|
||||
return ret <= 0 ? -1 : ret;
|
||||
}
|
||||
|
||||
case V_ASN1_INTEGER:
|
||||
case V_ASN1_ENUMERATED:
|
||||
/*
|
||||
* These are all have the same content format as ASN1_INTEGER
|
||||
*/
|
||||
return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
|
||||
break;
|
||||
case V_ASN1_ENUMERATED: {
|
||||
/* |i2c_ASN1_INTEGER| also handles ENUMERATED. */
|
||||
int ret = i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
|
||||
/* |i2c_ASN1_INTEGER| returns zero on error instead of -1. */
|
||||
return ret <= 0 ? -1 : ret;
|
||||
}
|
||||
|
||||
case V_ASN1_OCTET_STRING:
|
||||
case V_ASN1_NUMERICSTRING:
|
||||
|
||||
@@ -61,7 +61,7 @@
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* Free up an ASN1 structure */
|
||||
|
||||
@@ -79,17 +79,11 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL, *seqtt;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb;
|
||||
int i;
|
||||
if (!pval)
|
||||
return;
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
|
||||
return;
|
||||
if (aux && aux->asn1_cb)
|
||||
asn1_cb = aux->asn1_cb;
|
||||
else
|
||||
asn1_cb = 0;
|
||||
|
||||
switch (it->itype) {
|
||||
|
||||
@@ -104,7 +98,9 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
ASN1_primitive_free(pval, it);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
|
||||
if (i == 2)
|
||||
@@ -124,6 +120,7 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
*pval = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
ef = it->funcs;
|
||||
@@ -131,9 +128,11 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
ef->asn1_ex_free(pval, it);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
case ASN1_ITYPE_SEQUENCE: {
|
||||
if (!asn1_refcount_dec_and_test_zero(pval, it))
|
||||
return;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
|
||||
if (i == 2)
|
||||
@@ -162,6 +161,7 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
@@ -192,7 +192,7 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
|
||||
utype = typ->type;
|
||||
pval = &typ->value.asn1_value;
|
||||
if (!*pval)
|
||||
if (utype != V_ASN1_BOOLEAN && !*pval)
|
||||
return;
|
||||
} else if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
@@ -95,14 +95,8 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb;
|
||||
ASN1_VALUE **pseqval;
|
||||
int i;
|
||||
if (aux && aux->asn1_cb)
|
||||
asn1_cb = aux->asn1_cb;
|
||||
else
|
||||
asn1_cb = 0;
|
||||
|
||||
switch (it->itype) {
|
||||
|
||||
@@ -127,7 +121,9 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
goto memerr;
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i)
|
||||
@@ -146,8 +142,11 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
|
||||
goto auxerr2;
|
||||
break;
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
case ASN1_ITYPE_SEQUENCE: {
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i)
|
||||
@@ -173,6 +172,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
goto auxerr2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
|
||||
memerr2:
|
||||
@@ -271,7 +271,6 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_TYPE *typ;
|
||||
ASN1_STRING *str;
|
||||
int utype;
|
||||
|
||||
if (!it)
|
||||
@@ -308,10 +307,7 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
break;
|
||||
|
||||
default:
|
||||
str = ASN1_STRING_type_new(utype);
|
||||
if (it->itype == ASN1_ITYPE_MSTRING && str)
|
||||
str->flags |= ASN1_STRING_FLAG_MSTRING;
|
||||
*pval = (ASN1_VALUE *)str;
|
||||
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
|
||||
break;
|
||||
}
|
||||
if (*pval)
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
|
||||
#define IMPLEMENT_ASN1_STRING_FUNCTIONS(sname) \
|
||||
IMPLEMENT_ASN1_TYPE(sname) \
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(sname, sname, sname) \
|
||||
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(sname, sname, sname) \
|
||||
sname *sname##_new(void) \
|
||||
{ \
|
||||
return ASN1_STRING_type_new(V_##sname); \
|
||||
@@ -88,7 +88,7 @@ IMPLEMENT_ASN1_STRING_FUNCTIONS(ASN1_UNIVERSALSTRING)
|
||||
IMPLEMENT_ASN1_STRING_FUNCTIONS(ASN1_BMPSTRING)
|
||||
|
||||
IMPLEMENT_ASN1_TYPE(ASN1_NULL)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(ASN1_NULL)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_NULL)
|
||||
|
||||
IMPLEMENT_ASN1_TYPE(ASN1_OBJECT)
|
||||
|
||||
@@ -97,18 +97,20 @@ IMPLEMENT_ASN1_TYPE(ASN1_ANY)
|
||||
/* Just swallow an ASN1_SEQUENCE in an ASN1_STRING */
|
||||
IMPLEMENT_ASN1_TYPE(ASN1_SEQUENCE)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_TYPE, ASN1_ANY, ASN1_TYPE)
|
||||
|
||||
/* Multistring types */
|
||||
|
||||
IMPLEMENT_ASN1_MSTRING(ASN1_PRINTABLE, B_ASN1_PRINTABLE)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_name(ASN1_STRING, ASN1_PRINTABLE)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, ASN1_PRINTABLE,
|
||||
ASN1_PRINTABLE)
|
||||
|
||||
IMPLEMENT_ASN1_MSTRING(DISPLAYTEXT, B_ASN1_DISPLAYTEXT)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_name(ASN1_STRING, DISPLAYTEXT)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, DISPLAYTEXT, DISPLAYTEXT)
|
||||
|
||||
IMPLEMENT_ASN1_MSTRING(DIRECTORYSTRING, B_ASN1_DIRECTORYSTRING)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_name(ASN1_STRING, DIRECTORYSTRING)
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, DIRECTORYSTRING,
|
||||
DIRECTORYSTRING)
|
||||
|
||||
/* Three separate BOOLEAN type: normal, DEFAULT TRUE and DEFAULT FALSE */
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, -1)
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
/* Utility functions for manipulating fields and offsets */
|
||||
@@ -118,6 +118,7 @@ int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
}
|
||||
|
||||
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
assert(it->itype == ASN1_ITYPE_SEQUENCE);
|
||||
const ASN1_AUX *aux;
|
||||
if (!pval || !*pval) {
|
||||
return NULL;
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
#define _POSIX_C_SOURCE 201410L /* for gmtime_r */
|
||||
#endif
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
|
||||
@@ -122,6 +122,19 @@ int EVP_EncodedLength(size_t *out_len, size_t len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void) {
|
||||
EVP_ENCODE_CTX *ret = OPENSSL_malloc(sizeof(EVP_ENCODE_CTX));
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(ret, 0, sizeof(EVP_ENCODE_CTX));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx) {
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
|
||||
OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
|
||||
}
|
||||
@@ -265,14 +278,17 @@ static uint8_t base64_ascii_to_bin(uint8_t a) {
|
||||
const uint8_t is_slash = constant_time_eq_8(a, '/');
|
||||
const uint8_t is_equals = constant_time_eq_8(a, '=');
|
||||
|
||||
uint8_t ret = 0xff; // 0xff signals invalid.
|
||||
ret = constant_time_select_8(is_upper, a - 'A', ret); // [0,26)
|
||||
ret = constant_time_select_8(is_lower, a - 'a' + 26, ret); // [26,52)
|
||||
ret = constant_time_select_8(is_digit, a - '0' + 52, ret); // [52,62)
|
||||
ret = constant_time_select_8(is_plus, 62, ret);
|
||||
ret = constant_time_select_8(is_slash, 63, ret);
|
||||
// Padding maps to zero, to be further handled by the caller.
|
||||
ret = constant_time_select_8(is_equals, 0, ret);
|
||||
uint8_t ret = 0;
|
||||
ret |= is_upper & (a - 'A'); // [0,26)
|
||||
ret |= is_lower & (a - 'a' + 26); // [26,52)
|
||||
ret |= is_digit & (a - '0' + 52); // [52,62)
|
||||
ret |= is_plus & 62;
|
||||
ret |= is_slash & 63;
|
||||
// Invalid inputs, 'A', and '=' have all been mapped to zero. Map invalid
|
||||
// inputs to 0xff. Note '=' is padding and handled separately by the caller.
|
||||
const uint8_t is_valid =
|
||||
is_upper | is_lower | is_digit | is_plus | is_slash | is_equals;
|
||||
ret |= ~is_valid;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,17 +116,11 @@ static int mem_new(BIO *bio) {
|
||||
}
|
||||
|
||||
static int mem_free(BIO *bio) {
|
||||
BUF_MEM *b;
|
||||
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
b = (BUF_MEM *)bio->ptr;
|
||||
BUF_MEM *b = (BUF_MEM *)bio->ptr;
|
||||
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
b->data = NULL;
|
||||
}
|
||||
|
||||
@@ -320,7 +320,7 @@ static int conn_new(BIO *bio) {
|
||||
bio->init = 0;
|
||||
bio->num = -1;
|
||||
bio->flags = 0;
|
||||
bio->ptr = (char *)BIO_CONNECT_new();
|
||||
bio->ptr = BIO_CONNECT_new();
|
||||
return bio->ptr != NULL;
|
||||
}
|
||||
|
||||
@@ -340,10 +340,6 @@ static void conn_close_socket(BIO *bio) {
|
||||
}
|
||||
|
||||
static int conn_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bio->shutdown) {
|
||||
conn_close_socket(bio);
|
||||
}
|
||||
|
||||
@@ -146,10 +146,6 @@ static int fd_new(BIO *bio) {
|
||||
}
|
||||
|
||||
static int fd_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bio->shutdown) {
|
||||
if (bio->init) {
|
||||
BORINGSSL_CLOSE(bio->num);
|
||||
|
||||
@@ -126,13 +126,7 @@ BIO *BIO_new_fp(FILE *stream, int close_flag) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int file_new(BIO *bio) { return 1; }
|
||||
|
||||
static int file_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bio->shutdown) {
|
||||
return 1;
|
||||
}
|
||||
@@ -279,7 +273,7 @@ static const BIO_METHOD methods_filep = {
|
||||
BIO_TYPE_FILE, "FILE pointer",
|
||||
file_write, file_read,
|
||||
NULL /* puts */, file_gets,
|
||||
file_ctrl, file_new,
|
||||
file_ctrl, NULL /* create */,
|
||||
file_free, NULL /* callback_ctrl */,
|
||||
};
|
||||
|
||||
|
||||
@@ -127,12 +127,7 @@ static void bio_destroy_pair(BIO *bio) {
|
||||
}
|
||||
|
||||
static int bio_free(BIO *bio) {
|
||||
struct bio_bio_st *b;
|
||||
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
b = bio->ptr;
|
||||
struct bio_bio_st *b = bio->ptr;
|
||||
|
||||
assert(b != NULL);
|
||||
|
||||
|
||||
@@ -81,19 +81,7 @@ static int closesocket(int sock) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static int sock_new(BIO *bio) {
|
||||
bio->init = 0;
|
||||
bio->num = 0;
|
||||
bio->ptr = NULL;
|
||||
bio->flags = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sock_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bio->shutdown) {
|
||||
if (bio->init) {
|
||||
closesocket(bio->num);
|
||||
@@ -105,17 +93,15 @@ static int sock_free(BIO *bio) {
|
||||
}
|
||||
|
||||
static int sock_read(BIO *b, char *out, int outl) {
|
||||
int ret = 0;
|
||||
|
||||
if (out == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bio_clear_socket_error();
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
ret = recv(b->num, out, outl, 0);
|
||||
int ret = recv(b->num, out, outl, 0);
|
||||
#else
|
||||
ret = read(b->num, out, outl);
|
||||
int ret = read(b->num, out, outl);
|
||||
#endif
|
||||
BIO_clear_retry_flags(b);
|
||||
if (ret <= 0) {
|
||||
@@ -186,7 +172,7 @@ static const BIO_METHOD methods_sockp = {
|
||||
BIO_TYPE_SOCKET, "socket",
|
||||
sock_write, sock_read,
|
||||
NULL /* puts */, NULL /* gets, */,
|
||||
sock_ctrl, sock_new,
|
||||
sock_ctrl, NULL /* create */,
|
||||
sock_free, NULL /* callback_ctrl */,
|
||||
};
|
||||
|
||||
|
||||
@@ -42,19 +42,17 @@ static const uint8_t kSigma[10 * 16] = {
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
#define RIGHT_ROTATE(v, n) (((v) >> (n)) | ((v) << (64 - (n))))
|
||||
|
||||
// https://tools.ietf.org/html/rfc7693#section-3.1
|
||||
static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x,
|
||||
uint64_t y) {
|
||||
v[a] = v[a] + v[b] + x;
|
||||
v[d] = RIGHT_ROTATE(v[d] ^ v[a], 32);
|
||||
v[d] = CRYPTO_rotr_u64(v[d] ^ v[a], 32);
|
||||
v[c] = v[c] + v[d];
|
||||
v[b] = RIGHT_ROTATE(v[b] ^ v[c], 24);
|
||||
v[b] = CRYPTO_rotr_u64(v[b] ^ v[c], 24);
|
||||
v[a] = v[a] + v[b] + y;
|
||||
v[d] = RIGHT_ROTATE(v[d] ^ v[a], 16);
|
||||
v[d] = CRYPTO_rotr_u64(v[d] ^ v[a], 16);
|
||||
v[c] = v[c] + v[d];
|
||||
v[b] = RIGHT_ROTATE(v[b] ^ v[c], 63);
|
||||
v[b] = CRYPTO_rotr_u64(v[b] ^ v[c], 63);
|
||||
}
|
||||
|
||||
static void blake2b_transform(
|
||||
|
||||
@@ -29,8 +29,10 @@ static const unsigned kMaxDepth = 2048;
|
||||
// is_string_type returns one if |tag| is a string type and zero otherwise. It
|
||||
// ignores the constructed bit.
|
||||
static int is_string_type(unsigned tag) {
|
||||
// While BER supports constructed BIT STRINGS, OpenSSL misparses them. To
|
||||
// avoid acting on an ambiguous input, we do not support constructed BIT
|
||||
// STRINGS. See https://github.com/openssl/openssl/issues/12810.
|
||||
switch (tag & ~CBS_ASN1_CONSTRUCTED) {
|
||||
case CBS_ASN1_BITSTRING:
|
||||
case CBS_ASN1_OCTETSTRING:
|
||||
case CBS_ASN1_UTF8STRING:
|
||||
case CBS_ASN1_NUMERICSTRING:
|
||||
|
||||
@@ -404,6 +404,15 @@ int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBB_add_zeros(CBB *cbb, size_t len) {
|
||||
uint8_t *out;
|
||||
if (!CBB_add_space(cbb, &out, len)) {
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memset(out, 0, len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
|
||||
if (!CBB_flush(cbb) ||
|
||||
!cbb_buffer_add(cbb->base, out_data, len)) {
|
||||
|
||||
@@ -216,6 +216,14 @@ int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
|
||||
return cbs_get_length_prefixed(cbs, out, 3);
|
||||
}
|
||||
|
||||
int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
|
||||
const uint8_t *split = OPENSSL_memchr(CBS_data(cbs), c, CBS_len(cbs));
|
||||
if (split == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
|
||||
}
|
||||
|
||||
// parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
|
||||
// |*out| to the result. This is the encoding used in DER for both high tag
|
||||
// number form and OID components.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__APPLE__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -25,22 +25,20 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
#define U8TO32_LITTLE(p) \
|
||||
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
|
||||
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
|
||||
|
||||
// sigma contains the ChaCha constants, which happen to be an ASCII string.
|
||||
static const uint8_t sigma[16] = { 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3',
|
||||
'2', '-', 'b', 'y', 't', 'e', ' ', 'k' };
|
||||
|
||||
#define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
|
||||
|
||||
// QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round.
|
||||
#define QUARTERROUND(a, b, c, d) \
|
||||
x[a] += x[b]; x[d] = ROTATE(x[d] ^ x[a], 16); \
|
||||
x[c] += x[d]; x[b] = ROTATE(x[b] ^ x[c], 12); \
|
||||
x[a] += x[b]; x[d] = ROTATE(x[d] ^ x[a], 8); \
|
||||
x[c] += x[d]; x[b] = ROTATE(x[b] ^ x[c], 7);
|
||||
#define QUARTERROUND(a, b, c, d) \
|
||||
x[a] += x[b]; \
|
||||
x[d] = CRYPTO_rotl_u32(x[d] ^ x[a], 16); \
|
||||
x[c] += x[d]; \
|
||||
x[b] = CRYPTO_rotl_u32(x[b] ^ x[c], 12); \
|
||||
x[a] += x[b]; \
|
||||
x[d] = CRYPTO_rotl_u32(x[d] ^ x[a], 8); \
|
||||
x[c] += x[d]; \
|
||||
x[b] = CRYPTO_rotl_u32(x[b] ^ x[c], 7);
|
||||
|
||||
void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32],
|
||||
const uint8_t nonce[16]) {
|
||||
@@ -71,24 +69,25 @@ void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
|
||||
uint32_t counter) {
|
||||
assert(!buffers_alias(out, in_len, in, in_len) || in == out);
|
||||
|
||||
uint32_t counter_nonce[4]; counter_nonce[0] = counter;
|
||||
counter_nonce[1] = U8TO32_LITTLE(nonce + 0);
|
||||
counter_nonce[2] = U8TO32_LITTLE(nonce + 4);
|
||||
counter_nonce[3] = U8TO32_LITTLE(nonce + 8);
|
||||
uint32_t counter_nonce[4];
|
||||
counter_nonce[0] = counter;
|
||||
counter_nonce[1] = CRYPTO_load_u32_le(nonce + 0);
|
||||
counter_nonce[2] = CRYPTO_load_u32_le(nonce + 4);
|
||||
counter_nonce[3] = CRYPTO_load_u32_le(nonce + 8);
|
||||
|
||||
const uint32_t *key_ptr = (const uint32_t *)key;
|
||||
#if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64)
|
||||
// The assembly expects the key to be four-byte aligned.
|
||||
uint32_t key_u32[8];
|
||||
if ((((uintptr_t)key) & 3) != 0) {
|
||||
key_u32[0] = U8TO32_LITTLE(key + 0);
|
||||
key_u32[1] = U8TO32_LITTLE(key + 4);
|
||||
key_u32[2] = U8TO32_LITTLE(key + 8);
|
||||
key_u32[3] = U8TO32_LITTLE(key + 12);
|
||||
key_u32[4] = U8TO32_LITTLE(key + 16);
|
||||
key_u32[5] = U8TO32_LITTLE(key + 20);
|
||||
key_u32[6] = U8TO32_LITTLE(key + 24);
|
||||
key_u32[7] = U8TO32_LITTLE(key + 28);
|
||||
key_u32[0] = CRYPTO_load_u32_le(key + 0);
|
||||
key_u32[1] = CRYPTO_load_u32_le(key + 4);
|
||||
key_u32[2] = CRYPTO_load_u32_le(key + 8);
|
||||
key_u32[3] = CRYPTO_load_u32_le(key + 12);
|
||||
key_u32[4] = CRYPTO_load_u32_le(key + 16);
|
||||
key_u32[5] = CRYPTO_load_u32_le(key + 20);
|
||||
key_u32[6] = CRYPTO_load_u32_le(key + 24);
|
||||
key_u32[7] = CRYPTO_load_u32_le(key + 28);
|
||||
|
||||
key_ptr = key_u32;
|
||||
}
|
||||
@@ -99,14 +98,6 @@ void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
|
||||
|
||||
#else
|
||||
|
||||
#define U32TO8_LITTLE(p, v) \
|
||||
{ \
|
||||
(p)[0] = (v >> 0) & 0xff; \
|
||||
(p)[1] = (v >> 8) & 0xff; \
|
||||
(p)[2] = (v >> 16) & 0xff; \
|
||||
(p)[3] = (v >> 24) & 0xff; \
|
||||
}
|
||||
|
||||
// chacha_core performs 20 rounds of ChaCha on the input words in
|
||||
// |input| and writes the 64 output bytes to |output|.
|
||||
static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
|
||||
@@ -129,7 +120,7 @@ static void chacha_core(uint8_t output[64], const uint32_t input[16]) {
|
||||
x[i] += input[i];
|
||||
}
|
||||
for (i = 0; i < 16; ++i) {
|
||||
U32TO8_LITTLE(output + 4 * i, x[i]);
|
||||
CRYPTO_store_u32_le(output + 4 * i, x[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,25 +133,25 @@ void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
|
||||
uint8_t buf[64];
|
||||
size_t todo, i;
|
||||
|
||||
input[0] = U8TO32_LITTLE(sigma + 0);
|
||||
input[1] = U8TO32_LITTLE(sigma + 4);
|
||||
input[2] = U8TO32_LITTLE(sigma + 8);
|
||||
input[3] = U8TO32_LITTLE(sigma + 12);
|
||||
input[0] = CRYPTO_load_u32_le(sigma + 0);
|
||||
input[1] = CRYPTO_load_u32_le(sigma + 4);
|
||||
input[2] = CRYPTO_load_u32_le(sigma + 8);
|
||||
input[3] = CRYPTO_load_u32_le(sigma + 12);
|
||||
|
||||
input[4] = U8TO32_LITTLE(key + 0);
|
||||
input[5] = U8TO32_LITTLE(key + 4);
|
||||
input[6] = U8TO32_LITTLE(key + 8);
|
||||
input[7] = U8TO32_LITTLE(key + 12);
|
||||
input[4] = CRYPTO_load_u32_le(key + 0);
|
||||
input[5] = CRYPTO_load_u32_le(key + 4);
|
||||
input[6] = CRYPTO_load_u32_le(key + 8);
|
||||
input[7] = CRYPTO_load_u32_le(key + 12);
|
||||
|
||||
input[8] = U8TO32_LITTLE(key + 16);
|
||||
input[9] = U8TO32_LITTLE(key + 20);
|
||||
input[10] = U8TO32_LITTLE(key + 24);
|
||||
input[11] = U8TO32_LITTLE(key + 28);
|
||||
input[8] = CRYPTO_load_u32_le(key + 16);
|
||||
input[9] = CRYPTO_load_u32_le(key + 20);
|
||||
input[10] = CRYPTO_load_u32_le(key + 24);
|
||||
input[11] = CRYPTO_load_u32_le(key + 28);
|
||||
|
||||
input[12] = counter;
|
||||
input[13] = U8TO32_LITTLE(nonce + 0);
|
||||
input[14] = U8TO32_LITTLE(nonce + 4);
|
||||
input[15] = U8TO32_LITTLE(nonce + 8);
|
||||
input[13] = CRYPTO_load_u32_le(nonce + 0);
|
||||
input[14] = CRYPTO_load_u32_le(nonce + 4);
|
||||
input[15] = CRYPTO_load_u32_le(nonce + 8);
|
||||
|
||||
while (in_len > 0) {
|
||||
todo = sizeof(buf);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__APPLE__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__APPLE__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -67,76 +67,60 @@
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
static const struct {
|
||||
int nid;
|
||||
const char *name;
|
||||
const EVP_CIPHER *(*func)(void);
|
||||
} kCiphers[] = {
|
||||
{NID_aes_128_cbc, "aes-128-cbc", EVP_aes_128_cbc},
|
||||
{NID_aes_128_ctr, "aes-128-ctr", EVP_aes_128_ctr},
|
||||
{NID_aes_128_ecb, "aes-128-ecb", EVP_aes_128_ecb},
|
||||
{NID_aes_128_gcm, "aes-128-gcm", EVP_aes_128_gcm},
|
||||
{NID_aes_128_ofb128, "aes-128-ofb", EVP_aes_128_ofb},
|
||||
{NID_aes_192_cbc, "aes-192-cbc", EVP_aes_192_cbc},
|
||||
{NID_aes_192_ctr, "aes-192-ctr", EVP_aes_192_ctr},
|
||||
{NID_aes_192_ecb, "aes-192-ecb", EVP_aes_192_ecb},
|
||||
{NID_aes_192_gcm, "aes-192-gcm", EVP_aes_192_gcm},
|
||||
{NID_aes_192_ofb128, "aes-192-ofb", EVP_aes_192_ofb},
|
||||
{NID_aes_256_cbc, "aes-256-cbc", EVP_aes_256_cbc},
|
||||
{NID_aes_256_ctr, "aes-256-ctr", EVP_aes_256_ctr},
|
||||
{NID_aes_256_ecb, "aes-256-ecb", EVP_aes_256_ecb},
|
||||
{NID_aes_256_gcm, "aes-256-gcm", EVP_aes_256_gcm},
|
||||
{NID_aes_256_ofb128, "aes-256-ofb", EVP_aes_256_ofb},
|
||||
{NID_des_cbc, "des-cbc", EVP_des_cbc},
|
||||
{NID_des_ecb, "des-ecb", EVP_des_ecb},
|
||||
{NID_des_ede_cbc, "des-ede-cbc", EVP_des_ede_cbc},
|
||||
{NID_des_ede_ecb, "des-ede", EVP_des_ede},
|
||||
{NID_des_ede3_cbc, "des-ede3-cbc", EVP_des_ede3_cbc},
|
||||
{NID_rc2_cbc, "rc2-cbc", EVP_rc2_cbc},
|
||||
{NID_rc4, "rc4", EVP_rc4},
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_get_cipherbynid(int nid) {
|
||||
switch (nid) {
|
||||
case NID_rc2_cbc:
|
||||
return EVP_rc2_cbc();
|
||||
case NID_rc2_40_cbc:
|
||||
return EVP_rc2_40_cbc();
|
||||
case NID_des_ede3_cbc:
|
||||
return EVP_des_ede3_cbc();
|
||||
case NID_des_ede_cbc:
|
||||
return EVP_des_cbc();
|
||||
case NID_aes_128_cbc:
|
||||
return EVP_aes_128_cbc();
|
||||
case NID_aes_192_cbc:
|
||||
return EVP_aes_192_cbc();
|
||||
case NID_aes_256_cbc:
|
||||
return EVP_aes_256_cbc();
|
||||
default:
|
||||
return NULL;
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kCiphers); i++) {
|
||||
if (kCiphers[i].nid == nid) {
|
||||
return kCiphers[i].func();
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *EVP_get_cipherbyname(const char *name) {
|
||||
if (OPENSSL_strcasecmp(name, "rc4") == 0) {
|
||||
return EVP_rc4();
|
||||
} else if (OPENSSL_strcasecmp(name, "des-cbc") == 0) {
|
||||
return EVP_des_cbc();
|
||||
} else if (OPENSSL_strcasecmp(name, "des-ede3-cbc") == 0 ||
|
||||
// This is not a name used by OpenSSL, but tcpdump registers it
|
||||
// with |EVP_add_cipher_alias|. Our |EVP_add_cipher_alias| is a
|
||||
// no-op, so we support the name here.
|
||||
OPENSSL_strcasecmp(name, "3des") == 0) {
|
||||
return EVP_des_ede3_cbc();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-128-cbc") == 0) {
|
||||
return EVP_aes_128_cbc();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-192-cbc") == 0) {
|
||||
return EVP_aes_192_cbc();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-256-cbc") == 0) {
|
||||
return EVP_aes_256_cbc();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-128-ctr") == 0) {
|
||||
return EVP_aes_128_ctr();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-192-ctr") == 0) {
|
||||
return EVP_aes_192_ctr();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-256-ctr") == 0) {
|
||||
return EVP_aes_256_ctr();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-128-ecb") == 0) {
|
||||
return EVP_aes_128_ecb();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-192-ecb") == 0) {
|
||||
return EVP_aes_192_ecb();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-256-ecb") == 0) {
|
||||
return EVP_aes_256_ecb();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-128-gcm") == 0) {
|
||||
return EVP_aes_128_gcm();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-192-gcm") == 0) {
|
||||
return EVP_aes_192_gcm();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-256-gcm") == 0) {
|
||||
return EVP_aes_256_gcm();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-128-ofb") == 0) {
|
||||
return EVP_aes_128_ofb();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-192-ofb") == 0) {
|
||||
return EVP_aes_192_ofb();
|
||||
} else if (OPENSSL_strcasecmp(name, "aes-256-ofb") == 0) {
|
||||
return EVP_aes_256_ofb();
|
||||
} else if (OPENSSL_strcasecmp(name, "des-ecb") == 0) {
|
||||
return EVP_des_ecb();
|
||||
} else if (OPENSSL_strcasecmp(name, "des-ede") == 0) {
|
||||
return EVP_des_ede();
|
||||
} else if (OPENSSL_strcasecmp(name, "des-ede-cbc") == 0) {
|
||||
return EVP_des_ede_cbc();
|
||||
} else if (OPENSSL_strcasecmp(name, "rc2-cbc") == 0) {
|
||||
return EVP_rc2_cbc();
|
||||
if (name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// This is not a name used by OpenSSL, but tcpdump registers it with
|
||||
// |EVP_add_cipher_alias|. Our |EVP_add_cipher_alias| is a no-op, so we
|
||||
// support the name here.
|
||||
if (OPENSSL_strcasecmp(name, "3des") == 0) {
|
||||
name = "des-ede3-cbc";
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kCiphers); i++) {
|
||||
if (OPENSSL_strcasecmp(kCiphers[i].name, name) == 0) {
|
||||
return kCiphers[i].func();
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
@@ -343,7 +343,7 @@ static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
|
||||
if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
|
||||
if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
|
||||
ad_fixed, out, data_plus_mac_len, total,
|
||||
ad_fixed, out, data_len, total,
|
||||
tls_ctx->mac_key, tls_ctx->mac_key_len)) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
||||
return 0;
|
||||
@@ -406,14 +406,6 @@ static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
|
||||
EVP_sha1(), 1);
|
||||
}
|
||||
|
||||
static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
|
||||
EVP_sha256(), 0);
|
||||
}
|
||||
|
||||
static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
||||
size_t key_len, size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
@@ -428,22 +420,6 @@ static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
|
||||
EVP_sha1(), 1);
|
||||
}
|
||||
|
||||
static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
||||
EVP_sha256(), 0);
|
||||
}
|
||||
|
||||
static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
||||
EVP_sha384(), 0);
|
||||
}
|
||||
|
||||
static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
@@ -513,23 +489,6 @@ static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
|
||||
SHA256_DIGEST_LENGTH + 16, // key len (SHA256 + AES128)
|
||||
16, // nonce len (IV)
|
||||
16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
|
||||
SHA256_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_aes_128_cbc_sha256_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
|
||||
SHA_DIGEST_LENGTH + 32, // key len (SHA1 + AES256)
|
||||
16, // nonce len (IV)
|
||||
@@ -564,40 +523,6 @@ static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
|
||||
SHA256_DIGEST_LENGTH + 32, // key len (SHA256 + AES256)
|
||||
16, // nonce len (IV)
|
||||
16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
|
||||
SHA256_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_aes_256_cbc_sha256_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
|
||||
SHA384_DIGEST_LENGTH + 32, // key len (SHA384 + AES256)
|
||||
16, // nonce len (IV)
|
||||
16 + SHA384_DIGEST_LENGTH, // overhead (padding + SHA384)
|
||||
SHA384_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_aes_256_cbc_sha384_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
|
||||
SHA_DIGEST_LENGTH + 24, // key len (SHA1 + 3DES)
|
||||
8, // nonce len (IV)
|
||||
@@ -657,10 +582,6 @@ const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
|
||||
return &aead_aes_128_cbc_sha1_tls_implicit_iv;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
|
||||
return &aead_aes_128_cbc_sha256_tls;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
|
||||
return &aead_aes_256_cbc_sha1_tls;
|
||||
}
|
||||
@@ -669,14 +590,6 @@ const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
|
||||
return &aead_aes_256_cbc_sha1_tls_implicit_iv;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) {
|
||||
return &aead_aes_256_cbc_sha256_tls;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) {
|
||||
return &aead_aes_256_cbc_sha384_tls;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
|
||||
return &aead_des_ede3_cbc_sha1_tls;
|
||||
}
|
||||
|
||||
@@ -99,6 +99,17 @@ void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
|
||||
// which EVP_tls_cbc_digest_record supports.
|
||||
int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
|
||||
|
||||
// EVP_sha1_final_with_secret_suffix computes the result of hashing |len| bytes
|
||||
// from |in| to |ctx| and writes the resulting hash to |out|. |len| is treated
|
||||
// as secret and must be at most |max_len|, which is treated as public. |in|
|
||||
// must point to a buffer of at least |max_len| bytes. It returns one on success
|
||||
// and zero if inputs are too long.
|
||||
//
|
||||
// This function is exported for unit tests.
|
||||
OPENSSL_EXPORT int EVP_sha1_final_with_secret_suffix(
|
||||
SHA_CTX *ctx, uint8_t out[SHA_DIGEST_LENGTH], const uint8_t *in, size_t len,
|
||||
size_t max_len);
|
||||
|
||||
// EVP_tls_cbc_digest_record computes the MAC of a decrypted, padded TLS
|
||||
// record.
|
||||
//
|
||||
@@ -108,8 +119,8 @@ int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
|
||||
// md_out_size: the number of output bytes is written here.
|
||||
// header: the 13-byte, TLS record header.
|
||||
// data: the record data itself
|
||||
// data_plus_mac_size: the secret, reported length of the data and MAC
|
||||
// once the padding has been removed.
|
||||
// data_size: the secret, reported length of the data once the padding and MAC
|
||||
// have been removed.
|
||||
// data_plus_mac_plus_padding_size: the public length of the whole
|
||||
// record, including padding.
|
||||
//
|
||||
@@ -119,7 +130,7 @@ int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
|
||||
// padding too. )
|
||||
int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
size_t *md_out_size, const uint8_t header[13],
|
||||
const uint8_t *data, size_t data_plus_mac_size,
|
||||
const uint8_t *data, size_t data_size,
|
||||
size_t data_plus_mac_plus_padding_size,
|
||||
const uint8_t *mac_secret,
|
||||
unsigned mac_secret_length);
|
||||
|
||||
@@ -62,15 +62,6 @@
|
||||
#include "../fipsmodule/cipher/internal.h"
|
||||
|
||||
|
||||
// MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
|
||||
// field. (SHA-384/512 have 128-bit length.)
|
||||
#define MAX_HASH_BIT_COUNT_BYTES 16
|
||||
|
||||
// MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
|
||||
// Currently SHA-384/512 has a 128-byte block size and that's the largest
|
||||
// supported by TLS.)
|
||||
#define MAX_HASH_BLOCK_SIZE 128
|
||||
|
||||
int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
size_t block_size, size_t mac_size) {
|
||||
@@ -183,134 +174,110 @@ void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
|
||||
OPENSSL_memcpy(out, rotated_mac, md_size);
|
||||
}
|
||||
|
||||
// u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
|
||||
// big-endian order. The value of p is advanced by four.
|
||||
#define u32toBE(n, p) \
|
||||
do { \
|
||||
*((p)++) = (uint8_t)((n) >> 24); \
|
||||
*((p)++) = (uint8_t)((n) >> 16); \
|
||||
*((p)++) = (uint8_t)((n) >> 8); \
|
||||
*((p)++) = (uint8_t)((n)); \
|
||||
} while (0)
|
||||
|
||||
// u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
|
||||
// big-endian order. The value of p is advanced by eight.
|
||||
#define u64toBE(n, p) \
|
||||
do { \
|
||||
*((p)++) = (uint8_t)((n) >> 56); \
|
||||
*((p)++) = (uint8_t)((n) >> 48); \
|
||||
*((p)++) = (uint8_t)((n) >> 40); \
|
||||
*((p)++) = (uint8_t)((n) >> 32); \
|
||||
*((p)++) = (uint8_t)((n) >> 24); \
|
||||
*((p)++) = (uint8_t)((n) >> 16); \
|
||||
*((p)++) = (uint8_t)((n) >> 8); \
|
||||
*((p)++) = (uint8_t)((n)); \
|
||||
} while (0)
|
||||
|
||||
typedef union {
|
||||
SHA_CTX sha1;
|
||||
SHA256_CTX sha256;
|
||||
SHA512_CTX sha512;
|
||||
} HASH_CTX;
|
||||
|
||||
static void tls1_sha1_transform(HASH_CTX *ctx, const uint8_t *block) {
|
||||
SHA1_Transform(&ctx->sha1, block);
|
||||
}
|
||||
|
||||
static void tls1_sha256_transform(HASH_CTX *ctx, const uint8_t *block) {
|
||||
SHA256_Transform(&ctx->sha256, block);
|
||||
}
|
||||
|
||||
static void tls1_sha512_transform(HASH_CTX *ctx, const uint8_t *block) {
|
||||
SHA512_Transform(&ctx->sha512, block);
|
||||
}
|
||||
|
||||
// These functions serialize the state of a hash and thus perform the standard
|
||||
// "final" operation without adding the padding and length that such a function
|
||||
// typically does.
|
||||
static void tls1_sha1_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
|
||||
SHA_CTX *sha1 = &ctx->sha1;
|
||||
u32toBE(sha1->h[0], md_out);
|
||||
u32toBE(sha1->h[1], md_out);
|
||||
u32toBE(sha1->h[2], md_out);
|
||||
u32toBE(sha1->h[3], md_out);
|
||||
u32toBE(sha1->h[4], md_out);
|
||||
}
|
||||
|
||||
static void tls1_sha256_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
|
||||
SHA256_CTX *sha256 = &ctx->sha256;
|
||||
for (unsigned i = 0; i < 8; i++) {
|
||||
u32toBE(sha256->h[i], md_out);
|
||||
int EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx,
|
||||
uint8_t out[SHA_DIGEST_LENGTH],
|
||||
const uint8_t *in, size_t len,
|
||||
size_t max_len) {
|
||||
// Bound the input length so |total_bits| below fits in four bytes. This is
|
||||
// redundant with TLS record size limits. This also ensures |input_idx| below
|
||||
// does not overflow.
|
||||
size_t max_len_bits = max_len << 3;
|
||||
if (ctx->Nh != 0 ||
|
||||
(max_len_bits >> 3) != max_len || // Overflow
|
||||
ctx->Nl + max_len_bits < max_len_bits ||
|
||||
ctx->Nl + max_len_bits > UINT32_MAX) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void tls1_sha512_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
|
||||
SHA512_CTX *sha512 = &ctx->sha512;
|
||||
for (unsigned i = 0; i < 8; i++) {
|
||||
u64toBE(sha512->h[i], md_out);
|
||||
// We need to hash the following into |ctx|:
|
||||
//
|
||||
// - ctx->data[:ctx->num]
|
||||
// - in[:len]
|
||||
// - A 0x80 byte
|
||||
// - However many zero bytes are needed to pad up to a block.
|
||||
// - Eight bytes of length.
|
||||
size_t num_blocks = (ctx->num + len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
|
||||
size_t last_block = num_blocks - 1;
|
||||
size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
|
||||
|
||||
// The bounds above imply |total_bits| fits in four bytes.
|
||||
size_t total_bits = ctx->Nl + (len << 3);
|
||||
uint8_t length_bytes[4];
|
||||
length_bytes[0] = (uint8_t)(total_bits >> 24);
|
||||
length_bytes[1] = (uint8_t)(total_bits >> 16);
|
||||
length_bytes[2] = (uint8_t)(total_bits >> 8);
|
||||
length_bytes[3] = (uint8_t)total_bits;
|
||||
|
||||
// We now construct and process each expected block in constant-time.
|
||||
uint8_t block[SHA_CBLOCK] = {0};
|
||||
uint32_t result[5] = {0};
|
||||
// input_idx is the index into |in| corresponding to the current block.
|
||||
// However, we allow this index to overflow beyond |max_len|, to simplify the
|
||||
// 0x80 byte.
|
||||
size_t input_idx = 0;
|
||||
for (size_t i = 0; i < max_blocks; i++) {
|
||||
// Fill |block| with data from the partial block in |ctx| and |in|. We copy
|
||||
// as if we were hashing up to |max_len| and then zero the excess later.
|
||||
size_t block_start = 0;
|
||||
if (i == 0) {
|
||||
OPENSSL_memcpy(block, ctx->data, ctx->num);
|
||||
block_start = ctx->num;
|
||||
}
|
||||
if (input_idx < max_len) {
|
||||
size_t to_copy = SHA_CBLOCK - block_start;
|
||||
if (to_copy > max_len - input_idx) {
|
||||
to_copy = max_len - input_idx;
|
||||
}
|
||||
OPENSSL_memcpy(block + block_start, in + input_idx, to_copy);
|
||||
}
|
||||
|
||||
// Zero any bytes beyond |len| and add the 0x80 byte.
|
||||
for (size_t j = block_start; j < SHA_CBLOCK; j++) {
|
||||
// input[idx] corresponds to block[j].
|
||||
size_t idx = input_idx + j - block_start;
|
||||
// The barriers on |len| are not strictly necessary. However, without
|
||||
// them, GCC compiles this code by incorporating |len| into the loop
|
||||
// counter and subtracting it out later. This is still constant-time, but
|
||||
// it frustrates attempts to validate this.
|
||||
uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len));
|
||||
uint8_t is_padding_byte = constant_time_eq_8(idx, value_barrier_w(len));
|
||||
block[j] &= is_in_bounds;
|
||||
block[j] |= 0x80 & is_padding_byte;
|
||||
}
|
||||
|
||||
input_idx += SHA_CBLOCK - block_start;
|
||||
|
||||
// Fill in the length if this is the last block.
|
||||
crypto_word_t is_last_block = constant_time_eq_w(i, last_block);
|
||||
for (size_t j = 0; j < 4; j++) {
|
||||
block[SHA_CBLOCK - 4 + j] |= is_last_block & length_bytes[j];
|
||||
}
|
||||
|
||||
// Process the block and save the hash state if it is the final value.
|
||||
SHA1_Transform(ctx, block);
|
||||
for (size_t j = 0; j < 5; j++) {
|
||||
result[j] |= is_last_block & ctx->h[j];
|
||||
}
|
||||
}
|
||||
|
||||
// Write the output.
|
||||
for (size_t i = 0; i < 5; i++) {
|
||||
CRYPTO_store_u32_be(out + 4 * i, result[i]);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
|
||||
switch (EVP_MD_type(md)) {
|
||||
case NID_sha1:
|
||||
case NID_sha256:
|
||||
case NID_sha384:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return EVP_MD_type(md) == NID_sha1;
|
||||
}
|
||||
|
||||
int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
size_t *md_out_size, const uint8_t header[13],
|
||||
const uint8_t *data, size_t data_plus_mac_size,
|
||||
const uint8_t *data, size_t data_size,
|
||||
size_t data_plus_mac_plus_padding_size,
|
||||
const uint8_t *mac_secret,
|
||||
unsigned mac_secret_length) {
|
||||
HASH_CTX md_state;
|
||||
void (*md_final_raw)(HASH_CTX *ctx, uint8_t *md_out);
|
||||
void (*md_transform)(HASH_CTX *ctx, const uint8_t *block);
|
||||
unsigned md_size, md_block_size = 64, md_block_shift = 6;
|
||||
// md_length_size is the number of bytes in the length field that terminates
|
||||
// the hash.
|
||||
unsigned md_length_size = 8;
|
||||
|
||||
// Bound the acceptable input so we can forget about many possible overflows
|
||||
// later in this function. This is redundant with the record size limits in
|
||||
// TLS.
|
||||
if (data_plus_mac_plus_padding_size >= 1024 * 1024) {
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (EVP_MD_type(md)) {
|
||||
case NID_sha1:
|
||||
SHA1_Init(&md_state.sha1);
|
||||
md_final_raw = tls1_sha1_final_raw;
|
||||
md_transform = tls1_sha1_transform;
|
||||
md_size = SHA_DIGEST_LENGTH;
|
||||
break;
|
||||
|
||||
case NID_sha256:
|
||||
SHA256_Init(&md_state.sha256);
|
||||
md_final_raw = tls1_sha256_final_raw;
|
||||
md_transform = tls1_sha256_transform;
|
||||
md_size = SHA256_DIGEST_LENGTH;
|
||||
break;
|
||||
|
||||
case NID_sha384:
|
||||
SHA384_Init(&md_state.sha512);
|
||||
md_final_raw = tls1_sha512_final_raw;
|
||||
md_transform = tls1_sha512_transform;
|
||||
md_size = SHA384_DIGEST_LENGTH;
|
||||
md_block_size = 128;
|
||||
md_block_shift = 7;
|
||||
md_length_size = 16;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (EVP_MD_type(md) != NID_sha1) {
|
||||
// EVP_tls_cbc_record_digest_supported should have been called first to
|
||||
// check that the hash function is supported.
|
||||
assert(0);
|
||||
@@ -318,175 +285,54 @@ int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
|
||||
assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
|
||||
assert(md_block_size == (1u << md_block_shift));
|
||||
assert(md_size <= EVP_MAX_MD_SIZE);
|
||||
|
||||
static const size_t kHeaderLength = 13;
|
||||
|
||||
// kVarianceBlocks is the number of blocks of the hash that we have to
|
||||
// calculate in constant time because they could be altered by the
|
||||
// padding value.
|
||||
//
|
||||
// TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
|
||||
// required to be minimal. Therefore we say that the final |kVarianceBlocks|
|
||||
// blocks can vary based on the padding and on the hash used. This value
|
||||
// must be derived from public information.
|
||||
const size_t kVarianceBlocks =
|
||||
( 255 + 1 + // maximum padding bytes + padding length
|
||||
md_size + // length of hash's output
|
||||
md_block_size - 1 // ceiling
|
||||
) / md_block_size
|
||||
+ 1; // the 0x80 marker and the encoded message length could or not
|
||||
// require an extra block; since the exact value depends on the
|
||||
// message length; thus, one extra block is always added to run
|
||||
// in constant time.
|
||||
|
||||
// From now on we're dealing with the MAC, which conceptually has 13
|
||||
// bytes of `header' before the start of the data.
|
||||
size_t len = data_plus_mac_plus_padding_size + kHeaderLength;
|
||||
// max_mac_bytes contains the maximum bytes of bytes in the MAC, including
|
||||
// |header|, assuming that there's no padding.
|
||||
size_t max_mac_bytes = len - md_size - 1;
|
||||
// num_blocks is the maximum number of hash blocks.
|
||||
size_t num_blocks =
|
||||
(max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
|
||||
// In order to calculate the MAC in constant time we have to handle
|
||||
// the final blocks specially because the padding value could cause the
|
||||
// end to appear somewhere in the final |kVarianceBlocks| blocks and we
|
||||
// can't leak where. However, |num_starting_blocks| worth of data can
|
||||
// be hashed right away because no padding value can affect whether
|
||||
// they are plaintext.
|
||||
size_t num_starting_blocks = 0;
|
||||
// k is the starting byte offset into the conceptual header||data where
|
||||
// we start processing.
|
||||
size_t k = 0;
|
||||
// mac_end_offset is the index just past the end of the data to be MACed.
|
||||
size_t mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
|
||||
// c is the index of the 0x80 byte in the final hash block that contains
|
||||
// application data.
|
||||
size_t c = mac_end_offset & (md_block_size - 1);
|
||||
// index_a is the hash block number that contains the 0x80 terminating value.
|
||||
size_t index_a = mac_end_offset >> md_block_shift;
|
||||
// index_b is the hash block number that contains the 64-bit hash length, in
|
||||
// bits.
|
||||
size_t index_b = (mac_end_offset + md_length_size) >> md_block_shift;
|
||||
|
||||
if (num_blocks > kVarianceBlocks) {
|
||||
num_starting_blocks = num_blocks - kVarianceBlocks;
|
||||
k = md_block_size * num_starting_blocks;
|
||||
if (mac_secret_length > SHA_CBLOCK) {
|
||||
// HMAC pads small keys with zeros and hashes large keys down. This function
|
||||
// should never reach the large key case.
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// bits is the hash-length in bits. It includes the additional hash
|
||||
// block for the masked HMAC key.
|
||||
size_t bits = 8 * mac_end_offset; // at most 18 bits to represent
|
||||
|
||||
// Compute the initial HMAC block.
|
||||
bits += 8 * md_block_size;
|
||||
// hmac_pad is the masked HMAC key.
|
||||
uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
|
||||
OPENSSL_memset(hmac_pad, 0, md_block_size);
|
||||
assert(mac_secret_length <= sizeof(hmac_pad));
|
||||
uint8_t hmac_pad[SHA_CBLOCK];
|
||||
OPENSSL_memset(hmac_pad, 0, sizeof(hmac_pad));
|
||||
OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
|
||||
for (size_t i = 0; i < md_block_size; i++) {
|
||||
for (size_t i = 0; i < SHA_CBLOCK; i++) {
|
||||
hmac_pad[i] ^= 0x36;
|
||||
}
|
||||
|
||||
md_transform(&md_state, hmac_pad);
|
||||
SHA_CTX ctx;
|
||||
SHA1_Init(&ctx);
|
||||
SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
|
||||
SHA1_Update(&ctx, header, 13);
|
||||
|
||||
// The length check means |bits| fits in four bytes.
|
||||
uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
|
||||
OPENSSL_memset(length_bytes, 0, md_length_size - 4);
|
||||
length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
|
||||
length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
|
||||
length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
|
||||
length_bytes[md_length_size - 1] = (uint8_t)bits;
|
||||
|
||||
if (k > 0) {
|
||||
// k is a multiple of md_block_size.
|
||||
uint8_t first_block[MAX_HASH_BLOCK_SIZE];
|
||||
OPENSSL_memcpy(first_block, header, 13);
|
||||
OPENSSL_memcpy(first_block + 13, data, md_block_size - 13);
|
||||
md_transform(&md_state, first_block);
|
||||
for (size_t i = 1; i < k / md_block_size; i++) {
|
||||
md_transform(&md_state, data + md_block_size * i - 13);
|
||||
}
|
||||
// There are at most 256 bytes of padding, so we can compute the public
|
||||
// minimum length for |data_size|.
|
||||
size_t min_data_size = 0;
|
||||
if (data_plus_mac_plus_padding_size > SHA_DIGEST_LENGTH + 256) {
|
||||
min_data_size = data_plus_mac_plus_padding_size - SHA_DIGEST_LENGTH - 256;
|
||||
}
|
||||
|
||||
uint8_t mac_out[EVP_MAX_MD_SIZE];
|
||||
OPENSSL_memset(mac_out, 0, sizeof(mac_out));
|
||||
// Hash the public minimum length directly. This reduces the number of blocks
|
||||
// that must be computed in constant-time.
|
||||
SHA1_Update(&ctx, data, min_data_size);
|
||||
|
||||
// We now process the final hash blocks. For each block, we construct
|
||||
// it in constant time. If the |i==index_a| then we'll include the 0x80
|
||||
// bytes and zero pad etc. For each block we selectively copy it, in
|
||||
// constant time, to |mac_out|.
|
||||
for (size_t i = num_starting_blocks;
|
||||
i <= num_starting_blocks + kVarianceBlocks; i++) {
|
||||
uint8_t block[MAX_HASH_BLOCK_SIZE];
|
||||
uint8_t is_block_a = constant_time_eq_8(i, index_a);
|
||||
uint8_t is_block_b = constant_time_eq_8(i, index_b);
|
||||
for (size_t j = 0; j < md_block_size; j++) {
|
||||
uint8_t b = 0;
|
||||
if (k < kHeaderLength) {
|
||||
b = header[k];
|
||||
} else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
|
||||
b = data[k - kHeaderLength];
|
||||
}
|
||||
k++;
|
||||
|
||||
uint8_t is_past_c = is_block_a & constant_time_ge_8(j, c);
|
||||
uint8_t is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
|
||||
// If this is the block containing the end of the
|
||||
// application data, and we are at the offset for the
|
||||
// 0x80 value, then overwrite b with 0x80.
|
||||
b = constant_time_select_8(is_past_c, 0x80, b);
|
||||
// If this the the block containing the end of the
|
||||
// application data and we're past the 0x80 value then
|
||||
// just write zero.
|
||||
b = b & ~is_past_cp1;
|
||||
// If this is index_b (the final block), but not
|
||||
// index_a (the end of the data), then the 64-bit
|
||||
// length didn't fit into index_a and we're having to
|
||||
// add an extra block of zeros.
|
||||
b &= ~is_block_b | is_block_a;
|
||||
|
||||
// The final bytes of one of the blocks contains the
|
||||
// length.
|
||||
if (j >= md_block_size - md_length_size) {
|
||||
// If this is index_b, write a length byte.
|
||||
b = constant_time_select_8(
|
||||
is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
|
||||
}
|
||||
block[j] = b;
|
||||
}
|
||||
|
||||
md_transform(&md_state, block);
|
||||
md_final_raw(&md_state, block);
|
||||
// If this is index_b, copy the hash value to |mac_out|.
|
||||
for (size_t j = 0; j < md_size; j++) {
|
||||
mac_out[j] |= block[j] & is_block_b;
|
||||
}
|
||||
}
|
||||
|
||||
EVP_MD_CTX md_ctx;
|
||||
EVP_MD_CTX_init(&md_ctx);
|
||||
if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
|
||||
EVP_MD_CTX_cleanup(&md_ctx);
|
||||
// Hash the remaining data without leaking |data_size|.
|
||||
uint8_t mac_out[SHA_DIGEST_LENGTH];
|
||||
if (!EVP_sha1_final_with_secret_suffix(
|
||||
&ctx, mac_out, data + min_data_size, data_size - min_data_size,
|
||||
data_plus_mac_plus_padding_size - min_data_size)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Complete the HMAC in the standard manner.
|
||||
for (size_t i = 0; i < md_block_size; i++) {
|
||||
SHA1_Init(&ctx);
|
||||
for (size_t i = 0; i < SHA_CBLOCK; i++) {
|
||||
hmac_pad[i] ^= 0x6a;
|
||||
}
|
||||
|
||||
EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
|
||||
EVP_DigestUpdate(&md_ctx, mac_out, md_size);
|
||||
unsigned md_out_size_u;
|
||||
EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
|
||||
*md_out_size = md_out_size_u;
|
||||
EVP_MD_CTX_cleanup(&md_ctx);
|
||||
|
||||
SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
|
||||
SHA1_Update(&ctx, mac_out, SHA_DIGEST_LENGTH);
|
||||
SHA1_Final(md_out, &ctx);
|
||||
*md_out_size = SHA_DIGEST_LENGTH;
|
||||
return 1;
|
||||
}
|
||||
@@ -68,6 +68,7 @@
|
||||
#include "conf_def.h"
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
#include "../lhash/internal.h"
|
||||
|
||||
|
||||
DEFINE_LHASH_OF(CONF_VALUE)
|
||||
@@ -76,12 +77,16 @@ struct conf_st {
|
||||
LHASH_OF(CONF_VALUE) *data;
|
||||
};
|
||||
|
||||
static const char kDefaultSectionName[] = "default";
|
||||
|
||||
// The maximum length we can grow a value to after variable expansion. 64k
|
||||
// should be more than enough for all reasonable uses.
|
||||
#define MAX_CONF_VALUE_LENGTH 65536
|
||||
|
||||
static uint32_t conf_value_hash(const CONF_VALUE *v) {
|
||||
return (lh_strhash(v->section) << 2) ^ lh_strhash(v->name);
|
||||
const uint32_t section_hash = v->section ? OPENSSL_strhash(v->section) : 0;
|
||||
const uint32_t name_hash = v->name ? OPENSSL_strhash(v->name) : 0;
|
||||
return (section_hash << 2) ^ name_hash;
|
||||
}
|
||||
|
||||
static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
|
||||
@@ -155,12 +160,14 @@ static void value_free(CONF_VALUE *value) {
|
||||
OPENSSL_free(value);
|
||||
}
|
||||
|
||||
static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
|
||||
|
||||
void NCONF_free(CONF *conf) {
|
||||
if (conf == NULL || conf->data == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
lh_CONF_VALUE_doall(conf->data, value_free);
|
||||
lh_CONF_VALUE_doall_arg(conf->data, value_free_arg, NULL);
|
||||
lh_CONF_VALUE_free(conf->data);
|
||||
OPENSSL_free(conf);
|
||||
}
|
||||
@@ -390,6 +397,10 @@ const char *NCONF_get_string(const CONF *conf, const char *section,
|
||||
const char *name) {
|
||||
CONF_VALUE template, *value;
|
||||
|
||||
if (section == NULL) {
|
||||
section = kDefaultSectionName;
|
||||
}
|
||||
|
||||
OPENSSL_memset(&template, 0, sizeof(template));
|
||||
template.section = (char *) section;
|
||||
template.name = (char *) name;
|
||||
@@ -538,7 +549,7 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
section = OPENSSL_strdup("default");
|
||||
section = OPENSSL_strdup(kDefaultSectionName);
|
||||
if (section == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
|
||||
@@ -175,7 +175,13 @@ void OPENSSL_cpuid_setup(void) {
|
||||
hwcap = crypto_get_arm_hwcap_from_cpuinfo(&cpuinfo);
|
||||
}
|
||||
|
||||
// Clear NEON support if known broken.
|
||||
// Clear NEON support if known broken. Note, if NEON is available statically,
|
||||
// the non-NEON code is dropped and this workaround is a no-op.
|
||||
//
|
||||
// TODO(davidben): The Android NDK now builds with NEON statically available
|
||||
// by default. Cronet still has some consumers that support NEON-less devices
|
||||
// (b/150371744). Get metrics on whether they still see this CPU and, if not,
|
||||
// remove this check entirely.
|
||||
g_has_broken_neon = crypto_cpuinfo_has_broken_neon(&cpuinfo);
|
||||
if (g_has_broken_neon) {
|
||||
hwcap &= ~HWCAP_NEON;
|
||||
@@ -186,7 +192,10 @@ void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_armcap_P |= ARMV7_NEON;
|
||||
|
||||
// Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
|
||||
// /proc/cpuinfo. See https://crbug.com/596156.
|
||||
// /proc/cpuinfo. See https://crbug.com/boringssl/46. As of February 2021,
|
||||
// this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES
|
||||
// and PMULL extensions are very useful, so we still carry the workaround
|
||||
// for now.
|
||||
unsigned long hwcap2 = 0;
|
||||
if (getauxval != NULL) {
|
||||
hwcap2 = getauxval(AT_HWCAP2);
|
||||
|
||||
@@ -22,15 +22,15 @@
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
char CRYPTO_is_NEON_capable_at_runtime(void) {
|
||||
int CRYPTO_is_NEON_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV7_NEON) != 0;
|
||||
}
|
||||
|
||||
int CRYPTO_is_ARMv8_AES_capable(void) {
|
||||
int CRYPTO_is_ARMv8_AES_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV8_AES) != 0;
|
||||
}
|
||||
|
||||
int CRYPTO_is_ARMv8_PMULL_capable(void) {
|
||||
int CRYPTO_is_ARMv8_PMULL_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV8_PMULL) != 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -820,7 +820,7 @@ static void table_select(ge_precomp *t, int pos, signed char b) {
|
||||
//
|
||||
// Preconditions:
|
||||
// a[31] <= 127
|
||||
void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t *a) {
|
||||
void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]) {
|
||||
signed char e[64];
|
||||
signed char carry;
|
||||
ge_p1p1 r;
|
||||
|
||||
@@ -106,7 +106,7 @@ typedef struct {
|
||||
} ge_cached;
|
||||
|
||||
void x25519_ge_tobytes(uint8_t s[32], const ge_p2 *h);
|
||||
int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s);
|
||||
int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]);
|
||||
void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p);
|
||||
void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
|
||||
void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
|
||||
|
||||
@@ -58,11 +58,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_blake2.h>
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
#include <CCryptoBoringSSL_nid.h>
|
||||
|
||||
#include "../asn1/internal.h"
|
||||
#include "../internal.h"
|
||||
#include "../fipsmodule/digest/internal.h"
|
||||
|
||||
@@ -82,6 +83,7 @@ static const struct nid_to_digest nid_to_digest_mapping[] = {
|
||||
{NID_sha256, EVP_sha256, SN_sha256, LN_sha256},
|
||||
{NID_sha384, EVP_sha384, SN_sha384, LN_sha384},
|
||||
{NID_sha512, EVP_sha512, SN_sha512, LN_sha512},
|
||||
{NID_sha512_256, EVP_sha512_256, SN_sha512_256, LN_sha512_256},
|
||||
{NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1},
|
||||
// As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding
|
||||
// hash function when given a signature OID. To avoid unintended lax parsing
|
||||
@@ -152,13 +154,14 @@ static const EVP_MD *cbs_to_md(const CBS *cbs) {
|
||||
}
|
||||
|
||||
const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj) {
|
||||
// Handle objects with no corresponding OID.
|
||||
// Handle objects with no corresponding OID. Note we don't use |OBJ_obj2nid|
|
||||
// here to avoid pulling in the OID table.
|
||||
if (obj->nid != NID_undef) {
|
||||
return EVP_get_digestbynid(obj->nid);
|
||||
}
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, obj->data, obj->length);
|
||||
CBS_init(&cbs, OBJ_get0_data(obj), OBJ_length(obj));
|
||||
return cbs_to_md(&cbs);
|
||||
}
|
||||
|
||||
|
||||
@@ -550,6 +550,27 @@ void DSA_SIG_free(DSA_SIG *sig) {
|
||||
OPENSSL_free(sig);
|
||||
}
|
||||
|
||||
void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r,
|
||||
const BIGNUM **out_s) {
|
||||
if (out_r != NULL) {
|
||||
*out_r = sig->r;
|
||||
}
|
||||
if (out_s != NULL) {
|
||||
*out_s = sig->s;
|
||||
}
|
||||
}
|
||||
|
||||
int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
|
||||
if (r == NULL || s == NULL) {
|
||||
return 0;
|
||||
}
|
||||
BN_free(sig->r);
|
||||
BN_free(sig->s);
|
||||
sig->r = r;
|
||||
sig->s = s;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and
|
||||
// |b| as secret. This function internally uses Montgomery reduction, but
|
||||
// neither inputs nor outputs are in Montgomery form.
|
||||
|
||||
@@ -368,84 +368,6 @@ void ERR_clear_system_error(void) {
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
char *ERR_error_string(uint32_t packed_error, char *ret) {
|
||||
static char buf[ERR_ERROR_STRING_BUF_LEN];
|
||||
|
||||
if (ret == NULL) {
|
||||
// TODO(fork): remove this.
|
||||
ret = buf;
|
||||
}
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
// This is aimed to help catch callers who don't provide
|
||||
// |ERR_ERROR_STRING_BUF_LEN| bytes of space.
|
||||
OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
|
||||
#endif
|
||||
|
||||
return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
|
||||
}
|
||||
|
||||
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
|
||||
char lib_buf[64], reason_buf[64];
|
||||
const char *lib_str, *reason_str;
|
||||
unsigned lib, reason;
|
||||
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lib = ERR_GET_LIB(packed_error);
|
||||
reason = ERR_GET_REASON(packed_error);
|
||||
|
||||
lib_str = ERR_lib_error_string(packed_error);
|
||||
reason_str = ERR_reason_error_string(packed_error);
|
||||
|
||||
if (lib_str == NULL) {
|
||||
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
|
||||
lib_str = lib_buf;
|
||||
}
|
||||
|
||||
if (reason_str == NULL) {
|
||||
BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
|
||||
reason_str = reason_buf;
|
||||
}
|
||||
|
||||
BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
|
||||
packed_error, lib_str, reason_str);
|
||||
|
||||
if (strlen(buf) == len - 1) {
|
||||
// output may be truncated; make sure we always have 5 colon-separated
|
||||
// fields, i.e. 4 colons.
|
||||
static const unsigned num_colons = 4;
|
||||
unsigned i;
|
||||
char *s = buf;
|
||||
|
||||
if (len <= num_colons) {
|
||||
// In this situation it's not possible to ensure that the correct number
|
||||
// of colons are included in the output.
|
||||
return buf;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_colons; i++) {
|
||||
char *colon = strchr(s, ':');
|
||||
char *last_pos = &buf[len - 1] - num_colons + i;
|
||||
|
||||
if (colon == NULL || colon > last_pos) {
|
||||
// set colon |i| at last possible position (buf[len-1] is the
|
||||
// terminating 0). If we're setting this colon, then all whole of the
|
||||
// rest of the string must be colons in order to have the correct
|
||||
// number.
|
||||
OPENSSL_memset(last_pos, ':', num_colons - i);
|
||||
break;
|
||||
}
|
||||
|
||||
s = colon + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
// err_string_cmp is a compare function for searching error values with
|
||||
// |bsearch| in |err_string_lookup|.
|
||||
static int err_string_cmp(const void *a, const void *b) {
|
||||
@@ -530,7 +452,7 @@ static const char *const kLibraryNames[ERR_NUM_LIBS] = {
|
||||
"User defined functions", // ERR_LIB_USER
|
||||
};
|
||||
|
||||
const char *ERR_lib_error_string(uint32_t packed_error) {
|
||||
static const char *err_lib_error_string(uint32_t packed_error) {
|
||||
const uint32_t lib = ERR_GET_LIB(packed_error);
|
||||
|
||||
if (lib >= ERR_NUM_LIBS) {
|
||||
@@ -539,11 +461,16 @@ const char *ERR_lib_error_string(uint32_t packed_error) {
|
||||
return kLibraryNames[lib];
|
||||
}
|
||||
|
||||
const char *ERR_lib_error_string(uint32_t packed_error) {
|
||||
const char *ret = err_lib_error_string(packed_error);
|
||||
return ret == NULL ? "unknown library" : ret;
|
||||
}
|
||||
|
||||
const char *ERR_func_error_string(uint32_t packed_error) {
|
||||
return "OPENSSL_internal";
|
||||
}
|
||||
|
||||
const char *ERR_reason_error_string(uint32_t packed_error) {
|
||||
static const char *err_reason_error_string(uint32_t packed_error) {
|
||||
const uint32_t lib = ERR_GET_LIB(packed_error);
|
||||
const uint32_t reason = ERR_GET_REASON(packed_error);
|
||||
|
||||
@@ -579,6 +506,86 @@ const char *ERR_reason_error_string(uint32_t packed_error) {
|
||||
kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
|
||||
}
|
||||
|
||||
const char *ERR_reason_error_string(uint32_t packed_error) {
|
||||
const char *ret = err_reason_error_string(packed_error);
|
||||
return ret == NULL ? "unknown error" : ret;
|
||||
}
|
||||
|
||||
char *ERR_error_string(uint32_t packed_error, char *ret) {
|
||||
static char buf[ERR_ERROR_STRING_BUF_LEN];
|
||||
|
||||
if (ret == NULL) {
|
||||
// TODO(fork): remove this.
|
||||
ret = buf;
|
||||
}
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
// This is aimed to help catch callers who don't provide
|
||||
// |ERR_ERROR_STRING_BUF_LEN| bytes of space.
|
||||
OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
|
||||
#endif
|
||||
|
||||
return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
|
||||
}
|
||||
|
||||
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned lib = ERR_GET_LIB(packed_error);
|
||||
unsigned reason = ERR_GET_REASON(packed_error);
|
||||
|
||||
const char *lib_str = err_lib_error_string(packed_error);
|
||||
const char *reason_str = err_reason_error_string(packed_error);
|
||||
|
||||
char lib_buf[64], reason_buf[64];
|
||||
if (lib_str == NULL) {
|
||||
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
|
||||
lib_str = lib_buf;
|
||||
}
|
||||
|
||||
if (reason_str == NULL) {
|
||||
BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
|
||||
reason_str = reason_buf;
|
||||
}
|
||||
|
||||
BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
|
||||
packed_error, lib_str, reason_str);
|
||||
|
||||
if (strlen(buf) == len - 1) {
|
||||
// output may be truncated; make sure we always have 5 colon-separated
|
||||
// fields, i.e. 4 colons.
|
||||
static const unsigned num_colons = 4;
|
||||
unsigned i;
|
||||
char *s = buf;
|
||||
|
||||
if (len <= num_colons) {
|
||||
// In this situation it's not possible to ensure that the correct number
|
||||
// of colons are included in the output.
|
||||
return buf;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_colons; i++) {
|
||||
char *colon = strchr(s, ':');
|
||||
char *last_pos = &buf[len - 1] - num_colons + i;
|
||||
|
||||
if (colon == NULL || colon > last_pos) {
|
||||
// set colon |i| at last possible position (buf[len-1] is the
|
||||
// terminating 0). If we're setting this colon, then all whole of the
|
||||
// rest of the string must be colons in order to have the correct
|
||||
// number.
|
||||
OPENSSL_memset(last_pos, ':', num_colons - i);
|
||||
break;
|
||||
}
|
||||
|
||||
s = colon + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
|
||||
char buf[ERR_ERROR_STRING_BUF_LEN];
|
||||
char buf2[1024];
|
||||
@@ -738,6 +745,22 @@ void ERR_add_error_dataf(const char *format, ...) {
|
||||
err_set_error_data(buf);
|
||||
}
|
||||
|
||||
void ERR_set_error_data(char *data, int flags) {
|
||||
if (!(flags & ERR_FLAG_STRING)) {
|
||||
// We do not support non-string error data.
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
if (flags & ERR_FLAG_MALLOCED) {
|
||||
err_set_error_data(data);
|
||||
} else {
|
||||
char *copy = OPENSSL_strdup(data);
|
||||
if (copy != NULL) {
|
||||
err_set_error_data(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ERR_set_mark(void) {
|
||||
ERR_STATE *const state = err_get_state();
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -429,6 +429,15 @@ int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
|
||||
0, (void *)out_md);
|
||||
}
|
||||
|
||||
void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
|
||||
// Node references, but never calls this function, so for now we return NULL.
|
||||
// If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc.,
|
||||
// rather than reading |pkey->pkey.ptr| directly. This avoids problems if our
|
||||
// internal representation does not match the type the caller expects from
|
||||
// OpenSSL.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void OpenSSL_add_all_algorithms(void) {}
|
||||
|
||||
void OPENSSL_add_all_algorithms_conf(void) {}
|
||||
|
||||
@@ -369,8 +369,8 @@ EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **out, const uint8_t **inp,
|
||||
// Unlike OpenSSL, we do not support EC keys with this API. The raw EC
|
||||
// public key serialization requires knowing the group. In OpenSSL, calling
|
||||
// this function with |EVP_PKEY_EC| and setting |out| to NULL does not work.
|
||||
// It requires |*out| to include a partially-initiazed |EVP_PKEY| to extract
|
||||
// the group.
|
||||
// It requires |*out| to include a partially-initialized |EVP_PKEY| to
|
||||
// extract the group.
|
||||
default:
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
|
||||
goto err;
|
||||
|
||||
@@ -32,8 +32,6 @@ typedef struct { uint32_t words[16]; } block_t;
|
||||
|
||||
OPENSSL_STATIC_ASSERT(sizeof(block_t) == 64, "block_t has padding");
|
||||
|
||||
#define R(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
|
||||
|
||||
// salsa208_word_specification implements the Salsa20/8 core function, also
|
||||
// described in RFC 7914, section 3. It modifies the block at |inout|
|
||||
// in-place.
|
||||
@@ -42,38 +40,38 @@ static void salsa208_word_specification(block_t *inout) {
|
||||
OPENSSL_memcpy(&x, inout, sizeof(x));
|
||||
|
||||
for (int i = 8; i > 0; i -= 2) {
|
||||
x.words[4] ^= R(x.words[0] + x.words[12], 7);
|
||||
x.words[8] ^= R(x.words[4] + x.words[0], 9);
|
||||
x.words[12] ^= R(x.words[8] + x.words[4], 13);
|
||||
x.words[0] ^= R(x.words[12] + x.words[8], 18);
|
||||
x.words[9] ^= R(x.words[5] + x.words[1], 7);
|
||||
x.words[13] ^= R(x.words[9] + x.words[5], 9);
|
||||
x.words[1] ^= R(x.words[13] + x.words[9], 13);
|
||||
x.words[5] ^= R(x.words[1] + x.words[13], 18);
|
||||
x.words[14] ^= R(x.words[10] + x.words[6], 7);
|
||||
x.words[2] ^= R(x.words[14] + x.words[10], 9);
|
||||
x.words[6] ^= R(x.words[2] + x.words[14], 13);
|
||||
x.words[10] ^= R(x.words[6] + x.words[2], 18);
|
||||
x.words[3] ^= R(x.words[15] + x.words[11], 7);
|
||||
x.words[7] ^= R(x.words[3] + x.words[15], 9);
|
||||
x.words[11] ^= R(x.words[7] + x.words[3], 13);
|
||||
x.words[15] ^= R(x.words[11] + x.words[7], 18);
|
||||
x.words[1] ^= R(x.words[0] + x.words[3], 7);
|
||||
x.words[2] ^= R(x.words[1] + x.words[0], 9);
|
||||
x.words[3] ^= R(x.words[2] + x.words[1], 13);
|
||||
x.words[0] ^= R(x.words[3] + x.words[2], 18);
|
||||
x.words[6] ^= R(x.words[5] + x.words[4], 7);
|
||||
x.words[7] ^= R(x.words[6] + x.words[5], 9);
|
||||
x.words[4] ^= R(x.words[7] + x.words[6], 13);
|
||||
x.words[5] ^= R(x.words[4] + x.words[7], 18);
|
||||
x.words[11] ^= R(x.words[10] + x.words[9], 7);
|
||||
x.words[8] ^= R(x.words[11] + x.words[10], 9);
|
||||
x.words[9] ^= R(x.words[8] + x.words[11], 13);
|
||||
x.words[10] ^= R(x.words[9] + x.words[8], 18);
|
||||
x.words[12] ^= R(x.words[15] + x.words[14], 7);
|
||||
x.words[13] ^= R(x.words[12] + x.words[15], 9);
|
||||
x.words[14] ^= R(x.words[13] + x.words[12], 13);
|
||||
x.words[15] ^= R(x.words[14] + x.words[13], 18);
|
||||
x.words[4] ^= CRYPTO_rotl_u32(x.words[0] + x.words[12], 7);
|
||||
x.words[8] ^= CRYPTO_rotl_u32(x.words[4] + x.words[0], 9);
|
||||
x.words[12] ^= CRYPTO_rotl_u32(x.words[8] + x.words[4], 13);
|
||||
x.words[0] ^= CRYPTO_rotl_u32(x.words[12] + x.words[8], 18);
|
||||
x.words[9] ^= CRYPTO_rotl_u32(x.words[5] + x.words[1], 7);
|
||||
x.words[13] ^= CRYPTO_rotl_u32(x.words[9] + x.words[5], 9);
|
||||
x.words[1] ^= CRYPTO_rotl_u32(x.words[13] + x.words[9], 13);
|
||||
x.words[5] ^= CRYPTO_rotl_u32(x.words[1] + x.words[13], 18);
|
||||
x.words[14] ^= CRYPTO_rotl_u32(x.words[10] + x.words[6], 7);
|
||||
x.words[2] ^= CRYPTO_rotl_u32(x.words[14] + x.words[10], 9);
|
||||
x.words[6] ^= CRYPTO_rotl_u32(x.words[2] + x.words[14], 13);
|
||||
x.words[10] ^= CRYPTO_rotl_u32(x.words[6] + x.words[2], 18);
|
||||
x.words[3] ^= CRYPTO_rotl_u32(x.words[15] + x.words[11], 7);
|
||||
x.words[7] ^= CRYPTO_rotl_u32(x.words[3] + x.words[15], 9);
|
||||
x.words[11] ^= CRYPTO_rotl_u32(x.words[7] + x.words[3], 13);
|
||||
x.words[15] ^= CRYPTO_rotl_u32(x.words[11] + x.words[7], 18);
|
||||
x.words[1] ^= CRYPTO_rotl_u32(x.words[0] + x.words[3], 7);
|
||||
x.words[2] ^= CRYPTO_rotl_u32(x.words[1] + x.words[0], 9);
|
||||
x.words[3] ^= CRYPTO_rotl_u32(x.words[2] + x.words[1], 13);
|
||||
x.words[0] ^= CRYPTO_rotl_u32(x.words[3] + x.words[2], 18);
|
||||
x.words[6] ^= CRYPTO_rotl_u32(x.words[5] + x.words[4], 7);
|
||||
x.words[7] ^= CRYPTO_rotl_u32(x.words[6] + x.words[5], 9);
|
||||
x.words[4] ^= CRYPTO_rotl_u32(x.words[7] + x.words[6], 13);
|
||||
x.words[5] ^= CRYPTO_rotl_u32(x.words[4] + x.words[7], 18);
|
||||
x.words[11] ^= CRYPTO_rotl_u32(x.words[10] + x.words[9], 7);
|
||||
x.words[8] ^= CRYPTO_rotl_u32(x.words[11] + x.words[10], 9);
|
||||
x.words[9] ^= CRYPTO_rotl_u32(x.words[8] + x.words[11], 13);
|
||||
x.words[10] ^= CRYPTO_rotl_u32(x.words[9] + x.words[8], 18);
|
||||
x.words[12] ^= CRYPTO_rotl_u32(x.words[15] + x.words[14], 7);
|
||||
x.words[13] ^= CRYPTO_rotl_u32(x.words[12] + x.words[15], 9);
|
||||
x.words[14] ^= CRYPTO_rotl_u32(x.words[13] + x.words[12], 13);
|
||||
x.words[15] ^= CRYPTO_rotl_u32(x.words[14] + x.words[13], 18);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__APPLE__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__APPLE__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -285,8 +285,10 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
|
||||
// pointer to the 'top' of snum
|
||||
wnump = &(snum->d[num_n - 1]);
|
||||
|
||||
// Setup to 'res'
|
||||
res->neg = (numerator->neg ^ divisor->neg);
|
||||
// Setup |res|. |numerator| and |res| may alias, so we save |numerator->neg|
|
||||
// for later.
|
||||
const int numerator_neg = numerator->neg;
|
||||
res->neg = (numerator_neg ^ divisor->neg);
|
||||
if (!bn_wexpand(res, loop + 1)) {
|
||||
goto err;
|
||||
}
|
||||
@@ -379,14 +381,11 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
|
||||
bn_set_minimal_width(snum);
|
||||
|
||||
if (rem != NULL) {
|
||||
// Keep a copy of the neg flag in numerator because if |rem| == |numerator|
|
||||
// |BN_rshift| will overwrite it.
|
||||
int neg = numerator->neg;
|
||||
if (!BN_rshift(rem, snum, norm_shift)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_is_zero(rem)) {
|
||||
rem->neg = neg;
|
||||
rem->neg = numerator_neg;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,7 +456,7 @@ void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
|
||||
|
||||
int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
||||
const BIGNUM *numerator, const BIGNUM *divisor,
|
||||
BN_CTX *ctx) {
|
||||
unsigned divisor_min_bits, BN_CTX *ctx) {
|
||||
if (BN_is_negative(numerator) || BN_is_negative(divisor)) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
|
||||
return 0;
|
||||
@@ -497,8 +496,26 @@ int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
||||
r->neg = 0;
|
||||
|
||||
// Incorporate |numerator| into |r|, one bit at a time, reducing after each
|
||||
// step. At the start of each loop iteration, |r| < |divisor|
|
||||
for (int i = numerator->width - 1; i >= 0; i--) {
|
||||
// step. We maintain the invariant that |0 <= r < divisor| and
|
||||
// |q * divisor + r = n| where |n| is the portion of |numerator| incorporated
|
||||
// so far.
|
||||
//
|
||||
// First, we short-circuit the loop: if we know |divisor| has at least
|
||||
// |divisor_min_bits| bits, the top |divisor_min_bits - 1| can be incorporated
|
||||
// without reductions. This significantly speeds up |RSA_check_key|. For
|
||||
// simplicity, we round down to a whole number of words.
|
||||
assert(divisor_min_bits <= BN_num_bits(divisor));
|
||||
int initial_words = 0;
|
||||
if (divisor_min_bits > 0) {
|
||||
initial_words = (divisor_min_bits - 1) / BN_BITS2;
|
||||
if (initial_words > numerator->width) {
|
||||
initial_words = numerator->width;
|
||||
}
|
||||
OPENSSL_memcpy(r->d, numerator->d + numerator->width - initial_words,
|
||||
initial_words * sizeof(BN_ULONG));
|
||||
}
|
||||
|
||||
for (int i = numerator->width - initial_words - 1; i >= 0; i--) {
|
||||
for (int bit = BN_BITS2 - 1; bit >= 0; bit--) {
|
||||
// Incorporate the next bit of the numerator, by computing
|
||||
// r = 2*r or 2*r + 1. Note the result fits in one more word. We store the
|
||||
|
||||
@@ -157,10 +157,11 @@ int bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
|
||||
BN_CTX_start(ctx);
|
||||
unsigned shift;
|
||||
BIGNUM *gcd = BN_CTX_get(ctx);
|
||||
int ret = gcd != NULL &&
|
||||
int ret = gcd != NULL && //
|
||||
bn_mul_consttime(r, a, b, ctx) &&
|
||||
bn_gcd_consttime(gcd, &shift, a, b, ctx) &&
|
||||
bn_div_consttime(r, NULL, r, gcd, ctx) &&
|
||||
// |gcd| has a secret bit width.
|
||||
bn_div_consttime(r, NULL, r, gcd, /*divisor_min_bits=*/0, ctx) &&
|
||||
bn_rshift_secret_shift(r, r, shift, ctx);
|
||||
BN_CTX_end(ctx);
|
||||
return ret;
|
||||
|
||||
@@ -297,7 +297,7 @@ void bn_mul_comba4(BN_ULONG r[8], const BN_ULONG a[4], const BN_ULONG b[4]);
|
||||
void bn_mul_comba8(BN_ULONG r[16], const BN_ULONG a[8], const BN_ULONG b[8]);
|
||||
|
||||
// bn_sqr_comba8 sets |r| to |a|^2.
|
||||
void bn_sqr_comba8(BN_ULONG r[16], const BN_ULONG a[4]);
|
||||
void bn_sqr_comba8(BN_ULONG r[16], const BN_ULONG a[8]);
|
||||
|
||||
// bn_sqr_comba4 sets |r| to |a|^2.
|
||||
void bn_sqr_comba4(BN_ULONG r[8], const BN_ULONG a[4]);
|
||||
@@ -552,12 +552,15 @@ int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
|
||||
// bn_div_consttime behaves like |BN_div|, but it rejects negative inputs and
|
||||
// treats both inputs, including their magnitudes, as secret. It is, as a
|
||||
// result, much slower than |BN_div| and should only be used for rare operations
|
||||
// where Montgomery reduction is not available.
|
||||
// where Montgomery reduction is not available. |divisor_min_bits| is a
|
||||
// public lower bound for |BN_num_bits(divisor)|. When |divisor|'s bit width is
|
||||
// public, this can speed up the operation.
|
||||
//
|
||||
// Note that |quotient->width| will be set pessimally to |numerator->width|.
|
||||
OPENSSL_EXPORT int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
||||
const BIGNUM *numerator,
|
||||
const BIGNUM *divisor, BN_CTX *ctx);
|
||||
const BIGNUM *divisor,
|
||||
unsigned divisor_min_bits, BN_CTX *ctx);
|
||||
|
||||
// bn_is_relatively_prime checks whether GCD(|x|, |y|) is one. On success, it
|
||||
// returns one and sets |*out_relatively_prime| to one if the GCD was one and
|
||||
|
||||
@@ -115,10 +115,6 @@
|
||||
#include "../../internal.h"
|
||||
|
||||
|
||||
// The quick sieve algorithm approach to weeding out primes is Philip
|
||||
// Zimmermann's, as implemented in PGP. I have had a read of his comments and
|
||||
// implemented my own version.
|
||||
|
||||
// kPrimes contains the first 1024 primes.
|
||||
static const uint16_t kPrimes[] = {
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
|
||||
@@ -363,6 +359,18 @@ static int probable_prime_dh(BIGNUM *rnd, int bits, const BIGNUM *add,
|
||||
static int probable_prime_dh_safe(BIGNUM *rnd, int bits, const BIGNUM *add,
|
||||
const BIGNUM *rem, BN_CTX *ctx);
|
||||
|
||||
BN_GENCB *BN_GENCB_new(void) {
|
||||
BN_GENCB *callback = OPENSSL_malloc(sizeof(BN_GENCB));
|
||||
if (callback == NULL) {
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(callback, 0, sizeof(BN_GENCB));
|
||||
return callback;
|
||||
}
|
||||
|
||||
void BN_GENCB_free(BN_GENCB *callback) { OPENSSL_free(callback); }
|
||||
|
||||
void BN_GENCB_set(BN_GENCB *callback,
|
||||
int (*f)(int event, int n, struct bn_gencb_st *),
|
||||
void *arg) {
|
||||
|
||||
@@ -75,10 +75,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
|
||||
if (ret == NULL) {
|
||||
ret = BN_new();
|
||||
}
|
||||
if (ret == NULL) {
|
||||
goto end;
|
||||
}
|
||||
if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
|
||||
if (ret == NULL ||
|
||||
!BN_set_word(ret, BN_is_bit_set(a, 0))) {
|
||||
if (ret != in) {
|
||||
BN_free(ret);
|
||||
}
|
||||
@@ -88,17 +86,15 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
|
||||
}
|
||||
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (BN_is_zero(a) || BN_is_one(a)) {
|
||||
if (ret == NULL) {
|
||||
ret = BN_new();
|
||||
}
|
||||
if (ret == NULL) {
|
||||
goto end;
|
||||
}
|
||||
if (!BN_set_word(ret, BN_is_one(a))) {
|
||||
if (ret == NULL ||
|
||||
!BN_set_word(ret, BN_is_one(a))) {
|
||||
if (ret != in) {
|
||||
BN_free(ret);
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
#include <CCryptoBoringSSL_cipher.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
@@ -224,7 +225,6 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
|
||||
ctx->buf_len = 0;
|
||||
ctx->final_used = 0;
|
||||
ctx->block_mask = ctx->cipher->block_size - 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -238,16 +238,31 @@ int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
|
||||
}
|
||||
|
||||
// block_remainder returns the number of bytes to remove from |len| to get a
|
||||
// multiple of |ctx|'s block size.
|
||||
static int block_remainder(const EVP_CIPHER_CTX *ctx, int len) {
|
||||
// |block_size| must be a power of two.
|
||||
assert(ctx->cipher->block_size != 0);
|
||||
assert((ctx->cipher->block_size & (ctx->cipher->block_size - 1)) == 0);
|
||||
return len & (ctx->cipher->block_size - 1);
|
||||
}
|
||||
|
||||
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
const uint8_t *in, int in_len) {
|
||||
int i, j, bl;
|
||||
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output
|
||||
// does not overflow |*out_len|.
|
||||
int bl = ctx->cipher->block_size;
|
||||
if (bl > 1 && in_len > INT_MAX - bl) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
|
||||
i = ctx->cipher->cipher(ctx, out, in, in_len);
|
||||
if (i < 0) {
|
||||
int ret = ctx->cipher->cipher(ctx, out, in, in_len);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
} else {
|
||||
*out_len = i;
|
||||
*out_len = ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -257,7 +272,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
return in_len == 0;
|
||||
}
|
||||
|
||||
if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
|
||||
if (ctx->buf_len == 0 && block_remainder(ctx, in_len) == 0) {
|
||||
if (ctx->cipher->cipher(ctx, out, in, in_len)) {
|
||||
*out_len = in_len;
|
||||
return 1;
|
||||
@@ -267,8 +282,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
}
|
||||
}
|
||||
|
||||
i = ctx->buf_len;
|
||||
bl = ctx->cipher->block_size;
|
||||
int i = ctx->buf_len;
|
||||
assert(bl <= (int)sizeof(ctx->buf));
|
||||
if (i != 0) {
|
||||
if (bl - i > in_len) {
|
||||
@@ -277,7 +291,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
*out_len = 0;
|
||||
return 1;
|
||||
} else {
|
||||
j = bl - i;
|
||||
int j = bl - i;
|
||||
OPENSSL_memcpy(&ctx->buf[i], in, j);
|
||||
if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
|
||||
return 0;
|
||||
@@ -291,7 +305,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
*out_len = 0;
|
||||
}
|
||||
|
||||
i = in_len & ctx->block_mask;
|
||||
i = block_remainder(ctx, in_len);
|
||||
in_len -= i;
|
||||
if (in_len > 0) {
|
||||
if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
|
||||
@@ -353,8 +367,13 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
|
||||
|
||||
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
const uint8_t *in, int in_len) {
|
||||
int fix_len;
|
||||
unsigned int b;
|
||||
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output
|
||||
// does not overflow |*out_len|.
|
||||
unsigned int b = ctx->cipher->block_size;
|
||||
if (b > 1 && in_len > INT_MAX - (int)b) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
|
||||
int r = ctx->cipher->cipher(ctx, out, in, in_len);
|
||||
@@ -376,15 +395,12 @@ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
|
||||
}
|
||||
|
||||
b = ctx->cipher->block_size;
|
||||
assert(b <= sizeof(ctx->final));
|
||||
|
||||
int fix_len = 0;
|
||||
if (ctx->final_used) {
|
||||
OPENSSL_memcpy(out, ctx->final, b);
|
||||
out += b;
|
||||
fix_len = 1;
|
||||
} else {
|
||||
fix_len = 0;
|
||||
}
|
||||
|
||||
if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
|
||||
@@ -613,6 +629,18 @@ int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
return EVP_CipherInit(ctx, cipher, key, iv, 0);
|
||||
}
|
||||
|
||||
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
|
||||
return EVP_CipherFinal_ex(ctx, out, out_len);
|
||||
}
|
||||
|
||||
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
|
||||
return EVP_EncryptFinal_ex(ctx, out, out_len);
|
||||
}
|
||||
|
||||
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
|
||||
return EVP_DecryptFinal_ex(ctx, out, out_len);
|
||||
}
|
||||
|
||||
int EVP_add_cipher_alias(const char *a, const char *b) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -141,10 +141,22 @@ typedef struct {
|
||||
|
||||
static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, int enc) {
|
||||
int ret, mode;
|
||||
int ret;
|
||||
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
|
||||
const int mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
|
||||
|
||||
if (mode == EVP_CIPH_CTR_MODE) {
|
||||
switch (ctx->key_len) {
|
||||
case 16:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_128_ctr);
|
||||
break;
|
||||
|
||||
case 32:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_256_ctr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
|
||||
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
|
||||
if (hwaes_capable()) {
|
||||
ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
|
||||
@@ -353,6 +365,17 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
if (!iv && !key) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (ctx->key_len) {
|
||||
case 16:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
|
||||
break;
|
||||
|
||||
case 32:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
|
||||
break;
|
||||
}
|
||||
|
||||
if (key) {
|
||||
OPENSSL_memset(&gctx->gcm, 0, sizeof(gctx->gcm));
|
||||
gctx->ctr = aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm.gcm_key, NULL, key,
|
||||
@@ -888,6 +911,16 @@ static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
|
||||
size_t key_len, size_t tag_len) {
|
||||
const size_t key_bits = key_len * 8;
|
||||
|
||||
switch (key_bits) {
|
||||
case 128:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
|
||||
break;
|
||||
|
||||
case 256:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
|
||||
break;
|
||||
}
|
||||
|
||||
if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
|
||||
return 0; // EVP_AEAD_CTX_init should catch this.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -342,10 +342,10 @@ void DES_set_key(const DES_cblock *key, DES_key_schedule *schedule) {
|
||||
|
||||
// table contained 0213 4657
|
||||
t2 = ((t << 16L) | (s & 0x0000ffffL)) & 0xffffffffL;
|
||||
schedule->subkeys[i][0] = ROTATE(t2, 30) & 0xffffffffL;
|
||||
schedule->subkeys[i][0] = CRYPTO_rotr_u32(t2, 30);
|
||||
|
||||
t2 = ((s >> 16L) | (t & 0xffff0000L));
|
||||
schedule->subkeys[i][1] = ROTATE(t2, 26) & 0xffffffffL;
|
||||
schedule->subkeys[i][1] = CRYPTO_rotr_u32(t2, 26);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,8 +392,8 @@ static void DES_encrypt1(uint32_t *data, const DES_key_schedule *ks, int enc) {
|
||||
// <71755.204@CompuServe.COM> for pointing this out.
|
||||
// clear the top bits on machines with 8byte longs
|
||||
// shift left by 2
|
||||
r = ROTATE(r, 29) & 0xffffffffL;
|
||||
l = ROTATE(l, 29) & 0xffffffffL;
|
||||
r = CRYPTO_rotr_u32(r, 29);
|
||||
l = CRYPTO_rotr_u32(l, 29);
|
||||
|
||||
// I don't know if it is worth the effort of loop unrolling the
|
||||
// inner loop
|
||||
@@ -434,8 +434,8 @@ static void DES_encrypt1(uint32_t *data, const DES_key_schedule *ks, int enc) {
|
||||
}
|
||||
|
||||
// rotate and clear the top bits on machines with 8byte longs
|
||||
l = ROTATE(l, 3) & 0xffffffffL;
|
||||
r = ROTATE(r, 3) & 0xffffffffL;
|
||||
l = CRYPTO_rotr_u32(l, 3);
|
||||
r = CRYPTO_rotr_u32(r, 3);
|
||||
|
||||
FP(r, l);
|
||||
data[0] = l;
|
||||
@@ -454,8 +454,8 @@ static void DES_encrypt2(uint32_t *data, const DES_key_schedule *ks, int enc) {
|
||||
// sparc2. Thanks to Richard Outerbridge <71755.204@CompuServe.COM> for
|
||||
// pointing this out.
|
||||
// clear the top bits on machines with 8byte longs
|
||||
r = ROTATE(r, 29) & 0xffffffffL;
|
||||
l = ROTATE(l, 29) & 0xffffffffL;
|
||||
r = CRYPTO_rotr_u32(r, 29);
|
||||
l = CRYPTO_rotr_u32(l, 29);
|
||||
|
||||
// I don't know if it is worth the effort of loop unrolling the
|
||||
// inner loop
|
||||
@@ -495,8 +495,8 @@ static void DES_encrypt2(uint32_t *data, const DES_key_schedule *ks, int enc) {
|
||||
D_ENCRYPT(ks, r, l, 0);
|
||||
}
|
||||
// rotate and clear the top bits on machines with 8byte longs
|
||||
data[0] = ROTATE(l, 3) & 0xffffffffL;
|
||||
data[1] = ROTATE(r, 3) & 0xffffffffL;
|
||||
data[0] = CRYPTO_rotr_u32(l, 3);
|
||||
data[1] = CRYPTO_rotr_u32(r, 3);
|
||||
}
|
||||
|
||||
void DES_encrypt3(uint32_t *data, const DES_key_schedule *ks1,
|
||||
@@ -782,4 +782,3 @@ void DES_set_key_unchecked(const DES_cblock *key, DES_key_schedule *schedule) {
|
||||
#undef D_ENCRYPT
|
||||
#undef ITERATIONS
|
||||
#undef HALF_ITERATIONS
|
||||
#undef ROTATE
|
||||
@@ -218,7 +218,7 @@ how to use xors :-) I got it to its final state.
|
||||
#define D_ENCRYPT(ks, LL, R, S) \
|
||||
do { \
|
||||
LOAD_DATA(ks, R, S, u, t, E0, E1); \
|
||||
t = ROTATE(t, 4); \
|
||||
t = CRYPTO_rotr_u32(t, 4); \
|
||||
(LL) ^= \
|
||||
DES_SPtrans[0][(u >> 2L) & 0x3f] ^ DES_SPtrans[2][(u >> 10L) & 0x3f] ^ \
|
||||
DES_SPtrans[4][(u >> 18L) & 0x3f] ^ \
|
||||
@@ -230,8 +230,6 @@ how to use xors :-) I got it to its final state.
|
||||
#define ITERATIONS 16
|
||||
#define HALF_ITERATIONS 8
|
||||
|
||||
#define ROTATE(a, n) (((a) >> (n)) + ((a) << (32 - (n))))
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../../internal.h"
|
||||
#include "../bn/internal.h"
|
||||
|
||||
|
||||
#define OPENSSL_DH_MAX_MODULUS_BITS 10000
|
||||
@@ -403,3 +404,53 @@ int DH_up_ref(DH *dh) {
|
||||
CRYPTO_refcount_inc(&dh->references);
|
||||
return 1;
|
||||
}
|
||||
|
||||
DH *DH_get_rfc7919_2048(void) {
|
||||
// This is the prime from https://tools.ietf.org/html/rfc7919#appendix-A.1,
|
||||
// which is specifically approved for FIPS in appendix D of SP 800-56Ar3.
|
||||
static const BN_ULONG kFFDHE2048Data[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0x886b4238, 0x61285c97),
|
||||
TOBN(0xc6f34a26, 0xc1b2effa), TOBN(0xc58ef183, 0x7d1683b2),
|
||||
TOBN(0x3bb5fcbc, 0x2ec22005), TOBN(0xc3fe3b1b, 0x4c6fad73),
|
||||
TOBN(0x8e4f1232, 0xeef28183), TOBN(0x9172fe9c, 0xe98583ff),
|
||||
TOBN(0xc03404cd, 0x28342f61), TOBN(0x9e02fce1, 0xcdf7e2ec),
|
||||
TOBN(0x0b07a7c8, 0xee0a6d70), TOBN(0xae56ede7, 0x6372bb19),
|
||||
TOBN(0x1d4f42a3, 0xde394df4), TOBN(0xb96adab7, 0x60d7f468),
|
||||
TOBN(0xd108a94b, 0xb2c8e3fb), TOBN(0xbc0ab182, 0xb324fb61),
|
||||
TOBN(0x30acca4f, 0x483a797a), TOBN(0x1df158a1, 0x36ade735),
|
||||
TOBN(0xe2a689da, 0xf3efe872), TOBN(0x984f0c70, 0xe0e68b77),
|
||||
TOBN(0xb557135e, 0x7f57c935), TOBN(0x85636555, 0x3ded1af3),
|
||||
TOBN(0x2433f51f, 0x5f066ed0), TOBN(0xd3df1ed5, 0xd5fd6561),
|
||||
TOBN(0xf681b202, 0xaec4617a), TOBN(0x7d2fe363, 0x630c75d8),
|
||||
TOBN(0xcc939dce, 0x249b3ef9), TOBN(0xa9e13641, 0x146433fb),
|
||||
TOBN(0xd8b9c583, 0xce2d3695), TOBN(0xafdc5620, 0x273d3cf1),
|
||||
TOBN(0xadf85458, 0xa2bb4a9a), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
|
||||
BIGNUM *const ffdhe2048_p = BN_new();
|
||||
BIGNUM *const ffdhe2048_q = BN_new();
|
||||
BIGNUM *const ffdhe2048_g = BN_new();
|
||||
DH *const dh = DH_new();
|
||||
|
||||
if (!ffdhe2048_p || !ffdhe2048_q || !ffdhe2048_g || !dh) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
bn_set_static_words(ffdhe2048_p, kFFDHE2048Data,
|
||||
OPENSSL_ARRAY_SIZE(kFFDHE2048Data));
|
||||
|
||||
if (!BN_rshift1(ffdhe2048_q, ffdhe2048_p) ||
|
||||
!BN_set_word(ffdhe2048_g, 2) ||
|
||||
!DH_set0_pqg(dh, ffdhe2048_p, ffdhe2048_q, ffdhe2048_g)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return dh;
|
||||
|
||||
err:
|
||||
BN_free(ffdhe2048_p);
|
||||
BN_free(ffdhe2048_q);
|
||||
BN_free(ffdhe2048_g);
|
||||
DH_free(dh);
|
||||
return NULL;
|
||||
}
|
||||
@@ -68,6 +68,8 @@
|
||||
|
||||
int EVP_MD_type(const EVP_MD *md) { return md->type; }
|
||||
|
||||
int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); }
|
||||
|
||||
uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
|
||||
|
||||
size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
|
||||
@@ -177,6 +179,13 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) {
|
||||
EVP_MD_CTX_cleanup(out);
|
||||
// While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|.
|
||||
OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX));
|
||||
EVP_MD_CTX_init(in);
|
||||
}
|
||||
|
||||
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
|
||||
EVP_MD_CTX_init(out);
|
||||
return EVP_MD_CTX_copy_ex(out, in);
|
||||
|
||||
@@ -247,13 +247,21 @@ static void sha512_256_init(EVP_MD_CTX *ctx) {
|
||||
CHECK(SHA512_256_Init(ctx->md_data));
|
||||
}
|
||||
|
||||
static void sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
|
||||
CHECK(SHA512_256_Update(ctx->md_data, data, count));
|
||||
}
|
||||
|
||||
static void sha512_256_final(EVP_MD_CTX *ctx, uint8_t *md) {
|
||||
CHECK(SHA512_256_Final(md, ctx->md_data));
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) {
|
||||
out->type = NID_sha512_256;
|
||||
out->md_size = SHA512_256_DIGEST_LENGTH;
|
||||
out->flags = 0;
|
||||
out->init = sha512_256_init;
|
||||
out->update = sha512_update;
|
||||
out->final = sha512_final;
|
||||
out->update = sha512_256_update;
|
||||
out->final = sha512_256_final;
|
||||
out->block_size = 128;
|
||||
out->ctx_size = sizeof(SHA512_CTX);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ==================================================================== */
|
||||
|
||||
#ifndef OPENSSL_HEADER_DIGEST_MD32_COMMON_H
|
||||
#define OPENSSL_HEADER_DIGEST_MD32_COMMON_H
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#include <assert.h>
|
||||
@@ -59,22 +62,15 @@ extern "C" {
|
||||
|
||||
// This is a generic 32-bit "collector" for message digest algorithms. It
|
||||
// collects input character stream into chunks of 32-bit values and invokes the
|
||||
// block function that performs the actual hash calculations. To make use of
|
||||
// this mechanism, the following macros must be defined before including
|
||||
// md32_common.h.
|
||||
// block function that performs the actual hash calculations.
|
||||
//
|
||||
// One of |DATA_ORDER_IS_BIG_ENDIAN| or |DATA_ORDER_IS_LITTLE_ENDIAN| must be
|
||||
// defined to specify the byte order of the input stream.
|
||||
//
|
||||
// |HASH_CBLOCK| must be defined as the integer block size, in bytes.
|
||||
//
|
||||
// |HASH_CTX| must be defined as the name of the context structure, which must
|
||||
// have at least the following members:
|
||||
// To make use of this mechanism, the hash context should be defined with the
|
||||
// following parameters.
|
||||
//
|
||||
// typedef struct <name>_state_st {
|
||||
// uint32_t h[<chaining length> / sizeof(uint32_t)];
|
||||
// uint32_t Nl, Nh;
|
||||
// uint8_t data[HASH_CBLOCK];
|
||||
// uint8_t data[<block size>];
|
||||
// unsigned num;
|
||||
// ...
|
||||
// } <NAME>_CTX;
|
||||
@@ -83,186 +79,117 @@ extern "C" {
|
||||
// any truncation (e.g. 64 for SHA-224 and SHA-256, 128 for SHA-384 and
|
||||
// SHA-512).
|
||||
//
|
||||
// |HASH_UPDATE| must be defined as the name of the "Update" function to
|
||||
// generate.
|
||||
//
|
||||
// |HASH_TRANSFORM| must be defined as the the name of the "Transform"
|
||||
// function to generate.
|
||||
//
|
||||
// |HASH_FINAL| must be defined as the name of "Final" function to generate.
|
||||
//
|
||||
// |HASH_BLOCK_DATA_ORDER| must be defined as the name of the "Block" function.
|
||||
// That function must be implemented manually. It must be capable of operating
|
||||
// on *unaligned* input data in its original (data) byte order. It must have
|
||||
// this signature:
|
||||
//
|
||||
// void HASH_BLOCK_DATA_ORDER(uint32_t *state, const uint8_t *data,
|
||||
// size_t num);
|
||||
//
|
||||
// It must update the hash state |state| with |num| blocks of data from |data|,
|
||||
// where each block is |HASH_CBLOCK| bytes; i.e. |data| points to a array of
|
||||
// |HASH_CBLOCK * num| bytes. |state| points to the |h| member of a |HASH_CTX|,
|
||||
// and so will have |<chaining length> / sizeof(uint32_t)| elements.
|
||||
//
|
||||
// |HASH_MAKE_STRING(c, s)| must be defined as a block statement that converts
|
||||
// the hash state |c->h| into the output byte order, storing the result in |s|.
|
||||
// |h| is the hash state and is updated by a function of type
|
||||
// |crypto_md32_block_func|. |data| is the partial unprocessed block and has
|
||||
// |num| bytes. |Nl| and |Nh| maintain the number of bits processed so far.
|
||||
|
||||
#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
|
||||
#error "DATA_ORDER must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_CBLOCK
|
||||
#error "HASH_CBLOCK must be defined!"
|
||||
#endif
|
||||
#ifndef HASH_CTX
|
||||
#error "HASH_CTX must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_UPDATE
|
||||
#error "HASH_UPDATE must be defined!"
|
||||
#endif
|
||||
#ifndef HASH_TRANSFORM
|
||||
#error "HASH_TRANSFORM must be defined!"
|
||||
#endif
|
||||
#ifndef HASH_FINAL
|
||||
#error "HASH_FINAL must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_BLOCK_DATA_ORDER
|
||||
#error "HASH_BLOCK_DATA_ORDER must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_MAKE_STRING
|
||||
#error "HASH_MAKE_STRING must be defined!"
|
||||
#endif
|
||||
|
||||
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
|
||||
|
||||
#define HOST_c2l(c, l) \
|
||||
do { \
|
||||
(l) = (((uint32_t)(*((c)++))) << 24); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 16); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 8); \
|
||||
(l) |= (((uint32_t)(*((c)++)))); \
|
||||
} while (0)
|
||||
|
||||
#define HOST_l2c(l, c) \
|
||||
do { \
|
||||
*((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l)) & 0xff); \
|
||||
} while (0)
|
||||
|
||||
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
|
||||
|
||||
#define HOST_c2l(c, l) \
|
||||
do { \
|
||||
(l) = (((uint32_t)(*((c)++)))); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 8); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 16); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 24); \
|
||||
} while (0)
|
||||
|
||||
#define HOST_l2c(l, c) \
|
||||
do { \
|
||||
*((c)++) = (uint8_t)(((l)) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
|
||||
} while (0)
|
||||
|
||||
#endif // DATA_ORDER
|
||||
|
||||
int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
|
||||
const uint8_t *data = data_;
|
||||
// A crypto_md32_block_func should incorporate |num_blocks| of input from |data|
|
||||
// into |state|. It is assumed the caller has sized |state| and |data| for the
|
||||
// hash function.
|
||||
typedef void (*crypto_md32_block_func)(uint32_t *state, const uint8_t *data,
|
||||
size_t num_blocks);
|
||||
|
||||
// crypto_md32_update adds |len| bytes from |in| to the digest. |data| must be a
|
||||
// buffer of length |block_size| with the first |*num| bytes containing a
|
||||
// partial block. This function combines the partial block with |in| and
|
||||
// incorporates any complete blocks into the digest state |h|. It then updates
|
||||
// |data| and |*num| with the new partial block and updates |*Nh| and |*Nl| with
|
||||
// the data consumed.
|
||||
static inline void crypto_md32_update(crypto_md32_block_func block_func,
|
||||
uint32_t *h, uint8_t *data,
|
||||
size_t block_size, unsigned *num,
|
||||
uint32_t *Nh, uint32_t *Nl,
|
||||
const uint8_t *in, size_t len) {
|
||||
if (len == 0) {
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t l = c->Nl + (((uint32_t)len) << 3);
|
||||
if (l < c->Nl) {
|
||||
uint32_t l = *Nl + (((uint32_t)len) << 3);
|
||||
if (l < *Nl) {
|
||||
// Handle carries.
|
||||
c->Nh++;
|
||||
(*Nh)++;
|
||||
}
|
||||
c->Nh += (uint32_t)(len >> 29);
|
||||
c->Nl = l;
|
||||
*Nh += (uint32_t)(len >> 29);
|
||||
*Nl = l;
|
||||
|
||||
size_t n = c->num;
|
||||
size_t n = *num;
|
||||
if (n != 0) {
|
||||
if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
|
||||
OPENSSL_memcpy(c->data + n, data, HASH_CBLOCK - n);
|
||||
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
|
||||
n = HASH_CBLOCK - n;
|
||||
data += n;
|
||||
if (len >= block_size || len + n >= block_size) {
|
||||
OPENSSL_memcpy(data + n, in, block_size - n);
|
||||
block_func(h, data, 1);
|
||||
n = block_size - n;
|
||||
in += n;
|
||||
len -= n;
|
||||
c->num = 0;
|
||||
// Keep |c->data| zeroed when unused.
|
||||
OPENSSL_memset(c->data, 0, HASH_CBLOCK);
|
||||
*num = 0;
|
||||
// Keep |data| zeroed when unused.
|
||||
OPENSSL_memset(data, 0, block_size);
|
||||
} else {
|
||||
OPENSSL_memcpy(c->data + n, data, len);
|
||||
c->num += (unsigned)len;
|
||||
return 1;
|
||||
OPENSSL_memcpy(data + n, in, len);
|
||||
*num += (unsigned)len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
n = len / HASH_CBLOCK;
|
||||
n = len / block_size;
|
||||
if (n > 0) {
|
||||
HASH_BLOCK_DATA_ORDER(c->h, data, n);
|
||||
n *= HASH_CBLOCK;
|
||||
data += n;
|
||||
block_func(h, in, n);
|
||||
n *= block_size;
|
||||
in += n;
|
||||
len -= n;
|
||||
}
|
||||
|
||||
if (len != 0) {
|
||||
c->num = (unsigned)len;
|
||||
OPENSSL_memcpy(c->data, data, len);
|
||||
*num = (unsigned)len;
|
||||
OPENSSL_memcpy(data, in, len);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void HASH_TRANSFORM(HASH_CTX *c, const uint8_t data[HASH_CBLOCK]) {
|
||||
HASH_BLOCK_DATA_ORDER(c->h, data, 1);
|
||||
}
|
||||
|
||||
|
||||
int HASH_FINAL(uint8_t out[HASH_DIGEST_LENGTH], HASH_CTX *c) {
|
||||
// |c->data| always has room for at least one byte. A full block would have
|
||||
// crypto_md32_final incorporates the partial block and trailing length into the
|
||||
// digest state |h|. The trailing length is encoded in little-endian if
|
||||
// |is_big_endian| is zero and big-endian otherwise. |data| must be a buffer of
|
||||
// length |block_size| with the first |*num| bytes containing a partial block.
|
||||
// |Nh| and |Nl| contain the total number of bits processed. On return, this
|
||||
// function clears the partial block in |data| and
|
||||
// |*num|.
|
||||
//
|
||||
// This function does not serialize |h| into a final digest. This is the
|
||||
// responsibility of the caller.
|
||||
static inline void crypto_md32_final(crypto_md32_block_func block_func,
|
||||
uint32_t *h, uint8_t *data,
|
||||
size_t block_size, unsigned *num,
|
||||
uint32_t Nh, uint32_t Nl,
|
||||
int is_big_endian) {
|
||||
// |data| always has room for at least one byte. A full block would have
|
||||
// been consumed.
|
||||
size_t n = c->num;
|
||||
assert(n < HASH_CBLOCK);
|
||||
c->data[n] = 0x80;
|
||||
size_t n = *num;
|
||||
assert(n < block_size);
|
||||
data[n] = 0x80;
|
||||
n++;
|
||||
|
||||
// Fill the block with zeros if there isn't room for a 64-bit length.
|
||||
if (n > (HASH_CBLOCK - 8)) {
|
||||
OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - n);
|
||||
if (n > block_size - 8) {
|
||||
OPENSSL_memset(data + n, 0, block_size - n);
|
||||
n = 0;
|
||||
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
|
||||
block_func(h, data, 1);
|
||||
}
|
||||
OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - 8 - n);
|
||||
OPENSSL_memset(data + n, 0, block_size - 8 - n);
|
||||
|
||||
// Append a 64-bit length to the block and process it.
|
||||
uint8_t *p = c->data + HASH_CBLOCK - 8;
|
||||
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
|
||||
HOST_l2c(c->Nh, p);
|
||||
HOST_l2c(c->Nl, p);
|
||||
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
|
||||
HOST_l2c(c->Nl, p);
|
||||
HOST_l2c(c->Nh, p);
|
||||
#endif
|
||||
assert(p == c->data + HASH_CBLOCK);
|
||||
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
|
||||
c->num = 0;
|
||||
OPENSSL_memset(c->data, 0, HASH_CBLOCK);
|
||||
|
||||
HASH_MAKE_STRING(c, out);
|
||||
return 1;
|
||||
if (is_big_endian) {
|
||||
CRYPTO_store_u32_be(data + block_size - 8, Nh);
|
||||
CRYPTO_store_u32_be(data + block_size - 4, Nl);
|
||||
} else {
|
||||
CRYPTO_store_u32_le(data + block_size - 8, Nl);
|
||||
CRYPTO_store_u32_le(data + block_size - 4, Nh);
|
||||
}
|
||||
block_func(h, data, 1);
|
||||
*num = 0;
|
||||
OPENSSL_memset(data, 0, block_size);
|
||||
}
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
#endif
|
||||
|
||||
#endif // OPENSSL_HEADER_DIGEST_MD32_COMMON_H
|
||||
@@ -1232,6 +1232,10 @@ void ec_set_to_safe_point(const EC_GROUP *group, EC_RAW_POINT *out) {
|
||||
|
||||
void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) {}
|
||||
|
||||
int EC_GROUP_get_asn1_flag(const EC_GROUP *group) {
|
||||
return OPENSSL_EC_NAMED_CURVE;
|
||||
}
|
||||
|
||||
const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) {
|
||||
// This function exists purely to give callers a way to call
|
||||
// |EC_METHOD_get_field_type|. cryptography.io crashes if |EC_GROUP_method_of|
|
||||
|
||||
@@ -171,7 +171,6 @@ void EC_KEY_free(EC_KEY *r) {
|
||||
EC_GROUP_free(r->group);
|
||||
EC_POINT_free(r->pub_key);
|
||||
ec_wrapped_scalar_free(r->priv_key);
|
||||
BN_free(r->fixed_k);
|
||||
|
||||
CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data);
|
||||
|
||||
|
||||
@@ -729,10 +729,6 @@ struct ec_key_st {
|
||||
EC_POINT *pub_key;
|
||||
EC_WRAPPED_SCALAR *priv_key;
|
||||
|
||||
// fixed_k may contain a specific value of 'k', to be used in ECDSA signing.
|
||||
// This is only for the FIPS power-on tests.
|
||||
BIGNUM *fixed_k;
|
||||
|
||||
unsigned int enc_flag;
|
||||
point_conversion_form_t conv_form;
|
||||
|
||||
|
||||
@@ -61,14 +61,14 @@
|
||||
#include <CCryptoBoringSSL_sha.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../../internal.h"
|
||||
#include "../bn/internal.h"
|
||||
#include "../ec/internal.h"
|
||||
#include "../../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
// digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for
|
||||
// ECDSA. Note this value is not fully reduced modulo the order, only the
|
||||
// correct number of bits.
|
||||
// ECDSA.
|
||||
static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
|
||||
const uint8_t *digest, size_t digest_len) {
|
||||
const BIGNUM *order = &group->order;
|
||||
@@ -198,68 +198,104 @@ int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ecdsa_sign_setup(const EC_KEY *eckey, EC_SCALAR *out_kinv_mont,
|
||||
EC_SCALAR *out_r, const uint8_t *digest,
|
||||
size_t digest_len, const EC_SCALAR *priv_key) {
|
||||
static ECDSA_SIG *ecdsa_sign_impl(const EC_GROUP *group, int *out_retry,
|
||||
const EC_SCALAR *priv_key, const EC_SCALAR *k,
|
||||
const uint8_t *digest, size_t digest_len) {
|
||||
*out_retry = 0;
|
||||
|
||||
// Check that the size of the group order is FIPS compliant (FIPS 186-4
|
||||
// B.5.2).
|
||||
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
if (BN_num_bits(order) < 160) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER);
|
||||
return 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
EC_SCALAR k;
|
||||
// Compute r, the x-coordinate of k * generator.
|
||||
EC_RAW_POINT tmp_point;
|
||||
do {
|
||||
// Include the private key and message digest in the k generation.
|
||||
if (eckey->fixed_k != NULL) {
|
||||
if (!ec_bignum_to_scalar(group, &k, eckey->fixed_k)) {
|
||||
goto err;
|
||||
}
|
||||
if (ec_scalar_is_zero(group, &k)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
// Pass a SHA512 hash of the private key and digest as additional data
|
||||
// into the RBG. This is a hardening measure against entropy failure.
|
||||
OPENSSL_STATIC_ASSERT(SHA512_DIGEST_LENGTH >= 32,
|
||||
"additional_data is too large for SHA-512");
|
||||
SHA512_CTX sha;
|
||||
uint8_t additional_data[SHA512_DIGEST_LENGTH];
|
||||
SHA512_Init(&sha);
|
||||
SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG));
|
||||
SHA512_Update(&sha, digest, digest_len);
|
||||
SHA512_Final(additional_data, &sha);
|
||||
if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
EC_SCALAR r;
|
||||
if (!ec_point_mul_scalar_base(group, &tmp_point, k) ||
|
||||
!ec_get_x_coordinate_as_scalar(group, &r, &tmp_point)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Compute k^-1 in the Montgomery domain. This is |ec_scalar_to_montgomery|
|
||||
// followed by |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery|
|
||||
// followed by |ec_scalar_from_montgomery| is equivalent and slightly more
|
||||
// efficient. Note k is non-zero, so the inverse must exist.
|
||||
ec_scalar_inv0_montgomery(group, out_kinv_mont, &k);
|
||||
ec_scalar_from_montgomery(group, out_kinv_mont, out_kinv_mont);
|
||||
if (ec_scalar_is_zero(group, &r)) {
|
||||
*out_retry = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Compute r, the x-coordinate of generator * k.
|
||||
if (!ec_point_mul_scalar_base(group, &tmp_point, &k) ||
|
||||
!ec_get_x_coordinate_as_scalar(group, out_r, &tmp_point)) {
|
||||
goto err;
|
||||
}
|
||||
} while (ec_scalar_is_zero(group, out_r));
|
||||
// s = priv_key * r. Note if only one parameter is in the Montgomery domain,
|
||||
// |ec_scalar_mod_mul_montgomery| will compute the answer in the normal
|
||||
// domain.
|
||||
EC_SCALAR s;
|
||||
ec_scalar_to_montgomery(group, &s, &r);
|
||||
ec_scalar_mul_montgomery(group, &s, priv_key, &s);
|
||||
|
||||
ret = 1;
|
||||
// s = m + priv_key * r.
|
||||
EC_SCALAR tmp;
|
||||
digest_to_scalar(group, &tmp, digest, digest_len);
|
||||
ec_scalar_add(group, &s, &s, &tmp);
|
||||
|
||||
err:
|
||||
OPENSSL_cleanse(&k, sizeof(k));
|
||||
// s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery
|
||||
// domain. This is |ec_scalar_to_montgomery| followed by
|
||||
// |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by
|
||||
// |ec_scalar_from_montgomery| is equivalent and slightly more efficient.
|
||||
// Then, as above, only one parameter is in the Montgomery domain, so the
|
||||
// result is in the normal domain. Finally, note k is non-zero (or computing r
|
||||
// would fail), so the inverse must exist.
|
||||
ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2
|
||||
ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R
|
||||
ec_scalar_mul_montgomery(group, &s, &s, &tmp);
|
||||
if (ec_scalar_is_zero(group, &s)) {
|
||||
*out_retry = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ret = ECDSA_SIG_new();
|
||||
if (ret == NULL || //
|
||||
!bn_set_words(ret->r, r.words, order->width) ||
|
||||
!bn_set_words(ret->s, s.words, order->width)) {
|
||||
ECDSA_SIG_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest,
|
||||
size_t digest_len,
|
||||
const EC_KEY *eckey,
|
||||
const uint8_t *nonce,
|
||||
size_t nonce_len) {
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
||||
if (group == NULL || eckey->priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return NULL;
|
||||
}
|
||||
const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
|
||||
|
||||
EC_SCALAR k;
|
||||
if (!ec_scalar_from_bytes(group, &k, nonce, nonce_len)) {
|
||||
return NULL;
|
||||
}
|
||||
int retry_ignored;
|
||||
return ecdsa_sign_impl(group, &retry_ignored, priv_key, &k, digest,
|
||||
digest_len);
|
||||
}
|
||||
|
||||
// This function is only exported for testing and is not called in production
|
||||
// code.
|
||||
ECDSA_SIG *ECDSA_sign_with_nonce_and_leak_private_key_for_testing(
|
||||
const uint8_t *digest, size_t digest_len, const EC_KEY *eckey,
|
||||
const uint8_t *nonce, size_t nonce_len) {
|
||||
return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey,
|
||||
nonce, nonce_len);
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
|
||||
const EC_KEY *eckey) {
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
@@ -275,54 +311,28 @@ ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
|
||||
|
||||
int ok = 0;
|
||||
ECDSA_SIG *ret = ECDSA_SIG_new();
|
||||
EC_SCALAR kinv_mont, r_mont, s, m, tmp;
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
// Pass a SHA512 hash of the private key and digest as additional data
|
||||
// into the RBG. This is a hardening measure against entropy failure.
|
||||
OPENSSL_STATIC_ASSERT(SHA512_DIGEST_LENGTH >= 32,
|
||||
"additional_data is too large for SHA-512");
|
||||
SHA512_CTX sha;
|
||||
uint8_t additional_data[SHA512_DIGEST_LENGTH];
|
||||
SHA512_Init(&sha);
|
||||
SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG));
|
||||
SHA512_Update(&sha, digest, digest_len);
|
||||
SHA512_Final(additional_data, &sha);
|
||||
|
||||
digest_to_scalar(group, &m, digest, digest_len);
|
||||
for (;;) {
|
||||
if (!ecdsa_sign_setup(eckey, &kinv_mont, &r_mont, digest, digest_len,
|
||||
priv_key) ||
|
||||
!bn_set_words(ret->r, r_mont.words, order->width)) {
|
||||
goto err;
|
||||
EC_SCALAR k;
|
||||
if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Compute priv_key * r (mod order). Note if only one parameter is in the
|
||||
// Montgomery domain, |ec_scalar_mod_mul_montgomery| will compute the answer
|
||||
// in the normal domain.
|
||||
ec_scalar_to_montgomery(group, &r_mont, &r_mont);
|
||||
ec_scalar_mul_montgomery(group, &s, priv_key, &r_mont);
|
||||
|
||||
// Compute tmp = m + priv_key * r.
|
||||
ec_scalar_add(group, &tmp, &m, &s);
|
||||
|
||||
// Finally, multiply s by k^-1. That was retained in Montgomery form, so the
|
||||
// same technique as the previous multiplication works.
|
||||
ec_scalar_mul_montgomery(group, &s, &tmp, &kinv_mont);
|
||||
if (!bn_set_words(ret->s, s.words, order->width)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_is_zero(ret->s)) {
|
||||
// s != 0 => we have a valid signature
|
||||
break;
|
||||
int retry;
|
||||
ECDSA_SIG *sig =
|
||||
ecdsa_sign_impl(group, &retry, priv_key, &k, digest, digest_len);
|
||||
if (sig != NULL || !retry) {
|
||||
return sig;
|
||||
}
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
if (!ok) {
|
||||
ECDSA_SIG_free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
OPENSSL_cleanse(&kinv_mont, sizeof(kinv_mont));
|
||||
OPENSSL_cleanse(&r_mont, sizeof(r_mont));
|
||||
OPENSSL_cleanse(&s, sizeof(s));
|
||||
OPENSSL_cleanse(&tmp, sizeof(tmp));
|
||||
OPENSSL_cleanse(&m, sizeof(m));
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/* Copyright (c) 2021, Google Inc.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_ECDSA_INTERNAL_H
|
||||
#define OPENSSL_HEADER_CRYPTO_FIPSMODULE_ECDSA_INTERNAL_H
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// ecdsa_sign_with_nonce_for_known_answer_test behaves like |ECDSA_do_sign| but
|
||||
// takes a fixed nonce. This function is used as part of known-answer tests in
|
||||
// the FIPS module.
|
||||
ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest,
|
||||
size_t digest_len,
|
||||
const EC_KEY *eckey,
|
||||
const uint8_t *nonce,
|
||||
size_t nonce_len);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_ECDSA_INTERNAL_H
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__linux__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__x86_64__) && defined(__APPLE__)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
Loaded 100 of 366 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user