mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9825fa541 | ||
|
|
d180057628 | ||
|
|
17d95a6782 | ||
|
|
12a1784190 | ||
|
|
ce8d18032f | ||
|
|
067254c794 | ||
|
|
476397d837 | ||
|
|
facc82fbb4 | ||
|
|
3fb2ea5d41 | ||
|
|
a8911e0fad | ||
|
|
172d71fdc3 | ||
|
|
9c53b7a758 | ||
|
|
bd5abc2a12 | ||
|
|
9b5ef28601 | ||
|
|
f47c1950ae | ||
|
|
ab6b6fc81b | ||
|
|
127d3745c3 | ||
|
|
d89b2ef9ed | ||
|
|
684952cafa | ||
|
|
8273610176 | ||
|
|
53da7b3706 | ||
|
|
22fd389195 | ||
|
|
01c3fdfb85 | ||
|
|
eefb07cc59 | ||
|
|
158e2b140c | ||
|
|
8b7b68753f | ||
|
|
6c245a7f7f |
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
# file options
|
||||
|
||||
--swiftversion 5.0
|
||||
--swiftversion 5.4
|
||||
--exclude .build
|
||||
|
||||
# format options
|
||||
|
||||
@@ -37,6 +37,11 @@ if(BUILD_SHARED_LIBS)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE YES)
|
||||
endif()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND NOT CMAKE_CROSSCOMPILING)
|
||||
set(CMAKE_AR "/usr/bin/ar")
|
||||
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> crs <TARGET> <LINK_FLAGS> <OBJECTS>")
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
find_package(dispatch CONFIG)
|
||||
find_package(Foundation CONFIG)
|
||||
|
||||
+67
-19
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:5.1
|
||||
// swift-tools-version:5.4
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
@@ -20,15 +20,38 @@
|
||||
// Sources/CCryptoBoringSSL directory. The source repository is at
|
||||
// https://boringssl.googlesource.com/boringssl.
|
||||
//
|
||||
// BoringSSL Commit: 04b3213d43492b6c9e0434d8e2a4530a9938f958
|
||||
// BoringSSL Commit: f961de5c47ed265c3e758ec70dd15ece20809962
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let swiftSettings: [SwiftSetting] = [
|
||||
.define("CRYPTO_IN_SWIFTPM"),
|
||||
// To develop this on Apple platforms, uncomment this define.
|
||||
// .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
|
||||
]
|
||||
// To develop this on Apple platforms, set this to true
|
||||
let development = false
|
||||
|
||||
let swiftSettings: [SwiftSetting]
|
||||
let dependencies: [Target.Dependency]
|
||||
if development {
|
||||
swiftSettings = [
|
||||
.define("CRYPTO_IN_SWIFTPM"),
|
||||
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
|
||||
]
|
||||
dependencies = [
|
||||
"CCryptoBoringSSL",
|
||||
"CCryptoBoringSSLShims"
|
||||
]
|
||||
} else {
|
||||
swiftSettings = [
|
||||
.define("CRYPTO_IN_SWIFTPM"),
|
||||
]
|
||||
let platforms: [Platform] = [
|
||||
Platform.linux,
|
||||
Platform.android,
|
||||
Platform.windows,
|
||||
]
|
||||
dependencies = [
|
||||
.target(name: "CCryptoBoringSSL", condition: .when(platforms: platforms)),
|
||||
.target(name: "CCryptoBoringSSLShims", condition: .when(platforms: platforms))
|
||||
]
|
||||
}
|
||||
|
||||
let package = Package(
|
||||
name: "swift-crypto",
|
||||
@@ -40,6 +63,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 */
|
||||
@@ -47,20 +71,44 @@ let package = Package(
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.target(
|
||||
name: "CCryptoBoringSSL",
|
||||
cSettings: [
|
||||
/*
|
||||
* This define is required on Windows, but because we need older
|
||||
* versions of SPM, we cannot conditionally define this on Windows
|
||||
* only. Unconditionally define it instead.
|
||||
*/
|
||||
.define("WIN32_LEAN_AND_MEAN"),
|
||||
]
|
||||
name: "CCryptoBoringSSL",
|
||||
exclude: [
|
||||
"hash.txt",
|
||||
"include/boringssl_prefix_symbols_nasm.inc",
|
||||
"CMakeLists.txt",
|
||||
],
|
||||
cSettings: [
|
||||
/*
|
||||
* This define is required on Windows, but because we need older
|
||||
* versions of SPM, we cannot conditionally define this on Windows
|
||||
* only. Unconditionally define it instead.
|
||||
*/
|
||||
.define("WIN32_LEAN_AND_MEAN"),
|
||||
]
|
||||
),
|
||||
.target(name: "CCryptoBoringSSLShims", dependencies: ["CCryptoBoringSSL"]),
|
||||
.target(name: "Crypto", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims"], swiftSettings: swiftSettings),
|
||||
.target(name: "crypto-shasum", dependencies: ["Crypto"]),
|
||||
.target(
|
||||
name: "CCryptoBoringSSLShims",
|
||||
dependencies: ["CCryptoBoringSSL"],
|
||||
exclude: [
|
||||
"CMakeLists.txt"
|
||||
]
|
||||
),
|
||||
.target(
|
||||
name: "Crypto",
|
||||
dependencies: dependencies,
|
||||
exclude: [
|
||||
"CMakeLists.txt",
|
||||
"AEADs/Nonces.swift.gyb",
|
||||
"Digests/Digests.swift.gyb",
|
||||
"Key Agreement/ECDH.swift.gyb",
|
||||
"Signatures/ECDSA.swift.gyb",
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
),
|
||||
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "Crypto"]),
|
||||
.executableTarget(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.4 and later. Swift Crypto supported Swift 5.2 and 5.3 until version 2.1 and Swift 5.1 until version 2.0.
|
||||
|
||||
### 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.
|
||||
|
||||
+20
-16
@@ -17,7 +17,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/asn1/a_bool.c"
|
||||
"crypto/asn1/a_d2i_fp.c"
|
||||
"crypto/asn1/a_dup.c"
|
||||
"crypto/asn1/a_enum.c"
|
||||
"crypto/asn1/a_gentm.c"
|
||||
"crypto/asn1/a_i2d_fp.c"
|
||||
"crypto/asn1/a_int.c"
|
||||
@@ -25,6 +24,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 +33,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"
|
||||
@@ -70,6 +69,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/cipher_extra/e_aesctrhmac.c"
|
||||
"crypto/cipher_extra/e_aesgcmsiv.c"
|
||||
"crypto/cipher_extra/e_chacha20poly1305.c"
|
||||
"crypto/cipher_extra/e_des.c"
|
||||
"crypto/cipher_extra/e_null.c"
|
||||
"crypto/cipher_extra/e_rc2.c"
|
||||
"crypto/cipher_extra/e_rc4.c"
|
||||
@@ -77,16 +77,18 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/cipher_extra/tls_cbc.c"
|
||||
"crypto/cmac/cmac.c"
|
||||
"crypto/conf/conf.c"
|
||||
"crypto/cpu-aarch64-fuchsia.c"
|
||||
"crypto/cpu-aarch64-linux.c"
|
||||
"crypto/cpu-aarch64-win.c"
|
||||
"crypto/cpu-arm-linux.c"
|
||||
"crypto/cpu-arm.c"
|
||||
"crypto/cpu-intel.c"
|
||||
"crypto/cpu-ppc64le.c"
|
||||
"crypto/cpu_aarch64_apple.c"
|
||||
"crypto/cpu_aarch64_fuchsia.c"
|
||||
"crypto/cpu_aarch64_linux.c"
|
||||
"crypto/cpu_aarch64_win.c"
|
||||
"crypto/cpu_arm.c"
|
||||
"crypto/cpu_arm_linux.c"
|
||||
"crypto/cpu_intel.c"
|
||||
"crypto/cpu_ppc64le.c"
|
||||
"crypto/crypto.c"
|
||||
"crypto/curve25519/curve25519.c"
|
||||
"crypto/curve25519/spake25519.c"
|
||||
"crypto/des/des.c"
|
||||
"crypto/dh_extra/dh_asn1.c"
|
||||
"crypto/dh_extra/params.c"
|
||||
"crypto/digest_extra/digest_extra.c"
|
||||
@@ -146,8 +148,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/cipher/aead.c"
|
||||
"crypto/fipsmodule/cipher/cipher.c"
|
||||
"crypto/fipsmodule/cipher/e_aes.c"
|
||||
"crypto/fipsmodule/cipher/e_des.c"
|
||||
"crypto/fipsmodule/des/des.c"
|
||||
"crypto/fipsmodule/dh/check.c"
|
||||
"crypto/fipsmodule/dh/dh.c"
|
||||
"crypto/fipsmodule/digest/digest.c"
|
||||
@@ -158,7 +158,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/ec/felem.c"
|
||||
"crypto/fipsmodule/ec/oct.c"
|
||||
"crypto/fipsmodule/ec/p224-64.c"
|
||||
"crypto/fipsmodule/ec/p256-x86_64.c"
|
||||
"crypto/fipsmodule/ec/p256-nistz.c"
|
||||
"crypto/fipsmodule/ec/p256.c"
|
||||
"crypto/fipsmodule/ec/scalar.c"
|
||||
"crypto/fipsmodule/ec/simple.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"
|
||||
@@ -337,7 +337,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x
|
||||
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 AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
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
|
||||
@@ -364,17 +364,21 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm
|
||||
crypto/fipsmodule/armv8-mont.ios.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm.ios.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm.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 STREQUAL Linux AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
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/p256-armv8-asm.linux.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm.linux.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.linux.aarch64.S
|
||||
|
||||
@@ -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
|
||||
@@ -1,195 +0,0 @@
|
||||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This package is an SSL implementation written
|
||||
* by Eric Young (eay@cryptsoft.com).
|
||||
* The implementation was written so as to conform with Netscapes SSL.
|
||||
*
|
||||
* This library is free for commercial and non-commercial use as long as
|
||||
* the following conditions are aheared to. The following conditions
|
||||
* apply to all code found in this distribution, be it the RC4, RSA,
|
||||
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
||||
* included with this distribution is covered by the same copyright terms
|
||||
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
||||
*
|
||||
* Copyright remains Eric Young's, and as such any Copyright notices in
|
||||
* the code are not to be removed.
|
||||
* If this package is used in a product, Eric Young should be given attribution
|
||||
* as the author of the parts of the library used.
|
||||
* This can be in the form of a textual message at program startup or
|
||||
* in documentation (online or textual) provided with the package.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* "This product includes cryptographic software written by
|
||||
* Eric Young (eay@cryptsoft.com)"
|
||||
* The word 'cryptographic' can be left out if the rouines from the library
|
||||
* being used are not cryptographic related :-).
|
||||
* 4. If you include any Windows specific code (or a derivative thereof) from
|
||||
* the apps directory (application code) you must include an acknowledgement:
|
||||
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* The licence and distribution terms for any publically available version or
|
||||
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
||||
* copied and put under another distribution licence
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
/*
|
||||
* Code for ENUMERATED type: identical to INTEGER apart from a different tag.
|
||||
* for comments on encoding see a_int.c
|
||||
*/
|
||||
|
||||
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
|
||||
{
|
||||
int j, k;
|
||||
unsigned int i;
|
||||
unsigned char buf[sizeof(long) + 1];
|
||||
long d;
|
||||
|
||||
a->type = V_ASN1_ENUMERATED;
|
||||
if (a->length < (int)(sizeof(long) + 1)) {
|
||||
if (a->data != NULL)
|
||||
OPENSSL_free(a->data);
|
||||
if ((a->data =
|
||||
(unsigned char *)OPENSSL_malloc(sizeof(long) + 1)) != NULL)
|
||||
OPENSSL_memset((char *)a->data, 0, sizeof(long) + 1);
|
||||
}
|
||||
if (a->data == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return (0);
|
||||
}
|
||||
d = v;
|
||||
if (d < 0) {
|
||||
d = -d;
|
||||
a->type = V_ASN1_NEG_ENUMERATED;
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(long); i++) {
|
||||
if (d == 0)
|
||||
break;
|
||||
buf[i] = (int)d & 0xff;
|
||||
d >>= 8;
|
||||
}
|
||||
j = 0;
|
||||
for (k = i - 1; k >= 0; k--)
|
||||
a->data[j++] = buf[k];
|
||||
a->length = j;
|
||||
return (1);
|
||||
}
|
||||
|
||||
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
|
||||
{
|
||||
int neg = 0, i;
|
||||
|
||||
if (a == NULL)
|
||||
return (0L);
|
||||
i = a->type;
|
||||
if (i == V_ASN1_NEG_ENUMERATED)
|
||||
neg = 1;
|
||||
else if (i != V_ASN1_ENUMERATED)
|
||||
return -1;
|
||||
|
||||
OPENSSL_STATIC_ASSERT(sizeof(uint64_t) >= sizeof(long),
|
||||
"long larger than uint64_t");
|
||||
|
||||
if (a->length > (int)sizeof(uint64_t)) {
|
||||
/* hmm... a bit ugly */
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t r64 = 0;
|
||||
if (a->data != NULL) {
|
||||
for (i = 0; i < a->length; i++) {
|
||||
r64 <<= 8;
|
||||
r64 |= (unsigned char)a->data[i];
|
||||
}
|
||||
|
||||
if (r64 > LONG_MAX) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
long r = (long) r64;
|
||||
if (neg)
|
||||
r = -r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
|
||||
{
|
||||
ASN1_ENUMERATED *ret;
|
||||
int len, j;
|
||||
|
||||
if (ai == NULL)
|
||||
ret = ASN1_ENUMERATED_new();
|
||||
else
|
||||
ret = ai;
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (BN_is_negative(bn))
|
||||
ret->type = V_ASN1_NEG_ENUMERATED;
|
||||
else
|
||||
ret->type = V_ASN1_ENUMERATED;
|
||||
j = BN_num_bits(bn);
|
||||
len = ((j == 0) ? 0 : ((j / 8) + 1));
|
||||
if (ret->length < len + 4) {
|
||||
unsigned char *new_data = OPENSSL_realloc(ret->data, len + 4);
|
||||
if (!new_data) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
ret->data = new_data;
|
||||
}
|
||||
|
||||
ret->length = BN_bn2bin(bn, ret->data);
|
||||
return (ret);
|
||||
err:
|
||||
if (ret != ai)
|
||||
ASN1_ENUMERATED_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
|
||||
{
|
||||
BIGNUM *ret;
|
||||
|
||||
if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
|
||||
else if (ai->type == V_ASN1_NEG_ENUMERATED)
|
||||
BN_set_negative(ret, 1);
|
||||
return (ret);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -59,8 +59,10 @@
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
@@ -72,129 +74,110 @@ ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
|
||||
|
||||
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
|
||||
{
|
||||
int neg, ret;
|
||||
/* Compare signs */
|
||||
neg = x->type & V_ASN1_NEG;
|
||||
/* Compare signs. */
|
||||
int neg = x->type & V_ASN1_NEG;
|
||||
if (neg != (y->type & V_ASN1_NEG)) {
|
||||
if (neg)
|
||||
return -1;
|
||||
else
|
||||
return neg ? -1 : 1;
|
||||
}
|
||||
|
||||
int ret = ASN1_STRING_cmp(x, y);
|
||||
if (neg) {
|
||||
/* This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from
|
||||
* returning |INT_MIN|. */
|
||||
if (ret < 0) {
|
||||
return 1;
|
||||
} else if (ret > 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
ret = ASN1_STRING_cmp(x, y);
|
||||
|
||||
if (neg)
|
||||
return -ret;
|
||||
else
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* This converts an ASN1 INTEGER into its content encoding.
|
||||
* The internal representation is an ASN1_STRING whose data is a big endian
|
||||
* representation of the value, ignoring the sign. The sign is determined by
|
||||
* the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
|
||||
*
|
||||
* Positive integers are no problem: they are almost the same as the DER
|
||||
* encoding, except if the first byte is >= 0x80 we need to add a zero pad.
|
||||
*
|
||||
* Negative integers are a bit trickier...
|
||||
* The DER representation of negative integers is in 2s complement form.
|
||||
* The internal form is converted by complementing each octet and finally
|
||||
* adding one to the result. This can be done less messily with a little trick.
|
||||
* If the internal form has trailing zeroes then they will become FF by the
|
||||
* complement and 0 by the add one (due to carry) so just copy as many trailing
|
||||
* zeros to the destination as there are in the source. The carry will add one
|
||||
* to the last none zero octet: so complement this octet and add one and finally
|
||||
* complement any left over until you get to the start of the string.
|
||||
*
|
||||
* Padding is a little trickier too. If the first bytes is > 0x80 then we pad
|
||||
* with 0xff. However if the first byte is 0x80 and one of the following bytes
|
||||
* is non-zero we pad with 0xff. The reason for this distinction is that 0x80
|
||||
* followed by optional zeros isn't padded.
|
||||
*/
|
||||
|
||||
int i2c_ASN1_INTEGER(const ASN1_INTEGER *a, unsigned char **pp)
|
||||
/* negate_twos_complement negates |len| bytes from |buf| in-place, interpreted
|
||||
* as a signed, big-endian two's complement value. */
|
||||
static void negate_twos_complement(uint8_t *buf, size_t len)
|
||||
{
|
||||
int pad = 0, ret, i, neg;
|
||||
unsigned char *p, *n, pb = 0;
|
||||
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
neg = a->type & V_ASN1_NEG;
|
||||
if (a->length == 0)
|
||||
ret = 1;
|
||||
else {
|
||||
ret = a->length;
|
||||
i = a->data[0];
|
||||
if (ret == 1 && i == 0)
|
||||
neg = 0;
|
||||
if (!neg && (i > 127)) {
|
||||
pad = 1;
|
||||
pb = 0;
|
||||
} else if (neg) {
|
||||
if (i > 128) {
|
||||
pad = 1;
|
||||
pb = 0xFF;
|
||||
} else if (i == 128) {
|
||||
/*
|
||||
* Special case: if any other bytes non zero we pad:
|
||||
* otherwise we don't.
|
||||
*/
|
||||
for (i = 1; i < a->length; i++)
|
||||
if (a->data[i]) {
|
||||
pad = 1;
|
||||
pb = 0xFF;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret += pad;
|
||||
uint8_t borrow = 0;
|
||||
for (size_t i = len - 1; i < len; i--) {
|
||||
uint8_t t = buf[i];
|
||||
buf[i] = 0u - borrow - t;
|
||||
borrow |= t != 0;
|
||||
}
|
||||
if (pp == NULL)
|
||||
return (ret);
|
||||
p = *pp;
|
||||
|
||||
if (pad)
|
||||
*(p++) = pb;
|
||||
if (a->length == 0)
|
||||
*(p++) = 0;
|
||||
else if (!neg)
|
||||
OPENSSL_memcpy(p, a->data, (unsigned int)a->length);
|
||||
else {
|
||||
/* Begin at the end of the encoding */
|
||||
n = a->data + a->length - 1;
|
||||
p += a->length - 1;
|
||||
i = a->length;
|
||||
/* Copy zeros to destination as long as source is zero */
|
||||
while (!*n && i > 1) {
|
||||
*(p--) = 0;
|
||||
n--;
|
||||
i--;
|
||||
}
|
||||
/* Complement and increment next octet */
|
||||
*(p--) = ((*(n--)) ^ 0xff) + 1;
|
||||
i--;
|
||||
/* Complement any octets left */
|
||||
for (; i > 0; i--)
|
||||
*(p--) = *(n--) ^ 0xff;
|
||||
}
|
||||
|
||||
*pp += ret;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
|
||||
static int is_all_zeros(const uint8_t *in, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (in[i] != 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
|
||||
int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp)
|
||||
{
|
||||
if (in == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* |ASN1_INTEGER|s should be represented minimally, but it is possible to
|
||||
* construct invalid ones. Skip leading zeros so this does not produce an
|
||||
* invalid encoding or break invariants. */
|
||||
int start = 0;
|
||||
while (start < in->length && in->data[start] == 0) {
|
||||
start++;
|
||||
}
|
||||
|
||||
int is_negative = (in->type & V_ASN1_NEG) != 0;
|
||||
int pad;
|
||||
if (start >= in->length) {
|
||||
/* Zero is represented as a single byte. */
|
||||
is_negative = 0;
|
||||
pad = 1;
|
||||
} else if (is_negative) {
|
||||
/* 0x80...01 through 0xff...ff have a two's complement of 0x7f...ff
|
||||
* through 0x00...01 and need an extra byte to be negative.
|
||||
* 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff
|
||||
* through 0x80...00 and can be negated as-is. */
|
||||
pad = in->data[start] > 0x80 ||
|
||||
(in->data[start] == 0x80 &&
|
||||
!is_all_zeros(in->data + start + 1, in->length - start - 1));
|
||||
} else {
|
||||
/* If the high bit is set, the signed representation needs an extra
|
||||
* byte to be positive. */
|
||||
pad = (in->data[start] & 0x80) != 0;
|
||||
}
|
||||
|
||||
if (in->length - start > INT_MAX - pad) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
int len = pad + in->length - start;
|
||||
assert(len > 0);
|
||||
if (outp == NULL) {
|
||||
return len;
|
||||
}
|
||||
|
||||
if (pad) {
|
||||
(*outp)[0] = 0;
|
||||
}
|
||||
OPENSSL_memcpy(*outp + pad, in->data + start, in->length - start);
|
||||
if (is_negative) {
|
||||
negate_twos_complement(*outp, len);
|
||||
assert((*outp)[0] >= 0x80);
|
||||
} else {
|
||||
assert((*outp)[0] < 0x80);
|
||||
}
|
||||
*outp += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **out, const unsigned char **inp,
|
||||
long len)
|
||||
{
|
||||
ASN1_INTEGER *ret = NULL;
|
||||
const unsigned char *p, *pend;
|
||||
unsigned char *to, *s;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* This function can handle lengths up to INT_MAX - 1, but the rest of the
|
||||
* legacy ASN.1 code mixes integer types, so avoid exposing it to
|
||||
@@ -205,85 +188,69 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL)) {
|
||||
if ((ret = ASN1_INTEGER_new()) == NULL)
|
||||
return (NULL);
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
} else
|
||||
ret = (*a);
|
||||
|
||||
p = *pp;
|
||||
pend = p + len;
|
||||
|
||||
/*
|
||||
* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
|
||||
* a missing NULL parameter.
|
||||
*/
|
||||
s = (unsigned char *)OPENSSL_malloc((int)len + 1);
|
||||
if (s == NULL) {
|
||||
i = ERR_R_MALLOC_FAILURE;
|
||||
goto err;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
int is_negative;
|
||||
if (!CBS_is_valid_asn1_integer(&cbs, &is_negative)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return NULL;
|
||||
}
|
||||
to = s;
|
||||
if (!len) {
|
||||
/*
|
||||
* Strictly speaking this is an illegal INTEGER but we tolerate it.
|
||||
*/
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
} else if (*p & 0x80) { /* a negative number */
|
||||
ret->type = V_ASN1_NEG_INTEGER;
|
||||
if ((*p == 0xff) && (len != 1)) {
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
i = len;
|
||||
p += i - 1;
|
||||
to += i - 1;
|
||||
while ((!*p) && i) {
|
||||
*(to--) = 0;
|
||||
i--;
|
||||
p--;
|
||||
}
|
||||
/*
|
||||
* Special case: if all zeros then the number will be of the form FF
|
||||
* followed by n zero bytes: this corresponds to 1 followed by n zero
|
||||
* bytes. We've already written n zeros so we just append an extra
|
||||
* one and set the first byte to a 1. This is treated separately
|
||||
* because it is the only case where the number of bytes is larger
|
||||
* than len.
|
||||
*/
|
||||
if (!i) {
|
||||
*s = 1;
|
||||
s[len] = 0;
|
||||
len++;
|
||||
} else {
|
||||
*(to--) = (*(p--) ^ 0xff) + 1;
|
||||
i--;
|
||||
for (; i > 0; i--)
|
||||
*(to--) = *(p--) ^ 0xff;
|
||||
|
||||
ASN1_INTEGER *ret = NULL;
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_INTEGER_new();
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
if ((*p == 0) && (len != 1)) {
|
||||
p++;
|
||||
len--;
|
||||
}
|
||||
OPENSSL_memcpy(s, p, (int)len);
|
||||
ret = *out;
|
||||
}
|
||||
|
||||
if (ret->data != NULL)
|
||||
OPENSSL_free(ret->data);
|
||||
ret->data = s;
|
||||
ret->length = (int)len;
|
||||
if (a != NULL)
|
||||
(*a) = ret;
|
||||
*pp = pend;
|
||||
return (ret);
|
||||
/* Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First,
|
||||
* determine the size needed for a minimal result. */
|
||||
if (is_negative) {
|
||||
/* 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff
|
||||
* through 0x000100...001 and need one leading zero removed. 0x8000...00
|
||||
* through 0xff00...00 have a two's complement of 0x8000...00 through
|
||||
* 0x0100...00 and will be minimally-encoded as-is. */
|
||||
if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0xff &&
|
||||
!is_all_zeros(CBS_data(&cbs) + 1, CBS_len(&cbs) - 1)) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
} else {
|
||||
/* Remove the leading zero byte, if any. */
|
||||
if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0x00) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ASN1_STRING_set(ret, CBS_data(&cbs), CBS_len(&cbs))) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (is_negative) {
|
||||
ret->type = V_ASN1_NEG_INTEGER;
|
||||
negate_twos_complement(ret->data, ret->length);
|
||||
} else {
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
}
|
||||
|
||||
/* The value should be minimally-encoded. */
|
||||
assert(ret->length == 0 || ret->data[0] != 0);
|
||||
/* Zero is not negative. */
|
||||
assert(!is_negative || ret->length > 0);
|
||||
|
||||
*inp += len;
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
return ret;
|
||||
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret)))
|
||||
if (ret != NULL && (out == NULL || *out != ret)) {
|
||||
ASN1_INTEGER_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
|
||||
@@ -300,121 +267,196 @@ int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v)
|
||||
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
|
||||
{
|
||||
uint8_t *const newdata = OPENSSL_malloc(sizeof(uint64_t));
|
||||
if (newdata == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
if (v >= 0) {
|
||||
return ASN1_ENUMERATED_set_uint64(a, (uint64_t) v);
|
||||
}
|
||||
|
||||
if (!ASN1_ENUMERATED_set_uint64(a, 0 - (uint64_t) v)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_free(out->data);
|
||||
out->data = newdata;
|
||||
v = CRYPTO_bswap8(v);
|
||||
memcpy(out->data, &v, sizeof(v));
|
||||
|
||||
out->type = V_ASN1_INTEGER;
|
||||
a->type = V_ASN1_NEG_ENUMERATED;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type)
|
||||
{
|
||||
uint8_t buf[sizeof(uint64_t)];
|
||||
CRYPTO_store_u64_be(buf, v);
|
||||
size_t leading_zeros;
|
||||
for (leading_zeros = 0; leading_zeros < sizeof(uint64_t) - 1;
|
||||
leading_zeros++) {
|
||||
if (out->data[leading_zeros] != 0) {
|
||||
break;
|
||||
}
|
||||
for (leading_zeros = 0; leading_zeros < sizeof(buf); leading_zeros++) {
|
||||
if (buf[leading_zeros] != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
out->length = sizeof(uint64_t) - leading_zeros;
|
||||
OPENSSL_memmove(out->data, out->data + leading_zeros, out->length);
|
||||
|
||||
if (!ASN1_STRING_set(out, buf + leading_zeros,
|
||||
sizeof(buf) - leading_zeros)) {
|
||||
return 0;
|
||||
}
|
||||
out->type = type;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v)
|
||||
{
|
||||
return asn1_string_set_uint64(out, v, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v)
|
||||
{
|
||||
return asn1_string_set_uint64(out, v, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static int asn1_string_get_abs_uint64(uint64_t *out, const ASN1_STRING *a,
|
||||
int type)
|
||||
{
|
||||
if ((a->type & ~V_ASN1_NEG) != type) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
|
||||
return 0;
|
||||
}
|
||||
uint8_t buf[sizeof(uint64_t)] = {0};
|
||||
if (a->length > (int)sizeof(buf)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(buf + sizeof(buf) - a->length, a->data, a->length);
|
||||
*out = CRYPTO_load_u64_be(buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int asn1_string_get_uint64(uint64_t *out, const ASN1_STRING *a, int type)
|
||||
{
|
||||
if (!asn1_string_get_abs_uint64(out, a, type)) {
|
||||
return 0;
|
||||
}
|
||||
if (a->type & V_ASN1_NEG) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a)
|
||||
{
|
||||
return asn1_string_get_uint64(out, a, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a)
|
||||
{
|
||||
return asn1_string_get_uint64(out, a, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static long asn1_string_get_long(const ASN1_STRING *a, int type)
|
||||
{
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t v;
|
||||
if (!asn1_string_get_abs_uint64(&v, a, type)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
int64_t i64;
|
||||
int fits_in_i64;
|
||||
/* Check |v != 0| to handle manually-constructed negative zeros. */
|
||||
if ((a->type & V_ASN1_NEG) && v != 0) {
|
||||
i64 = (int64_t)(0u - v);
|
||||
fits_in_i64 = i64 < 0;
|
||||
} else {
|
||||
i64 = (int64_t)v;
|
||||
fits_in_i64 = i64 >= 0;
|
||||
}
|
||||
OPENSSL_STATIC_ASSERT(sizeof(long) <= sizeof(int64_t), "long is too big");
|
||||
|
||||
if (fits_in_i64 && LONG_MIN <= i64 && i64 <= LONG_MAX) {
|
||||
return (long)i64;
|
||||
}
|
||||
|
||||
err:
|
||||
/* This function's return value does not distinguish overflow from -1. */
|
||||
ERR_clear_error();
|
||||
return -1;
|
||||
}
|
||||
|
||||
long ASN1_INTEGER_get(const ASN1_INTEGER *a)
|
||||
{
|
||||
int neg = 0, i;
|
||||
|
||||
if (a == NULL)
|
||||
return (0L);
|
||||
i = a->type;
|
||||
if (i == V_ASN1_NEG_INTEGER)
|
||||
neg = 1;
|
||||
else if (i != V_ASN1_INTEGER)
|
||||
return -1;
|
||||
|
||||
OPENSSL_STATIC_ASSERT(sizeof(uint64_t) >= sizeof(long),
|
||||
"long larger than uint64_t");
|
||||
|
||||
if (a->length > (int)sizeof(uint64_t)) {
|
||||
/* hmm... a bit ugly, return all ones */
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t r64 = 0;
|
||||
if (a->data != NULL) {
|
||||
for (i = 0; i < a->length; i++) {
|
||||
r64 <<= 8;
|
||||
r64 |= (unsigned char)a->data[i];
|
||||
}
|
||||
|
||||
if (r64 > LONG_MAX) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
long r = (long) r64;
|
||||
if (neg)
|
||||
r = -r;
|
||||
|
||||
return r;
|
||||
return asn1_string_get_long(a, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
|
||||
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
|
||||
{
|
||||
return asn1_string_get_long(a, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
|
||||
int type)
|
||||
{
|
||||
ASN1_INTEGER *ret;
|
||||
int len, j;
|
||||
|
||||
if (ai == NULL)
|
||||
ret = ASN1_INTEGER_new();
|
||||
else
|
||||
if (ai == NULL) {
|
||||
ret = ASN1_STRING_type_new(type);
|
||||
} else {
|
||||
ret = ai;
|
||||
}
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (BN_is_negative(bn) && !BN_is_zero(bn))
|
||||
ret->type = V_ASN1_NEG_INTEGER;
|
||||
else
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
j = BN_num_bits(bn);
|
||||
len = ((j == 0) ? 0 : ((j / 8) + 1));
|
||||
if (ret->length < len + 4) {
|
||||
unsigned char *new_data = OPENSSL_realloc(ret->data, len + 4);
|
||||
if (!new_data) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
ret->data = new_data;
|
||||
|
||||
if (BN_is_negative(bn) && !BN_is_zero(bn)) {
|
||||
ret->type = type | V_ASN1_NEG;
|
||||
} else {
|
||||
ret->type = type;
|
||||
}
|
||||
ret->length = BN_bn2bin(bn, ret->data);
|
||||
/* Correct zero case */
|
||||
if (!ret->length) {
|
||||
ret->data[0] = 0;
|
||||
ret->length = 1;
|
||||
|
||||
int len = BN_num_bytes(bn);
|
||||
if (!ASN1_STRING_set(ret, NULL, len) ||
|
||||
!BN_bn2bin_padded(ret->data, len, bn)) {
|
||||
goto err;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
|
||||
err:
|
||||
if (ret != ai)
|
||||
ASN1_INTEGER_free(ret);
|
||||
return (NULL);
|
||||
if (ret != ai) {
|
||||
ASN1_STRING_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
|
||||
{
|
||||
return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
|
||||
{
|
||||
return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
|
||||
}
|
||||
|
||||
static BIGNUM *asn1_string_to_bn(const ASN1_STRING *ai, BIGNUM *bn, int type)
|
||||
{
|
||||
if ((ai->type & ~V_ASN1_NEG) != type) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BIGNUM *ret;
|
||||
if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
|
||||
else if (ai->type & V_ASN1_NEG)
|
||||
BN_set_negative(ret, 1);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
|
||||
{
|
||||
BIGNUM *ret;
|
||||
|
||||
if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
|
||||
else if (ai->type == V_ASN1_NEG_INTEGER)
|
||||
BN_set_negative(ret, 1);
|
||||
return (ret);
|
||||
return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
|
||||
{
|
||||
return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
+270
-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,187 @@ 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((unsigned char)((*v)[0])) ||
|
||||
!isdigit((unsigned char)((*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 */
|
||||
|
||||
|
||||
@@ -59,11 +59,12 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1_mac.h>
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
/* Cross-module errors from crypto/x509/i2d_pr.c. */
|
||||
@@ -103,117 +104,54 @@ 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 void asn1_put_length(unsigned char **pp, int length);
|
||||
|
||||
int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
|
||||
int *pclass, long omax)
|
||||
int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag,
|
||||
int *out_class, long in_len)
|
||||
{
|
||||
int i, ret;
|
||||
long l;
|
||||
const unsigned char *p = *pp;
|
||||
int tag, xclass, inf;
|
||||
long max = omax;
|
||||
|
||||
if (!max)
|
||||
goto err;
|
||||
ret = (*p & V_ASN1_CONSTRUCTED);
|
||||
xclass = (*p & V_ASN1_PRIVATE);
|
||||
i = *p & V_ASN1_PRIMITIVE_TAG;
|
||||
if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
|
||||
p++;
|
||||
if (--max == 0)
|
||||
goto err;
|
||||
l = 0;
|
||||
while (*p & 0x80) {
|
||||
l <<= 7L;
|
||||
l |= *(p++) & 0x7f;
|
||||
if (--max == 0)
|
||||
goto err;
|
||||
if (l > (INT_MAX >> 7L))
|
||||
goto err;
|
||||
}
|
||||
l <<= 7L;
|
||||
l |= *(p++) & 0x7f;
|
||||
tag = (int)l;
|
||||
if (--max == 0)
|
||||
goto err;
|
||||
} else {
|
||||
tag = i;
|
||||
p++;
|
||||
if (--max == 0)
|
||||
goto err;
|
||||
if (in_len < 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
/* TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to
|
||||
* reject non-minimal lengths, which are only allowed in BER. However,
|
||||
* Android sometimes needs allow a non-minimal length in certificate
|
||||
* signature fields (see b/18228011). Make this only apply to that field,
|
||||
* while requiring DER elsewhere. Better yet, it should be limited to an
|
||||
* preprocessing step in that part of Android. */
|
||||
unsigned tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
CBS cbs, body;
|
||||
CBS_init(&cbs, *inp, (size_t)in_len);
|
||||
if (!CBS_get_any_ber_asn1_element(&cbs, &body, &tag, &header_len,
|
||||
/*out_ber_found=*/NULL, &indefinite) ||
|
||||
indefinite ||
|
||||
!CBS_skip(&body, header_len) ||
|
||||
/* Bound the length to comfortably fit in an int. Lengths in this
|
||||
* module often switch between int and long without overflow checks. */
|
||||
CBS_len(&body) > INT_MAX / 2) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
/* Convert between tag representations. */
|
||||
int tag_class = (tag & CBS_ASN1_CLASS_MASK) >> CBS_ASN1_TAG_SHIFT;
|
||||
int constructed = (tag & CBS_ASN1_CONSTRUCTED) >> CBS_ASN1_TAG_SHIFT;
|
||||
int tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
|
||||
|
||||
/* To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. */
|
||||
if (xclass == V_ASN1_UNIVERSAL && tag > V_ASN1_MAX_UNIVERSAL)
|
||||
goto err;
|
||||
|
||||
*ptag = tag;
|
||||
*pclass = xclass;
|
||||
if (!asn1_get_length(&p, &inf, 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;
|
||||
if (tag_class == V_ASN1_UNIVERSAL && tag_number > V_ASN1_MAX_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return 0x80;
|
||||
}
|
||||
*pp = p;
|
||||
return (ret | inf);
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
|
||||
return (0x80);
|
||||
}
|
||||
|
||||
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
|
||||
long max)
|
||||
{
|
||||
const unsigned char *p = *pp;
|
||||
unsigned long ret = 0;
|
||||
unsigned long i;
|
||||
|
||||
if (max-- < 1)
|
||||
return 0;
|
||||
if (*p == 0x80) {
|
||||
*inf = 1;
|
||||
ret = 0;
|
||||
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;
|
||||
}
|
||||
/*
|
||||
* Bound the length to comfortably fit in an int. Lengths in this module
|
||||
* often switch between int and long without overflow checks.
|
||||
*/
|
||||
if (ret > INT_MAX / 2)
|
||||
return 0;
|
||||
*pp = p;
|
||||
*rl = (long)ret;
|
||||
return 1;
|
||||
*inp = CBS_data(&body);
|
||||
*out_len = CBS_len(&body);
|
||||
*out_tag = tag_number;
|
||||
*out_class = tag_class;
|
||||
return constructed;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -370,8 +308,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 +344,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)
|
||||
@@ -75,17 +74,8 @@
|
||||
*/
|
||||
#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 +87,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 +160,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 +171,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 +218,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 +248,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 +258,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 +313,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 +324,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 +341,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,16 +371,6 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
if (!len)
|
||||
break;
|
||||
q = p;
|
||||
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;
|
||||
}
|
||||
/*
|
||||
* This determines the OPTIONAL flag value. The field cannot be
|
||||
* omitted if it is the last of a SEQUENCE and there is still
|
||||
@@ -424,13 +401,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 +434,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 +473,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 +487,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 +507,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 +541,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 +553,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,16 +583,6 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
while (len > 0) {
|
||||
ASN1_VALUE *skfield;
|
||||
const unsigned char *q = p;
|
||||
/* See if EOC found */
|
||||
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;
|
||||
}
|
||||
skfield = NULL;
|
||||
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
|
||||
-1, 0, 0, ctx, depth)) {
|
||||
@@ -645,10 +596,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 +632,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 +659,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 +674,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 +699,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 +714,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 +830,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;
|
||||
}
|
||||
@@ -948,169 +853,12 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
const unsigned char *p;
|
||||
if (len < 2)
|
||||
return 0;
|
||||
p = *in;
|
||||
if (!p[0] && !p[1]) {
|
||||
*in += 2;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 +884,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 +921,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;
|
||||
@@ -222,7 +223,6 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
int nullerr) {
|
||||
const ASN1_ADB *adb;
|
||||
const ASN1_ADB_TABLE *atbl;
|
||||
long selector;
|
||||
ASN1_VALUE **sfld;
|
||||
int i;
|
||||
if (!(tt->flags & ASN1_TFLG_ADB_MASK)) {
|
||||
@@ -243,14 +243,11 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
return adb->null_tt;
|
||||
}
|
||||
|
||||
/* Convert type to a long:
|
||||
/* Convert type to a NID:
|
||||
* NB: don't check for NID_undef here because it
|
||||
* might be a legitimate value in the table */
|
||||
if (tt->flags & ASN1_TFLG_ADB_OID) {
|
||||
selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
|
||||
} else {
|
||||
selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
|
||||
}
|
||||
assert(tt->flags & ASN1_TFLG_ADB_OID);
|
||||
int selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
|
||||
|
||||
/* Try to find matching entry in table Maybe should check application types
|
||||
* first to allow application override? Might also be useful to have a flag
|
||||
|
||||
@@ -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 */,
|
||||
};
|
||||
|
||||
@@ -314,4 +308,10 @@ int BIO_rw_filename(BIO *bio, const char *filename) {
|
||||
BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename);
|
||||
}
|
||||
|
||||
long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); }
|
||||
|
||||
long BIO_seek(BIO *bio, long offset) {
|
||||
return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL);
|
||||
}
|
||||
|
||||
#endif // OPENSSL_TRUSTY
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -71,18 +71,6 @@ int BIO_printf(BIO *bio, const char *format, ...) {
|
||||
va_start(args, format);
|
||||
out_len = vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
// On Windows, vsnprintf returns -1 rather than the requested length on
|
||||
// truncation
|
||||
if (out_len < 0) {
|
||||
va_start(args, format);
|
||||
out_len = _vscprintf(format, args);
|
||||
va_end(args);
|
||||
assert(out_len >= (int)sizeof(buf));
|
||||
}
|
||||
#endif
|
||||
|
||||
if (out_len < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
@@ -67,9 +69,9 @@ static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) {
|
||||
CBS contents;
|
||||
unsigned tag;
|
||||
size_t header_len;
|
||||
|
||||
int indefinite;
|
||||
if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len,
|
||||
ber_found)) {
|
||||
ber_found, &indefinite)) {
|
||||
return 0;
|
||||
}
|
||||
if (*ber_found) {
|
||||
@@ -91,11 +93,14 @@ static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// is_eoc returns true if |header_len| and |contents|, as returned by
|
||||
// |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value.
|
||||
static char is_eoc(size_t header_len, CBS *contents) {
|
||||
return header_len == 2 && CBS_len(contents) == 2 &&
|
||||
OPENSSL_memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
|
||||
// cbs_get_eoc returns one if |cbs| begins with an "end of contents" (EOC) value
|
||||
// and zero otherwise. If an EOC was found, it advances |cbs| past it.
|
||||
static int cbs_get_eoc(CBS *cbs) {
|
||||
if (CBS_len(cbs) >= 2 &&
|
||||
CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) {
|
||||
return CBS_skip(cbs, 2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
|
||||
@@ -114,21 +119,20 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
}
|
||||
|
||||
while (CBS_len(in) > 0) {
|
||||
if (looking_for_eoc && cbs_get_eoc(in)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
CBS contents;
|
||||
unsigned tag, child_string_tag = string_tag;
|
||||
size_t header_len;
|
||||
int ber_found;
|
||||
int indefinite;
|
||||
CBB *out_contents, out_contents_storage;
|
||||
|
||||
if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len,
|
||||
&ber_found)) {
|
||||
/*out_ber_found=*/NULL, &indefinite)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_eoc(header_len, &contents)) {
|
||||
return looking_for_eoc;
|
||||
}
|
||||
|
||||
if (string_tag != 0) {
|
||||
// This is part of a constructed string. All elements must match
|
||||
// |string_tag| up to the constructed bit and get appended to |out|
|
||||
@@ -151,11 +155,9 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
out_contents = &out_contents_storage;
|
||||
}
|
||||
|
||||
if (CBS_len(&contents) == header_len && header_len > 0 &&
|
||||
CBS_data(&contents)[header_len - 1] == 0x80) {
|
||||
// This is an indefinite length element.
|
||||
if (indefinite) {
|
||||
if (!cbs_convert_ber(in, out_contents, child_string_tag,
|
||||
1 /* looking for eoc */, depth + 1) ||
|
||||
/*looking_for_eoc=*/1, depth + 1) ||
|
||||
!CBB_flush(out)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -169,7 +171,7 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
if (tag & CBS_ASN1_CONSTRUCTED) {
|
||||
// Recurse into children.
|
||||
if (!cbs_convert_ber(&contents, out_contents, child_string_tag,
|
||||
0 /* not looking for eoc */, depth + 1)) {
|
||||
/*looking_for_eoc=*/0, depth + 1)) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -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.
|
||||
@@ -271,13 +279,20 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
|
||||
tag |= tag_number;
|
||||
|
||||
// Tag [UNIVERSAL 0] is reserved for use by the encoding. Reject it here to
|
||||
// avoid some ambiguity around ANY values and BER indefinite-length EOCs. See
|
||||
// https://crbug.com/boringssl/455.
|
||||
if ((tag & ~CBS_ASN1_CONSTRUCTED) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*out = tag;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
size_t *out_header_len, int *out_ber_found,
|
||||
int ber_ok) {
|
||||
int *out_indefinite, int ber_ok) {
|
||||
CBS header = *cbs;
|
||||
CBS throwaway;
|
||||
|
||||
@@ -286,6 +301,10 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
}
|
||||
if (ber_ok) {
|
||||
*out_ber_found = 0;
|
||||
*out_indefinite = 0;
|
||||
} else {
|
||||
assert(out_ber_found == NULL);
|
||||
assert(out_indefinite == NULL);
|
||||
}
|
||||
|
||||
unsigned tag;
|
||||
@@ -325,6 +344,7 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
*out_header_len = header_len;
|
||||
}
|
||||
*out_ber_found = 1;
|
||||
*out_indefinite = 1;
|
||||
return CBS_get_bytes(cbs, out, header_len);
|
||||
}
|
||||
|
||||
@@ -387,16 +407,18 @@ int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
|
||||
|
||||
int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
size_t *out_header_len) {
|
||||
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
|
||||
NULL, 0 /* DER only */);
|
||||
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, NULL, NULL,
|
||||
/*ber_ok=*/0);
|
||||
}
|
||||
|
||||
int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
size_t *out_header_len, int *out_ber_found) {
|
||||
size_t *out_header_len, int *out_ber_found,
|
||||
int *out_indefinite) {
|
||||
int ber_found_temp;
|
||||
return cbs_get_any_asn1_element(
|
||||
cbs, out, out_tag, out_header_len,
|
||||
out_ber_found ? out_ber_found : &ber_found_temp, 1 /* BER allowed */);
|
||||
out_ber_found ? out_ber_found : &ber_found_temp, out_indefinite,
|
||||
/*ber_ok=*/1);
|
||||
}
|
||||
|
||||
static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
|
||||
|
||||
@@ -67,7 +67,7 @@ Lshort:
|
||||
ldp x24,x25,[x3] // load key
|
||||
ldp x26,x27,[x3,#16]
|
||||
ldp x28,x30,[x4] // load counter
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
ror x24,x24,#32
|
||||
ror x25,x25,#32
|
||||
ror x26,x26,#32
|
||||
@@ -228,7 +228,7 @@ Loop:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -285,7 +285,7 @@ Less_than_64:
|
||||
add x15,x15,x16,lsl#32
|
||||
add x17,x17,x19,lsl#32
|
||||
add x20,x20,x21,lsl#32
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -351,7 +351,7 @@ ChaCha20_neon:
|
||||
ldp x28,x30,[x4] // load counter
|
||||
ld1 {v27.4s},[x4]
|
||||
ld1 {v31.4s},[x5]
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev64 v24.4s,v24.4s
|
||||
ror x24,x24,#32
|
||||
ror x25,x25,#32
|
||||
@@ -649,7 +649,7 @@ Loop_neon:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -729,7 +729,7 @@ Ltail_neon:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -847,7 +847,7 @@ L512_or_more_neon:
|
||||
ldp x28,x30,[x4] // load counter
|
||||
ld1 {v27.4s},[x4]
|
||||
ld1 {v31.4s},[x5]
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev64 v24.4s,v24.4s
|
||||
ror x24,x24,#32
|
||||
ror x25,x25,#32
|
||||
@@ -1360,7 +1360,7 @@ Loop_upper_neon:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -1874,7 +1874,7 @@ Loop_lower_neon:
|
||||
add x1,x1,#64
|
||||
add v21.4s,v21.4s,v25.4s
|
||||
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
|
||||
@@ -68,7 +68,7 @@ ChaCha20_ctr32:
|
||||
ldp x24,x25,[x3] // load key
|
||||
ldp x26,x27,[x3,#16]
|
||||
ldp x28,x30,[x4] // load counter
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
ror x24,x24,#32
|
||||
ror x25,x25,#32
|
||||
ror x26,x26,#32
|
||||
@@ -229,7 +229,7 @@ ChaCha20_ctr32:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -286,7 +286,7 @@ ChaCha20_ctr32:
|
||||
add x15,x15,x16,lsl#32
|
||||
add x17,x17,x19,lsl#32
|
||||
add x20,x20,x21,lsl#32
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -352,7 +352,7 @@ ChaCha20_neon:
|
||||
ldp x28,x30,[x4] // load counter
|
||||
ld1 {v27.4s},[x4]
|
||||
ld1 {v31.4s},[x5]
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev64 v24.4s,v24.4s
|
||||
ror x24,x24,#32
|
||||
ror x25,x25,#32
|
||||
@@ -650,7 +650,7 @@ ChaCha20_neon:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -730,7 +730,7 @@ ChaCha20_neon:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -848,7 +848,7 @@ ChaCha20_512_neon:
|
||||
ldp x28,x30,[x4] // load counter
|
||||
ld1 {v27.4s},[x4]
|
||||
ld1 {v31.4s},[x5]
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev64 v24.4s,v24.4s
|
||||
ror x24,x24,#32
|
||||
ror x25,x25,#32
|
||||
@@ -1361,7 +1361,7 @@ ChaCha20_512_neon:
|
||||
add x20,x20,x21,lsl#32
|
||||
ldp x19,x21,[x1,#48]
|
||||
add x1,x1,#64
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
@@ -1875,7 +1875,7 @@ ChaCha20_512_neon:
|
||||
add x1,x1,#64
|
||||
add v21.4s,v21.4s,v25.4s
|
||||
|
||||
#ifdef __ARMEB__
|
||||
#ifdef __AARCH64EB__
|
||||
rev x5,x5
|
||||
rev x7,x7
|
||||
rev x9,x9
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -19,28 +19,24 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#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 +67,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 +96,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 +118,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 +131,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;
|
||||
|
||||
@@ -50,7 +50,6 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_cipher.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cipher.h>
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_crypto.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#include "../fipsmodule/cipher/internal.h"
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
#define EVP_AEAD_AES_GCM_SIV_NONCE_LEN 12
|
||||
@@ -857,22 +857,15 @@ static const EVP_AEAD aead_aes_256_gcm_siv = {
|
||||
|
||||
#if defined(AES_GCM_SIV_ASM)
|
||||
|
||||
static char avx_aesni_capable(void) {
|
||||
const uint32_t ecx = OPENSSL_ia32cap_P[1];
|
||||
|
||||
return (ecx & (1 << (57 - 32))) != 0 /* AESNI */ &&
|
||||
(ecx & (1 << 28)) != 0 /* AVX */;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) {
|
||||
if (avx_aesni_capable()) {
|
||||
if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) {
|
||||
return &aead_aes_128_gcm_siv_asm;
|
||||
}
|
||||
return &aead_aes_128_gcm_siv;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) {
|
||||
if (avx_aesni_capable()) {
|
||||
if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) {
|
||||
return &aead_aes_256_gcm_siv_asm;
|
||||
}
|
||||
return &aead_aes_256_gcm_siv;
|
||||
|
||||
+99
-78
@@ -59,7 +59,6 @@
|
||||
#include <CCryptoBoringSSL_nid.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../delocate.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
@@ -88,17 +87,21 @@ static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_cbc) {
|
||||
memset(out, 0, sizeof(EVP_CIPHER));
|
||||
out->nid = NID_des_cbc;
|
||||
out->block_size = 8;
|
||||
out->key_len = 8;
|
||||
out->iv_len = 8;
|
||||
out->ctx_size = sizeof(EVP_DES_KEY);
|
||||
out->flags = EVP_CIPH_CBC_MODE;
|
||||
out->init = des_init_key;
|
||||
out->cipher = des_cbc_cipher;
|
||||
}
|
||||
static const EVP_CIPHER evp_des_cbc = {
|
||||
/* nid = */ NID_des_cbc,
|
||||
/* block_size = */ 8,
|
||||
/* key_len = */ 8,
|
||||
/* iv_len = */ 8,
|
||||
/* ctx_size = */ sizeof(EVP_DES_KEY),
|
||||
/* flags = */ EVP_CIPH_CBC_MODE,
|
||||
/* app_data = */ NULL,
|
||||
/* init = */ des_init_key,
|
||||
/* cipher = */ des_cbc_cipher,
|
||||
/* cleanup = */ NULL,
|
||||
/* ctrl = */ NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_des_cbc(void) { return &evp_des_cbc; }
|
||||
|
||||
static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
size_t in_len) {
|
||||
@@ -107,25 +110,29 @@ static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
}
|
||||
in_len -= ctx->cipher->block_size;
|
||||
|
||||
EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data;
|
||||
EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data;
|
||||
for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) {
|
||||
DES_ecb_encrypt((DES_cblock *) (in + i), (DES_cblock *) (out + i),
|
||||
DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
|
||||
&dat->ks.ks, ctx->encrypt);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ecb) {
|
||||
memset(out, 0, sizeof(EVP_CIPHER));
|
||||
out->nid = NID_des_ecb;
|
||||
out->block_size = 8;
|
||||
out->key_len = 8;
|
||||
out->iv_len = 0;
|
||||
out->ctx_size = sizeof(EVP_DES_KEY);
|
||||
out->flags = EVP_CIPH_ECB_MODE;
|
||||
out->init = des_init_key;
|
||||
out->cipher = des_ecb_cipher;
|
||||
}
|
||||
static const EVP_CIPHER evp_des_ecb = {
|
||||
/* nid = */ NID_des_ecb,
|
||||
/* block_size = */ 8,
|
||||
/* key_len = */ 8,
|
||||
/* iv_len = */ 0,
|
||||
/* ctx_size = */ sizeof(EVP_DES_KEY),
|
||||
/* flags = */ EVP_CIPH_ECB_MODE,
|
||||
/* app_data = */ NULL,
|
||||
/* init = */ des_init_key,
|
||||
/* cipher = */ des_ecb_cipher,
|
||||
/* cleanup = */ NULL,
|
||||
/* ctrl = */ NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_des_ecb(void) { return &evp_des_ecb; }
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
@@ -137,7 +144,7 @@ typedef struct {
|
||||
static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, int enc) {
|
||||
DES_cblock *deskey = (DES_cblock *)key;
|
||||
DES_EDE_KEY *dat = (DES_EDE_KEY*) ctx->cipher_data;
|
||||
DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
|
||||
|
||||
DES_set_key(&deskey[0], &dat->ks.ks[0]);
|
||||
DES_set_key(&deskey[1], &dat->ks.ks[1]);
|
||||
@@ -147,8 +154,8 @@ static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
}
|
||||
|
||||
static int des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
|
||||
const uint8_t *in, size_t in_len) {
|
||||
DES_EDE_KEY *dat = (DES_EDE_KEY*) ctx->cipher_data;
|
||||
const uint8_t *in, size_t in_len) {
|
||||
DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
|
||||
|
||||
DES_ede3_cbc_encrypt(in, out, in_len, &dat->ks.ks[0], &dat->ks.ks[1],
|
||||
&dat->ks.ks[2], (DES_cblock *)ctx->iv, ctx->encrypt);
|
||||
@@ -156,22 +163,26 @@ static int des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede3_cbc) {
|
||||
memset(out, 0, sizeof(EVP_CIPHER));
|
||||
out->nid = NID_des_ede3_cbc;
|
||||
out->block_size = 8;
|
||||
out->key_len = 24;
|
||||
out->iv_len = 8;
|
||||
out->ctx_size = sizeof(DES_EDE_KEY);
|
||||
out->flags = EVP_CIPH_CBC_MODE;
|
||||
out->init = des_ede3_init_key;
|
||||
out->cipher = des_ede3_cbc_cipher;
|
||||
}
|
||||
static const EVP_CIPHER evp_des_ede3_cbc = {
|
||||
/* nid = */ NID_des_ede3_cbc,
|
||||
/* block_size = */ 8,
|
||||
/* key_len = */ 24,
|
||||
/* iv_len = */ 8,
|
||||
/* ctx_size = */ sizeof(DES_EDE_KEY),
|
||||
/* flags = */ EVP_CIPH_CBC_MODE,
|
||||
/* app_data = */ NULL,
|
||||
/* init = */ des_ede3_init_key,
|
||||
/* cipher = */ des_ede3_cbc_cipher,
|
||||
/* cleanup = */ NULL,
|
||||
/* ctrl = */ NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_des_ede3_cbc(void) { return &evp_des_ede3_cbc; }
|
||||
|
||||
static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, int enc) {
|
||||
DES_cblock *deskey = (DES_cblock *) key;
|
||||
DES_EDE_KEY *dat = (DES_EDE_KEY *) ctx->cipher_data;
|
||||
const uint8_t *iv, int enc) {
|
||||
DES_cblock *deskey = (DES_cblock *)key;
|
||||
DES_EDE_KEY *dat = (DES_EDE_KEY *)ctx->cipher_data;
|
||||
|
||||
DES_set_key(&deskey[0], &dat->ks.ks[0]);
|
||||
DES_set_key(&deskey[1], &dat->ks.ks[1]);
|
||||
@@ -180,17 +191,21 @@ static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede_cbc) {
|
||||
memset(out, 0, sizeof(EVP_CIPHER));
|
||||
out->nid = NID_des_ede_cbc;
|
||||
out->block_size = 8;
|
||||
out->key_len = 16;
|
||||
out->iv_len = 8;
|
||||
out->ctx_size = sizeof(DES_EDE_KEY);
|
||||
out->flags = EVP_CIPH_CBC_MODE;
|
||||
out->init = des_ede_init_key;
|
||||
out->cipher = des_ede3_cbc_cipher;
|
||||
}
|
||||
static const EVP_CIPHER evp_des_ede_cbc = {
|
||||
/* nid = */ NID_des_ede_cbc,
|
||||
/* block_size = */ 8,
|
||||
/* key_len = */ 16,
|
||||
/* iv_len = */ 8,
|
||||
/* ctx_size = */ sizeof(DES_EDE_KEY),
|
||||
/* flags = */ EVP_CIPH_CBC_MODE,
|
||||
/* app_data = */ NULL,
|
||||
/* init = */ des_ede_init_key,
|
||||
/* cipher = */ des_ede3_cbc_cipher,
|
||||
/* cleanup = */ NULL,
|
||||
/* ctrl = */ NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_des_ede_cbc(void) { return &evp_des_ede_cbc; }
|
||||
|
||||
static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
|
||||
const uint8_t *in, size_t in_len) {
|
||||
@@ -208,30 +223,36 @@ static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede) {
|
||||
memset(out, 0, sizeof(EVP_CIPHER));
|
||||
out->nid = NID_des_ede_ecb;
|
||||
out->block_size = 8;
|
||||
out->key_len = 16;
|
||||
out->iv_len = 0;
|
||||
out->ctx_size = sizeof(DES_EDE_KEY);
|
||||
out->flags = EVP_CIPH_ECB_MODE;
|
||||
out->init = des_ede_init_key;
|
||||
out->cipher = des_ede_ecb_cipher;
|
||||
}
|
||||
static const EVP_CIPHER evp_des_ede = {
|
||||
/* nid = */ NID_des_ede_ecb,
|
||||
/* block_size = */ 8,
|
||||
/* key_len = */ 16,
|
||||
/* iv_len = */ 0,
|
||||
/* ctx_size = */ sizeof(DES_EDE_KEY),
|
||||
/* flags = */ EVP_CIPH_ECB_MODE,
|
||||
/* app_data = */ NULL,
|
||||
/* init = */ des_ede_init_key,
|
||||
/* cipher = */ des_ede_ecb_cipher,
|
||||
/* cleanup = */ NULL,
|
||||
/* ctrl = */ NULL,
|
||||
};
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede3) {
|
||||
memset(out, 0, sizeof(EVP_CIPHER));
|
||||
out->nid = NID_des_ede3_ecb;
|
||||
out->block_size = 8;
|
||||
out->key_len = 24;
|
||||
out->iv_len = 0;
|
||||
out->ctx_size = sizeof(DES_EDE_KEY);
|
||||
out->flags = EVP_CIPH_ECB_MODE;
|
||||
out->init = des_ede3_init_key;
|
||||
out->cipher = des_ede_ecb_cipher;
|
||||
}
|
||||
const EVP_CIPHER *EVP_des_ede(void) { return &evp_des_ede; }
|
||||
|
||||
const EVP_CIPHER* EVP_des_ede3_ecb(void) {
|
||||
return EVP_des_ede3();
|
||||
}
|
||||
static const EVP_CIPHER evp_des_ede3 = {
|
||||
/* nid = */ NID_des_ede3_ecb,
|
||||
/* block_size = */ 8,
|
||||
/* key_len = */ 24,
|
||||
/* iv_len = */ 0,
|
||||
/* ctx_size = */ sizeof(DES_EDE_KEY),
|
||||
/* flags = */ EVP_CIPH_ECB_MODE,
|
||||
/* app_data = */ NULL,
|
||||
/* init = */ des_ede3_init_key,
|
||||
/* cipher = */ des_ede_ecb_cipher,
|
||||
/* cleanup = */ NULL,
|
||||
/* ctrl = */ NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_des_ede3(void) { return &evp_des_ede3; }
|
||||
|
||||
const EVP_CIPHER *EVP_des_ede3_ecb(void) { return EVP_des_ede3(); }
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../internal.h"
|
||||
@@ -99,6 +98,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 +118,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 +129,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);
|
||||
@@ -161,8 +171,7 @@ OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8,
|
||||
"wrong chacha20_poly1305_seal_data size");
|
||||
|
||||
OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) {
|
||||
const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
|
||||
return sse41_capable;
|
||||
return CRYPTO_is_SSE4_1_capable();
|
||||
}
|
||||
|
||||
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/* 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. */
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_APPLE) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
static int has_hw_feature(const char *name) {
|
||||
int value;
|
||||
size_t len = sizeof(value);
|
||||
if (sysctlbyname(name, &value, &len, NULL, 0) != 0) {
|
||||
return 0;
|
||||
}
|
||||
if (len != sizeof(int)) {
|
||||
// This should not happen. All the values queried should be integer-valued.
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Per sys/sysctl.h:
|
||||
//
|
||||
// Selectors that return errors are not support on the system. Supported
|
||||
// features will return 1 if they are recommended or 0 if they are supported
|
||||
// but are not expected to help performance. Future versions of these
|
||||
// selectors may return larger values as necessary so it is best to test for
|
||||
// non zero.
|
||||
return value != 0;
|
||||
}
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
// Apple ARM64 platforms have NEON and cryptography extensions available
|
||||
// statically, so we do not need to query them. In particular, there sometimes
|
||||
// are no sysctls corresponding to such features. See below.
|
||||
#if !defined(__ARM_NEON) || !defined(__ARM_FEATURE_AES) || \
|
||||
!defined(__ARM_FEATURE_SHA2)
|
||||
#error "NEON and crypto extensions should be statically available."
|
||||
#endif
|
||||
OPENSSL_armcap_P =
|
||||
ARMV7_NEON | ARMV8_AES | ARMV8_PMULL | ARMV8_SHA1 | ARMV8_SHA256;
|
||||
|
||||
// macOS has sysctls named both like "hw.optional.arm.FEAT_SHA512" and like
|
||||
// "hw.optional.armv8_2_sha512". There does not appear to be documentation on
|
||||
// which to use. The "armv8_2_sha512" style omits statically-available
|
||||
// features, while the "FEAT_SHA512" style includes them. However, the
|
||||
// "FEAT_SHA512" style was added in macOS 12, so we use the older style for
|
||||
// better compatibility and handle static features above.
|
||||
if (has_hw_feature("hw.optional.armv8_2_sha512")) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA512;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // OPENSSL_AARCH64 && OPENSSL_APPLE && !OPENSSL_STATIC_ARMCAP
|
||||
+8
-7
@@ -12,7 +12,7 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include "internal.h"
|
||||
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_FUCHSIA) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
@@ -23,16 +23,14 @@
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
uint32_t hwcap;
|
||||
zx_status_t rc = zx_system_get_features(ZX_FEATURE_KIND_CPU, &hwcap);
|
||||
if (rc != ZX_OK || (hwcap & ZX_ARM64_FEATURE_ISA_ASIMD) == 0) {
|
||||
// Matching OpenSSL, if NEON/ASIMD is missing, don't report other features
|
||||
// either.
|
||||
// If NEON/ASIMD is missing, don't report other features either. This
|
||||
// matches OpenSSL, and the other features depend on SIMD registers.
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,9 +45,12 @@ void OPENSSL_cpuid_setup(void) {
|
||||
if (hwcap & ZX_ARM64_FEATURE_ISA_SHA1) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA1;
|
||||
}
|
||||
if (hwcap & ZX_ARM64_FEATURE_ISA_SHA2) {
|
||||
if (hwcap & ZX_ARM64_FEATURE_ISA_SHA256) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA256;
|
||||
}
|
||||
if (hwcap & ZX_ARM64_FEATURE_ISA_SHA512) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA512;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP
|
||||
#endif // OPENSSL_AARCH64 && OPENSSL_FUCHSIA && !OPENSSL_STATIC_ARMCAP
|
||||
+6
-4
@@ -12,7 +12,7 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include "internal.h"
|
||||
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_LINUX) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
@@ -21,8 +21,6 @@
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
@@ -36,6 +34,7 @@ void OPENSSL_cpuid_setup(void) {
|
||||
static const unsigned long kPMULL = 1 << 4;
|
||||
static const unsigned long kSHA1 = 1 << 5;
|
||||
static const unsigned long kSHA256 = 1 << 6;
|
||||
static const unsigned long kSHA512 = 1 << 21;
|
||||
|
||||
if ((hwcap & kNEON) == 0) {
|
||||
// Matching OpenSSL, if NEON is missing, don't report other features
|
||||
@@ -57,6 +56,9 @@ void OPENSSL_cpuid_setup(void) {
|
||||
if (hwcap & kSHA256) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA256;
|
||||
}
|
||||
if (hwcap & kSHA512) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA512;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP
|
||||
#endif // OPENSSL_AARCH64 && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP
|
||||
+4
-4
@@ -13,7 +13,7 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include "internal.h"
|
||||
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_WINDOWS) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
@@ -22,8 +22,6 @@
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
// We do not need to check for the presence of NEON, as Armv8-A always has it
|
||||
@@ -36,6 +34,8 @@ void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA1;
|
||||
OPENSSL_armcap_P |= ARMV8_SHA256;
|
||||
}
|
||||
// As of writing, Windows does not have a |PF_*| value for ARMv8.2 SHA-512
|
||||
// extensions. When it does, add it here.
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // OPENSSL_AARCH64 && OPENSSL_WINDOWS && !OPENSSL_STATIC_ARMCAP
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include "internal.h"
|
||||
|
||||
#if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
+5
-4
@@ -12,9 +12,10 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include "internal.h"
|
||||
|
||||
#if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP)
|
||||
#if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
@@ -23,7 +24,7 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "cpu-arm-linux.h"
|
||||
#include "cpu_arm_linux.h"
|
||||
|
||||
#define AT_HWCAP 16
|
||||
#define AT_HWCAP2 26
|
||||
@@ -226,4 +227,4 @@ int CRYPTO_has_broken_NEON(void) { return g_has_broken_neon; }
|
||||
|
||||
int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
|
||||
|
||||
#endif // OPENSSL_ARM && !OPENSSL_STATIC_ARMCAP
|
||||
#endif // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP
|
||||
File renamed without changes.
+1
-2
@@ -54,8 +54,7 @@
|
||||
* copied and put under another distribution licence
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM) && (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if defined(OPENSSL_PPC64LE)
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
|
||||
#include <CCryptoBoringSSL_crypto.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
|
||||
#include "fipsmodule/rand/fork_detect.h"
|
||||
#include "fipsmodule/rand/internal.h"
|
||||
#include "internal.h"
|
||||
@@ -88,22 +86,31 @@ HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0;
|
||||
|
||||
#if defined(OPENSSL_STATIC_ARMCAP)
|
||||
|
||||
// See ARM ACLE for the definitions of these macros. Note |__ARM_FEATURE_AES|
|
||||
// covers both AES and PMULL and |__ARM_FEATURE_SHA2| covers SHA-1 and SHA-256.
|
||||
// https://developer.arm.com/architectures/system-architectures/software-standards/acle
|
||||
// https://github.com/ARM-software/acle/issues/152
|
||||
//
|
||||
// TODO(davidben): Do we still need |OPENSSL_STATIC_ARMCAP_*| or are the
|
||||
// standard flags and -march sufficient?
|
||||
HIDDEN uint32_t OPENSSL_armcap_P =
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_NEON) || \
|
||||
(defined(__ARM_NEON__) || defined(__ARM_NEON))
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON)
|
||||
ARMV7_NEON |
|
||||
#endif
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO)
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_AES)
|
||||
ARMV8_AES |
|
||||
#endif
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO)
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_AES)
|
||||
ARMV8_PMULL |
|
||||
#endif
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_SHA2)
|
||||
ARMV8_SHA1 |
|
||||
#endif
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO)
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_SHA2)
|
||||
ARMV8_SHA256 |
|
||||
#endif
|
||||
#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO)
|
||||
ARMV8_PMULL |
|
||||
#if defined(__ARM_FEATURE_SHA512)
|
||||
ARMV8_SHA512 |
|
||||
#endif
|
||||
0;
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_rand.h>
|
||||
#include <CCryptoBoringSSL_sha.h>
|
||||
@@ -37,6 +36,10 @@
|
||||
// Various pre-computed constants.
|
||||
#include "./curve25519_tables.h"
|
||||
|
||||
#if defined(OPENSSL_NO_ASM)
|
||||
#define FIAT_25519_NO_ASM
|
||||
#endif
|
||||
|
||||
#if defined(BORINGSSL_CURVE25519_64BIT)
|
||||
#include "../../third_party/fiat/curve25519_64.h"
|
||||
#else
|
||||
@@ -503,27 +506,21 @@ static void ge_p3_tobytes(uint8_t s[32], const ge_p3 *h) {
|
||||
int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) {
|
||||
fe u;
|
||||
fe_loose v;
|
||||
fe v3;
|
||||
fe w;
|
||||
fe vxx;
|
||||
fe_loose check;
|
||||
|
||||
fe_frombytes(&h->Y, s);
|
||||
fe_1(&h->Z);
|
||||
fe_sq_tt(&v3, &h->Y);
|
||||
fe_mul_ttt(&vxx, &v3, &d);
|
||||
fe_sub(&v, &v3, &h->Z); // u = y^2-1
|
||||
fe_sq_tt(&w, &h->Y);
|
||||
fe_mul_ttt(&vxx, &w, &d);
|
||||
fe_sub(&v, &w, &h->Z); // u = y^2-1
|
||||
fe_carry(&u, &v);
|
||||
fe_add(&v, &vxx, &h->Z); // v = dy^2+1
|
||||
|
||||
fe_sq_tl(&v3, &v);
|
||||
fe_mul_ttl(&v3, &v3, &v); // v3 = v^3
|
||||
fe_sq_tt(&h->X, &v3);
|
||||
fe_mul_ttl(&h->X, &h->X, &v);
|
||||
fe_mul_ttt(&h->X, &h->X, &u); // x = uv^7
|
||||
|
||||
fe_pow22523(&h->X, &h->X); // x = (uv^7)^((q-5)/8)
|
||||
fe_mul_ttt(&h->X, &h->X, &v3);
|
||||
fe_mul_ttt(&h->X, &h->X, &u); // x = uv^3(uv^7)^((q-5)/8)
|
||||
fe_mul_ttl(&w, &u, &v); // w = u*v
|
||||
fe_pow22523(&h->X, &w); // x = w^((q-5)/8)
|
||||
fe_mul_ttt(&h->X, &h->X, &u); // x = u*w^((q-5)/8)
|
||||
|
||||
fe_sq_tt(&vxx, &h->X);
|
||||
fe_mul_ttl(&vxx, &vxx, &v);
|
||||
@@ -820,7 +817,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);
|
||||
|
||||
+10
-11
@@ -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
|
||||
+2
-4
@@ -59,7 +59,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#include "../../internal.h"
|
||||
#include "../internal.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -253,7 +253,7 @@ static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
|
||||
|
||||
if (priv_key) {
|
||||
if (!BIO_indent(bp, off, 128) ||
|
||||
BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
|
||||
BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
@@ -368,7 +368,7 @@ static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
|
||||
}
|
||||
order = BN_new();
|
||||
if (order == NULL || !EC_GROUP_get_order(group, order, NULL) ||
|
||||
BIO_printf(bp, "%s: (%d bit)\n", ecstr, BN_num_bits(order)) <= 0) {
|
||||
BIO_printf(bp, "%s: (%u bit)\n", ecstr, BN_num_bits(order)) <= 0) {
|
||||
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) {
|
||||
|
||||
@@ -50,8 +50,6 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../modes/internal.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include "../../internal.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
@@ -30,18 +30,14 @@ extern "C" {
|
||||
#define HWAES
|
||||
#define HWAES_ECB
|
||||
|
||||
OPENSSL_INLINE int hwaes_capable(void) {
|
||||
return (OPENSSL_ia32cap_get()[1] & (1 << (57 - 32))) != 0;
|
||||
}
|
||||
OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_AESNI_capable(); }
|
||||
|
||||
#define VPAES
|
||||
#if defined(OPENSSL_X86_64)
|
||||
#define VPAES_CTR32
|
||||
#endif
|
||||
#define VPAES_CBC
|
||||
OPENSSL_INLINE int vpaes_capable(void) {
|
||||
return (OPENSSL_ia32cap_get()[1] & (1 << (41 - 32))) != 0;
|
||||
}
|
||||
OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_SSSE3_capable(); }
|
||||
|
||||
#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
|
||||
#define HWAES
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -636,7 +636,7 @@ _aes_hw_ctr32_encrypt_blocks:
|
||||
//
|
||||
// [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice
|
||||
// [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice
|
||||
#ifndef __ARMEB__
|
||||
#ifndef __AARCH64EB__
|
||||
rev w8, w8
|
||||
#endif
|
||||
add w10, w8, #1
|
||||
|
||||
@@ -637,7 +637,7 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
//
|
||||
// [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice
|
||||
// [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice
|
||||
#ifndef __ARMEB__
|
||||
#ifndef __AARCH64EB__
|
||||
rev w8, w8
|
||||
#endif
|
||||
add w10, w8, #1
|
||||
|
||||
@@ -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
|
||||
|
||||
Loaded 100 of 421 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user