# OpenSSL

**URL:** <https://www.ruby-forum.com/t/openssl/190522>\
**Category:** IronRuby\
**Created:** [June 22, 2010, 2:36am UTC](https://www.ruby-forum.com/t/openssl/190522 "2010-06-22T02:36:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [June 22, 2010, 2:36am UTC](https://www.ruby-forum.com/t/openssl/190522/1 "2010-06-22T02:36:31Z")

</div>

Hello, everyone!

After flailing about a bit this weekend with my utter lack of  
understanding  
of cryptography in general, I’ve started work on fleshing out OpenSSL  
support. I’ll be working on it here:  
[http://github.com/hotgazpacho/ironruby/tree/openssl](http://github.com/hotgazpacho/ironruby/tree/openssl)

I’ve written some code, more than I probably should have without specs  
first  
😛 As this is my first stab at writing extensions for IronRuby, I would  
appreciate it someone from the core team could take a quick look at it  
and  
make sure I’m headed down the right path.

Now that I have a better handle as to what is going on, I’m going to  
proceed  
with some spec writing, based off of the MRI C code, found here:

> <https://github.com/ruby/ruby/tree/ruby_1_8_7/ext/openssl>
>
> //github.com/ruby/ruby/tree/ruby\_1\_8\_7/ext/openssl

Before I get too far, should I be targeting 1.8.7, or something in the  
1.9  
series? I haven’t checked to see how/if they differ, but I’d like to  
target  
one for now to get a base down, and perform an necessary porting later.

One more question: When defining Ruby properties, do I need to define a  
static C# method for each of the get and set methods, like so:

> <https://gist.github.com/hotgazpacho/447738>

[http://gist.github.com/447738](http://gist.github.com/447738)or is there a way to define a property on  
an  
underlying C# object, and mark it with a single attribute for get and  
set,  
like so:

> <https://gist.github.com/hotgazpacho/447733>

Thanks!

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [June 22, 2010, 3:32am UTC](https://www.ruby-forum.com/t/openssl/190522/2 "2010-06-22T03:32:08Z")

</div>

Youâ€™re heading the right direction and thanks for taking care of this  
module!

A few comments:

- 

```
     RSA constructors: does Ruby convert any parameters via to_s, 

```

to\_str, etc.? If so youâ€™ll need to use [DefaultProtocol] attribute or  
other appropriate conversions. The easiest way how to find out what  
conversions are used in MRI is like so:

class C  
def respond\_to? name  
puts name  
false  
end  
end

RSA.new (C.new, C.new, C.new)

- 

```
     These are not very efficient:

          private static byte[] PemToDer(string pem_encoded_key) {
          private static string DerToPem(byte[] der_data, bool 

```

isPrivate) {

You can use RubyEncoder class to encode/decode base64 (ReadBase64,  
WriteBase64). It might need some tweaks but thatâ€™s all right, feel free  
to change it.

- 

```
     The methods â€œnâ€, â€œeâ€, etc. should return MutableString 

```

instead of byte[]. Byte[] is not a native Ruby type.

- 

```
     You shouldnâ€™t catch all exceptions in DecodeRSAPrivateKey, 

```

especially when youâ€™re throwing them in the same method:

```
                catch (Exception) {

                    return new RSAParameters();

                }

```

Does Ruby throw any exceptions there? Which?

- 

```
     This could be done better using shift operator:

```

byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };

count = BitConverter.ToInt32(modint, 0);

count = (lowbyte \<\< 24) | (highbyte \<\< 16)

- 

```
     Our coding convention is to use braces consistently and 

```

â€œelseâ€, â€œfinallyâ€, â€œcatchâ€ etc. right next to closing brace:

```
                if (bt == 0x81) {

                    count = binr.ReadByte(); // data size in 

```

next byte

```
                } else {

```

As for compat, Iâ€™d target 1.9 first. Write specs and run them against  
both MRIs. Then we can decide based upon how much they differ.

Accessors â€“ this is the pattern we currently use:

> <https://gist.github.com/hotgazpacho/447738>

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Will G.  
Sent: Monday, June 21, 2010 5:17 PM  
To: ironruby-core  
Subject: [Ironruby-core] OpenSSL

Hello, everyone!

After flailing about a bit this weekend with my utter lack of  
understanding of cryptography in general, I’ve started work on fleshing  
out OpenSSL support. I’ll be working on it here:  
[http://github.com/hotgazpacho/ironruby/tree/openssl](http://github.com/hotgazpacho/ironruby/tree/openssl)

I’ve written some code, more than I probably should have without specs  
first 😛 As this is my first stab at writing extensions for IronRuby, I  
would appreciate it someone from the core team could take a quick look  
at it and make sure I’m headed down the right path.

Now that I have a better handle as to what is going on, I’m going to  
proceed with some spec writing, based off of the MRI C code, found here:

> **[ruby/ext/openssl at ruby\_1\_8\_7 · ruby/ruby](https://github.com/ruby/ruby/tree/ruby_1_8_7/ext/openssl)**
>
> ruby\_1\_8\_7/ext/openssl

Before I get too far, should I be targeting 1.8.7, or something in the  
1.9 series? I haven’t checked to see how/if they differ, but I’d like to  
target one for now to get a base down, and perform an necessary porting  
later.

One more question: When defining Ruby properties, do I need to define a  
static C# method for each of the get and set methods, like so:

> <https://gist.github.com/hotgazpacho/447738>

or is there a way to define a property on an underlying C# object, and  
mark it with a single attribute for get and set, like so:

> <https://gist.github.com/hotgazpacho/447733>

Thanks!

–  
Will G.  
[http://hotgazpacho.org/](http://hotgazpacho.org/)

---

<div class="post-metadata">

**Author:** ![hotgazpacho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a587f6/32.png) [@hotgazpacho](https://www.ruby-forum.com/u/hotgazpacho)\
**Post date:** [June 22, 2010, 4:43am UTC](https://www.ruby-forum.com/t/openssl/190522/3 "2010-06-22T04:43:33Z")

</div>

Thanks for the feedback, Tomas!

Please pardon the poor coding; I was more concerned with wrapping my  
head  
around cryptography than producing good code (it should probably be  
considered more of a spike than anything). I will definitely correct  
those,  
as well as take a look at the areas you mentioned.

Looking forward to fleshing this out; OpenSSL is a necessary library for  
a  
number of cool Ruby libs (Capistrano) and platforms (Heroku).

–  
Will G.  
[http://hotgazpacho.org/](http://hotgazpacho.org/)

On Mon, Jun 21, 2010 at 9:23 PM, Tomas M. \<
