Skip to content

Commit d9b59e5

Browse files
committed
Compile with -DSTRICT_R_HEADERS -DR_NO_REMAP
1 parent 1b8034a commit d9b59e5

25 files changed

+177
-176
lines changed

openssl.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 94919da2-cd64-435c-ad4a-d22bcd028827
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

src/Makevars.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PKG_CPPFLAGS = @cflags@
1+
PKG_CPPFLAGS = @cflags@ -DSTRICT_R_HEADERS -DR_NO_REMAP
22
PKG_LIBS = -Lbcrypt -lstatbcrypt @libs@
33
STATLIB = bcrypt/libstatbcrypt.a
44
LIBBCRYPT = bcrypt/bcrypt_pbkdf.o bcrypt/blowfish.o

src/Makevars.win

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ endif
1313

1414
STATLIB = bcrypt/libstatbcrypt.a
1515
LIBBCRYPT = bcrypt/bcrypt_pbkdf.o bcrypt/blowfish.o
16-
PKG_CPPFLAGS = $(OPENSSL_CFLAGS) -DOPENSSL_SUPPRESS_DEPRECATED
16+
PKG_CPPFLAGS = $(OPENSSL_CFLAGS) -DOPENSSL_SUPPRESS_DEPRECATED -DSTRICT_R_HEADERS -DR_NO_REMAP
1717
PKG_LIBS = $(OPENSSL_LIBS) -Lbcrypt -lstatbcrypt
1818

1919

src/aes.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
SEXP R_aes_any(SEXP x, SEXP key, SEXP iv, SEXP encrypt, SEXP cipher) {
1313
int strength = LENGTH(key);
1414
if(strength != 16 && strength != 24 && strength != 32)
15-
error("key must be of length 16 (aes-128), 24 (aes-192) or 32 (aes-256)");
15+
Rf_error("key must be of length 16 (aes-128), 24 (aes-192) or 32 (aes-256)");
1616

1717
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
1818
const EVP_CIPHER *cph = EVP_get_cipherbyname(CHAR(STRING_ELT(cipher, 0)));
@@ -25,14 +25,14 @@ SEXP R_aes_any(SEXP x, SEXP key, SEXP iv, SEXP encrypt, SEXP cipher) {
2525
Rf_error("aes-gcm requires an iv of length 12");
2626
}
2727
//GCM mode has shorter IV from the others
28-
bail(EVP_CipherInit_ex(ctx, cph, NULL, NULL, NULL, asLogical(encrypt)));
28+
bail(EVP_CipherInit_ex(ctx, cph, NULL, NULL, NULL, Rf_asLogical(encrypt)));
2929
bail(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, LENGTH(iv), NULL));
3030
} else
3131
#endif //EVP_CIPH_GCM_MODE
3232
if(LENGTH(iv) != 16){
3333
Rf_error("aes requires an iv of length 16");
3434
}
35-
bail(EVP_CipherInit_ex(ctx, cph, NULL, RAW(key), RAW(iv), asLogical(encrypt)));
35+
bail(EVP_CipherInit_ex(ctx, cph, NULL, RAW(key), RAW(iv), Rf_asLogical(encrypt)));
3636

3737
int blocksize = EVP_CIPHER_CTX_block_size(ctx);
3838
int remainder = LENGTH(x) % blocksize;
@@ -58,7 +58,7 @@ SEXP R_aes_any(SEXP x, SEXP key, SEXP iv, SEXP encrypt, SEXP cipher) {
5858
int total = cur - buf;
5959
EVP_CIPHER_CTX_cleanup(ctx);
6060
EVP_CIPHER_CTX_free(ctx);
61-
SEXP out = allocVector(RAWSXP, total);
61+
SEXP out = Rf_allocVector(RAWSXP, total);
6262
memcpy(RAW(out), buf, total);
6363
OPENSSL_free(buf);
6464
return out;

src/base64.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SEXP R_base64_encode(SEXP bin, SEXP linebreaks){
1111
BIO *bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem()));
1212

1313
//No linebreaks
14-
if(!asLogical(linebreaks))
14+
if(!Rf_asLogical(linebreaks))
1515
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
1616

1717
bail(BIO_set_close(bio, BIO_NOCLOSE));
@@ -23,8 +23,8 @@ SEXP R_base64_encode(SEXP bin, SEXP linebreaks){
2323
BIO_get_mem_ptr(bio, &buf);
2424

2525
//return a character vector
26-
SEXP out = PROTECT(allocVector(STRSXP, 1));
27-
SET_STRING_ELT(out, 0, mkCharLen(buf->data, buf->length));
26+
SEXP out = PROTECT(Rf_allocVector(STRSXP, 1));
27+
SET_STRING_ELT(out, 0, Rf_mkCharLen(buf->data, buf->length));
2828
UNPROTECT(1);
2929

3030
//Cleanup and return
@@ -49,7 +49,7 @@ SEXP R_base64_decode(SEXP text){
4949
}
5050

5151
//create raw output vector
52-
SEXP out = PROTECT(allocVector(RAWSXP, bin_len));
52+
SEXP out = PROTECT(Rf_allocVector(RAWSXP, bin_len));
5353
memcpy(RAW(out), bin, bin_len);
5454
UNPROTECT(1);
5555

src/bignum.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
#include "utils.h"
77

88
BIGNUM *r2bignum(SEXP x){
9-
if(!inherits(x, "bignum"))
10-
error("Argument is not valid bignum");
9+
if(!Rf_inherits(x, "bignum"))
10+
Rf_error("Argument is not valid bignum");
1111
BIGNUM *val = BN_bin2bn(RAW(x), LENGTH(x), NULL);
1212
bail(val != NULL);
1313
return val;
1414
}
1515

1616
SEXP bignum2r(const BIGNUM *val){
17-
SEXP out = PROTECT(allocVector(RAWSXP, BN_num_bytes(val)));
17+
SEXP out = PROTECT(Rf_allocVector(RAWSXP, BN_num_bytes(val)));
1818
bail(BN_bn2bin(val, RAW(out)) >= 0);
19-
setAttrib(out, R_ClassSymbol, mkString("bignum"));
19+
Rf_setAttrib(out, R_ClassSymbol, Rf_mkString("bignum"));
2020
UNPROTECT(1);
2121
return out;
2222
}
@@ -25,7 +25,7 @@ SEXP R_parse_bignum(SEXP x, SEXP hex){
2525
BIGNUM *val = BN_new();
2626
if(TYPEOF(x) == RAWSXP){
2727
bail(NULL != BN_bin2bn(RAW(x), LENGTH(x), val));
28-
} else if(asLogical(hex)){
28+
} else if(Rf_asLogical(hex)){
2929
bail(BN_hex2bn(&val, CHAR(STRING_ELT(x, 0))));
3030
} else {
3131
bail(BN_dec2bn(&val, CHAR(STRING_ELT(x, 0))));
@@ -38,12 +38,12 @@ SEXP R_parse_bignum(SEXP x, SEXP hex){
3838
SEXP R_bignum_as_character(SEXP x, SEXP hex){
3939
BIGNUM *val = r2bignum(x);
4040
char *str;
41-
if(asLogical(hex)){
41+
if(Rf_asLogical(hex)){
4242
bail(!!(str = BN_bn2hex(val)));
4343
} else {
4444
bail(!!(str = BN_bn2dec(val)));
4545
}
46-
SEXP res = mkString(str);
46+
SEXP res = Rf_mkString(str);
4747
OPENSSL_free(str);
4848
BN_free(val);
4949
return res;
@@ -52,7 +52,7 @@ SEXP R_bignum_as_character(SEXP x, SEXP hex){
5252
SEXP R_bignum_as_integer(SEXP x){
5353
BIGNUM *val = r2bignum(x);
5454
int res = BN_div_word(val, (BN_ULONG) INT_MAX + 1);
55-
return ScalarInteger(BN_num_bits(val) ? NA_INTEGER : res);
55+
return Rf_ScalarInteger(BN_num_bits(val) ? NA_INTEGER : res);
5656
}
5757

5858
SEXP R_bignum_add(SEXP x, SEXP y){
@@ -171,10 +171,10 @@ SEXP R_bignum_compare(SEXP x, SEXP y){
171171
int out = BN_cmp(val1, val2);
172172
BN_free(val1);
173173
BN_free(val2);
174-
return ScalarInteger(out);
174+
return Rf_ScalarInteger(out);
175175
}
176176

177177
SEXP R_bignum_bits(SEXP x){
178178
BIGNUM *num = r2bignum(x);
179-
return ScalarInteger(BN_num_bits(num));
179+
return Rf_ScalarInteger(BN_num_bits(num));
180180
}

src/cert.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SEXP R_cert_info(SEXP bin, SEXP name_format){
1919
int len;
2020
X509_NAME *name;
2121
BIO *b;
22-
SEXP out = PROTECT(allocVector(VECSXP, 7));
22+
SEXP out = PROTECT(Rf_allocVector(VECSXP, 7));
2323

2424
//Note: for some reason XN_FLAG_MULTILINE messes up UTF8
2525
int flags = Rf_length(name_format) ? Rf_asInteger(name_format) : XN_FLAG_RFC2253;
@@ -30,8 +30,8 @@ SEXP R_cert_info(SEXP bin, SEXP name_format){
3030
bail(X509_NAME_print_ex(b, name, 0, flags & ~ASN1_STRFLGS_ESC_MSB));
3131
len = BIO_read(b, buf, bufsize);
3232
BIO_free(b);
33-
SET_VECTOR_ELT(out, 0, allocVector(STRSXP, 1));
34-
SET_STRING_ELT(VECTOR_ELT(out, 0), 0, mkCharLenCE(buf, len, CE_UTF8));
33+
SET_VECTOR_ELT(out, 0, Rf_allocVector(STRSXP, 1));
34+
SET_STRING_ELT(VECTOR_ELT(out, 0), 0, Rf_mkCharLenCE(buf, len, CE_UTF8));
3535
X509_NAME_free(name);
3636

3737
//issuer name name
@@ -40,50 +40,50 @@ SEXP R_cert_info(SEXP bin, SEXP name_format){
4040
bail(X509_NAME_print_ex(b, name, 0, flags & ~ASN1_STRFLGS_ESC_MSB));
4141
len = BIO_read(b, buf, bufsize);
4242
BIO_free(b);
43-
SET_VECTOR_ELT(out, 1, allocVector(STRSXP, 1));
44-
SET_STRING_ELT(VECTOR_ELT(out, 1), 0, mkCharLenCE(buf, len, CE_UTF8));
43+
SET_VECTOR_ELT(out, 1, Rf_allocVector(STRSXP, 1));
44+
SET_STRING_ELT(VECTOR_ELT(out, 1), 0, Rf_mkCharLenCE(buf, len, CE_UTF8));
4545
X509_NAME_free(name);
4646

4747
//sign algorithm
4848
const ASN1_BIT_STRING *signature;
4949
const X509_ALGOR *sig_alg;
5050
MY_X509_get0_signature(&signature, &sig_alg, cert);
5151
OBJ_obj2txt(buf, sizeof(buf), sig_alg->algorithm, 0);
52-
SET_VECTOR_ELT(out, 2, mkString(buf));
52+
SET_VECTOR_ELT(out, 2, Rf_mkString(buf));
5353

5454
//signature
55-
SET_VECTOR_ELT(out, 3, allocVector(RAWSXP, signature->length));
55+
SET_VECTOR_ELT(out, 3, Rf_allocVector(RAWSXP, signature->length));
5656
memcpy(RAW(VECTOR_ELT(out, 3)), signature->data, signature->length);
5757

5858
//start date
59-
SET_VECTOR_ELT(out, 4, allocVector(STRSXP, 2));
59+
SET_VECTOR_ELT(out, 4, Rf_allocVector(STRSXP, 2));
6060
b = BIO_new(BIO_s_mem());
6161
bail(ASN1_TIME_print(b, X509_get_notBefore(cert)));
6262
len = BIO_read(b, buf, bufsize);
6363
BIO_free(b);
64-
SET_STRING_ELT(VECTOR_ELT(out, 4), 0, mkCharLen(buf, len));
64+
SET_STRING_ELT(VECTOR_ELT(out, 4), 0, Rf_mkCharLen(buf, len));
6565

6666
//expiration date
6767
b = BIO_new(BIO_s_mem());
6868
bail(ASN1_TIME_print(b, X509_get_notAfter(cert)));
6969
len = BIO_read(b, buf, bufsize);
7070
BIO_free(b);
71-
SET_STRING_ELT(VECTOR_ELT(out, 4), 1, mkCharLen(buf, len));
71+
SET_STRING_ELT(VECTOR_ELT(out, 4), 1, Rf_mkCharLen(buf, len));
7272

7373
//test for self signed
74-
SET_VECTOR_ELT(out, 5, ScalarLogical(X509_verify(cert, X509_get_pubkey(cert))));
74+
SET_VECTOR_ELT(out, 5, Rf_ScalarLogical(X509_verify(cert, X509_get_pubkey(cert))));
7575

7676
//check for alternative names (requires x509v3 extensions !!)
7777
GENERAL_NAMES *subjectAltNames = X509_get_ext_d2i (cert, NID_subject_alt_name, NULL, NULL);
7878
int numalts = sk_GENERAL_NAME_num (subjectAltNames);
7979
if(numalts > 0) {
80-
SET_VECTOR_ELT(out, 6, allocVector(STRSXP, numalts));
80+
SET_VECTOR_ELT(out, 6, Rf_allocVector(STRSXP, numalts));
8181
unsigned char *tmpbuf;
8282
for (int i = 0; i < numalts; i++) {
8383
const GENERAL_NAME *name = sk_GENERAL_NAME_value(subjectAltNames, i);
8484
len = ASN1_STRING_to_UTF8(&tmpbuf, name->d.ia5);
8585
if(len > 0){
86-
SET_STRING_ELT(VECTOR_ELT(out, 6), i, mkCharLenCE((char*) tmpbuf, len, CE_UTF8));
86+
SET_STRING_ELT(VECTOR_ELT(out, 6), i, Rf_mkCharLenCE((char*) tmpbuf, len, CE_UTF8));
8787
OPENSSL_free(tmpbuf);
8888
}
8989
}
@@ -104,7 +104,7 @@ SEXP R_pubkey_verify_cert(SEXP cert, SEXP pubkey){
104104
int res = X509_verify(crt, pkey);
105105
X509_free(crt);
106106
EVP_PKEY_free(pkey);
107-
return ScalarLogical(res);
107+
return Rf_ScalarLogical(res);
108108
}
109109

110110
SEXP R_cert_verify_cert(SEXP cert, SEXP chain, SEXP bundle) {
@@ -145,7 +145,7 @@ SEXP R_cert_verify_cert(SEXP cert, SEXP chain, SEXP bundle) {
145145
X509_free(crt);
146146

147147
if(err)
148-
error("Certificate validation failed: %s", err);
148+
Rf_error("Certificate validation failed: %s", err);
149149

150-
return ScalarLogical(TRUE);
150+
return Rf_ScalarLogical(TRUE);
151151
}

src/diffie.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SEXP R_diffie_hellman(SEXP key, SEXP peerkey){
2828
/* Determine buffer length */
2929
size_t skeylen = 0;
3030
bail(EVP_PKEY_derive(ctx, NULL, &skeylen) > 0);
31-
SEXP out = allocVector(RAWSXP, skeylen);
31+
SEXP out = Rf_allocVector(RAWSXP, skeylen);
3232
bail(EVP_PKEY_derive(ctx, RAW(out), &skeylen) > 0);
3333

3434
/* cleanup */

src/envelope.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ SEXP R_envelope_encrypt(SEXP data, SEXP pubkey) {
4141
EVP_CIPHER_CTX_free(ctx);
4242

4343
/* Create output vector */
44-
SEXP res = PROTECT(allocVector(VECSXP, 3));
45-
SET_VECTOR_ELT(res, 0, allocVector(RAWSXP, ivlen));
46-
SET_VECTOR_ELT(res, 1, allocVector(RAWSXP, ekl[0]));
47-
SET_VECTOR_ELT(res, 2, allocVector(RAWSXP, len1 + len2));
44+
SEXP res = PROTECT(Rf_allocVector(VECSXP, 3));
45+
SET_VECTOR_ELT(res, 0, Rf_allocVector(RAWSXP, ivlen));
46+
SET_VECTOR_ELT(res, 1, Rf_allocVector(RAWSXP, ekl[0]));
47+
SET_VECTOR_ELT(res, 2, Rf_allocVector(RAWSXP, len1 + len2));
4848
memcpy(RAW(VECTOR_ELT(res, 0)), iv, ivlen);
4949
memcpy(RAW(VECTOR_ELT(res, 1)), ek[0], ekl[0]);
5050
memcpy(RAW(VECTOR_ELT(res, 2)), out, len1 + len2);
@@ -66,14 +66,14 @@ SEXP R_envelope_decrypt(SEXP data, SEXP iv, SEXP session, SEXP key) {
6666

6767
/* Verify key size */
6868
if(LENGTH(session) != EVP_PKEY_size(pkey))
69-
error("Invalid Session key, must be %d bytes", EVP_PKEY_size(pkey));
69+
Rf_error("Invalid Session key, must be %d bytes", EVP_PKEY_size(pkey));
7070

7171

7272
/* Alloc buffers and init */
7373
const EVP_CIPHER *cipher = EVP_aes_256_cbc();
7474
int ivlen = EVP_CIPHER_iv_length(cipher);
7575
if(ivlen != LENGTH(iv))
76-
error("Invalid IV, must be %d bytes", ivlen);
76+
Rf_error("Invalid IV, must be %d bytes", ivlen);
7777
bail(EVP_OpenInit(ctx, EVP_aes_256_cbc(), RAW(session), LENGTH(session), RAW(iv), pkey));
7878

7979
/* This is an overestimate */
@@ -89,7 +89,7 @@ SEXP R_envelope_decrypt(SEXP data, SEXP iv, SEXP session, SEXP key) {
8989
EVP_CIPHER_CTX_free(ctx);
9090

9191
/* Create RAW vector */
92-
SEXP res = allocVector(RAWSXP, len1 + len2);
92+
SEXP res = Rf_allocVector(RAWSXP, len1 + len2);
9393
memcpy(RAW(res), out, len1 + len2);
9494
free(out);
9595
return res;

src/hash.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ unsigned int digest_string(unsigned char *x, int len, SEXP key, const char *algo
2222
#endif
2323

2424
if(!md)
25-
error("Unknown cryptographic algorithm %s\n", algo);
25+
Rf_error("Unknown cryptographic algorithm %s\n", algo);
2626

2727
if(key == R_NilValue){
2828
bail(EVP_Digest(x, len, md_value, &md_len, md, NULL));
@@ -35,27 +35,27 @@ unsigned int digest_string(unsigned char *x, int len, SEXP key, const char *algo
3535
SEXP R_digest_raw(SEXP x, SEXP algo, SEXP key){
3636
/* Check inputs */
3737
if(TYPEOF(x) != RAWSXP)
38-
error("Argument 'x' must be a raw vector.");
38+
Rf_error("Argument 'x' must be a raw vector.");
3939

4040
/* Convert the Raw vector to an unsigned char */
4141
unsigned char md_value[EVP_MAX_MD_SIZE];
42-
unsigned int md_len = digest_string(RAW(x), length(x), key, CHAR(asChar(algo)), md_value);
42+
unsigned int md_len = digest_string(RAW(x), Rf_length(x), key, CHAR(Rf_asChar(algo)), md_value);
4343

4444
/* create raw output vector */
45-
SEXP out = PROTECT(allocVector(RAWSXP, md_len));
45+
SEXP out = PROTECT(Rf_allocVector(RAWSXP, md_len));
4646
memcpy(RAW(out), md_value, md_len);
4747
UNPROTECT(1);
4848
return out;
4949
}
5050

5151
SEXP R_digest(SEXP x, SEXP algo, SEXP key){
52-
if(!isString(x))
53-
error("Argument 'x' must be a character vector.");
54-
if(!isString(algo))
55-
error("Argument 'algo' must be a character vector.");
52+
if(!Rf_isString(x))
53+
Rf_error("Argument 'x' must be a character vector.");
54+
if(!Rf_isString(algo))
55+
Rf_error("Argument 'algo' must be a character vector.");
5656

57-
int len = length(x);
58-
SEXP out = PROTECT(allocVector(STRSXP, len));
57+
int len = Rf_length(x);
58+
SEXP out = PROTECT(Rf_allocVector(STRSXP, len));
5959
for (int i = 0; i < len; i++) {
6060
/* check for NA */
6161
if(STRING_ELT(x, i) == NA_STRING) {
@@ -66,14 +66,14 @@ SEXP R_digest(SEXP x, SEXP algo, SEXP key){
6666
const char* str = CHAR(STRING_ELT(x, i));
6767
int stringlen = LENGTH(STRING_ELT(x, i));
6868
unsigned char md_value[EVP_MAX_MD_SIZE];
69-
unsigned int md_len = digest_string( (unsigned char*) str, stringlen, key, CHAR(asChar(algo)), md_value);
69+
unsigned int md_len = digest_string( (unsigned char*) str, stringlen, key, CHAR(Rf_asChar(algo)), md_value);
7070

7171
/* create character vector */
7272
char strbuf[2*md_len+1];
7373
for (int i = 0; i < md_len; i++) {
7474
snprintf(strbuf + i*2, 3, "%02x", (unsigned int) md_value[i]);
7575
}
76-
SET_STRING_ELT(out, i, mkChar(strbuf));
76+
SET_STRING_ELT(out, i, Rf_mkChar(strbuf));
7777
}
7878
UNPROTECT(1);
7979
return out;

0 commit comments

Comments
 (0)