{"id":510,"date":"2018-12-17T16:41:55","date_gmt":"2018-12-17T07:41:55","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/12\/17\/p12-to-pem-with-js"},"modified":"2018-12-17T16:41:55","modified_gmt":"2018-12-17T07:41:55","slug":"p12-to-pem-with-js","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/12\/17\/p12-to-pem-with-js\/","title":{"rendered":"JavaScript\u3067p12\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8a3c\u660e\u66f8\u3068\u79d8\u5bc6\u9375\u3092\u53d6\u5f97\u3059\u308b"},"content":{"rendered":"

JavaScript\u3092\u4f7f\u3063\u3066\u3001\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u3001p12\u30d5\u30a1\u30a4\u30eb\u304b\u3089pem\u306e\u66f8\u5f0f\u3067\u8a3c\u660e\u66f8\u3068\u79d8\u5bc6\u9375\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

forge\u306e\u4f7f\u7528<\/h2>\n

\u4ee5\u4e0b\u3001forge<\/a>\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u3066\u3086\u304d\u307e\u3059\u3002\u30d6\u30e9\u30a6\u30b6\u3067\u8aad\u307f\u8fbc\u3080\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bmin\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n

const forge = require('node-forge\/dist\/forge.min.js');<\/code><\/pre>\n

p12\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f<\/h2>\n

p12\u30d5\u30a1\u30a4\u30eb\u306f\u30d6\u30e9\u30a6\u30b6\u3067\u6dfb\u4ed8\u3055\u308c\u308b\u60f3\u5b9a\u3067\u3059\u306e\u3067\u3001\u307e\u305a\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u89e3\u8aad\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002<\/p>\n

\/\/ p12Buffer\u306f\u3001\u6dfb\u4ed8\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092Buffer\u306b\u5909\u63db\u3057\u305f\u3082\u306e\nconst p12String = p12Buffer.toString('base64');\n\n\/\/ base64\u304b\u3089\u30c7\u30b3\u30fc\u30c9\nconst p12Der = forge.util.decode64(p12String);\n\n\/\/ ASN.1\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\nconst p12Asn1 = forge.asn1.fromDer(p12Der);\n\n\/\/ p12\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\nconst p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1);<\/code><\/pre>\n

\u6ce8\u610f\u70b9\u3068\u3057\u3066\u306f\u3001p12\u30d5\u30a1\u30a4\u30eb\u306b\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u306f\u3001forge.pkcs12.pkcs12FromAsn1(p12Asn1, 'password')<\/code>\u306e\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3053\u3068\u3067\u3059\u3002Google API Console\u3067\u53d6\u5f97\u3057\u305f\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067notasecret<\/code>\u306b\u306a\u3063\u3066\u3044\u305f\u308a\u3059\u308b\b\u306e\u3067\u3001\u8981\u4ef6\u306b\u3042\u308f\u305b\u3066\u5b9f\u88c5\u3092\u884c\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u8a3c\u660e\u66f8\u306e\u53d6\u5f97<\/h2>\n

\u8a3c\u660e\u66f8\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002Bag\u306f\u3001p12\u30d5\u30a1\u30a4\u30eb\u306b\u304a\u3051\u308b\u3001\u60c5\u5831\u3092\u5165\u308c\u3066\u304a\u304f\u7bb1\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n

\/\/ Bag\u30bf\u30a4\u30d7\u304b\u3089\u8a3c\u660e\u66f8\u3092\u53d6\u5f97\n\/\/ https:\/\/tools.ietf.org\/html\/rfc7292#section-4.2.3\nconst cert = p12.getBags({ bagType: forge.pki.oids.certBag })[forge.pki.oids.certBag][0].cert;\n\n\/\/ pem\u306e\u66f8\u5f0f\u3067\u53d6\u5f97\nconst pemCert = forge.pki.certificateToPem(cert);<\/code><\/pre>\n

\u79d8\u5bc6\u9375\u306e\u53d6\u5f97<\/h2>\n

\u79d8\u5bc6\u9375\u3082\u57fa\u672c\u7684\u306b\u306f\u540c\u69d8\u3067\u3059\u304c\u3001\u9593\u306b\u5909\u63db\u306e\u51e6\u7406\u304c\u5165\u308a\u307e\u3059\u3002<\/p>\n

\/\/ Bag\u30bf\u30a4\u30d7\u304b\u3089\u79d8\u5bc6\u9375\u3092\u53d6\u5f97\u3002\u79d8\u5bc6\u9375\u306a\u306e\u3067PKCS#8\u3067\u53d6\u308a\u51fa\u305b\u308b\n\/\/ https:\/\/tools.ietf.org\/html\/rfc7292#section-4.2.2\nconst privateKey = p12.getBags({ bagType: forge.pki.oids.pkcs8ShroudedKeyBag })[forge.pki.oids.pkcs8ShroudedKeyBag][0].key;\n\n\/\/ ASN.1\u306eRSA\u79d8\u5bc6\u9375\u306b\u5909\u63db\nconst rsaPrivateKey = forge.pki.privateKeyToAsn1(privateKey);\n\n\/\/ PKCS#8\u306ePrivateKeyInfo\u3067\u30e9\u30c3\u30d7\u3059\u308b\n\/\/ https:\/\/tools.ietf.org\/html\/rfc5208#section-5\nconst privateKeyInfo = forge.pki.wrapRsaPrivateKey(rsaPrivateKey);\n\n\/\/ pem\u306e\u66f8\u5f0f\u3067\u53d6\u5f97\nconst pemPrivate = forge.pki.privateKeyInfoToPem(privateKeyInfo);<\/code><\/pre>\n

\u79d8\u5bc6\u9375\u306fp12.getBags({friendlyName: "privatekey"})<\/code>\u306e\u3088\u3046\u306bfriendlyName<\/code>\u3092\u3064\u304b\u3063\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001\u57fa\u672c\u7684\u306b\u4efb\u610f\u3067\u8a2d\u5b9a\u3067\u304d\u3066\u3057\u307e\u3046\u306e\u3067\u3001Bag\u30bf\u30a4\u30d7\u3067\u53d6\u308b\u306e\u304c\u3088\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n

\u307e\u3068\u3081<\/h2>\n
\/*\n\u51fa\u529b: \n-----BEGIN CERTIFICATE-----\nbase64\u30a8\u30f3\u30b3\u30fc\u30c9\n-----END CERTIFICATE-----\n*\/\nconsole.log(pemCert.replace(\/\r\n\/g, '\n'))\n\n\/*\n\u51fa\u529b: \n-----BEGIN PRIVATE KEY-----\nbase64\u30a8\u30f3\u30b3\u30fc\u30c9\n-----END PRIVATE KEY-----\n*\/\nconsole.log(pemPrivate.replace(\/\r\n\/g, '\n'))<\/code><\/pre>\n

\u7121\u4e8bpem\u66f8\u5f0f\u3067\u001b\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

JavaScript\u3092\u4f7f\u3063\u3066\u3001\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u3001p12\u30d5\u30a1\u30a4\u30eb\u304b\u3089pem\u306e\u66f8\u5f0f\u3067\u8a3c\u660e\u66f8\u3068\u79d8\u5bc6\u9375\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3059\u3002 forge\u306e\u4f7f\u7528 \u4ee5\u4e0b\u3001forge\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u3066\u3086\u304d\u307e\u3059\u3002\u30d6\u30e9\u30a6\u30b6\u3067\u8aad\u307f\u8fbc\u3080\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bmin\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 const forge = require('node-forge\/dist\/forge.min.js'); p12\u30d5\u30a1 […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/510"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=510"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/510\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}