Browse OpenSSL punycode specs (6 of 19)

Proofs / OpenSSL punycode / The standard

rfc_digit_case

Changing the case of any letters among the deltas changes nothing: the same result, length and code points (RFC 3492 5: "A decoder MUST recognize the letters in both uppercase and lowercase forms").

spec
35 lines of C
with its headers
87 lines
status
Proved
6 of 19
On this page

The spec

The spec is the C program harness/rfc_digit_case.c, 35 lines, shown whole as it is in the repository at 7556530. It includes harness/laws_common.h (52 lines). Its first comment states the property in full, with every bound.

C
// Law rfc_digit_case: The decoder reads the digits of the deltas in either case. For any input (any octets, any
// length the allocator provides) and any capacity (any unsigned int), the input with any of the letters of its deltas
// (after the last '-'; all octets when there is no '-' or it is the first one) changed to the other case decodes to
// the same result, length and code points (each into an array of exactly that capacity holding any values). The
// octets before the last '-' are code points, not digits: they keep their case.
#include "laws_common.h"

int main(void) {
	// Choose the inputs: any octets, the same with any letters of the deltas in the other case, any capacity
	size_t len = upto(PTRDIFF_MAX);
	unsigned char *input = any_bytes(len);
	unsigned char *other = copy_of(input, len);
	unsigned int cap = nondet_u32();
	unsigned int *out1 = any_uints(cap);
	unsigned int *out2 = any_uints(cap);
	if (input == NULL || other == NULL || out1 == NULL || out2 == NULL)
		return 0;
	size_t literal = last_delimiter(input, len);
	for (size_t i = literal > 0 ? literal + 1 : 0; i < len; i++)
		if (nondet_u8() == 0) {
			if (other[i] >= 'a' && other[i] <= 'z')
				other[i] = other[i] - 32;
			else if (other[i] >= 'A' && other[i] <= 'Z')
				other[i] = other[i] + 32;
		}
	unsigned int count1 = cap, count2 = cap;
	int r1 = ossl_punycode_decode((const char *) input, len, out1, &count1);
	int r2 = ossl_punycode_decode((const char *) other, len, out2, &count2);
	// Check. RFC 3492 5: "A decoder MUST recognize the letters in both uppercase and lowercase forms (including
	// mixtures of both forms)."
	gp_assert(r1 == r2 && count1 == count2);
	if (r1 == 1)
		gp_assert(uints_eq(out1, out2, count1));
	return 0;
}
harness/laws_common.h, 52 lines
C
// laws_common.h: trivial helpers shared by the short laws (each law includes it once). No models, no algorithms.
#include <stdint.h>
#include <stdlib.h>
#include "quickproof.h"
#include "crypto/punycode.h"                         // ossl_punycode_decode, ossl_a2ulabel
// OpenSSL's allocator in this build (patch/shim/ossl_shim.c): set to 1 when CRYPTO_zalloc fails (malloc may fail at
// any call in the theorems), and the number of its blocks not freed yet.
extern int ossl_shim_zalloc_failed;
extern long ossl_shim_live;
static size_t upto(size_t max) {                     // any value from 0 to max
	size_t value = nondet_size();
	return value < max ? value : max;
}
static unsigned char *any_bytes(size_t n) {          // a new object of n arbitrary bytes (NULL if malloc fails)
	unsigned char *bytes = malloc(n);
	if (bytes != NULL)
		nondet_bytes(bytes, n);
	return bytes;
}
static unsigned int *any_uints(size_t n) {           // a new array of n arbitrary unsigned ints (NULL if malloc fails)
	unsigned int *values = malloc(n * sizeof(unsigned int));
	for (size_t i = 0; values != NULL && i < n; i++)
		values[i] = nondet_u32();
	return values;
}
static unsigned char *copy_of(const unsigned char *source, size_t n) {   // a new object holding source[0..n-1]
	if (source == NULL)                                                   // (NULL if source is or malloc fails)
		return NULL;
	unsigned char *copy = malloc(n);
	for (size_t i = 0; copy != NULL && i < n; i++)
		copy[i] = source[i];
	return copy;
}
static int bytes_eq(const unsigned char *a, const unsigned char *b, size_t n) {   // a[0..n-1] == b[0..n-1]
	for (size_t i = 0; i < n; i++)
		if (a[i] != b[i])
			return 0;
	return 1;
}
static int uints_eq(const unsigned int *a, const unsigned int *b, size_t n) {     // a[0..n-1] == b[0..n-1]
	for (size_t i = 0; i < n; i++)
		if (a[i] != b[i])
			return 0;
	return 1;
}
static size_t last_delimiter(const unsigned char *s, size_t n) {   // the position of the last '-', 0 if none
	size_t last = 0;
	for (size_t i = 0; i < n; i++)
		if (s[i] == '-')
			last = i;
	return last;
}

The theorem

QuickProof generated this statement from the spec, in src/contracts/rfc_digit_case.v:

Rocq
Theorem rfc_digit_case : harness_safe whole_prog.

src/contracts/laws.v states it on the spec's own program, whole_prog (the spec linked with the code under test), with its proof:

Rocq
Theorem rfc_digit_case : harness_safe OpensslPunycode.contracts.rfc_digit_case.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 OpensslPunycode.contracts.suite.suite)))))))))). Qed.

Read as a statement about runs, it says that no run of the program has undefined behaviour or a failing check, for every value the spec can draw. That reading rests on VST's adequacy theorem, which is not yet instantiated in Rocq (formerly Coq) for QuickProof's environment.

The C files (units) its program links
  • crypto/punycode.c
  • patch/units/packet_ext.c
  • patch/shim/ossl_shim.c
  • quickproof/libc/libc.c

What it leaves out

  • What the first comment of the spec excludes, and its bounds.
  • Like every spec of this proof: termination (a run that never returns is not a counterexample), one thread, and binaries built by a compiler (the theorem is about the code as CompCert defines C for x86-64, in a release build, compiled against a small header shim instead of OpenSSL's generated headers).
  • The rest of OpenSSL: only crypto/punycode.c and the parts of crypto/packet.c it reaches are code under test.

The summary page says what the proof as a whole covers.

How it was proved

AI agents wrote the proof, and Rocq checks it. qp verify passes on this repository: this statement is the one generated from the spec at the contract commit, and its proof uses only Rocq's standard axioms and VST's abstract malloc library.