On Thu, Jan 24, 2013 at 05:25:48PM +0000, Tovey, Dwight (LaserJet R&D FW Eng.) 
wrote:

> On Jan 23, 2013, at 4:18 PM, kap...@mizera.cz wrote:
> 
> > I have build the whole pub-key (in DER) from yours pubkey.bin by adding 
> > public key header - as wrote  w...@omnigroup.com
> > 
> > If I did not make error, it could work now - try it.
> > It is in attachment.
> > 
> > openssl asn1parse -in pub-key.der -inform der -strparse 18
> > 
> > and you will see the same as by parsing yours pubkey.bin.
> > 
> > To see whole public key structure:
> > openssl asn1parse -in pub-key.der -inform der
> > 
> > --kapetr
> > 
> 
> This worked.  I translated the .der key into PEM format, which I was then 
> able to load into my python script with M2Crypto.RSA.load_pub_key().  I could 
> then use the resulting RSA_pub object to encrypt my data and send it to the 
> device.
> 
> So, my next question is, how did you add the public key header?  What does 
> this header look like?  If I know that then I should be able to add it within 
> my script and handle it all directly.

Here's a hex dump of a complete public key:

0000000    30  81  9f  30  0d  06  09  2a  86  48  86  f7  0d  01  01  01
0000020    05  00  03  81  8d  00  30  81  89  02  81  81  00  9a  a7  8d
0000040    3f  46  10  6e  1c  5d  90  ac  43  e7  49  d7  ca  15  62  f3
0000060    0c  d3  af  8b  28  74  c7  49  fe  aa  3d  51  9c  7f  14  70
0000100    88  9f  94  24  29  33  6f  e6  88  29  c0  57  35  d1  92  b3
0000120    4c  de  48  79  8e  94  e0  d8  7a  16  94  57  57  35  ba  4f
0000140    de  b7  44  37  c1  56  2e  e3  41  14  54  9f  b3  c5  9a  a5
0000160    4a  a9  8e  00  67  1c  e2  35  ee  cb  f9  fc  f0  53  d7  f8
0000200    1d  73  95  26  36  c1  1f  c8  2c  29  3e  7f  36  e5  31  0a
0000220    8d  5b  c9  aa  35  c5  21  28  51  80  ba  e8  9b  02  03  01
0000240    00  01            

This is an ASN.1 structure:

    0:d=0  hl=3 l= 159 cons: SEQUENCE          
    3:d=1  hl=2 l=  13 cons: SEQUENCE          
    5:d=2  hl=2 l=   9 prim: OBJECT            :rsaEncryption
   16:d=2  hl=2 l=   0 prim: NULL              
   18:d=1  hl=3 l= 141 prim: BIT STRING        

The tag "30" means "SEQUENCE". The bytes "81 9f" encode the sequence
length, in this case 144 + 15 = 159 bytes (excluding the tag and the
length).

The first element of the sequece is also a sequence which encodes
the algorithm and parameters (RSA and no parameters). This sequence
is of length 0d == 13. Its first element (tag == 06 == Object ID)
is an OID of length 9:

        2a  86  48  86  f7  0d  01  01  01

which decodes to:

        1.2.840.113549.1.1.1    RSA Encryption

What followis is a tag of 05 (NULL) with a length of 0.

Finally, the key is encoded as a BITSTRING: (type == 03) (length
==  81  8d = 128 + 13 = 141). Since the bit string's length is a
multiple of 8, the firstr octet encodes 0 padding bits, the remaining
octets are the ASN.1 sequence encoding a sequence of the modulus
and exponent, which is the public key you started with.

-- 
        Viktor.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to