1313/*
1414 * Data Conversion
1515 */
16- #define OSSL_IMPL_ARY2SK (name , type , expected_class , dup ) \
17- VALUE \
18- ossl_##name##_ary2sk0(VALUE ary) \
19- { \
20- STACK_OF(type) *sk; \
21- VALUE val; \
22- type *x; \
23- int i; \
24- \
25- Check_Type(ary, T_ARRAY); \
26- sk = sk_##type##_new_null(); \
27- if (!sk) ossl_raise(eOSSLError, NULL); \
28- \
29- for (i = 0; i < RARRAY_LEN(ary); i++) { \
30- val = rb_ary_entry(ary, i); \
31- if (!rb_obj_is_kind_of(val, expected_class)) { \
32- sk_##type##_pop_free(sk, type##_free); \
33- ossl_raise(eOSSLError, "object in array not" \
34- " of class ##type##"); \
35- } \
36- x = dup(val); /* NEED TO DUP */ \
37- sk_ ##type ##_push(sk, x); \
38- } \
39- return (VALUE)sk; \
40- } \
41- \
42- STACK_OF(type) * \
43- ossl_protect_##name##_ary2sk(VALUE ary, int *status) \
44- { \
45- return (STACK_OF(type)*)rb_protect( \
46- (VALUE (*)(VALUE))ossl_##name##_ary2sk0, \
47- ary, \
48- status); \
49- } \
50- \
51- STACK_OF(type) * \
52- ossl_##name##_ary2sk(VALUE ary) \
53- { \
54- STACK_OF(type) *sk; \
55- int status = 0; \
56- \
57- sk = ossl_protect_##name##_ary2sk(ary, &status); \
58- if (status) rb_jump_tag(status); \
59- \
60- return sk; \
16+ #define OSSL_IMPL_ARY2SK (name , type , expected_class , dup ) \
17+ VALUE \
18+ ossl_##name##_ary2sk0(VALUE ary) \
19+ { \
20+ STACK_OF(type) *sk; \
21+ VALUE val; \
22+ type *x; \
23+ int i; \
24+ \
25+ Check_Type(ary, T_ARRAY); \
26+ sk = sk_##type##_new_null(); \
27+ if (!sk) ossl_raise(eOSSLError, NULL); \
28+ \
29+ for (i = 0; i < RARRAY_LEN(ary); i++) { \
30+ val = rb_ary_entry(ary, i); \
31+ if (!rb_obj_is_kind_of(val, expected_class)) { \
32+ sk_##type##_pop_free(sk, type##_free); \
33+ ossl_raise(eOSSLError, "object in array not" \
34+ " of class ##type##"); \
35+ } \
36+ x = dup(val); /* NEED TO DUP */ \
37+ sk_ ##type ##_push(sk, x); \
38+ } \
39+ return (VALUE)sk; \
40+ } \
41+ \
42+ STACK_OF(type) * \
43+ ossl_protect_##name##_ary2sk(VALUE ary, int *status) \
44+ { \
45+ return (STACK_OF(type)*)rb_protect( \
46+ (VALUE (*)(VALUE))ossl_##name##_ary2sk0, \
47+ ary, \
48+ status); \
49+ } \
50+ \
51+ STACK_OF(type) * \
52+ ossl_##name##_ary2sk(VALUE ary) \
53+ { \
54+ STACK_OF(type) *sk; \
55+ int status = 0; \
56+ \
57+ sk = ossl_protect_##name##_ary2sk(ary, &status); \
58+ if (status) rb_jump_tag(status); \
59+ \
60+ return sk; \
6161}
6262OSSL_IMPL_ARY2SK (x509 , X509 , cX509Cert , DupX509CertPtr )
6363
64- #define OSSL_IMPL_SK2ARY (name , type ) \
65- VALUE \
66- ossl_##name##_sk2ary(const STACK_OF(type) *sk) \
67- { \
68- type *t; \
69- int i, num; \
70- VALUE ary; \
71- \
72- RUBY_ASSERT(sk != NULL); \
73- num = sk_##type##_num(sk); \
74- ary = rb_ary_new_capa(num); \
75- \
76- for (i=0; i<num; i++) { \
77- t = sk_##type##_value(sk, i); \
78- rb_ary_push(ary, ossl_##name##_new(t)); \
79- } \
80- return ary; \
64+ #define OSSL_IMPL_SK2ARY (name , type ) \
65+ VALUE \
66+ ossl_##name##_sk2ary(const STACK_OF(type) *sk) \
67+ { \
68+ type *t; \
69+ int i, num; \
70+ VALUE ary; \
71+ \
72+ RUBY_ASSERT(sk != NULL); \
73+ num = sk_##type##_num(sk); \
74+ ary = rb_ary_new_capa(num); \
75+ \
76+ for (i=0; i<num; i++) { \
77+ t = sk_##type##_value(sk, i); \
78+ rb_ary_push(ary, ossl_##name##_new(t)); \
79+ } \
80+ return ary; \
8181}
8282OSSL_IMPL_SK2ARY (x509 , X509 )
8383OSSL_IMPL_SK2ARY (x509crl , X509_CRL )
@@ -97,14 +97,14 @@ ossl_str_new(const char *ptr, long len, int *pstate)
9797
9898 str = rb_protect (ossl_str_new_i , len , & state );
9999 if (pstate )
100- * pstate = state ;
100+ * pstate = state ;
101101 if (state ) {
102- if (!pstate )
103- rb_set_errinfo (Qnil );
104- return Qnil ;
102+ if (!pstate )
103+ rb_set_errinfo (Qnil );
104+ return Qnil ;
105105 }
106106 if (ptr )
107- memcpy (RSTRING_PTR (str ), ptr , len );
107+ memcpy (RSTRING_PTR (str ), ptr , len );
108108 return str ;
109109}
110110
@@ -117,7 +117,7 @@ ossl_buf2str(char *buf, int len)
117117 str = ossl_str_new (buf , len , & state );
118118 OPENSSL_free (buf );
119119 if (state )
120- rb_jump_tag (state );
120+ rb_jump_tag (state );
121121 return str ;
122122}
123123
@@ -129,10 +129,10 @@ ossl_bin2hex(unsigned char *in, char *out, size_t inlen)
129129
130130 assert (inlen <= LONG_MAX / 2 );
131131 for (i = 0 ; i < inlen ; i ++ ) {
132- unsigned char p = in [i ];
132+ unsigned char p = in [i ];
133133
134- out [i * 2 + 0 ] = hex [p >> 4 ];
135- out [i * 2 + 1 ] = hex [p & 0x0f ];
134+ out [i * 2 + 0 ] = hex [p >> 4 ];
135+ out [i * 2 + 1 ] = hex [p & 0x0f ];
136136 }
137137}
138138
@@ -143,14 +143,14 @@ VALUE
143143ossl_pem_passwd_value (VALUE pass )
144144{
145145 if (NIL_P (pass ))
146- return Qnil ;
146+ return Qnil ;
147147
148148 StringValue (pass );
149149
150150 /* PEM_BUFSIZE is currently used as the second argument of pem_password_cb,
151151 * that is +max_len+ of ossl_pem_passwd_cb() */
152152 if (RSTRING_LEN (pass ) > PEM_BUFSIZE )
153- ossl_raise (eOSSLError , "password must not be longer than %d bytes" , PEM_BUFSIZE );
153+ ossl_raise (eOSSLError , "password must not be longer than %d bytes" , PEM_BUFSIZE );
154154
155155 return pass ;
156156}
@@ -160,7 +160,7 @@ ossl_pem_passwd_cb0(VALUE flag)
160160{
161161 VALUE pass = rb_yield (flag );
162162 if (NIL_P (pass ))
163- return Qnil ;
163+ return Qnil ;
164164 StringValue (pass );
165165 return pass ;
166166}
@@ -173,46 +173,46 @@ ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
173173 VALUE rflag , pass = (VALUE )pwd_ ;
174174
175175 if (RTEST (pass )) {
176- /* PEM_def_callback(buf, max_len, flag, StringValueCStr(pass)) does not
177- * work because it does not allow NUL characters and truncates to 1024
178- * bytes silently if the input is over 1024 bytes */
179- if (RB_TYPE_P (pass , T_STRING )) {
180- len = RSTRING_LEN (pass );
181- if (len <= max_len ) {
182- memcpy (buf , RSTRING_PTR (pass ), len );
183- return (int )len ;
184- }
185- }
186- OSSL_Debug ("passed data is not valid String???" );
187- return -1 ;
176+ /* PEM_def_callback(buf, max_len, flag, StringValueCStr(pass)) does not
177+ * work because it does not allow NUL characters and truncates to 1024
178+ * bytes silently if the input is over 1024 bytes */
179+ if (RB_TYPE_P (pass , T_STRING )) {
180+ len = RSTRING_LEN (pass );
181+ if (len <= max_len ) {
182+ memcpy (buf , RSTRING_PTR (pass ), len );
183+ return (int )len ;
184+ }
185+ }
186+ OSSL_Debug ("passed data is not valid String???" );
187+ return -1 ;
188188 }
189189
190190 if (!rb_block_given_p ()) {
191- return PEM_def_callback (buf , max_len , flag , NULL );
191+ return PEM_def_callback (buf , max_len , flag , NULL );
192192 }
193193
194194 while (1 ) {
195- /*
196- * when the flag is nonzero, this password
197- * will be used to perform encryption; otherwise it will
198- * be used to perform decryption.
199- */
200- rflag = flag ? Qtrue : Qfalse ;
201- pass = rb_protect (ossl_pem_passwd_cb0 , rflag , & status );
202- if (status ) {
203- /* ignore an exception raised. */
204- rb_set_errinfo (Qnil );
205- return -1 ;
206- }
207- if (NIL_P (pass ))
208- return -1 ;
209- len = RSTRING_LEN (pass );
210- if (len > max_len ) {
211- rb_warning ("password must not be longer than %d bytes" , max_len );
212- continue ;
213- }
214- memcpy (buf , RSTRING_PTR (pass ), len );
215- break ;
195+ /*
196+ * when the flag is nonzero, this password
197+ * will be used to perform encryption; otherwise it will
198+ * be used to perform decryption.
199+ */
200+ rflag = flag ? Qtrue : Qfalse ;
201+ pass = rb_protect (ossl_pem_passwd_cb0 , rflag , & status );
202+ if (status ) {
203+ /* ignore an exception raised. */
204+ rb_set_errinfo (Qnil );
205+ return -1 ;
206+ }
207+ if (NIL_P (pass ))
208+ return -1 ;
209+ len = RSTRING_LEN (pass );
210+ if (len > max_len ) {
211+ rb_warning ("password must not be longer than %d bytes" , max_len );
212+ continue ;
213+ }
214+ memcpy (buf , RSTRING_PTR (pass ), len );
215+ break ;
216216 }
217217 return (int )len ;
218218}
@@ -247,7 +247,7 @@ VALUE
247247ossl_to_der_if_possible (VALUE obj )
248248{
249249 if (rb_respond_to (obj , ossl_s_to_der ))
250- return ossl_to_der (obj );
250+ return ossl_to_der (obj );
251251 return obj ;
252252}
253253
@@ -289,12 +289,12 @@ ossl_raise(VALUE exc, const char *fmt, ...)
289289 VALUE err ;
290290
291291 if (fmt ) {
292- va_start (args , fmt );
293- err = rb_vsprintf (fmt , args );
294- va_end (args );
292+ va_start (args , fmt );
293+ err = rb_vsprintf (fmt , args );
294+ va_end (args );
295295 }
296296 else {
297- err = Qnil ;
297+ err = Qnil ;
298298 }
299299
300300 rb_exc_raise (ossl_make_error (exc , err ));
@@ -434,17 +434,17 @@ ossl_fips_mode_set(VALUE self, VALUE enabled)
434434 return enabled ;
435435#elif defined(OPENSSL_FIPS ) || defined(OPENSSL_IS_AWSLC )
436436 if (RTEST (enabled )) {
437- int mode = FIPS_mode ();
438- if (!mode && !FIPS_mode_set (1 )) /* turning on twice leads to an error */
439- ossl_raise (eOSSLError , "Turning on FIPS mode failed" );
437+ int mode = FIPS_mode ();
438+ if (!mode && !FIPS_mode_set (1 )) /* turning on twice leads to an error */
439+ ossl_raise (eOSSLError , "Turning on FIPS mode failed" );
440440 } else {
441- if (!FIPS_mode_set (0 )) /* turning off twice is OK */
442- ossl_raise (eOSSLError , "Turning off FIPS mode failed" );
441+ if (!FIPS_mode_set (0 )) /* turning off twice is OK */
442+ ossl_raise (eOSSLError , "Turning off FIPS mode failed" );
443443 }
444444 return enabled ;
445445#else
446446 if (RTEST (enabled ))
447- ossl_raise (eOSSLError , "This version of OpenSSL does not support FIPS mode" );
447+ ossl_raise (eOSSLError , "This version of OpenSSL does not support FIPS mode" );
448448 return enabled ;
449449#endif
450450}
@@ -473,8 +473,8 @@ ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
473473 }
474474
475475 switch (CRYPTO_memcmp (p1 , p2 , len1 )) {
476- case 0 : return Qtrue ;
477- default : return Qfalse ;
476+ case 0 : return Qtrue ;
477+ default : return Qfalse ;
478478 }
479479}
480480
@@ -996,13 +996,13 @@ Init_openssl(void)
996996#if OSSL_OPENSSL_PREREQ (3 , 0 , 0 )
997997 Qtrue
998998#elif defined(OPENSSL_FIPS )
999- Qtrue
999+ Qtrue
10001000#elif defined(OPENSSL_IS_AWSLC ) // AWS-LC FIPS can only be enabled during compile time.
1001- FIPS_mode () ? Qtrue : Qfalse
1001+ FIPS_mode () ? Qtrue : Qfalse
10021002#else
1003- Qfalse
1003+ Qfalse
10041004#endif
1005- );
1005+ );
10061006
10071007 rb_define_module_function (mOSSL , "fips_mode" , ossl_fips_mode_get , 0 );
10081008 rb_define_module_function (mOSSL , "fips_mode=" , ossl_fips_mode_set , 1 );
0 commit comments