Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix identify for some public key pem file that missing object identifier #92

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions bin/jsencrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -1766,13 +1766,13 @@ function hex2b64(h) {

// convert a base64 string to hex
function b64tohex(s) {
var ret = ""
var ret = "";
var i;
var k = 0; // b64 state, 0-3
var slop;
for(i = 0; i < s.length; ++i) {
if(s.charAt(i) == b64pad) break;
v = b64map.indexOf(s.charAt(i));
var v = b64map.indexOf(s.charAt(i));
if(v < 0) continue;
if(k == 0) {
ret += int2char(v >> 2);
Expand Down Expand Up @@ -3898,7 +3898,6 @@ RSAKey.prototype.parseKey = function (pem) {
asn1 = asn1.sub[2].sub[0];
}
if (asn1.sub.length === 9) {

// Parse the private key.
modulus = asn1.sub[1].getHexStringValue(); //bigint
this.n = parseBigInt(modulus, 16);
Expand All @@ -3923,19 +3922,23 @@ RSAKey.prototype.parseKey = function (pem) {

var coefficient = asn1.sub[8].getHexStringValue(); //bigint
this.coeff = parseBigInt(coefficient, 16);

}
else if (asn1.sub.length === 2) {

// Parse the public key.
var bit_string = asn1.sub[1];
var sequence = bit_string.sub[0];
var sequence;
if (asn1.sub[0].tag == 0x02) {
// Parse the public key, without object identifier, then asn1.sub is a sequence of integers.
sequence = asn1;
}
else {
// Parse the public key, with object identifier, then asn1.sub[0] and asn1.sub[1] are sequences.
// asn1.sub[0] is a sequence included object identifier info, asn1.sub[1] is a sequence of integers.
sequence = asn1.sub[1].sub[0];
}

modulus = sequence.sub[0].getHexStringValue();
this.n = parseBigInt(modulus, 16);
public_exponent = sequence.sub[1].getHexStringValue();
this.e = parseInt(public_exponent, 16);

}
else {
return false;
Expand Down
16 changes: 8 additions & 8 deletions bin/jsencrypt.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/jsbn/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ function hex2b64(h) {

// convert a base64 string to hex
function b64tohex(s) {
var ret = ""
var ret = "";
var i;
var k = 0; // b64 state, 0-3
var slop;
for(i = 0; i < s.length; ++i) {
if(s.charAt(i) == b64pad) break;
v = b64map.indexOf(s.charAt(i));
var v = b64map.indexOf(s.charAt(i));
if(v < 0) continue;
if(k == 0) {
ret += int2char(v >> 2);
Expand Down
17 changes: 10 additions & 7 deletions src/jsencrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ RSAKey.prototype.parseKey = function (pem) {
asn1 = asn1.sub[2].sub[0];
}
if (asn1.sub.length === 9) {

// Parse the private key.
modulus = asn1.sub[1].getHexStringValue(); //bigint
this.n = parseBigInt(modulus, 16);
Expand All @@ -76,19 +75,23 @@ RSAKey.prototype.parseKey = function (pem) {

var coefficient = asn1.sub[8].getHexStringValue(); //bigint
this.coeff = parseBigInt(coefficient, 16);

}
else if (asn1.sub.length === 2) {

// Parse the public key.
var bit_string = asn1.sub[1];
var sequence = bit_string.sub[0];
var sequence;
if (asn1.sub[0].tag == 0x02) {
// Parse the public key, without object identifier, then asn1.sub is a sequence of integers.
sequence = asn1;
}
else {
// Parse the public key, with object identifier, then asn1.sub[0] and asn1.sub[1] are sequences.
// asn1.sub[0] is a sequence included object identifier info, asn1.sub[1] is a sequence of integers.
sequence = asn1.sub[1].sub[0];
}

modulus = sequence.sub[0].getHexStringValue();
this.n = parseBigInt(modulus, 16);
public_exponent = sequence.sub[1].getHexStringValue();
this.e = parseInt(public_exponent, 16);

}
else {
return false;
Expand Down
97 changes: 82 additions & 15 deletions test/mocha.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ body {
margin: 60px 50px;
}

#mocha ul, #mocha li {
#mocha ul,
#mocha li {
margin: 0;
padding: 0;
}
Expand All @@ -18,7 +19,8 @@ body {
list-style: none;
}

#mocha h1, #mocha h2 {
#mocha h1,
#mocha h2 {
margin: 0;
}

Expand Down Expand Up @@ -67,11 +69,11 @@ body {
}

#mocha .test.pass.medium .duration {
background: #C09853;
background: #c09853;
}

#mocha .test.pass.slow .duration {
background: #B94A48;
background: #b94a48;
}

#mocha .test.pass::before {
Expand All @@ -87,7 +89,7 @@ body {
font-size: 9px;
margin-left: 5px;
padding: 2px 5px;
color: white;
color: #fff;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
Expand Down Expand Up @@ -134,19 +136,71 @@ body {
overflow: auto;
}

#mocha .test pre {
#mocha .test .html-error {
overflow: auto;
color: black;
line-height: 1.5;
display: block;
float: left;
clear: left;
font: 12px/1.5 monaco, monospace;
margin: 5px;
padding: 15px;
border: 1px solid #eee;
max-width: 85%; /*(1)*/
max-width: -webkit-calc(100% - 42px);
max-width: -moz-calc(100% - 42px);
max-width: calc(100% - 42px); /*(2)*/
max-height: 300px;
word-wrap: break-word;
border-bottom-color: #ddd;
-webkit-border-radius: 3px;
-webkit-box-shadow: 0 1px 3px #eee;
-moz-box-shadow: 0 1px 3px #eee;
box-shadow: 0 1px 3px #eee;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}

#mocha .test .html-error pre.error {
border: none;
-webkit-border-radius: 0;
-moz-border-radius: 0;
border-radius: 0;
-webkit-box-shadow: 0;
-moz-box-shadow: 0;
box-shadow: 0;
padding: 0;
margin: 0;
margin-top: 18px;
max-height: none;
}

/**
* (1): approximate for browsers not supporting calc
* (2): 42 = 2*15 + 2*10 + 2*1 (padding + margin + border)
* ^^ seriously
*/
#mocha .test pre {
display: block;
float: left;
clear: left;
font: 12px/1.5 monaco, monospace;
margin: 5px;
padding: 15px;
border: 1px solid #eee;
max-width: 85%; /*(1)*/
max-width: -webkit-calc(100% - 42px);
max-width: -moz-calc(100% - 42px);
max-width: calc(100% - 42px); /*(2)*/
word-wrap: break-word;
border-bottom-color: #ddd;
-webkit-box-shadow: 0 1px 3px #eee;
-moz-box-shadow: 0 1px 3px #eee;
box-shadow: 0 1px 3px #eee;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}

#mocha .test h2 {
Expand All @@ -166,10 +220,12 @@ body {
text-align: center;
background: #eee;
font-size: 15px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-transition: opacity 200ms;
-moz-transition: opacity 200ms;
-webkit-transition:opacity 200ms;
-moz-transition:opacity 200ms;
-o-transition:opacity 200ms;
transition: opacity 200ms;
opacity: 0.3;
color: #888;
Expand Down Expand Up @@ -215,6 +271,17 @@ body {
#mocha-stats .progress {
float: right;
padding-top: 0;

/**
* Set safe initial values, so mochas .progress does not inherit these
* properties from Bootstrap .progress (which causes .progress height to
* equal line height set in Bootstrap).
*/
height: auto;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
background-color: initial;
}

#mocha-stats em {
Expand Down Expand Up @@ -242,14 +309,14 @@ body {
height: 40px;
}

#mocha code .comment { color: #ddd }
#mocha code .init { color: #2F6FAD }
#mocha code .string { color: #5890AD }
#mocha code .keyword { color: #8A6343 }
#mocha code .number { color: #2F6FAD }
#mocha code .comment { color: #ddd; }
#mocha code .init { color: #2f6fad; }
#mocha code .string { color: #5890ad; }
#mocha code .keyword { color: #8a6343; }
#mocha code .number { color: #2f6fad; }

@media screen and (max-device-width: 480px) {
#mocha {
#mocha {
margin: 60px 0px;
}

Expand Down
Loading