[6a3a178] | 1 | /**
|
---|
| 2 | * Javascript implementation of a basic Public Key Infrastructure, including
|
---|
| 3 | * support for RSA public and private keys.
|
---|
| 4 | *
|
---|
| 5 | * @author Dave Longley
|
---|
| 6 | *
|
---|
| 7 | * Copyright (c) 2010-2013 Digital Bazaar, Inc.
|
---|
| 8 | */
|
---|
| 9 | var forge = require('./forge');
|
---|
| 10 | require('./asn1');
|
---|
| 11 | require('./oids');
|
---|
| 12 | require('./pbe');
|
---|
| 13 | require('./pem');
|
---|
| 14 | require('./pbkdf2');
|
---|
| 15 | require('./pkcs12');
|
---|
| 16 | require('./pss');
|
---|
| 17 | require('./rsa');
|
---|
| 18 | require('./util');
|
---|
| 19 | require('./x509');
|
---|
| 20 |
|
---|
| 21 | // shortcut for asn.1 API
|
---|
| 22 | var asn1 = forge.asn1;
|
---|
| 23 |
|
---|
| 24 | /* Public Key Infrastructure (PKI) implementation. */
|
---|
| 25 | var pki = module.exports = forge.pki = forge.pki || {};
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * NOTE: THIS METHOD IS DEPRECATED. Use pem.decode() instead.
|
---|
| 29 | *
|
---|
| 30 | * Converts PEM-formatted data to DER.
|
---|
| 31 | *
|
---|
| 32 | * @param pem the PEM-formatted data.
|
---|
| 33 | *
|
---|
| 34 | * @return the DER-formatted data.
|
---|
| 35 | */
|
---|
| 36 | pki.pemToDer = function(pem) {
|
---|
| 37 | var msg = forge.pem.decode(pem)[0];
|
---|
| 38 | if(msg.procType && msg.procType.type === 'ENCRYPTED') {
|
---|
| 39 | throw new Error('Could not convert PEM to DER; PEM is encrypted.');
|
---|
| 40 | }
|
---|
| 41 | return forge.util.createBuffer(msg.body);
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Converts an RSA private key from PEM format.
|
---|
| 46 | *
|
---|
| 47 | * @param pem the PEM-formatted private key.
|
---|
| 48 | *
|
---|
| 49 | * @return the private key.
|
---|
| 50 | */
|
---|
| 51 | pki.privateKeyFromPem = function(pem) {
|
---|
| 52 | var msg = forge.pem.decode(pem)[0];
|
---|
| 53 |
|
---|
| 54 | if(msg.type !== 'PRIVATE KEY' && msg.type !== 'RSA PRIVATE KEY') {
|
---|
| 55 | var error = new Error('Could not convert private key from PEM; PEM ' +
|
---|
| 56 | 'header type is not "PRIVATE KEY" or "RSA PRIVATE KEY".');
|
---|
| 57 | error.headerType = msg.type;
|
---|
| 58 | throw error;
|
---|
| 59 | }
|
---|
| 60 | if(msg.procType && msg.procType.type === 'ENCRYPTED') {
|
---|
| 61 | throw new Error('Could not convert private key from PEM; PEM is encrypted.');
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | // convert DER to ASN.1 object
|
---|
| 65 | var obj = asn1.fromDer(msg.body);
|
---|
| 66 |
|
---|
| 67 | return pki.privateKeyFromAsn1(obj);
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * Converts an RSA private key to PEM format.
|
---|
| 72 | *
|
---|
| 73 | * @param key the private key.
|
---|
| 74 | * @param maxline the maximum characters per line, defaults to 64.
|
---|
| 75 | *
|
---|
| 76 | * @return the PEM-formatted private key.
|
---|
| 77 | */
|
---|
| 78 | pki.privateKeyToPem = function(key, maxline) {
|
---|
| 79 | // convert to ASN.1, then DER, then PEM-encode
|
---|
| 80 | var msg = {
|
---|
| 81 | type: 'RSA PRIVATE KEY',
|
---|
| 82 | body: asn1.toDer(pki.privateKeyToAsn1(key)).getBytes()
|
---|
| 83 | };
|
---|
| 84 | return forge.pem.encode(msg, {maxline: maxline});
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Converts a PrivateKeyInfo to PEM format.
|
---|
| 89 | *
|
---|
| 90 | * @param pki the PrivateKeyInfo.
|
---|
| 91 | * @param maxline the maximum characters per line, defaults to 64.
|
---|
| 92 | *
|
---|
| 93 | * @return the PEM-formatted private key.
|
---|
| 94 | */
|
---|
| 95 | pki.privateKeyInfoToPem = function(pki, maxline) {
|
---|
| 96 | // convert to DER, then PEM-encode
|
---|
| 97 | var msg = {
|
---|
| 98 | type: 'PRIVATE KEY',
|
---|
| 99 | body: asn1.toDer(pki).getBytes()
|
---|
| 100 | };
|
---|
| 101 | return forge.pem.encode(msg, {maxline: maxline});
|
---|
| 102 | };
|
---|